Multi-Currency EA - Problem with TP & SL not getting to OrderSend.

 

So I’m working on my first Multi-Currency EA from the template given by DADALI ARWALY
https://www.mql5.com/en/code/28181.

I’m using <Deleted> as my Buy Sell Signal and TP & SL.
This works very well now but I’m having one problem that only happens sometimes and that is that my EA don’t ad the TP & SL to the order, so the order is without TP & SL. I need help finding out why this is happening and how to prevent it.




The Signal:

int Signal(string sym)
  {
  if(lastTradeBar!=Time[0])
  { 
  if(PFTP_BuySignal > 0 && PFTP_BuySignal_Prev == 0 && PFTP_Rate > PFTP_Rate_Value)
     {
       myTP = PFTP_TP1;
       mySL = PFTP_BuySL;
      return (1);
     }
  if(PFTP_SellSignal > 0 && PFTP_SellSignal_Prev == 0 && PFTP_Rate > PFTP_Rate_Value)
     {
       myTP = PFTP_TP1;
       mySL = PFTP_SellSL;
      return (-1);
     }
   else
      return (0);
       lastTradeBar=Time[0];
     };
   return (0);
  }



BuyOrder

void BuyOrder(string sym)
  {
   double bid = MarketInfo(sym,MODE_BID);
   double ask = MarketInfo(sym,MODE_ASK);
   double point=MarketInfo(sym,MODE_POINT);
   int digits=(int)MarketInfo(sym,MODE_DIGITS);
   string CommentStringInfo = ComentString    +" - "+   sym +" - "+ _Period;

   if(digits == 3 || digits == 5)
      PipValue = 10;

   double SL = ask - StopLoss*PipValue*point;
   if(StopLoss == 0)
      SL = 0;
   double TP = ask + TakeProfit*PipValue*point;
   if(TakeProfit == 0)
      TP = 0;
   int ticket = -1;
   if(true)
      ticket = OrderSend(sym, OP_BUY, Lots, ask, Slippage, 0, 0, CommentStringInfo, Magic, 0, clrBlue);
   else
      ticket = OrderSend(sym, OP_BUY, Lots, ask, Slippage, mySL, myTP, CommentStringInfo, Magic, 0, clrBlue);
   if(ticket > -1)
     {
      if(true)
        {
         bool sel = OrderSelect(ticket, SELECT_BY_TICKET);
         bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), mySL, myTP, 0, clrBlue);
         if(ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }

     }
   else
     {
      Print("OrderSend() error - ", ErrorDescription(GetLastError()));
     }
  }

I'm guesing my problem lies here

 if(true)
      ticket = OrderSend(sym, OP_BUY, Lots, ask, Slippage, 0, 0, CommentStringInfo, Magic, 0, clrBlue);
   else
      ticket = OrderSend(sym, OP_BUY, Lots, ask, Slippage, mySL, myTP, CommentStringInfo, Magic, 0, clrBlue);

But changing that to mySL & myTP gives me this error:

2020.11.09 21:01:15.772 Multi Currency Template V1.1 EURJPY,M30: OrderSend() error - invalid stops


Files:
 
There is no need to open an order and then set the stops. Simplify your code - do it in one step. TP/SL on OrderSend has been fine for years.
          Build 500 № 9 2013.05.09
          Need help me mql4 guru add take profit to this EA - Take Profit - MQL4 programming forum 2017.09.27
 
William Roeder:
There is no need to open an order and then set the stops. Simplify your code - do it in one step. TP/SL on OrderSend has been fine for years.
          Build 500 № 9 2013.05.09
          Need help me mql4 guru add take profit to this EA - Take Profit - MQL4 programming forum 2017.09.27
I will simplify it but I’m still wondering why I’m getting some orders that don’t get the SL & TP.

As it is now if the order wouldn’t get a SL and TP I should get it on the next part with the modify.

I have just entered a Print function to see if the variable is sending 0 to the OrderSend and if so the problem lies higher up in the code.

 
 
William Roeder:
There is no need to open an order and then set the stops. Simplify your code - do it in one step. TP/SL on OrderSend has been fine for years.
          Build 500 № 9 2013.05.09
          Need help me mql4 guru add take profit to this EA - Take Profit - MQL4 programming forum 2017.09.27

If you change it as follows, your code will work. You need to change other parts in this way.


ticket = OrderSend(sym, OP_BUY, Lots, ask, Slippage, mySL, myTP, CommentStringInfo, Magic, 0, clrBlue);
      /// 
/////////If you change it as follows, your code will work. You need to change other parts in this way.
      {
      ticket = OrderSend(sym, OP_BUY, Lots, ask, Slippage, 0,0, CommentStringInfo, Magic, 0, clrBlue);
      // NormalizeDouble(mySL,digits), NormalizeDouble(myTP,digits)
      if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) 
                   OrderModify(ticket,OrderOpenPrice(),NormalizeDouble(mySL,digits), NormalizeDouble(myTP,digits),0,clrBlue);
      }  
      
      //
 
   double SL = ask - StopLoss*PipValue*point;
   
   double TP = ask + TakeProfit*PipValue*point;

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Reason: