Trailing Stop is not Executed in Backtesting. No Errors.... Please help because I'm stumped.

 

I have been working on this simple EA to get the hang of the programming language. It's not intended to be super profitable but more of a get rich slowly scheme while I suplement it with my trading expertise. My problem is that i can't get the trailing stop to be executed even when I set if(OrdersTotal()>0). It's like the code isn't even there. Please help me as soon as possible so I can correct the problem

P.S. I know that "strategery" isn't a word. It's a George W. Bush word.

Code for both the trailing stops is in the IncludeFile.mqh

 

It's bad practice to hard code values instead of using variables . . . if you hadn't done that you wouldn't have the mismatch in your Magic Numbers between your buy OrderSend and your BuyTrailingStop . . .

You also need to check return values from OrderSend and and deal with any errors that arise appropriately . . .

 

Thank you for that advice about doing the variables instead of hardcoding; I wouldnt have ever noticed that... the trades execute flawlessly but its the OrderModify that isn't working. Like I said before, There are no errors, its like the code isn't even there.

 
35806:

Thank you for that advice about doing the variables instead of hardcoding; I wouldnt have ever noticed that... the trades execute flawlessly but its the OrderModify that isn't working. Like I said before, There are no errors, its like the code isn't even there.

The point is that because the Magic Numbers don't match then the Modify for your Buy orders will never execute . . . if you only have Buy orders then you will never get any orders modified.
 

Even after i fixed that issue the trailing stops still arent being executed. As far as I know everything is OK since both MetaEditor isn't recognizing any errors. The backtester doesn't show any errors either... Here is an updated copy of the code. Thank you for your help. the trailing stop code is the same.

 

What are you using this EA on ? what Symbol ? if you are trading anything with more than 2 Digits then you are reducing your MaxStopLoss significantly, and as one of your tests is CurrentStopLoss<MaxStopLoss to decide if you are going to OrderModify or not . . . then you will probably never OrderModify.

MaxStopLoss=NormalizeDouble(MaxStopLoss,2); why hard code to 2 digits ?

If you want to find errors you need INFORMATION . . how do you get it ? ADD Print statements . . . print all your variables, when something isn't working you can check your variable values to see if they are right or wrong . . then you have info and you can find where your issue is.

 

I did the error log print thing and it still doesn't return any errors. I also changed MaxStopLoss=NormalizeDouble(MaxStopLoss,2); to MaxStopLoss=NormalizeDouble(MaxStopLoss,MODE_DIGITS); and applied the same generic code to the whole program. The backtesting still doesnt execute the trailing stop and doesn't return any errors. Here are the updated files.

 

If you take a look here: https://docs.mql4.com/constants/marketinfo you will see that MODE_DIGITS has a value of 12, try Digits instead https://docs.mql4.com/predefined/variables/digits

So you added Print statements to print all your variables to the Experts log ? and are the variables correct when BuyTrailingStop and SellTrailingStop are called ? ah, I see . . you didn't add any Print statements . . .

These are your conditional statements . . .

if(OrderMagicNumber()==MagicNumber && OrderSymbol()==argSymbol && OrderType()==OP_SELL 
      && CurrentStopLoss<MaxStopLoss && PipsProfit>=MinProfit)

if(OrderMagicNumber()==MagicNumber && OrderSymbol()==argSymbol && OrderType()==OP_BUY 
      && CurrentStopLoss<MaxStopLoss && PipsProfit>=MinProfit)

. . . if you want to know why the contents of the () braces doesn't equal true you need to know what the values of the variables inside the braces are . . .

Immediately before each of these lines add one or more Print statements to print ALL the variables in the braces out to the log . . . or don't . . maybe you can find the problem staring at the code for hours.

 
Once the order is placed and running how does BuyTrailingStop or SellTrailingStop get called ? answer . . they don't.
 

i though that if you do () in those brackets it refers to the current order... the order selected earlier in the code. I can't stare at the code for hours. That would be completely inefficient j/k. Ok i read how to do a print statement and i seel what you mean. Sorry for the misunderstanding, I'm a beginner and don't really know the terms. I thought you meant printing the errorlog. I will print all the variables and post again. Thank you for all of your help.

 

Add this code before the return(0); in Movingvaveragejstrategery.mq4

for(int Counter=0;Counter<=OrdersTotal()-1;Counter++)
   {
   OrderSelect(Counter,SELECT_BY_POS);
   
   if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==OP_SELL )
      SellTrailingStop(Symbol(), TrailingStop, MinimumProfit, MagicNumber);
      
   if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==OP_BUY )      
      BuyTrailingStop(Symbol(), TrailingStop, MinimumProfit, MagicNumber);  
      
   }