• Australian (ASX) Stock Market Forum

Hello and welcome to Aussie Stock Forums!

To gain full access you must register. Registration is free and takes only a few seconds to complete.

Already a member? Log in here.

Amibroker Backtest Script

Discussion in 'Software and Data' started by Pervaz, Dec 28, 2018.

  1. Pervaz

    Pervaz

    Posts:
    10
    Likes Received:
    3
    Joined:
    Dec 28, 2018
    Hi All

    Happy 2019 to everyone.
    I am a FX and Equities trader from the Sunny UK and recently moved to Aus.

    I have decided to move away from my MT4 platform and am using Amibroker. I am a trader not a programmer and having a difficulty to create a back test script.

    EMA30 = MA( C,30);
    Buy1 = EMA30 < C;
    Sell1 = EMA30 > C;

    SL_Buy = (H - L);
    SL_Sell = (H - L);

    TP_Buy = H + (H - L);
    TP_Sell = L- (H - L);

    BuyPrice = Close;
    SellPrice = Close;

    Buy = buy1;
    Sell = SL_Buy OR TP_Buy;
    Short = Sell1;
    Cover = SL_Sell OR TP_Sell;

    I am trying to create a back test script. I have set my Stop Loss and Take Profit, based on a simple moving average entry. However when i run the backtest function no trade has taken place, I checked the exploration which identifies the entry points.

    Any helping hands ?

    Regards
     
  2. Skate

    Skate

    Posts:
    1,149
    Likes Received:
    1,316
    Joined:
    Dec 28, 2013
    @Pervaz we have some of the best coders in the business here at ASF - many are on holidays.

    If you're eager for an answer there is a dedicated Amibroker forum to ask related questions

    https://forum.amibroker.com/

    Heads up

    Please take the time ask a good question.

    How to ask a good question
    First off follow the Amibroker forum rules & read "How to use the site" that's a given for any forum (but in this case it's a must)

    Use the search feature
    Please use the search field in the Amibroker forum, there are high chances that somebody already had the same problem and the answer is already available. If you have follow-up question, reply in existing thread instead of creating new topic.

    Make your question clear and provide necessary information

    When asking a question, imagine yourself as you were to answer your question.

    Is your question clear enough?

    Information
    Does your question include ALL necessary information (screenshots, formula) that is required for person who wants to help you, to know what the problem is, without seeing your computer screen?

    Do not leave the room for the guessing work.

    Here are some tips
    what you did?
    what you expected as result?
    why you need it to happen?
    what actually happened instead?
    what are the challenges?

    Very Important
    Be polite when asking questions. Use proper grammar and avoid spelling mistakes. Make sure you post the code that you wrote

    Code Tags
    To ask an Amibroker question I'm assuming you have Amibroker software v6 or above (lower versions are not supported)
    Use code tags so your formula is properly displayed. If the code produces some unexpected output, please include the snapshot of chart or Analysis and explain why do you think it is incorrect

    If you want to comment on somebody's else code you need to provide original reference (where did you get the code from)

    Explain the GOAL
    @Pervaz give members at the Amibroker forum all the background Information and explain the GOAL you want to accomplish

    Often for someone to give you a proper solution, it is very important to know why you want to do something. Usually there are dozens of ways to achieve the same thing and best solution depends on knowing exactly why you need to do it.

    Background
    @Pervaz without knowing the background to your question they will be lost, their guesses and assumptions may be totally incorrect and you will be wasting their time and your time too.

    Skate.
     
    Pervaz likes this.
  3. Pervaz

    Pervaz

    Posts:
    10
    Likes Received:
    3
    Joined:
    Dec 28, 2018
    Hi Skate

    Thanks for the heads up and valuable netiquettes here

    Regards
     
    Skate likes this.
  4. Sir Burr

    Sir Burr

    Posts:
    482
    Likes Received:
    31
    Joined:
    Feb 15, 2006
    I cut and paste the above code into the Formula Editor and sent to Analysis Window.

    Set the Filter to a Watchlist (ASX100) and ran a backtest. Results for the backtest are showing for me.

    Have you set Initial Equity, Position, Periodicity Commissions etc in Settings as they are not in the code?
     
  5. jjbinks

    jjbinks

    Posts:
    163
    Likes Received:
    35
    Joined:
    May 14, 2013
    SL_Buy and SL_Sell are the same thing hence your sell and cover are the same. So you would be generating sell and cover signals simulatenously.

    Also H-L seems like an odd signal to buy or sell
     
  6. Pervaz

    Pervaz

    Posts:
    10
    Likes Received:
    3
    Joined:
    Dec 28, 2018
    Hi

    Thanks for your response jjbinks and Sir Burr - i discovered that the SL and TP were incorrect <gggrr>

    I didnt think it would be that difficult to create a back testing template which i could re-use again
    Have a great and propserous New Year

    Pervaz
     
  7. Pervaz

    Pervaz

    Posts:
    10
    Likes Received:
    3
    Joined:
    Dec 28, 2018
    Hi all,

    I am using what i thought was a simple testing strategy. Long only for engulfing candles to buy at tmrws price, with a stop loss of todays close and a take profit of the todays difference between high and Low.

    When i run it thru the explore function it correctly identifies the entry points.

    Question: However when i run it thru the back test function it doesnt apply the SL (if it gets hit) or hit the TP level. I have tried to implement the ApplyStop function but that doesnt seem to pick up the SL and TP levels

    Any helping hands ?

    Regards


    SetTradeDelays(1,1,1,1);

    /*Long only for Bull Engulfing */
    BullEB = Ref(C,-1) < Ref(O,-1) AND C > O AND O <=Ref(C,-1) AND C > Ref(O,-1);
    /*Stop Loss - Low of current candle*/
    SL_Buy = L;
    /* Take profit - length of todays candle*/
    TP_Buy = C + (H - L);


    /*Long only for Bull Engulfing */
    Buy = BullEB;
    /* Buy at tommorow open*/
    BuyPrice = Ref(Open,1);
    /* Close at TP or SL*/
    Sell = SL_Buy OR TP_Buy;

    /* Filter to check values = */
    Filter = Buy;
    AddColumn (H,"H");
    AddColumn (L,"L");
    AddColumn (C,"C");
    AddColumn (Ref(Open,1),"Tmrw_O");
    AddColumn (Buy,"Buy");
    AddColumn (BuyPrice,"BuyPrice");
    AddColumn (SL_Buy,"SL");
    AddColumn (TP_Buy,"TP");
     
  8. captain black

    captain black

    Posts:
    1,136
    Likes Received:
    232
    Joined:
    Oct 24, 2005
    Hi @Pervaz,
    Apologies for the brief reply, I'm on holidays with a patchy 3G connection but saw your post and thought I'd help point you in the right direction.

    Buy and Sell need to be Boolean statements (ie either True or False).

    SL_Buy = L; simply returns the value of the Low array.

    The Set trade delays function set to (1,1,1,1) takes care of your trade delays.
    BuyPrice = Ref(Open,1); is looking into the future at tomorrows Open price.

    Amibroker is very much based on formulas and code, if you intend to use it then learning how to code is vital.

    As a start I would recommend reading through the manual and especially all the tutorials. The link below is a tutorial on backtesting:

    https://www.amibroker.com/guide/h_backtest.html
     
    jjbinks and Skate like this.
  9. Pervaz

    Pervaz

    Posts:
    10
    Likes Received:
    3
    Joined:
    Dec 28, 2018
    Hi Captain Black

    Happy New Year.

    Thanks for the tips - very invaluable - i agree with your comment regarding learning to code with Amibroker and this was my first attempt at creating a backtest script here.

    BullEB = Ref(C,-1) < Ref(O,-1) AND C > O AND O <=Ref(C,-1) AND C > Ref(O,-1);
    BearEB = Ref(C,-1) > Ref(O,-1) AND C < O AND O >= Ref(C,-1) AND C < Ref(O,-1);

    /*Long only for Bull Engulfing */
    Engulfing = BullEB;
    Buy = C > EMA(C,30) AND Engulfing;

    /* Short */
    EB = BearEB ;
    Short = C < EMA(C,30) AND EB;

    /* Buy at tomorrow open*/
    BuyPrice = Ref(Open,1);
    SellPrice = Close;

    /* Short at tomorrow open*/
    ShortPrice = Ref(Open,1);
    CoverPrice = Close;

    /*Stop Loss - Low of current candle*/
    SL_Buy = BuyPrice - L;
    SL_Short = ShortPrice - H;

    /* Take profit - length of todays candle*/
    TP_Buy = (H - L);
    TP_Cover = (H - L);


    /* Close at TP or SL*/
    Sell = 0;
    Cover = 0;
    ApplyStop(stopTypeLoss, stopModePoint, SL_Buy);
    ApplyStop(stopTypeProfit, stopModePoint, TP_Buy);
    ApplyStop(stopTypeLoss, stopModePoint, SL_Short);
    ApplyStop(stopTypeProfit, stopModePoint, TP_Cover);

    Ive been reading all the tutorials and have managed to patch a simple engulfing script and managed to back test it just to see the power of Amibroker. Ive purchased the professional package after a recommendation from another trading friend who has been using it for a few years and I am very impressed.



    Regards
     
    captain black likes this.
  10. captain black

    captain black

    Posts:
    1,136
    Likes Received:
    232
    Joined:
    Oct 24, 2005
    A Happy New Year to you too @Pervaz :)

    Your Buy/Sell/Cover/Short code are all Boolean now, good stuff.

    I'll repeat what I wrote in my previous post as understanding signal timing is important before moving on to more complex code.

    To buy at tomorrows open you would use:

    Code:
    Settradedelays(1,1,1,1)
    BuyPrice = Open
    
    The Settradedelays function takes care of your trade delays.

    Code:
    BuyPrice = Ref(Open,1);
    
    Your code above looks 1 bar further into the future.

    The same with your Sellprice statement.

    I would also recommend rereading the tutorial on backtesting and applystop as your applystop calculations also appear to be incorrect.
     
    Pervaz likes this.
  11. Pervaz

    Pervaz

    Posts:
    10
    Likes Received:
    3
    Joined:
    Dec 28, 2018
    Hi CaptainBlack

    Apologies for the delay in replying.

    Ive been looking at the code and re-reading the tutorials but seem to have hit a stumbling block.

    Ive created an empty database and loaded up FX prices (EUR/USD only for hourly prices) and am using a dummy strategy to test.

    The rules are very simple: buy on engulfing candle with price above a 30 EMA. SL = Low (or High for a bear). TP = the difference between the H and L (so create a 1:1 R)

    ---- Code
    SetTradeDelays( 1, 1, 1, 1 );
    SetOption("AllowSameBarExit",False);
    SetOption( "ActivateStopsImmediately", False );
    ExitAtStop = 0;

    BullEB = Ref(C,-1) < Ref(O,-1) AND C > O AND O <=Ref(C,-1) AND C > Ref(O,-1);
    BearEB = Ref(C,-1) > Ref(O,-1) AND C < O AND O >= Ref(C,-1) AND C < Ref(O,-1);

    /*Long only for Bull Engulfing */
    Engulfing = BullEB;
    Buy = C > EMA(C,30) AND Engulfing;

    /* Short = BearEB ; */
    EB = BearEB;
    Short = C < EMA(C,30) AND EB;

    /* Buy at todays close*/
    BuyPrice = Close;
    SellPrice = Close;

    /* Short at todays close*/
    ShortPrice = Close;
    CoverPrice = Close;

    /*Stop Loss - Low of current candle*/
    SL_Buy = Low;
    SL_Short = High;

    /* Take profit - length of todays candle*/
    TP_Buy = (H - L);
    TP_Cover = (H - L);


    /* Close at TP or SL*/
    Sell = 0;
    Cover = 0;
    ApplyStop(stopTypeLoss, stopModePoint, SL_Buy);
    ApplyStop(stopTypeProfit, stopModePoint, TP_Buy);
    ApplyStop(stopTypeLoss, stopModePoint, SL_Short);
    ApplyStop(stopTypeProfit, stopModePoint, TP_Cover);

    /* Filter to check values = */
    Filter = Buy OR Sell OR Short OR Cover;
    AddColumn (H,"H",1.5);
    AddColumn (L,"L",1.5);
    AddColumn (BuyPrice, "BuyPrice",1.5);
    AddColumn (SellPrice, "SellPrice",1.5);
    AddColumn (SL_Buy, "SL_Buy",1.5);
    AddColumn (TP_Buy, "TP_Buy",1.5);

    The indicator itself works as I went thru the data and manually checked it - the arrows mark up.

    However when i run the explorer many signals are missing :(

    Can you give a hint where i am going wrong ?

    Regards
     
  12. Pervaz

    Pervaz

    Posts:
    10
    Likes Received:
    3
    Joined:
    Dec 28, 2018
    Hi Captain black

    Updated: I saw the issue and have resolved it and all is working

    Thanks for your help

    Pervaz
     
  13. captain black

    captain black

    Posts:
    1,136
    Likes Received:
    232
    Joined:
    Oct 24, 2005
    Thanks for taking the time to say thanks :)

    Happy to help.
     
Loading...

Share This Page