Percentage Trail Stop

Dec 7, 2011
6
1
14
Good Evening,

I have been trying to develop a trail stop using percentage but not having much luck. This trail stop seems to stop out at the initial trailing stop pips and I can't figure out why. This EA is a modified version of the one on the forex blog with help from admin. However it seems to stop out after 7 pips. My goal is to get this EA working and then modify it again for different trail stop levels such as
10 pips - 20%
20 pips - 40 % etc up to 60 pips at 70%.
Help appreciated.

MQL4:
#property copyright "Campbell Congdon"
#property link      "campbell_congdon@hotmail.com"
extern double InitialTrailingStop = 8;	
extern double trailing_stop_percentage = 20;
 
 
 
// Set it to some value above 0 to activate stop-loss	
extern double StopLoss = 0; 	
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {double PointValue;
  for (int i = 0; i < OrdersTotal(); i++) 
  {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      //Calculate the point value in case there are extra digits in the quotes
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
      else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
      //Normalize trailing stop value to the point value
      double TSTP = InitialTrailingStop * PointValue;
 
      if (OrderType() == OP_BUY)
            { if ((trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP))
            { TSTP = NormalizeDouble((Bid - OrderOpenPrice()) * trailing_stop_percentage / 100, Digits);
            { if (OrderStopLoss() < (Bid - TSTP))
            {  OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red);
      }
      }
      }
      else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red);
      }
 
      else if (OrderType() == OP_SELL)
         { if ((trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP))
         { TSTP = NormalizeDouble((OrderOpenPrice() - Ask) * trailing_stop_percentage / 100, Digits);
         { if ((OrderStopLoss() > (Ask + InitialTrailingStop * PointValue)) || (OrderStopLoss() == 0))
         { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red);
      }
      }
      }
 
         else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red);
      }
   	}
 
 
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,222
1,506
144
Odesa
www.earnforex.com
It's quite difficult to understand how you want your trailing stop to function from your description.

For example, we are in 19 pips profit. 20% is 3.8 (rounded to 4) pips, so if Bid = 1.3420, the SL is set to 1.3416. Then profit moves up to 20 pips, now we use 40% and that's 8 pips, so if Bid = 1.3421, the SL is set to 1.3413 - below the previous level. In the end it isn't trailing anything if it can go back.
 
Dec 7, 2011
6
1
14
Hi thanks for replying - thinking about it I probably didn't define the goal very well. Ideally - this trailing stop would have an initial start at 10 pips.
Once 10 pips were hit it would have a stop loss (not a trailing stop?!) at + 2 pips -20%. Once price hit 20 pips the stop loss would move to 40% or +8 pips. etc up to 60 pips whereupon the stop is 70% of profit which is 42 pips.

The problem here is when price continues upwards how do you keep the trailing stop running at 70% of profit so you can keep with a trend and not have a stop loss sit at 42 pips? I think another function would have to take over.

At the moment I have around 4 months coding experience with bools and strategies (and success!) but not the order functions. I tried to code a percentage trailing stop (with admin's help) above but this stop moves back (if you are long) when price moves back. I have also commenced coding the levels above and the trailing stop would move backwards as well.

At the moment the EA above will put the stop loss on at around 8 pips and if price is at 8 pips it will get taken out very quickly. So there is something wrong there. Below is the code I did for doing different stop levels depending on profit:

MQL4:
/+------------------------------------------------------------------+
//|                                                    Blueprintv1,2.mq4 |
//|                                                 Campbell Congdon |
//|                                     [email]campbell_congdon@hotmail.com[/email] |
//+------------------------------------------------------------------+
#property copyright "Campbell Congdon"
#property link      "campbell_congdon@hotmail.com"
extern double InitialTrailingStop = 10;	
extern double trailing_stop_percentage = 20;
extern double SecTrailingStop = 20;
extern double sec_trailing_stop_percentage = 40;
extern double ThirdTrailingStop = 30;
extern double thr_trailing_stop_percentage = 50;
extern double FourthTrailingStop = 40;
extern double fourth_trailing_stop_percentage = 60;
extern double FifthTrailingStop = 60;
extern double fifth_trailing_stop_percentage = 70;
 
// Set it to some value above 0 to activate stop-loss	
extern double StopLoss = 40; 	
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {double PointValue;
  for (int i = 0; i < OrdersTotal(); i++) 
  {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      //Calculate the point value in case there are extra digits in the quotes
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
      else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
      //Normalize trailing stop value to the point value
      double TSTP = InitialTrailingStop * PointValue;
      double TSTP2 = SecTrailingStop * PointValue;
      double TSTP3 = ThirdTrailingStop * PointValue;
      double TSTP4 = FourthTrailingStop * PointValue;
      double TSTP5 = FifthTrailingStop * PointValue;
 
 
      if (OrderType() == OP_BUY)
      {
         if ((trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP) &&(OrderStopLoss() < (Bid - TSTP)))
         { TSTP = NormalizeDouble((Bid - OrderOpenPrice()) * trailing_stop_percentage / 100, Digits);
 
         { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red);
 
              }
              }
              }
          {if ((sec_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP2)&&(OrderStopLoss() < (Bid - TSTP2))) 
          { TSTP2 = NormalizeDouble((Bid - OrderOpenPrice()) * sec_trailing_stop_percentage / 100, Digits); 
 
          { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP2, OrderTakeProfit(), Red); 
           }
           }
           }
 
 
         { if ((thr_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP3)&& (OrderStopLoss() < (Bid - TSTP3)))
         { TSTP3 = NormalizeDouble((Bid - OrderOpenPrice()) * thr_trailing_stop_percentage / 100, Digits);
 
          { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP3, OrderTakeProfit(), Red); 
 
              }
              }
              }
         {if ((fourth_trailing_stop_percentage > 0) && (Bid - OrderOpenPrice() >= TSTP4)&& (OrderStopLoss() < (Bid - TSTP4)))
         { TSTP4 = NormalizeDouble((Bid - OrderOpenPrice()) * fourth_trailing_stop_percentage / 100, Digits);
 
          { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP4, OrderTakeProfit(), Red); 
 
               }
               }
               }
         {if ((fifth_trailing_stop_percentage > 0) &&  (Bid - OrderOpenPrice() >= TSTP5)&& (OrderStopLoss() < (Bid - TSTP5)))
          { TSTP5 = NormalizeDouble((Bid - OrderOpenPrice()) * fifth_trailing_stop_percentage / 100, Digits);
 
          { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP5, OrderTakeProfit(), Red); 
 
        }
       }
 
     else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red);
 
    else if (OrderType() == OP_SELL)
 
          if ((trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP)&&((OrderStopLoss() > (Ask + InitialTrailingStop * PointValue)) || (OrderStopLoss() == 0)))
         { TSTP = NormalizeDouble((OrderOpenPrice() - Ask) * trailing_stop_percentage / 100, Digits);
 
         {OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red);
 
       }
       }
 
         if ((sec_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP2)&& ((OrderStopLoss() > (Ask + SecTrailingStop * PointValue)) || (OrderStopLoss() == 0)))
         { TSTP2 = NormalizeDouble((OrderOpenPrice() - Ask) * sec_trailing_stop_percentage / 100, Digits);
 
         { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP2, OrderTakeProfit(), Red);
       }
       }
 
 
 
      if ((thr_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP3)&& ((OrderStopLoss() > (Ask + ThirdTrailingStop * PointValue)) || (OrderStopLoss() == 0)))
         { TSTP3 = NormalizeDouble((OrderOpenPrice() - Ask) * thr_trailing_stop_percentage / 100, Digits);
 
         { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP3, OrderTakeProfit(), Red);
        }
        }
 
 
     if ((fourth_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP4)&& ((OrderStopLoss() > (Ask + FourthTrailingStop * PointValue)) || (OrderStopLoss() == 0)))
         { TSTP4 = NormalizeDouble((OrderOpenPrice() - Ask) * fourth_trailing_stop_percentage / 100, Digits);
         { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP4, OrderTakeProfit(), Red);     
        }
        }
 
 
     if ((fifth_trailing_stop_percentage > 0) && (OrderOpenPrice() - Ask >= TSTP5)&&((OrderStopLoss() > (Ask + FifthTrailingStop * PointValue)) || (OrderStopLoss() == 0)))
         { TSTP5 = NormalizeDouble((OrderOpenPrice() - Ask) * fifth_trailing_stop_percentage / 100, Digits);
 
         { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP5, OrderTakeProfit(), Red);
        }
 
        }
 
 
       else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red);
 }
}
 
//----
 
//----
   return(0);
}
//+------------------------------------------------------------------
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,222
1,506
144
Odesa
www.earnforex.com
So before the last level (42 pips), the stop-loss is set only to fixed levels (2, 8, 15, 24), right? But after hitting 60 pips of profit and the SL is set to 42, it should trail every pip of profit increase as 1 to 0.7, right?
 
Dec 7, 2011
6
1
14
Yes. I thought that this would be easier to code rather than percentages. The percentages can be worked out manually. In this case it becomes a jumping stop loss. These settings could be variable depending on what the trader desires. at 61 pips the jump would be just 1 pip keeping the trade with a 70% locked in profit becoming a trail stop. I am going to research and hopefully code something correctly this weekend.
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,222
1,506
144
Odesa
www.earnforex.com
Some guidelines:

