Amibroker AFL: help with formula (If... wait, condition?)

Discussion in 'App Development' started by TomKrol, Mar 20, 2016.

  1. TomKrol

    TomKrol

    Hello,

    I am beginner at coding and would like to ask for your support.
    I have some simple formula in Amibroker (afl language):

    Code:
     buyCon1 = Cross( MACD(), Signal() );
     BuyCon2 = Cross(MA(C,30),MA(C,10));
    
     Buy = buyCon1 AND buyCon2;
    
    Question: how can I add to the above something like:
    If buyCon1 is true LOOK for 5 days/sessions for BuyCon2 signal.
    If both are true: buy, if not: don't buy.

    From my perspective it's terribly complicated, I would appreciate any advice.

    Thanks!
    Tom Krol
     
  2. Metamega

    Metamega

    Welcome to the struggle. I've been slowly learning AFL and quite enjoy it. It's as easy or complex as you want it to be. For my needs it seems like a perfect solution compared to Python or R. Just having a lot of functions built in and knowing their solid is great. I couldn't imagine making my own back testing engine. The back tester also has a lot of flexibility if you want to get into the custom back tester options which I haven't had a need for.

    I'd sugges the yahoo amibroker forum for questions like this. Little slow at times but as long as you come in with some AFL and the issue someone will chime in. Usually anyone who come in and ask "I need this code" with no attempt at trying themselves just gets moved a long. It'd be endless if everyone coded the requests and everyone has their own projects their working on besides wasting time on writing some of the ridiculous requests I've seen. But something like yours would get a solid reply.

    As a beginner myself I think the easiest option is using the BarsSince(Array) function. It calculates the bars since the array was true.

    So say you wanted it to be 5 days bang on....

    buyCon1 = Cross( MACD(), Signal() );
    BuyCon2 = Cross(MA(C,30),MA(C,10));
    Entry = BuyCon2 AND BarsSince(buyCon1) == 5;

    Buy = Entry ;
    Sell = 0;

    Perhaps you wanted within a 5 day window, just change the == to < 5 , etc.

    Also be aware of how AFL counts bars. For instance in a back test, it will label a trade which entered on a bar and exited the next bar as a holding day of 2 bars.

    If you wanted to fool around with it visually you could try using some Plot functions and opening it in a new chart and adding the AFL or drag and clicking the AFL to the chart.

    buyCon1 = Cross( MACD(), Signal() );
    BuyCon2 = Cross(MA(C,30),MA(C,10));
    Entry = BuyCon2 AND BarsSince(buyCon1) == 5;

    Buy = Entry ;
    Sell = 0;

    PlotShapes(shapedigit1*buyCon1,colororange);
    PlotShapes(shapedigit2*buycon2,colorlightblue);
    PlotShapes(shapeUpArrow*Entry,colorgreen);

    Just put that in a charts AFL ( I usually make a new blank chart as to not screw with my templates) and just scroll a chart and look for the shapes and make sure everything is happening when it should. It'll plot a shapedigit1 at each con1 and shapedigit 2 at buy2 and when the 2 line up for entry at Entry it should plot a uparrow. Good way to verify AFL visually.


    Edit: It does get frustrating at times to try and program an idea. It's very easy to have an idea of what you want to do but to program it in any language in a mathematical form that a computer can understand is very difficult. It is satisfying though when you do nail what you wanted to do though.
     
    TomKrol likes this.
  3. Alex27

    Alex27

    Have you compared Amibroker to Sierra Charts for automation? I think there both good but keen to hear from someone who has used both.
     
  4. Metamega

    Metamega

    I've never looked into it or had the desire for auto trading. Here's a good old thread on someone's setup using Amibroker to develop and Openquant for execution.


    http://www.elitetrader.com/et/index...ding-robust-automated-trading-systems.125961/

    Theirs a few posts in Amibroker user guide about using automation. Could check out the Yahoo group also for some info. I know theirs a beta for automation with IB. I think it's been beta forever and probably just a liability Amibroker wants to avoid by saying official. They have all the tools with the source code for plug ins and such but it's way far beyond my knowledge of programming.

    When you have to start getting into all the looping and static var stuff like the examples I've seen, it's beyond my needs. But experienced programmers seem to grasp it easy.
     
  5. M.ST.

    M.ST.

    If people would care to read then they would understand.
    IBcontroller of AmiBroker is called beta only for the reason of future TWS/API changes.
    It's even mentioned on their website http://www.amibroker.com/at/index.html#BETAFAQ

    Other than that IBcontroller runs just fine with current TWS/API.
     
    Alex27 likes this.
  6. Metamega

    Metamega

    Thanks for the link. I've just seen that topic on here before about it being beta. Just throwing some guidance to him on what to look for. It's not something I've read up on myself.

    The documentation for Amibroker is sometimes overwhelming. Devlogs, Amibroker knowledge base, the user knowledge base, tutorials, articles, not to mention the monstrous manual, I'd be lying if I said I've come close to reading all the documents.
     
  7. M.ST.

    M.ST.

  8. TomKrol

    TomKrol

    @Metamega: thank you so much for such a great response! Looks like BarsSince() function will do just fine in this situation - I tried to find something like this in the manual but without success.
    Thanks for letting me know about yahoo amibroker forum as well, have to agree that it's disrespectful to simply ask "write this code for me" so even though I'm not experienced in coding I always at least try to add sth from myself when asking question :)

    Thanks once again!
    Tom
     
  9. Metamega

    Metamega

    https://www.amibroker.com/guide/a_funref.html

    Here's a good link to just go through the multiple built in functions and see their description.

    Always plenty of ways to do achieve the same goal, usually theirs one that's more efficient though.

    Feel free to pm me whenever If you want with basic questions. I'm no expert but I do seem to learn more when I try to solve other people's issues.
     
  10. M.ST.

    M.ST.

    Bad idea.

    Because of

     
    #10     Mar 21, 2016