Download MetaTrader 5

trailing stop

To add comments, please log in or register
RTaylor
159
RTaylor  

I'm not a coder but I hope I'm giving enough info here for one of you MT4 geniuses to answer!

I have a custom EA which was designed according to a set of trading rules and formulas.  

When I run it through strategy tester, the trailing stop seems to work properly.   But in my demo account it's not working.

The inputs are:  TrailStart (which is turned on when the floating P/L of the total account reaches a certain percentage)

And TrailStop, (which is a % of the TrailStart)

Example:   TrailStart is set at 1%.   Trailstop is set at 10%.  

Therefore if TrailStart is set at 1%, and falls down to .9%, the open positions are closed.

I tested the EA on Friday in a 100,000 USD demo account.  The profit rose above TrailStart, which was set at .0015 then dropped below the TrailStop, to .00145%

The Floating P.L actually rose above and below the TrailStop several times... and my positions were never closed.

I found these pieces of code in the EA.  Can you help me identify what is not working properly?   (You'll see a reference to news.  part of the EA has a news "hold" which prohibits trading during major announcements.  But the news function was turned off).


void SetTrailingStop(string Symbol) {
   if(TrailStop<=0||TrailStart<=0) return;
   double newsl =0, trail_start=0, trail_stop=0;
   trail_start = AccountBalance()*TrailStart;
   double stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL); 
   for (int i = 0; i < OrdersTotal(); i++) {
      int select = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(!select || OrderMagicNumber() != Magic_number || OrderSymbol() != Symbol )continue;
      if(OrderType() == OP_BUY) {
         newsl = NormalizeDouble(OrderClosePrice() - MathAbs(OrderOpenPrice() - OrderClosePrice())*TrailStop,_Digits); 
         if ( (trail_start==0||OrderProfit()+OrderCommission()+OrderSwap() > trail_start) && newsl- OrderStopLoss()> Point )
         bool mod = OrderModify(OrderTicket(),OrderOpenPrice(),newsl,OrderTakeProfit(), 0, clrWhite); 
      }
      else
      if(OrderType() == OP_SELL) {
         newsl = NormalizeDouble(OrderClosePrice() + MathAbs(OrderOpenPrice() - OrderClosePrice())*TrailStop,_Digits); 
         if( (OrderProfit()+OrderCommission()+OrderSwap() > trail_start) && (OrderStopLoss()==0 || OrderStopLoss() - newsl > Point ) )
         bool mod = OrderModify(OrderTicket(),OrderOpenPrice(),newsl,OrderTakeProfit(), 0, clrWhite);
      }
   }
   return;
}

I can post other parts of the code if this helps.

Thank you!

To add comments, please log in or register