1. You'll have to check what's your current "stage" of stop-loss for a given position by comparing the current stop-loss (OrderStopLoss()) to 2 pips, 8 pips, etc. Of course, it should be done using the input parameters rather than absolute values.

2. Don't forget about MarketInfo(Symbol(), MODE_STOPLEVEL);
 
Dec 7, 2011
6
1
14
I couldn't get this out. I have no errors but the programming is clearly wrong as no stop loss is appearing in mt4 on the trades. I couldn't really find any clear programming examples to use either. Most of the trailing stop EAs on the net don't work. I have to research that mode stop level indication you gave.

MQL4:
/+------------------------------------------------------------------+
#property copyright "not working"
#property link      "notworking@hotmail.com"
extern double InitialTrailingStop = 10;	
extern double trailing_stop_level1 = 3;
extern double SecTrailingStop = 20;
extern double trailing_stop_level2 = 8;
extern double ThirdTrailingStop = 30;
extern double trailing_stop_level3 = 15;
extern double FourthTrailingStop = 40;
extern double trailing_stop_level4 = 24;
extern double FifthTrailingStop = 60;
extern double trailing_stop_level5 = 42;
extern double TrailPercent = 70;
extern double Trail_From   = 61.0;
// Set it to some value above 0 to activate stop-loss	
extern double StopLoss = 40; 	
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {double PointValue;
  for (int i = 0; i < OrdersTotal(); i++) 
  {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      //Calculate the point value in case there are extra digits in the quotes
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
      else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
      //Normalize trailing stop value to the point value
      double TSTP = InitialTrailingStop * PointValue;
      double TSTP2 = SecTrailingStop * PointValue;
      double TSTP3 = ThirdTrailingStop * PointValue;
      double TSTP4 = FourthTrailingStop * PointValue;
      double TSTP5 = FifthTrailingStop * PointValue;
      double Trail_From = Trail_From * PointValue;
      double sl = OrderStopLoss();
      double My_Profit;
      double My_Trail;
      double My_SL;
 
 
 
      if (OrderType() == OP_BUY)
      {
         if (((InitialTrailingStop> 0) && (Bid - OrderOpenPrice() >= TSTP) && (Bid - OrderOpenPrice() < TSTP2)))
         sl = (trailing_stop_level1 * PointValue);
         { OrderModify(OrderTicket(), OrderOpenPrice(),sl, OrderTakeProfit(), Red);
              }
              }
          { if (((SecTrailingStop> 0) && (Bid - OrderOpenPrice() >= TSTP2) && (Bid - OrderOpenPrice() <TSTP3)))
          sl = (trailing_stop_level2 * PointValue); 
          { OrderModify(OrderTicket(), OrderOpenPrice(),sl, OrderTakeProfit(), Red); 
           }
           }
 
 
         { if (((ThirdTrailingStop  > 0) && (Bid - OrderOpenPrice() >= TSTP3)&& (Bid - OrderOpenPrice() <TSTP4)))
           sl = (trailing_stop_level3 * PointValue); 
          { OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), Red); 
              }
              }
 
         {if ((( FourthTrailingStop  > 0) && (Bid - OrderOpenPrice() >= TSTP4)&& (Bid - OrderOpenPrice() <TSTP5)))
         sl = (trailing_stop_level4 * PointValue); 
          { OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), Red); 
               }
               }
 
 
         {if (( FifthTrailingStop  > 0) &&  (Bid - OrderOpenPrice() >= TSTP5))
          sl = (trailing_stop_level5*PointValue);
          { OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), Red); 
 
          }
          }
            My_Profit = MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice();
           My_Trail = MathMin(My_Profit * TrailPercent/100,Digits);
             My_SL = NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID)-My_Trail,Digits);
         { if (My_Profit > Trail_From)
          OrderModify(OrderTicket(),OrderOpenPrice(),My_SL,OrderTakeProfit(),0, Red);
 
          }
       }
  if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red);
 
 
       if (OrderType() == OP_SELL)
 
         { if (((InitialTrailingStop> 0) && (OrderOpenPrice() - Ask >= TSTP) && (OrderOpenPrice() - Ask < TSTP2)))
           sl = (trailing_stop_level1 * PointValue);
          {OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), Red);
       }
       }
 
       {  if ((( SecTrailingStop > 0) && (OrderOpenPrice() - Ask >= TSTP2) && (OrderOpenPrice() - Ask < TSTP3)))
          sl = (trailing_stop_level1 * PointValue);
         {OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), Red);
 
       }
       }
 
    {    
      if (((ThirdTrailingStop > 0) && (OrderOpenPrice() - Ask >= TSTP3) && (OrderOpenPrice() - Ask < TSTP4)))
           sl = (trailing_stop_level1 * PointValue);
          {OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), Red);
 
        }
        }
    {   
     if (((FourthTrailingStop > 0) && (OrderOpenPrice() - Ask >= TSTP4) && (OrderOpenPrice() - Ask < TSTP5)))
         sl = (trailing_stop_level1 * PointValue);
         {OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), Red);
 
        }
        }
 
 
        {if (( FifthTrailingStop  > 0) &&  (OrderOpenPrice() - Ask >= TSTP5))
         sl = (trailing_stop_level5*PointValue);
         {OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), Red);
 
         }
         }
 
         My_Profit = OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK);
         My_Trail = MathMin(My_Profit * TrailPercent/100,Digits);
         My_SL = NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK)+My_Trail,Digits);
         if (My_Profit > Trail_From)
         { OrderModify(OrderTicket(),OrderOpenPrice(),My_SL,OrderTakeProfit(),0,Red);
 
 
        }
 
      if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red);
 
