HELP: Trailing Stop with Break Even - Need help with my script

 

Hi

Below is my attempt at a trailing stop. Need help making it smarter and more reliable.

I would like it to break even after 5 pips profit and thereafter trail by 5 pips.

Please help.

//+------------------------------------------------------------------+
//| Expert trail stop order function                                 |
//+------------------------------------------------------------------+
void TrailStopOrder()
{
   // Make sure data is current
   RefreshRates();
   
   // If it's an open buy order         
   if (OrderType() == OP_BUY)
   {
      // If the open buy order is profitable, trail it
      if (Bid - OrderOpenPrice() > trailingStepValue)
      {
         if (OrderStopLoss() < Bid - trailingStepValue)
         {
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - trailingStepValue, 0, Blue))
               Print("OrderTrailStop Buy failed with error #", GetLastError());
         }
      }
   }
   // Else if it's an open sell order         
   else if (OrderType() == OP_SELL)
   {
      // If the open sell order is profitable, trail it
      if (OrderOpenPrice() - Ask > trailingStepValue)
      {
         if (OrderStopLoss() > Ask + trailingStepValue)
         {
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + trailingStepValue, 0, Blue))
               Print("OrderTrailStop Sell failed with error #", GetLastError());
         }
      }
   }
}
 
Sameer Shariff:

Hi

Below is my attempt at a trailing stop. Need help making it smarter and more reliable.

I would like it to break even after 5 pips profit and thereafter trail by 5 pips.

Please help.

Try with the code below:


//+------------------------------------------------------------------+
//| Expert trail stop order function                                 |
//+------------------------------------------------------------------+
void TrailStopOrder()
{
   // Make sure data is current
   RefreshRates();
   
   // If it's an open buy order    

   for(int i=OrdersTotal()-1; i>=0; i--)
      {
        if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES)==true)
          {
            if(OrderSymbol()==_Symbol) //&&(OrderMagicNumber()==MagicNumber // if use MagicNumber
              {
                if(OrderType() == OP_BUY)
                   {
                     // If the open buy order is profitable, trail it
                     if(Bid - OrderOpenPrice() > trailingStepValue)
                        {
                          if(OrderStopLoss() < Bid - trailingStepValue)
                            {
                              if(!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - trailingStepValue, 0, Blue))
                                Print("OrderTrailStop Buy failed with error #", GetLastError());
                            }
                        }
                   }
                // Else if it's an open sell order         
                else if(OrderType() == OP_SELL)
                  {
                    // If the open sell order is profitable, trail it
                    if(OrderOpenPrice() - Ask > trailingStepValue)
                      {
                        if(OrderStopLoss() > Ask + trailingStepValue)
                          {
                            if(!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + trailingStepValue, 0, Blue))
                            Print("OrderTrailStop Sell failed with error #", GetLastError());
                          }
                      }
                  } 
              }
          }
     }
}