Change Stop Loss After Trade Is Profit

 

Hi!

I have been trying to make my EA change the Stop loss value if the current trade almost have reached it's target take profit level. I have not yet found a way to do this.

Can you guys please help me?

One of the codes I have tested is this, but this code makes the absolutely last trade on a backtest loose everything I have on the account because of stop out. Can you help me find a way to make a more efficient trailing stop?

int minute;
extern double TrailingStop = 5;

double PointValue;
  for (int i = 0; i < OrdersTotal(); i++) 
if (minute != Minute())
{  
  {
      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 = TrailingStop * PointValue;
 
      if (OrderType() == OP_BUY)
      {
         if ((Bid - OrderOpenPrice()) > TSTP)
         {
            if (OrderStopLoss() < (Bid - TSTP))
            {
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red);
               minute = Minute();
            }
         }
         else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red);
            minute = Minute();
      }
      else if (OrderType() == OP_SELL)
      {
         if ((OrderOpenPrice() - Ask) > TrailingStop * PointValue)
         {
            if ((OrderStopLoss() > (Ask + TrailingStop * PointValue)) || (OrderStopLoss() == 0))
            {
               OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red);
               minute = Minute();
            }
         }
         else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0))
            OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red);
            minute = Minute();
      }
        }
}
 
In my opinion the best trailing stop is not the one that just trails the price if moving in the positive price i.e. if the trade is +5 pips sl immediately moves by 5 pips maintaining trailing stop distance from current price that is dangerous. A good implementation is like the one in mt4 by default where it only activates if the price is more than trailing stop distance in the positive direction and only moves in steps i.e. if price is at trailing distance in the positive direction trailing is activated sl is adjusted and then the next adjustment only occurs after the price moves a step of say 10 pips from the previous adjustment point and not with every pip above the previous adjustment point. This to some level atleast allows for small swings before the trend continues and avoids the trade being closed prematurely.
 
tonny:
In my opinion the best trailing stop is not the one that just trails the price if moving in the positive price i.e. if the trade is +5 pips sl immediately moves by 5 pips maintaining trailing stop distance from current price that is dangerous. A good implementation is like the one in mt4 by default where it only activates if the price is more than trailing stop distance in the positive direction and only moves in steps i.e. if price is at trailing distance in the positive direction trailing is activated sl is adjusted and then the next adjustment only occurs after the price moves a step of say 10 pips from the previous adjustment point and not with every pip above the previous adjustment point. This to some level atleast allows for small swings before the trend continues and avoids the trade being closed prematurely.


What you are describing here, is exactly what I am looking to make..

Could you help me with some coding?

 
Someone?
 
YoungMoney:
What you are describing here, is exactly what I am looking to make..
Could you help me with some coding?
Since there are no slaves here, there are only two choices: learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you. Where is your attempt.
 

Here is my trailing stop function. (GreedyTrailing to false makes the SL to only be trailed if the new SL is above the open price=

extern bool   GreedyTrailing           = true;
extern int    g_TrailingStop           = 10;
extern int    Slippage                 = 5;
extern int    MagicNumber              = 12345;
double DecimalPip;

int init()
{
   DecimalPip = GetDecimalPip();
}

void TrailStops()
{
   double TS_price; 
   int Type;
   double stoplevel = getStopLevelInPips();
   for(int i = OrdersTotal()-1; i >= 0; i--)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      {
         Type = OrderType();
         if(g_TrailingStop < stoplevel) g_TrailingStop = stoplevel;
         if(Type == OP_BUY)
         {
            TS_price = Bid - (g_TrailingStop*DecimalPip);
            if(TS_price > OrderStopLoss()+stoplevel*DecimalPip && (TS_price > OrderOpenPrice() || GreedyTrailing == true))
            {
               if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS_price, OrderTakeProfit(), 0, Pink))
                  Print(ShortName +" (OrderModify Error) "+ ErrorDescription(GetLastError()));
            }
            
         } else if(Type == OP_SELL) {
             
             TS_price = Ask + (g_TrailingStop*DecimalPip);
             if(TS_price < OrderStopLoss()-stoplevel*DecimalPip && (TS_price < OrderOpenPrice() || GreedyTrailing == true))
            {
               if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS_price, OrderTakeProfit(), 0, Pink))
                    Print(ShortName +" (OrderModify Error) "+ ErrorDescription(GetLastError()));
            }
         }
      }
   }
}

double GetDecimalPip()
{
   switch(Digits)
   {
      case 5: return(0.0001);
      case 4: return(0.0001);
      case 3: return(0.001);
      default: return(0.01);
   }
}

double MyNormalizeDouble(double price)
{
   return (NormalizeDouble(price, Digits));
}

double getStopLevelInPips()
{
   double s = MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0;
   if(Digits == 5) s = s / 10;
   return(s);
}
 
flaab:

Here is my trailing stop function. (GreedyTrailing to false makes the SL to only be trailed if the new SL is above the open price=

Thank you very much for your code..

When adding a similiar code to my EA I get this error:

'(' - function definition unexpected    ssover.mq4 (62, 21)

Do you know what I can do with it?

 
YoungMoney:

Thank you very much for your code..

When adding a similiar code to my EA I get this error:

Do you know what I can do with it?


You probably have a missing ; somewhere . . .
 
RaptorUK:

You probably have a missing ; somewhere . . .

I have lirerally checker the whole code for missing characters, but I can't find anything.

I even wrote the whole code from scratch. Didn't help.

 
YoungMoney:

I have lirerally checker the whole code for missing characters, but I can't find anything.

I even wrote the whole code from scratch. Didn't help.


Well I'd help but I can't see you code . . so not much I can do.
 
extern string optionFour = "Trailing Stop";
extern bool   GreedyTrailing           = true;
extern int    g_TrailingStop           = 10;
extern int    Slippage                 = 5;

double StopLossLevel, TakeProfitLevel;
int total, Ticket, cnt;
double DecimalPip;

void TrailStops()
{
   double TS_price; 
   int Type;
   double stoplevel = getStopLevelInPips();
   for(int i = OrdersTotal()-1; i >= 0; i--)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      {
         Type = OrderType();
         if(g_TrailingStop < stoplevel) g_TrailingStop = stoplevel;
         if(Type == OP_BUY)
         {
            TS_price = Bid - (g_TrailingStop*DecimalPip);
            if(TS_price > OrderStopLoss()+ stoplevel*DecimalPip && (TS_price > OrderOpenPrice() || GreedyTrailing == true))
            {
               if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS_price, OrderTakeProfit(), 0, Pink))
                  {
                  Print("(OrderModify Error)");
                  }
            }
            
         } else if(Type == OP_SELL) {
             
             TS_price = Ask + (g_TrailingStop*DecimalPip);
             if(TS_price < OrderStopLoss()-stoplevel*DecimalPip && (TS_price < OrderOpenPrice() || GreedyTrailing == true))
            {
               if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS_price, OrderTakeProfit(), 0, Pink))
                    Print("(OrderModify Error)");
            }
         }
        }


double GetDecimalPip()
{
   switch(Digits)
   {
      case 5: return(0.0001);
      case 4: return(0.0001);
      case 3: return(0.001);
      default: return(0.01);
   }
}

double MyNormalizeDouble(double price)
{
   return (NormalizeDouble(price, Digits));
}

double getStopLevelInPips()
{
   double s = MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0;
   if(Digits == 5) s = s / 10;
   return(s);
}

int init()
{
   DecimalPip = GetDecimalPip();
}
if(GreedyTrailing == true)
{   
   TrailStops();
}

That should help..

I of course have code for the EA which checks the signals too, but I doubt there is something wrong with that code since that worked good before I put the TrailingStop in.

Thanks for your help, RaptorUK.

Reason: