AFL Coding Help NEEDED

Discussion in 'Trading Software' started by inPeace, Aug 16, 2007.

  1. inPeace


    hi , i like to write a order-cancelling action with two chained conditions but dont know how.

    it's like this:
    given : A, B, C

    conidtion 1:
    IF last-price = A+ B, trigger condition 2
    condition 2:
    when last-price = A+C , Cancel buy or sell order

    in the above, condition1 must precede condition2 (OR without condition1, condition2 will not be triggerred)

    i'm using TWS platform.

  2. opm8


  3. inPeace


    i have sent it to them last night.
    still waiting for their approval to post. they may reject it.

    is there any other place to look for help? Thanks.
  4. They wont reject your post... It's an honest question....

    Beware, C = Close in AFL

    the basic syntax is this

    a1 = value;
    b1 = value;
    c1 = value;
    lastprice = Close;

    if lastprice = (a1+b1);
    condition 2;

    That is the basic syntax.... Need the rest of the formula to properly write condition 2

    In order for condition 2 to exist where are you generating the buy/sell order????
  5. inPeace


    Thanks :)
    i still cannot see my post at the yahoo group. well never mind.

    So do you mean i can put another "if" statement inside the first one?

    llike this:

    if lastprice = (a1+b1);
    if LASTPRICE = (a1+B1);






    A1 = 50;
    B1 = 50;
    C1 = 50;
    D1 = 50;

    //param trigger button to reset status
    resetStatus = ParamTrigger("resetStatus","resetStatus");

    DateNumNow = Now(3);
    DN = DateNum();
    TN = TimeNum();

    //check if order placed
    OrderPlaced = StaticVarGet("OrderPlaced");


    OrderPlaced = StaticVarGet("OrderPlaced");

    //check if order is triggered, place order if condition fullfiled
    if( (IsEmpty(OrderPlaced) OR OrderPlaced==0) )

    ibc = GetTradingInterface("IB");
    IBcStatus = ibc.IsConnected();

    if( IBcStatus )
    //Long side, place order


    LimitBuy = D1+A1;
    parentIDLong = ibc.PlaceOrder(Name(), "BUY", 1, "LMT", LimitBuy, 0, "DAY", False );
    ibc.PlaceOrder(Name(), "SELL", 1, "LMT", LimitBuy+B1, 0, "DAY", False, 1, "", parentIDLong );
    ibc.PlaceOrder(Name(), "SELL", 1, "TRAIL", C1, C1, "DAY", True, 1, "", parentIDLong );

    //short side, place order

    LimitSell = D1+B1;
    parentIDShort = ibc.PlaceOrder(Name(), "SELL", 1, "LMT", LimitSell, 0, "DAY", False );
    ibc.PlaceOrder(Name(), "BUY", 1, "LMT", LimitSell-D1, 0, "DAY", False, 1, "", parentIDShort );
    ibc.PlaceOrder(Name(), "BUY", 1, "TRAIL", C1, C1, "DAY", True, 1, "", parentIDShort );

  6. I see you are working on AT with ibc.....

    Refer to the ref guide for proper formatting of an if-else loop as it will not work on arrays...

    And yes, you can nest if-else statements. Be aware, if the variables inside the if-else loops are need elswhere you have to ensure they are static variables .... StaticVarSet, etc....

    iif works on arrays.....


    Try this...

    Condition1 = Lastprice == (a1+b1);
    Condition2 = Lastprice == (a1+C1);
    for ( i = 1; i < BarCount; i++ )
    if (Condition1)
    if (Condition2)


    Then use the cancel variable to initiate a cancel order. I dont see a cancel submission order in your AT code....

    Or you could write it like this

    cancel = iif(lastprice==(a1+b1),iif(lastprice==(a1+c1),True,False),False);

    Much simpler and more compact....

    Just to confirm, you are looking for when c1 and c2 are both met at the same time, correct?
  7. inPeace


    You cant see any "cancel submission" because i dont have any. i still a code idiot. just pick up somebody's creation and modify to see if it works for my need. kind of stealth-diy, hahaa

    so you mean it needs a "cancel variable" for the whole thing to work?

    i'm not sure the meaning of "both met at the same time" ...
    it should be like "without condition1, condition2 will not be triggered" or the condition for condition2 is condition1...

    precisely speaking, condition1 must be met BEFORE condition2 in time.. if condition1 is not met, even condition2 is met, it will not be triggered.

    nevertheless, i like the succint version very much. it is beautiful.
  8. Herman is the author of this code, and very helpful I might add. I was having issues with it sending multiple orders and he kindly recoded it on his end to eliminate the duplicates...

    Perhapsby contacting herman via email, he can add the cancellation code in it for you.....
  9. Graham (alis caveman) is good too . He'll do it for a very reasonble fee . jake
  10. I'm finding he is getting very hard to get a hold of Jake. Other people have complained on this board in the past, though I can vouch for the high quality of his work.

    I just hope this is temporary.
    #10     Aug 19, 2007