Quantcast
Channel: Market Calls
Viewing all articles
Browse latest Browse all 2070

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

$
0
0

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.

Related Readings and Observations

  • Nifty Futures – State of the Market Before the Election Results WeekNifty Futures – State of the Market Before the Election Results Week The big week filled with events is about to start and its good time to have a check up what happened so far in this week and what is the current state of the market and what is the context going on.
  • TRIN finally shoots above 1TRIN finally shoots above 1   Nifty today(Aug 17,2009) Shoots abvove 1 indicating a strong bearishness for shorter term. Also broken the major supports 4530 levels as indicated in previous posts. Also […]
  • Copper Breaking Down?Copper Breaking Down? Now copper is trading around $2.8626 & as we can see on charts , after spending more than 10 trading session , finally copper providing a downside breakout with aggressive volume. The […]
  • Introduction to Option Action – Video TutorialIntroduction to Option Action – Video Tutorial In this video tutorial we will explore the basic features and functionalities of Option Action, How to create option strategies and how to interpret the payoff graph.
  • The Death of 25 PaisaThe Death of 25 Paisa From June 30 2011, 50 paise will be the minimum coin accepted in the markets as all denominations below it will cease to be legal currency. Also, the entries in books of accounts, pricing […]
  • O2A – Odin Diet to ASCII Data DownloaderO2A – Odin Diet to ASCII Data Downloader O2A - Odin to ASCII extracts Market Watch data from Odin Diet clients, and outputs the data to ASCII files. The program currently captures Equities, Futures & Options scrips of NSE […]

Viewing all articles
Browse latest Browse all 2070

Trending Articles