Results 1 to 7 of 7

Thread: Almost ready EA, code works for BUY orders, but not SELL orders

  1. #1
    Hi all.

    I'm new on this forum, but not new to guide trading and EA's. I'm very happy to see the activity on Cliqforex and looking to get to know you.

    I'm just about ready with my little simple EA, but based on coding the SL ordermodify for brief trades.

    What I'm trying to achieve is that;
    When price reaches 20pips profit: adjust SL from -40pip to -20pips. (Reduce SL in half)
    When price reaches 40pips profit: Trailingstop kicks in. (Breake even )

    This works perfectly well for LONG positions, but maybe not for brief positions.
    If anyone can spot a clear error/solution, I'd be eternally grateful.

    Here is the code :

    Inserted Code dual newSL; if((Bid-OrderOpenPrice())gt;=200*Point) newSL=OrderOpenPrice()-200*Point; if((Bid-OrderOpenPrice())gt;=400*Point) newSL=Bid-(TrailingStop*Point); newSL=NormalizeDouble(newSL,Digits); if(newSLgt;OrderStopLoss()) OrderModify(OrderTicket(),OrderOpenPrice(),newSL,O rderTakeProfit(),0,Green); return(0); } else // move to brief position if((OrderOpenPrice()-Ask)gt;=200*Point) newSL=OrderOpenPrice() 200*Point; if((OrderOpenPrice()-Ask)gt;=400*Point) newSL=(TrailingStop*Point) Ask; newSL=NormalizeDouble(newSL,Digits); if(newSLlt;OrderStopLoss() }
    Thanks beforehand,
    Oddvin

  2. #2
    Quote Originally Posted by ;
    if((OrderOpenPrice()-Ask)gt;=200*Stage) newSL=OrderOpenPrice() 200*Stage;
    if((OrderOpenPrice()-Ask)gt;=400*Stage) newSL=(TrailingStop*Stage) Ask;
    Maybe this really is a typo and has to be such as that:
    Inserted Code newSL=NormalizeDouble(#91;colour=Red#93;newSL#91;/colour#93;,Digits);
    PS: Please wrap your code into CODE tags when posting here from the forum to preserve the formatting. Additionally it's always considered bad style (bad readybility) to use the short form for one-line if constructs. You should do readers of your code a favor and use the long syntax, although it's allowed to leave away the braces.

  3. #3
    7bit, thank you for your observation. Regrettably, it was a typo in my post, but maybe not the reason behind the SELL ordermodify error.

    What is happening is very strange; as soon as a SELL order is opened (with correct S/L), it modifies and eliminates the S/L completely. When price reaches the first target ( 20pip) in my favour, it adds a new S/L and if price goes bacwards, it eliminates the S/L again.

    As to your other comment, I am quite new to communiing and additionally FX forums, so please excuse me if I am not after common forum integrity. Do you mean that I should post the EA code?

    Thank you for your kind help.

  4. #4
    Quote Originally Posted by ;
    Do you believe I should post the entire EA code?

    Thank you for your kind help.
    The objective is allowing you to sucessfully fix this issue. First you should learn to make extensive usage of this Print() work, this is the most important friend when it comes to debugging.

    Go through your code as though you were the pc (where you feel the computer goes when after the code) and print at each step every factor that may have some influence to make sure the computer does really exactly what you think it would do.

    Make it so it literally tells you what exactly it is doing and what's going on and if it implements, this will quickly show you where the issue is.

    Inserted Code Print(entered instant if division.) ; Print(Order has this SL: , OrderStopLoss()); Print(newSL currently has this respect: , newSL); Print(calling OrderModify() with these values: newSL, ..., ...);

  5. #5
    Thank you for inputs and your tips.

    I played with some tweakings to no usage and almost gave in lol. I then understood that the ea initially recognized the newSL value (such as SELL orders) as zero, thus instantly altered the order free of SL.

    Once I put this simple line on top: newSL=OrderStopLoss(); , the issue vanished. I managed to implement TrailingStep. No big thing, but for a newbie coder like me it is a terrific accomplishment and very useful:--RRB-

    NEW CODE:

    Inserted Code else // move to short standing{ newSL=OrderStopLoss(); if((OrderOpenPrice()-Ask)gt;200*Stage) newSL=OrderOpenPrice() (200*Stage); if((OrderOpenPrice()-Ask)gt;400*Stage) newSL=Ask (Point*TrailingStop);{ newSL=NormalizeDouble(newSL,Digits); if(((OrderStopLoss()-(TrailingStep*Stage))gt;newSL) || (OrderStopLoss()==0)) OrderModify(OrderTicket(),OrderOpenPrice(),newSL,O rderTakeProfit(),0,Red); return(0);

    I do not know why this is required for Economy orders only, however seem to work seamlessly.

  6. #6
    Quote Originally Posted by ;
    I don't know why this is needed for Sell orders only, but seem to function seamlessly.
    Because a stop loss of 0 signifies no stop loss. And if comparing current price with current stop loss then 0 will always be smaller compared to the price for buy orders, but when you've got a sell order then 0 will mean the stop is completely on theother* side (already trailed too much), so it'd never track it. You need to take care of the 0 separately for market orders, for buy orders the 0 is currently on the right side of this trade.

    I did not see it when looking at your code, though I have had this problem myself some months ago. I solved it in my library of functions and since then never needed to consider it.

  7. #7
    You're of course right. Once I think through it it make sense. Great feeling to get it solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •