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

johnnybegoode

Trader
Jul 19, 2016
56
0
22
47
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)
 

johnnybegoode

Trader
Jul 19, 2016
56
0
22
47
Ok, I would close first then.
Calling Close function first then Trailing function.

I keep getting "Invalid Stop" for my trailing function

MQL5:
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(),")");
                  }
         }
      }
}
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,254
1,508
144
Odesa
www.earnforex.com
At what point do you get the Invalid Stop error?

The problem I see here is that you calculate SL2 and TP2 as if they were for a Sell position but then check whether position is a Buy to proceed with them. Similarly, SL4 and TP4 are calculated for a Buy position but then are applied to a Sell.
 

johnnybegoode

Trader
Jul 19, 2016
56
0
22
47
I think the problem is with another function that calls
Close_Losing_Position();
and
Trail_Winning_Position();
functions.

Since the condition is still true, it keep on looping the PositionModify
of "trailing stop loss" and "trailing take profit"
The 2nd round is when I get the "Invalid Stop".
The 1st round is successful.

Is there any way to just run
Close_Losing_Position();
and
Trail_Winning_Position();
just once until "trailing stop loss" and "trailing take profit" is triggered?

MQL5:
void LockInProfit()
{
   if (condition == true)
   {      
        {
         Ordering=false;
         Close_Losing_Position();
         Trail_Winning_Position();
        }
   }
}
 

johnnybegoode

Trader
Jul 19, 2016
56
0
22
47
PositionModify successfully 1st round, then for some reason, it PositionModify again with invalid stops?

Screen_Shot_2017_01_16_at_10_55_27_PM.png