Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

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 Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Interactive Brokers – Smart Order Chart Trading Module using…

The IB Controller is an interface designed to facilitate automatic trading with AmiBroker and Interactive Brokers (IB) TWS (Trader Workstation). It serves as a...
Rajandran R
8 min read

Introducing OpenAlgo V1.0: The Ultimate Open-Source Algorithmic Trading Framework…

OpenAlgo V1.0 is an open-source algorithmic trading platform to automate your trading strategies using Amibroker, Tradingview, Metatrader, Python etc. Designed with the modern trader...
Rajandran R
2 min read

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 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