Trailing Stop EA Problem

 

Hi everyone, I'm new here and I'm new to FX and EA writing in general. I've been working on an EA that would change my Stoploss once the price has moved a certain amount. But, I can't get it to work and I can't find the solution.

So any Help would be greatly appreciated!

Here is the code :

bool SMod = false;
    if (OrderSelect(STicket, SELECT_BY_TICKET))
     {
      if (OrderType() == OP_BUY && Ask >= OrderOpenPrice() + 20 * Point)
       {
        SMod = OrderModify (STicket, OrderOpenPrice(), OrderStopLoss() + 20 * Point, OrderTakeProfit(), 0);
        if (!SMod)
         {
          Alert("OrderModify Start Sequence FAILED!", GetLastError());
         }
        else
         {
          Alert("OrderModify SUCCESS");
         }
       }
      if (OrderType() == OP_SELL && Bid <= OrderOpenPrice() - 20 *Point)
       {
        SMod = OrderModify(STicket, OrderOpenPrice(), OrderStopLoss() - 20 * Point, OrderTakeProfit(), 0);
        if (!SMod)
         {
          Alert("OrderModify Start Sequence FAILED!", GetLastError());
         }
         else
         {
          Alert("OrderModify SUCCESS");
         }
       }
     }
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. if (OrderSelect(STicket, SELECT_BY_TICKET))
    EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush/file) of ticket numbers required.
  3. You select the ticket but you have no idea if it has opened (OrderType is still pending) or closed (OrderCloseTime non-zero.)
  4.  if (OrderType() == OP_BUY && Ask >= OrderOpenPrice() + 20 * Point){
         SMod = OrderModify (STicket, OrderOpenPrice(), OrderStopLoss() + 20 * Point, OrderTakeProfit(), 0);
    As soon as the market up moves 20 points (2 pips/5 digit broker) you move the SL up 20 points (to Break even.) Then on the very next tick you again try to move it up 20 points more. Compute your new stop loss and check if it is above the current value and above the OrderOpenPrice, then modify.

  5. There is Tick, PIP, and Point. They are all different in general. A tick is the smallest change of price. A Point is the least significant digit quoted. In currencies a pip is defined as 0.0001 (or for JPY 0.01)

    On a 4 digit broker a point (0.0001) = pip (0.0001). [JPY 0.01 == 0.01] On a 5 digit broker a point (0.00001) = 1/10 pip (0.00010/10). Just because you quote an extra digit doesn't change the value of a pip. (0.0001 == 0.00010) EA's must adjust pips to points (for mq4.) In currencies a tick is a point. Price can change by least significant digit (1.23456 -> 1.23457)

    In metals a Tick is still the smallest change but is larger than a point. If price can change from 123.25 to 123.50, you have a TickSize of 0.25 and a point of 0.01. Pip has no meaning.

    This is why you don't use TickValue by itself. Only as a ratio with TickSize. See DeltaValuePerLot()


 
WHRoeder:
  1. There is Tick, PIP, and Point. They are all different in general. A tick is the smallest change of price. A Point is the least significant digit quoted. In currencies a pip is defined as 0.0001 (or for JPY 0.01)

    On a 4 digit broker a point (0.0001) = pip (0.0001). [JPY 0.01 == 0.01] On a 5 digit broker a point (0.00001) = 1/10 pip (0.00010/10). Just because you quote an extra digit doesn't change the value of a pip. (0.0001 == 0.00010) EA's must adjust pips to points (for mq4.) In currencies a tick is a point. Price can change by least significant digit (1.23456 -> 1.23457)

    In metals a Tick is still the smallest change but is larger than a point. If price can change from 123.25 to 123.50, you have a TickSize of 0.25 and a point of 0.01. Pip has no meaning.

    This is why you don't use TickValue by itself. Only as a ratio with TickSize. See DeltaValuePerLot()



Thank you for your very helpful comment. But, I still have some problems :

1. I didn't understand the part 5 of your comment. I use a 5 Digit broker (5 for pairs like EURUSD and 3 for USDJPY). In EA I want to change my Stoploss by for example 0.020 (20 pipettes or 2 pips) each time the market moves by 20 pipettes (0.020), now I don't understand how to incorporate your TickValue TickSize ratio in the code.

2.After using your tips I've changed the code to the following, but i get the Error #1 ("No error returned but the result is unknown"):

extern int TL1 = 20;

void OnTick()
  {
    int    STicket         = 0;
    bool   Smod            = false;
    bool   SMod            = false;
    double UpIndicator     = 0;
    double DownIndicator   = 0;
    for (int i= 0; i<=OrdersHistoryTotal(); i++)
     {
      Smod = OrderSelect(i, SELECT_BY_POS);
      if (Smod == true)
       {
        STicket = OrderTicket();
        UpIndicator     = OrderOpenPrice() + TL1 * Point;
        DownIndicator   = OrderOpenPrice() - TL1 * Point;
       }
     }
     if (OrderCloseTime() == 0)
      {
       if (OrderType() == OP_BUY && Ask >= UpIndicator)
        {
         UpIndicator = UpIndicator + TL1 * Point;
         SMod = OrderModify (STicket, OrderOpenPrice(), OrderStopLoss() + TL1 * Point, OrderTakeProfit(), 0);
         if (!SMod)
          {
           Alert("OrderModify FAILED! Error : ", GetLastError());
          }
        } 
       if (OrderType() == OP_SELL && Bid <= DownIndicator)
        {
         DownIndicator = DownIndicator - TL1 * Point;
         SMod = OrderModify (STicket, OrderOpenPrice(), OrderStopLoss() - TL1 * Point, OrderTakeProfit(), 0);
         if (!SMod)
          {
           Alert("OrderModify FAILED! Error : ", GetLastError());
          }
        } 
      }
  }
 
  1. Gabaloo: In EA I want to change my Stoploss by for example 0.020 (20 pipettes or 2 pips)
    UpIndicator += TL1 * Point;
    N * Point is n pipettes on a 5 digit broker but n * pips on a 4 digit broker. Reread "EA's must adjust pips to points."
  2. Compute your new stop loss and check if it is above the current value and above the OrderOpenPrice, then modify.
    Reread that. You aren't computing the new SL relative to the market (trailing,) and modifying the order to the new SL.
 
WHRoeder:
  1. Gabaloo: In EA I want to change my Stoploss by for example 0.020 (20 pipettes or 2 pips)
    N * Point is n pipettes on a 5 digit broker but n * pips on a 4 digit broker. Reread "EA's must adjust pips to points."
  2. Compute your new stop loss and check if it is above the current value and above the OrderOpenPrice, then modify.
    Reread that. You aren't computing the new SL relative to the market (trailing,) and modifying the order to the new SL.

Oh wow, thank you so much for tips, I also noticed something else, my broker's platform (Alpari's Metatrader) doesn't yield correct results on Saturdays and Sundays , I'll have to wait till Monday to see if it finally works and then I'll post the results.

I would like to get better at EA programming and mql4 in general, I guess I have to write more programs and find some books online to read.

Thanks again.

 
Gabaloo: I also noticed something else, my broker's platform (Alpari's Metatrader) doesn't yield correct results on Saturdays and Sundays , I'll have to wait till Monday to see if it finally works and then I'll post the results.
Market is closed on the weekend; you can't get any results on Saturdays and Sundays live. You can always use the strategy tester in visual mode.