If you are planning to backtest a trading system with leverage in Amibroker this tutorial will help you to backtest leveraged trading systems properly. This example helps cash market trader to build their leverage trading systems properly.
What is Leverage?
Leverage is a way to increase the purchasing capacity of the trading instrument. It is a facility provided by the broker firm to maximize the trader’s returns. Playing a leveraged strategy will result in maximized returns with lower capital and at the same time it also directly increase the trading risk involved.
For Example, Let say if the broker provides 4 times leverage then with Rs40,000 investment one can take a position up to Rs160,000.
Assume that you are having a investing capital of Rs1,00,000 and in every trade you are planning to invest Rs40,000 per trade. Whenever there is a 20EMA and 50 EMA positive crossover long entry will be made with a position sizing of 40,000 x 4 times the leverage provided by the broker. Hence the Net position value per trade is Rs1,60,000
Though Initial capital is Rs1,00,000 and the trade involves leverage one need to maintain buffer money to manage the Market to Market (MTM) Loss if any. Hence Rs40,000 is used for position sizing to invest in every trade and the remaining Rs60,000 is used to maintain buffer money to manage the MTM loss.
In case if you are new to backtesting in Amibroker then check out this tutorial.
Amibroker AFL code with Leverage Settings
EMA1 = EMA( C,20); EMA2 = EMA( C,50); /*Buy and Sell Condition */ Buy = Cross(EMA1,EMA2); Sell = Cross(EMA2,EMA1); leverage = 4; //4 times leverage SetOption("InitialEquity",100000); SetOption("accountmargin",100/leverage); SetOption("FuturesMode", False); SetPositionSize(40000*leverage,spsValue);
Here is the Backtesting result with 4 times leverage in Infosys on a daily timeframe,
If Leverage = 4
Returns with x4 times leverage = 71.46%
Drawdown (Max Risk incurred) = – Rs 62034
Here is the Back testing result with 1 times leverage(i.e no leverage) in Infosys on a daily timeframe,
If Leverage = 1 (i.e no leverage)
Returns with x4 times leverage = 17.73%
Drawdown (Max Risk incurred) = – Rs 15385
Controlling the Margin Via Backtesting Settings
One can control the leverage from the code itself as shown in the above afl code. Alternatively you can also control the investment capital and leverage from the Backtester settings. However if you are controlling from the code then code will take higher precedence.
Account margin setting defines the percentage margin requirement for the entire account. The default value of the Account margin is 100. This means that you have to provide 100% funds to enter the trade.
When you buy on margin you are simply borrowing money from your broker to buy stock. If your broker provides 4 times leverage then enter the account margin as 25%. If your initial equity is set to 100000 your buying power will be then 400000 (x4 times leverage) and you will be able to enter bigger positions than your initial capital.
In the next tutorial we will be discussing about how to set the margin trading for futures instrument.