EA has an issue with trailing stoploss not working sometimes (non coder)

so that’s about it, new to Forex, took a basic MACD EA and tweaked it to an 83% hit rate AND a 1:3 Risk:Reward ratio, so a pretty solid build to move to demo… or it WOULD be, if the trailing stop worked all the time.

the T/P is set at 80, the Trailing Stop at 30, so why i the “average loss” 10x the “average profit”? because every once in a while the stop doesn’t happen, and I lose 1500$!? when combing through the strategy tester sometimes the stop takes over 2 hours to “modify” onto the order.

Is there any code to insert into the EA to make a standard trailing stop modification, so it happens right away? (I know in demo/live I could run a separate EA for the s/l, but I can’t backtest 2 EAs at once)

What follows is every line with trailing stop on it:


input double Stoploss      =30;  ...

//--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }


  //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }

Hi Filmbuff88,

I have assumed that your EA is either long or short at any one time only?

I have assumed that only this EA is trading on a single chart all by itself?

First I suspect you may be running this EA more than once on the same pair or different pairs and there is some interaction with the other EA.

Normally every EA uses a different magic number so there is no clash with other EA’s.

Looking at the code I would have expected it to be more specific.

if(TrailingStop>0) is far to generic.

It should be more like:-

if(TrailingStop>0 && BuyMarketCount(Symbol(),MagicNumber)>0)

This wont work for you as it references other functions you don’t have. But what it does is to first check that there is an open OP_BUY order to trail the stop on and that it is for this charts pair and it has this EA’s magic number. This is vital if you are running more that one EA at a time.

Same for the Sell order.

I also notice that that the long and short code are different???

On the long code should if(OrderStopLoss()<Bid-Point*TrailingStop) not really be like the short code and include the or statement, as this may also be part of your problem.

if((OrderStopLoss()<Bid-Point*TrailingStop) || (OrderStopLoss()==0))

For more information on the above you need the book “Expert Advisor Programming” by Andrew R Young. That is where I learnt to write my own EA’s.

Regards, Trader9.