MQL5 - Pair Trading: Close LOSING Position and Set Trailing Stop to WINNING Position Simultaneously

 

MQL5 - Pair Trading:

When the condition is met,

how do I do this simultaneously?

1) Close the LOSING Position immediately (Position could be a Sell/Buy order of any currency)

2) Set Trailing Stop to WINNING Position immediately (Position could be a Sell/Buy order of another currency)

 
What have you tried ? What have you searched ?
 
I got "invalid stop" error for PositionModify
void Trail_Winning_Position()
{

int    digits1=(int)SymbolInfoInteger(Symbol_1,SYMBOL_DIGITS); // number of decimal places
double point1=SymbolInfoDouble(Symbol_1,SYMBOL_POINT);         // point
double bid1=SymbolInfoDouble(Symbol_1,SYMBOL_BID);               // current price for closing
double SL1=bid1-Trailing_Stop_Loss*point1;                            // unnormalized SL value
SL1=NormalizeDouble(SL1,digits1);                              // normalizing Stop Loss
double TP1=bid1+Take_Profit*point1;                                   // unnormalized TP value
TP1=NormalizeDouble(TP1,digits1);                              // normalizing Take Profit

int    digits2=(int)SymbolInfoInteger(Symbol_2,SYMBOL_DIGITS); // number of decimal places
double point2=SymbolInfoDouble(Symbol_2,SYMBOL_POINT);         // point
double ask2=SymbolInfoDouble(Symbol_2,SYMBOL_ASK);               // current price for closing
double SL2=ask2+Trailing_Stop_Loss*point2;                            // unnormalized SL value
SL2=NormalizeDouble(SL2,digits2);                              // normalizing Stop Loss
double TP2=ask2-Take_Profit*point2;                                   // unnormalized TP value
TP2=NormalizeDouble(TP2,digits2);                              // normalizing Take Profit

int    digits3=(int)SymbolInfoInteger(Symbol_1,SYMBOL_DIGITS); // number of decimal places
double point3=SymbolInfoDouble(Symbol_1,SYMBOL_POINT);         // point
double ask3=SymbolInfoDouble(Symbol_1,SYMBOL_ASK);               // current price for closing
double SL3=ask3+1000*point3;                            // unnormalized SL value
SL3=NormalizeDouble(SL3,digits3);                              // normalizing Stop Loss
double TP3=ask3-1000*point3;                                   // unnormalized TP value
TP3=NormalizeDouble(TP3,digits3);                              // normalizing Take Profit

int    digits4=(int)SymbolInfoInteger(Symbol_2,SYMBOL_DIGITS); // number of decimal places
double point4=SymbolInfoDouble(Symbol_2,SYMBOL_POINT);         // point
double bid4=SymbolInfoDouble(Symbol_2,SYMBOL_BID);               // current price for closing
double SL4=bid4-1000*point4;                            // unnormalized SL value
SL4=NormalizeDouble(SL4,digits4);                              // normalizing Stop Loss
double TP4=bid4+1000*point4;                                   // unnormalized TP value
TP4=NormalizeDouble(TP4,digits4);                              // normalizing Take Profit

      if ( PositionSelect(Symbol_1) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)>0 && (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) )
         {
            if  ( trade.PositionModify(Symbol_1, SL1, TP1) )
            Print("Trailing Started for ", Symbol_1);
         }
      
      }
      
      if ( PositionSelect(Symbol_1) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)>0 && (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) )
         {
            if  ( trade.PositionModify(Symbol_1, SL3, TP3) )
            Print("Trailing Started for ", Symbol_1);
         }
      
      }
      
      if ( PositionSelect(Symbol_2) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)>0 && (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) )
         {
            if  ( trade.PositionModify(Symbol_2, SL2, TP2) )
            Print("Trailing Started for ", Symbol_2);
         }
      
      }
      
      if ( PositionSelect(Symbol_2) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)>0 && (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) )
         {
            if  ( trade.PositionModify(Symbol_2, SL4, TP4) )
            Print("Trailing Started for ", Symbol_2);
         }
      
      }
}      

void Close_Losing_Position()
{
      if ( PositionSelect(Symbol_1) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)<0 )
         {
               if( !trade.PositionClose(Symbol_1,9999) )
               {
            //--- failure message
            Print(Symbol_1, "PositionClose() method failed. Return code=",trade.ResultRetcode(),
                  ". Code description: ",trade.ResultRetcodeDescription());
               }
                
               else
               {
            Print(Symbol_1, "PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
                  " (",trade.ResultRetcodeDescription(),")");
               }
         }
      }
      
      if ( PositionSelect(Symbol_2) )
      {
         if ( PositionGetDouble(POSITION_PROFIT)<0 )
         {
                  if( !trade.PositionClose(Symbol_2,9999) )
                  {
               //--- failure message
               Print(Symbol_2, "PositionClose() method failed. Return code=",trade.ResultRetcode(),
                     ". Code description: ",trade.ResultRetcodeDescription());
                  }
                    
                  else
                  {
               Print(Symbol_2, "PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
                     " (",trade.ResultRetcodeDescription(),")");
                  }
         }
      }
}
 
Reason: