The concept of margin and leverage can be a tricky one to setup correctly in a backtest environment. Each country and broker may have different rules and regulations for how margin is applied. To further compound the confusion, the definition of margin itself can also differ. In some markets, margin is simply a fixed size deposit you must provide to enter into a position. In other’s (like Forex) you need to be aware of both an initial margin (the deposit) to open a position and subsequent maintenance margin to keep the position open.
Backtrader’s architecture only supports the concept of margin as an initial deposit. Should you want to simulate maintenance margin and margin closeouts, you will need to manage this yourself. In this post, we are going to just do that.
Second, we crank up the size to $200,000 and force a margin close out as the trade goes against us.
You can see from the terminal output, it took 3 days before the position was closed. In addition, the trade lost so much money, that there was not enough cash left to cover the margin on the second trade.
Before we begin
There are a number of things we must consider when setting up Backtrader to account correctly for margin and leverage.- The regulatory requirements in your country/region
- The type of asset you are trading.
- Your broker’s rules/calculation method.
Doing the research
To make sure we setup correctly we need to take a look at the Oanda website to see how they calculate margin and margin closeouts. First of all, I suggest you become familiar with margin jargon. Oanda has a good overview of all the margin related terms on their USA regulatory overview page. For some reason, they do not include a summary of terms on the European version! So Europeans should still take a look at the US page to get familiar with the terminology, then search for their own regulatory page. https://www.oanda.com/resources/legal/united-states/legal/margin-rules Some terms that we really need to be familiar with are as follows. However, there are many more terms listed on the page. I do encourage you to read them all!.Margin: A good faith deposit or performance bond. In leveraged trading, the margin amount is held in deposit while the trade is open. The amount of margin required to enter a trade is determined by the rules discussed below. Although there is no minimum margin deposit required to open an fxTrade account with OANDA, the Margin Available in your account will limit the size of the positions you can open. Leverage: The reciprocal of Margin. For example, 2% margin is the same as 50:1 leverage. The maximum leverage allowed is determined by the regulators in each geographic region. Clients and OANDA may choose to be conservative and limit leverage utilized to lower levels than allowed by the regulators. Margin Closeout Value: 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. See the Margin Closeout Value Calculation Example below for an example of how to calculate your account equity. Initial Margin: The Initial Margin for a trade is equal to the trade size multiplied by the Margin Requirement. This amount is then converted into the currency of the account. When opening a new trade, your Initial Margin must be less than or equal to your Margin Available. If your Initial Margin is greater than your Margin Available, you cannot open the trade. Margin Closeout: 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. If trading is unavailable for certain open positions at the time of the margin closeout, those positions will remain open and the fxTrade platform will continue to monitor your margin requirements. When the markets reopen for the remaining open positions, another margin closeout may occur if your account remains under-margined.
Check your margin rates
Before you trade a particular instrument, you need to know the margin rate for it. Also be aware that because your account has a leverage of 50:1, it does not mean that you will always get to use it all. As you will see, some exotic pairs and CFD’s require a 5% (20:1) initial margin regardless of how much leverage your account has. US customers also have to deal with comparatively strict regulatory compliance which forces a maximum leverage of 20:1 on all minor pairs. You can thank the CFTC for protecting your wallet.The Commodity Futures Trading Commission (CFTC) limits leverage available to retail forex traders in the United States to 50:1 on major currency pairs and 20:1 for all others.https://www.oanda.com/resources/legal/united-states/legal/margin-rates
Margin Calculations
The initial margin and margin closeout descriptions quoted above provide all the information we need to make the calculations needed in the strategy. They are: Initial Margin = (price x size) / leverage. Margin Closeout Value* = Balance + open PnL Margin Closeout = Margin Closeout Value < (Initial Margin / 2) *In backtrader the margin closeout value is simply the same as the self.broker.getvalue().Putting it all together
In this code example, we are going to trade EUR/USD using a base USD base account subject to US regulations and utilizing 50:1 leverage. A copy of the data I use for testing can be found here: EUR_USD-2005-2017-D1''' Author: www.backtest-rookies.com MIT License Copyright (c) 2017 backtest-rookies.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' import backtrader as bt from datetime import datetime class Mltests(bt.Strategy): ''' This strategy contains some additional methods that can be used to calcuate whether a position should be subject to a margin close out from Oanda. ''' params = (('size', 100000),) def __init__(self): self.count = 0 self.buybars = [1, 150] self.closebars = [100, 250] def next(self): bar = len(self.data) self.dt = self.data.datetime.date() if not self.position: if bar in self.buybars: self.cash_before = self.broker.getcash() value = self.broker.get_value() entry = self.buy(size=self.p.size) entry.addinfo(name='Entry') else: ''' First check if margin close out conditions are met. If not, check to see if we should close the position through the strategy rules. If a close out occurs, we need to addinfo to the order so that we can log it properly later ''' mco_result = self.check_mco( value=self.broker.get_value(), margin_used=self.margin ) if mco_result == True: close = self.close() close.addinfo(name='MCO') elif bar in self.closebars: close = self.close() close.addinfo(name='Close') self.count += 1 def notify_trade(self, trade): if trade.isclosed: print('{}: Trade closed '.format(self.dt)) print('{}: PnL Gross {}, Net {}\n\n'.format(self.dt, round(trade.pnl,2), round(trade.pnlcomm,2))) def notify_order(self,order): if order.status == order.Completed: ep = order.executed.price es = order.executed.size tv = ep * es leverage = self.broker.getcommissioninfo(self.data).get_leverage() self.margin = abs((ep * es) / leverage) if 'name' in order.info: if order.info['name'] == 'Entry': print('{}: Entry Order Completed '.format(self.dt)) print('{}: Order Executed Price: {}, Executed Size {}'.format(self.dt,ep,es,)) print('{}: Position Value: {} Margin Used: {}'.format(self.dt,tv,self.margin)) elif order.info['name'] == 'MCO': print('{}: WARNING: Margin Close Out'.format(self.dt)) else: print('{}: Close Order Completed'.format(self.dt)) def check_mco(self, value, margin_used): ''' Make a check to see if the margin close out value has fallen below half of the margin used, close the position. value: value of the portfolio for a given data. This is essentially the same as the margin close out value which is balance + pnl margin_used: Initial margin ''' if value < (margin_used /2): return True else: return False class OandaCSVData(bt.feeds.GenericCSVData): params = ( ('nullvalue', float('NaN')), ('dtformat', '%Y-%m-%dT%H:%M:%S.%fZ'), ('datetime', 6), ('time', -1), ('open', 5), ('high', 3), ('low', 4), ('close', 1), ('volume', 7), ('openinterest', -1), ) tframes = dict( minutes=bt.TimeFrame.Minutes, daily=bt.TimeFrame.Days, weekly=bt.TimeFrame.Weeks, monthly=bt.TimeFrame.Months) #Variable for our starting cash startcash = 10000 #Create an instance of cerebro cerebro = bt.Cerebro() #Set commission cerebro.broker.setcommission(leverage=50) #Add our strategy cerebro.addstrategy(Mltests) #get oanda data data = OandaCSVData( dataname='data/EUR_USD-2005-2017-D1.csv', fromdate=datetime(2005,1,1), todate=datetime(2006,1,1)) #Add the data to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(startcash) # Run over everything cerebro.run() portvalue = cerebro.broker.getvalue() pnl = portvalue - startcash print('Final Portfolio Value: ${}'.format(portvalue)) print('P/L: ${}'.format(pnl)) #Finally plot the end results cerebro.plot(style='candlestick')
Commentary
For testing purposes, the strategy is pretty simple. It just opens and closes portions on set bars. Next, thecheck_mco()
method is used to check whether a margin close out should be applied. One we are in a position, we need to call check_mco()
on every call of next()
to see if the margin close out value has dipped below the threshold. If so, we close the position and use addinfo()
to flag that it was closed due to having insufficient margin rather than as part of the strategy.
Finally, we set self.margin
attribute once we receive the order.Completed
notification. This allows us to access the executed price and therefore calculate the executed initial margin requirement. We do this following a notification (rather than during the next()
call) because, for market orders, Backtrader fills the order using the opening price of the following bar. If it gaps up or down, we could run the risk of incorrectly calculating the initial margin.
The Results
The first two screenshots are when the trade size is set to $100,000. The script executes without a margin close out. This is the baseline test.



Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.
Brave
Backtest Rookies is a registered with Brave publisher!
Brave users can drop us a tip.
Alternatively, support us by switching to Brave using this referral link and we will receive some BAT!
Tradingview
Referral Link
Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!
PayPal
BTC
3HxNVyh5729ieTfPnTybrtK7J7QxJD9gjD
ETH
0x9a2f88198224d59e5749bacfc23d79507da3d431
LTC
M8iUBnb8KamHR18gpgi2sSG7jopddFJi8S
Great article…I really like your blog!
Wondering if you still planning to write that another post which would be about how to get around the different base currency vs account currency issue?
Sorry for the late reply!
Your kind words are appreciated. Currently, there are no plans in the near future. Other items are in the pipeline. However, the request is noted and will add it to the todo list.