Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

How to Set Leverage in Amibroker While Backtesting a Trading System?

1 min read

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.

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Amibroker Execution Module – Hedged Index Futures for Directional…

In the last tutorial, we learn the importance of Hedging Futures Position with OTM Options for Reduced Margin. This tutorial focus on how to...
Rajandran R
14 min read

Multi Legged Options Execution – Amibroker Module – Button…

This tutorial provides you a basic framework for sending multi-legged options module and how to exit the multi-legged option module using a simple button...
Rajandran R
8 min read

Sending Option Orders and Exiting Option Orders in Algomojo…

This tutorial helps you to understand the options trading module and the requirements to set up and send and exit option orders from Amibroker...
Rajandran R
5 min read

One Reply to “How to Set Leverage in Amibroker While Backtesting a…”

  1. Hello Rajandran,
    This query is not related to the above mentioned topic, so do take note of this, I want an afl in which the cross hairs are synchronized on multiple windows, can you help?
    Thanks,
    Regards,
    Sudhin Bathija

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More