Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. 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

Backtesting Template for Testing Future Scripts in Amibroker

1 min read

For a Non Programmers it is really challenging to understand how to backtest future scripts in Amibroker. To solve this issue i had created a simple backtesting template where most of your backtesting settings are eliminated and its quite easier to understand too.

Backtesting Template

1)Download Backtesting Template for Amibroker
2)Unzip to local folder and copy backtest.afl to c:/program files/amibroker/formulas/include folder
3)Now add #include <backtest.afl>  at the beginning of your trading system afl.
4)Goto New Analysis->Select your trading system to be backtested for futures mode and select the script to be backtested.
4)Goto Backtester Settings and Select the Positions – Long/Short and Select the required Periodicity as shown below and Press the Backtest Button.

Bactester Settings

[wp_ad_camp_5]

 
Backtesting Template Explained

In this case we taken nifty futures as an example with an Initial Capital of 2 Lakhs and the goal is to trade a trading system with fixed shares 100 shares i.e 2 lots of Nifty every time. Rs100 of fixed Brokerage cost(Brokerage+Slippage+Transaction Cost) included in every leg of transaction.

https://gist.github.com/5e4a87f337f1786dc8c7

SetTradeDelays – SetTradeDelays allows you to control trade delay. For example if your trade occurs at the end of the candle and if you want to execute your Buy/Sell/Long/Short orders then you can settradedelays(1,1,1,1) and generally settradedelays(0,0,0,0) represents no delay in execution.

Setpositionsize – This function defines how many number of shares you want to trade every time. Can be varied according to your trading style.

InitialEquity – Represents the Initial Capital Required for Backtesting Future data. And it can be varied according to your trading style and capital requirement to be backtested.

RoundLotSize – Roundlotsize is where you have to define your lot size and in case of nifty it is 50. You have to vary it according to the instrument you are willing to backtest.

CommissionMode – You can set the commissionmode depends upon how your broker charges you. Set it according to your type of broker or brokerage plan you adopted. Shown the values to be setted for different style of brokerage commissions.

0 – use portfolio manager commission table
1 – percent of trade
2 – Rs per trade
3 – Rs per share/contract

CommissionAmount – The amount to be charged for one leg of transaction and once again it depends upon your brokerage plan.

MarginRequirement – This represents your broker margin to be placed for nifty futures and could vary from broker to broker. Nominally for Nifty the brokerage margin comes around 10%.

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. 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

Fisher Transform – Amibroker AFL Code

The Fisher Transform is a technical analysis indicator developed by John F. Ehlers, designed to identify potential turning points in the price movements of...
Rajandran R
2 min read

My Experience Converting SSL Hybrid from PineScript to Amibroker…

As a trader and coding enthusiast, I often find myself needing to translate trading indicators from one programming language to another. Recently, I embarked...
Rajandran R
8 min read

Line Trading – OpenAlgo Automation Module for Amibroker Users

The line Trading Automation tool is designed for Manual traders who want to perform level-based trade execution faster and also bring some advanced trade...
Rajandran R
59 sec read

10 Replies to “Backtesting Template for Testing Future Scripts in Amibroker”

  1. Hi ,

    Can you frame me code for back testing a entire portfolio.

    Where initial investment is ” Rs. 100000″

    Per Script only Rs 10000 are Allowed to trade ( for N num of shares)
    i.e.
    LT share trading at 1400 so automatically Rs 10000/1400= 7 shares only
    Andra bank trading at 60 so automatically Rs 10000/60= 166 shares only

    Like this Max open scripts position anytime should 5 only.

    i would be very thankful if you can please help me out

    thanks in advance
    regards
    Sameer

  2. Why this does not work for bigger lot sizes. Was trying for VEDL

    SetTradeDelays( 0, 0, 0, 0 );
    RoundLotSize = 3500;
    SetPositionSize ( 3500,spsShares);
    SetOption( “InitialEquity”, 50000 );
    SetOption( “MinShares”, 1 );
    SetOption( “MinPosValue”, 1 );
    SetOption( “FuturesMode”, True );
    SetOption( “AllowPositionShrinking”, True );
    SetOption( “ActivateStopsImmediately”, False );
    SetOption( “ReverseSignalForcesExit”, False );
    SetOption( “AllowSameBarExit”, False );
    SetOption( “CommissionMode”, 2 );
    SetOption( “CommissionAmount”, 20 );
    SetOption( “InterestRate”, 0 );
    SetOption( “MarginRequirement”, 10 );
    SetOption( “AccountMargin”, 10 );

    always saying no results found

  3. How do you backtest contractwise data,is there a way to stitch the data or is there anything else that can be done,please advice.

  4. Hi Rajandaran,

    You are doing a great job.
    I had one question.
    How one can back test three intraday trading systems simultaneously.
    With one common equity curve.

    Say for an ex
    1. Ema crossover based
    2. Bollinger band
    3. Supertrend based.

    Please advise

    All running on 5 mins interval.

    Regards,

    Yogesh

  5. How one can back test three different systems ema cross MACD and super trend with same Exit and on same time frame (5 Mins) and generate single equity curve.

    Thanks.

  6. afl code where we see live ltp o h l c % change change like nse

    Sectoral Indices :

    Broad Market Indices :

Leave a Reply

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