Margin Close Out Value Oanda Live Trading

Hi All,


I need a help to find a way to calculate Margin Closeout Value dynamically for OANDA live trading to close some ill position when it comes to margin close out condition

In Oanda live trading If your Margin Closeout Value falls to less than half of your Margin Used, all open positions will be automatically closed using the current fxTrade rates at the time of closing. 

The Margin Closeout Value is equal to your balance plus your unrealized P/L from all open positions, converted into the currency of the account, all calculated using the current midpoint rates. 

I would like to check every minute or second if Margin Closeout Value( Balance+TotalUnrealised Profit) < Margin Used*0.6 and start to close some bad positions to avoid margin closeout. I can access to Unrealised profit (self.Portfolio.TotalUnrealisedProfit) and Total Margin Used (self.Portfolio.TotalMarginUsed), however I could not find a way to access dynamically the account balance, since it changes every downsizing of ill positions. Any advice ?


Best Regards


Update Backtest

Hi M.Yildiz, you can create a custom margin call model to implement the logic. The default margin call will trigger when marginRemaining <= totalPortfolioValue * 0.05. Per your request, it should be marginRemaining <= totalPortfolioValue * 0.6.

Python doesn't support the margin call model at this time. We've created a Github issue to implement this feature with a Python wrapper.

We've added this feature in this Pull Request. Now you can customize the margin call model and use the event handler "OnMarginCallWarning(self):" to place the order when there is a margin call warning. Please see the attached algorithm.

You can use TotalPortfolioValue in calculation 

TotalPortfolioValue = Cash + TotalUnrealisedProfit + TotalUnleveredAbsoluteHoldingsCost


Update Backtest


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This discussion is closed