• 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.

Most liked posts in thread: Amibroker FAQ

  1. Trav.

    Trav.

    Posts:
    734
    Likes Received:
    873
    Joined:
    Nov 27, 2017
    I was thinking that there a few users here and some novices like myself, so I better explain the snippets comment from above.

    In AFL Formula editor you can save the snippets of code that you create ( find while surfing the web etc ) and store in the library for future use. The images below show the default code ( green box ) and the user created code ( pink box ). When adding a snippet you will be prompted to add some properties as shown. Once this is done the file UserSnippets.xml is created or updated.

    upload_2020-5-27_6-45-13.pngupload_2020-5-27_6-45-58.png
     
    Lone Wolf, Skate, rnr and 3 others like this.
  2. Trav.

    Trav.

    Posts:
    734
    Likes Received:
    873
    Joined:
    Nov 27, 2017
    @Lone Wolf awesome stuff mate, works great and thanks for finding a fix.

    updated version of the AFL attached with fix for aligning foreign plots , Date on X axis, ATH in chart title and another column in the exploration called "Signal" to display buy / sell signal.

    Enjoy
     

    Attached Files:

  3. howardbandy

    howardbandy

    Posts:
    838
    Likes Received:
    93
    Joined:
    Jun 13, 2007
    Using the ValueWhen function might be easier.

    The key is this line:
    SellLowClose = C < ValueWhen( Buy, L );

    The ValueWhen function finds the bar of the most recent Buy,
    then returns the value of the Low on that bar.

    The right hand side of the assignment tests every bar to see if
    the Close is less than the Low of the bar of the Buy.
    If it is, set SellLowClose to True.


    Best, Howard

    ---------------------

    // ExitUsingValueWhen.afl

    // Buy when fast moving average
    // crosses up through slow moving average
    // Sell when the Close is lower
    // than the Low on the bar of the Buy

    FastMALB = 10;
    SlowMALB = 30;
    FastMA = MA( C, FastMALB );
    SlowMA = MA( C, SlowMALB );
    Buy = Cross( FastMA, SlowMA );
    SellMA = Cross( SlowMA, FastMA );

    SellLowClose = C < ValueWhen( Buy, L );

    //Sell = SellMA;
    Sell = SellLowClose;

    Buy = ExRem( Buy, Sell );
    Sell = ExRem( sell, Buy );


    Plot( C, "C", colorBlack, styleCandle );
    Plot( FastMA, "FastMA", colorGreen, styleLine );
    Plot( SlowMA, "SlowMA", colorBlue, styleLine );

    shapearrows = Buy * shapeUpArrow + Sell * shapeDownArrow;
    PlotShapes( shapearrows, IIf( Buy, colorGreen, colorRed ),
    0, IIf( Buy, Low, High ) );

    // end //
     
    Indoril, Gringotts Bank and rb250660 like this.
  4. julieta

    julieta

    Posts:
    9
    Likes Received:
    19
    Joined:
    Oct 19, 2014
    csnip.PNGcomsec.PNG
    step1 end of day prices step2 as in pic get date download you get all the warrants etc witch have to be deleted with a script
     
    CNHTractor, qldfrog and dutchie like this.
  5. Lone Wolf

    Lone Wolf

    Posts:
    436
    Likes Received:
    144
    Joined:
    Dec 4, 2008
    One thing that caught me out recently was the Drag-drop folder. I was working on some code and didn't realise it hadn't been moved out of the hidden folder yet. As I didn't have hidden folders displayed, when I copied the files over to my laptop to take away with me on a trip I didn't get a copy of the code I was working on.

    Make sure hidden folders are displayed if you want to copy everything.
    upload_2020-5-27_22-9-24.png
     
    Skate, qldfrog and Trav. like this.
  6. Lone Wolf

    Lone Wolf

    Posts:
    436
    Likes Received:
    144
    Joined:
    Dec 4, 2008
    It just depends whether you want to modify the original code.
    Insert makes a copy, so you can then modify that code all you want without worrying about overwriting the original code.

    Only use insert linked if you want to make and save changes to the original code.
     
    CNHTractor, Skate and Trav. like this.
  7. Trav.

    Trav.

    Posts:
    734
    Likes Received:
    873
    Joined:
    Nov 27, 2017
    @Roller_1 funny enough I came across this today and i'm sure that you could modify the CBT code to achieve what you want ? or at least give you some ideas

    The below example displays the yearly returns at the end of your back test report

    https://alvarezquanttrading.com/amibroker-sample-code/

    upload_2020-5-29_13-41-47.png

    Code:
    // Provided by Cesar Alvarez www.AlvarezQuantTrading.com
    // this code is for daily bar tests////////////////////////////////////////////////////////////
    // Repalce with your code
    SetOption("MaxOpenPositions",10);
    SetPositionSize(10,spsPercentOfEquity);Buy = RSI(2) < 5;
    Sell = RSI(2) > 80;Short = Cover = 0;
    // End of replace
    ///////////////////////////////////////////////////////////// Or you can add the following to the end of your current code
    // will not output anything if there are less than 200 bars in your test
    SetCustomBacktestProc("");
    if(Status("action") == actionPortfolio)
    {
        dnStart = Status("rangefromdate");
        dnEnd = Status("rangetodate");
        yrStart = int(dnStart/10000);
        yrEnd = int(dnEnd/10000);    bo = GetBacktesterObject();
        bo.backtest();    ver = Version();    if(BarCount > 200)
        {
            dt = DateNum();
            if(ver <= 5.4)
                eq = Foreign("~~~EQUITY", "C");
            else
                eq = bo.EquityArray();        // This could also be done by compressing to Monthly bars
            eqM = 0; // end of year equity - saving 2001 data in bar 101, 2002 in bar 102, ...
            for (i = 1; i < BarCount-1; i++)
            {
                // are we at the end of a year
                if (int(dt[i+1]/10000) > int(dt[i]/10000) )
                    eqM[int(dt[i]/10000)] = eq[i];
            }        // save the last equity
            eqM[yrStart-1] = GetOption("InitialEquity");
            eqM[yrEnd] = eq[BarCount-1];        // output the results
            for (i=yrStart; i<= yrEnd; i++)
            {
                bo.AddcustomMetric("" + (i + 1900) + " Ret % ", 100*(eqM[i]/eqM[i-1] - 1));
            }
        }
    }
     
    Warr87, qldfrog and Roller_1 like this.