//----
 
//----
   return(0);
}
//+----------------------------------------------
 
Last edited by a moderator:
  • 👍
Reactions: Enivid

Enivid

Administrator
Staff member
Nov 30, 2008
19,222
1,506
144
Odesa
www.earnforex.com
Some problems in your code:

1. You've messed up with the curly brackets big time.
2. When you set a stop-loss with OrderModify(), you have to set the price level of the stop-loss, not it's pips value. So you sl should be = Bid - (trailing_stop_level1 * PointValue).
3. Monitor your Journal tab in MT4 to see what's your EA is trying to do and why MetaTrader server rejects that.
 
Dec 7, 2011
6
1
14
Geez - i saw where others had put things like "sl" and "MySL" etc in their ordermodify function.

Despite these issues - is the logic correct in terms of the algorithms?
 
Dec 7, 2011
6
1
14
Hi Enivid,

I made some changes to the EA, its giving me the correct stop loss only that if price reverses then the stop reverses. Do you know the logic of telling the EA that the highest profit has been reached?
I am thinking of adding a line at each level:
{if(OrderStopLoss() < OrderOpenPrice()+trailing_stop_level4)

This will help the trailing stop stay at level 4 from level 3. But not sure that this won't be just accepted and then if the function below is true the stop will move back down.

Also if I put this EA on different currency pairs - I get the Stop loss as appearing for the different currency pairs this is on. I think this is a magic number issue - would that be right?

Thanks

MQL4:
//+------------------------------------------------------------------+
//|                                                    BlueprintV1.8.mq4 |
//|                                                 Not working|
//|                                   [email]cam_tong@hotmail.com[/email] |
//+------------------------------------------------------------------+
#property copyright "not working"
#property link      "notworking@hotmail.com"
extern double InitialTrailingStop = 10;	
extern double trailing_stop_level1 = 3;
extern double SecTrailingStop = 20;
extern double trailing_stop_level2 = 8;
extern double ThirdTrailingStop = 30;
extern double trailing_stop_level3 = 15;
extern double FourthTrailingStop = 40;
extern double trailing_stop_level4 = 24;
extern double FifthTrailingStop = 60;
extern double trailing_stop_level5 = 42;
extern double TrailPercent = 70;
extern double Trail_From   = 61.0;
// Set it to some value above 0 to activate stop-loss	
extern double StopLoss = 0; 	
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {double PointValue;
  for (int i = 0; i < OrdersTotal(); i++) 
  {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      //Calculate the point value in case there are extra digits in the quotes
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
      else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);
      //Normalize trailing stop value to the point value
      double TSTP = InitialTrailingStop * PointValue;
      double TSTP2 = SecTrailingStop * PointValue;
      double TSTP3 = ThirdTrailingStop * PointValue;
      double TSTP4 = FourthTrailingStop * PointValue;
      double TSTP5 = FifthTrailingStop * PointValue;
      double Trail_From = Trail_From * PointValue;
      double sl = OrderStopLoss();
      double My_Profit;
      double My_Trail;
      double My_SL;
 
 
 
      if (OrderType() == OP_BUY)
      {
         if (((InitialTrailingStop> 0) && (Bid - OrderOpenPrice() >= TSTP) && (Bid - OrderOpenPrice() < TSTP2)))
 
        { OrderModify (OrderTicket(), OrderOpenPrice(),OrderOpenPrice()+ trailing_stop_level1 * PointValue, OrderTakeProfit(), Red);
              }
              }
          { if (((SecTrailingStop> 0) && (Bid - OrderOpenPrice() >= TSTP2) && (Bid - OrderOpenPrice() <TSTP3)))
 
          { OrderModify (OrderTicket(), OrderOpenPrice(),OrderOpenPrice() + trailing_stop_level2 * PointValue, OrderTakeProfit(), Red); 
           }
           }
 
 
         { if (((ThirdTrailingStop  > 0) && (Bid - OrderOpenPrice() >= TSTP3)&& (Bid - OrderOpenPrice() <TSTP4)))
 
          { OrderModify (OrderTicket(), OrderOpenPrice(), OrderOpenPrice()+trailing_stop_level3 * PointValue, OrderTakeProfit(), Red); 
              }
              }
 
         {if ((( FourthTrailingStop  > 0) && (Bid - OrderOpenPrice() >= TSTP4)&& (Bid - OrderOpenPrice() <TSTP5)))
        {if(OrderStopLoss() < OrderOpenPrice()+trailing_stop_level4)
          { OrderModify (OrderTicket(), OrderOpenPrice(),OrderOpenPrice()+trailing_stop_level4 * PointValue, OrderTakeProfit(), Red); 
               }
               }
               }
 
         {if (( FifthTrailingStop  > 0) &&  (Bid - OrderOpenPrice() >= TSTP5) && (Bid - OrderOpenPrice() < Trail_From))
 
          { OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + trailing_stop_level5 * PointValue, OrderTakeProfit(), Red); 
 
          }
          }
           My_Profit = MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice();
           My_Trail = MathMin(My_Profit * TrailPercent/100,Digits);
         { if (Bid - OrderOpenPrice() > TSTP5)
         { if(OrderStopLoss()< Bid  - My_Trail * PointValue)
         { OrderModify(OrderTicket(),OrderOpenPrice(),Bid - My_Trail*PointValue,OrderTakeProfit(),0, Red);
 
        }
          }
       }
       }
  if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red);
 
       if (OrderType() == OP_SELL)
 
         { if (((InitialTrailingStop> 0) && (OrderOpenPrice() - Ask >= TSTP) && (OrderOpenPrice() - Ask < TSTP2)))
 
          {OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-trailing_stop_level1 * PointValue, OrderTakeProfit(), Red);
       }
       }
 
       {  if ((( SecTrailingStop > 0) && (OrderOpenPrice() - Ask >= TSTP2) && (OrderOpenPrice() - Ask < TSTP3)))
 
         {OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()- trailing_stop_level2 * PointValue, OrderTakeProfit(), Red);
 
       }
       }
 
    {    
      if (((ThirdTrailingStop > 0) && (OrderOpenPrice() - Ask >= TSTP3) && (OrderOpenPrice() - Ask < TSTP4)))
 
          {OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-trailing_stop_level3 * PointValue, OrderTakeProfit(), Red);
 
        }
        }
    {   
     if (((FourthTrailingStop > 0) && (OrderOpenPrice() - Ask >= TSTP4) && (OrderOpenPrice() - Ask < TSTP5)))
 
         {OrderModify(OrderTicket(), OrderOpenPrice(),OrderOpenPrice() - trailing_stop_level4 * PointValue , OrderTakeProfit(), Red);
 
        }
        }
 
 
        {if (( FifthTrailingStop  > 0) &&  (OrderOpenPrice() - Ask >= TSTP5) && (OrderOpenPrice() - Ask < Trail_From))
 
         {OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - trailing_stop_level5 * PointValue, OrderTakeProfit(), Red);
 
         }
         }
 
         My_Profit = OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK);
         My_Trail = MathMin(My_Profit * TrailPercent/100,Digits);
        {if((OrderOpenPrice()-Ask)> TSTP5)
        {if((OrderStopLoss()>(Ask+My_Trail*PointValue)) || (OrderStopLoss()==0))
        { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+My_Trail*PointValue,OrderTakeProfit(),0,Red);
         }
         }
 
        }
 
 if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red);
 
//----
 
//----
   return(0);
}
//+--------------------------------------------------
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,222
1,506
144
Odesa
www.earnforex.com
To prevent trailing stop loss from going back, you could perform a simple check before each OrderModify():
MQL4:
if (NewSL > OrderStopLoss()) OrderModify(...);

and for Sell orders:
MQL4:
if (NewSL < OrderStopLoss()) OrderModify(...);

If you want your EA to work only with the orders in the currency pair it is attached to, you can add this line after OrderSelect():

MQL4:
if (OrderSymbol() != Symbol()) continue;
 
Last edited: