Trailing Stop

 

Hi All,

Any suggestions and/or criticism for why my trailing stop isn't working?

void TrailingStop()
{
   
   if (OrdersTotal() == 1)
   {
      for (int i = OrdersTotal() - 1; i >=0; i--)
      {
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == True)
         {
            if (OrderType() == OP_BUY)
            {
               if (OrderStopLoss() < Bid - 30 * _Point)
               {
                  double a = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - 30 *_Point, 0, 0, Green);
               }
            }
            
            if (OrderType() == OP_SELL)
            {
               if (OrderStopLoss() > Ask + 30 * _Point)
               {
                  double b = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + 30 * _Point, 0, 0, Red);
               }
            }      
         }         
      }   
   }
}

Hope you can help!

 
RainyDay:

Hi All,

Any suggestions and/or criticism for why my trailing stop isn't working?

Hope you can help!

What does it do i have a few TS ea's may do the trick


Let me know

 
RainyDay:

Hi All,

Any suggestions and/or criticism for why my trailing stop isn't working?

Hope you can help!

what do you mean by "isn't working"?

double a = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - 30 *_Point, 0, 0, Green);
double b = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + 30 * _Point, 0, 0, Red);

OrderModify is bool type.

OrderModify - Trade Functions - MQL4 Reference
OrderModify - Trade Functions - MQL4 Reference
  • docs.mql4.com
[in]  Arrow color for StopLoss/TakeProfit modifications in the chart. If the parameter is missing or has CLR_NONE value, the arrows will not be shown in the chart. Open price and expiration time can be changed only for pending orders. If unchanged values are passed as the function parameters, the error...
 
RainyDay:

Hi All,

Any suggestions and/or criticism for why my trailing stop isn't working?

Hope you can help!

Here is it working now 

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   tStop(Symbol(),100,0);
  }
//+------------------------------------------------------------------+
void tStop(string symb,int stop, int MN)// Symbol + stop in pips + magic number
  {
   double bsl=NormalizeDouble(MarketInfo(symb,MODE_BID)-stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS));
   double ssl=NormalizeDouble(MarketInfo(symb,MODE_ASK)+stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS));

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MN)
            if(OrderSymbol()==symb)

               if(OrderType()==OP_BUY && (OrderStopLoss()<bsl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),bsl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Buy's Stop Trailled to "+(string)bsl);
                       }else{
                     Print(symb+" Buy's Stop Trail ERROR");
                    }

               if(OrderType()==OP_SELL && (OrderStopLoss()>ssl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),ssl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Sell's Stop Trailled to "+(string)ssl);
                       }else{
                     Print(symb+" Sell's Stop Trail ERROR");
                    }
     }
  }
//+------------------------------------------------------------------+
 
void TrailingStopTrail(int type, double TS, double step, bool aboveBE) //set Stop Loss to "TS" if price is going your way with "step"
  {
   int total = OrdersTotal();
   TS = NormalizeDouble(TS, Digits());
   step = NormalizeDouble(step, Digits());
   for(int i = total-1; i >= 0; i--)
     {
      while(IsTradeContextBusy()) Sleep(100);
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderMagicNumber() != MagicNumber || OrderSymbol() != Symbol() || OrderType() != type) continue;
          RefreshRates();
      if(type == OP_BUY && (!aboveBE || Ask > OrderOpenPrice() + TS) && (NormalizeDouble(OrderStopLoss(), Digits()) <= 0 || Ask > OrderStopLoss() + TS + step))
         myOrderModify(OrderTicket(), Ask - TS, 0);
      else if(type == OP_SELL && (!aboveBE || Bid < OrderOpenPrice() - TS) && (NormalizeDouble(OrderStopLoss(), Digits()) <= 0 || Bid < OrderStopLoss() - TS - step))
         myOrderModify(OrderTicket(), Bid + TS, 0);
     }
  }

//Ontick
  TrailingStopTrail(OP_BUY, 25 * Point(), 50 * Point(), true); //Trailing Stop = trail
  TrailingStopTrail(OP_SELL, 25 * Point(), 50 * Point(), true);  //Trailing Stop = trail
 
   TS = NormalizeDouble(TS, Digits());
   step = NormalizeDouble(step, Digits());
   :
      myOrderModify(OrderTicket(), Ask - TS, 0);
  1. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong

  2. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  3. while(IsTradeContextBusy()) Sleep(100);
    After Sleep and between server calls the market will have changed. You must update your variables. To use any pre-defined variables and series arrays you must call RefreshRates
              RefreshRates - Timeseries and Indicators Access - MQL4 Reference

Reason: