- MQL5 Wizard: Development of trading robots for MetaTrader 5
- Code profiling - Developing programs
- Debug - Main menu
Hi everyone. I am trying to implement a trailing stop. The code compiles, but the trailing stop function is not working in tester. I am trying to call the function in the very first line after OnTick, so i would assume that the function will be called every time there is a new tick. But no orders are being modified when i test the code. Can anyone spot why this is not working?
for(int i=OrdersTotal()-1;i=0;i--){You have to write i>=0
for(int i=OrdersTotal()-1;i=0;i--){
I think that you want
for(int i=OrdersTotal()-1;i>=0;i--){
I think that you want
Thank you so much for looking at the code. However, I implemented your suggestions but the trail function is still not working:( If anyone have
any other ideas im happy to hear. Been going over it so many times myself, and i really cant figure out where the issue is..
Hi everyone. I am trying to implement a trailing stop. The code compiles, but the trailing stop function is not working in tester. I am trying to call the function in the very first line after OnTick, so i would assume that the function will be called every time there is a new tick. But no orders are being modified when i test the code. Can anyone spot why this is not working?
//+------------------------------------------------------------------+ //| Trailing Stop Function | //+------------------------------------------------------------------+ void AdjustTrail(){ for(int i=OrdersTotal()-1;i=0;i--){ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(OrderMagicNumber()==magicNB) if(OrderType()==OP_BUY){ if(Bid-OrderOpenPrice()>TrailBegin*Point) if(OrderStopLoss()<Bid-TrailStep*Point) if(!OrderModify(OrderTicket(),Bid-(TrailStep*Point),OrderTakeProfit(),0,CLR_NONE)){ int err = GetLastError(); Print("SL modify error"); }// OrderModify }//buy order else if (OrderType()==OP_SELL){ if(OrderOpenPrice()- Ask >TrailBegin*Point) if(OrderStopLoss()<Ask+TrailStep*Point) if(!OrderModify(OrderTicket(),Ask+(TrailStep*Point),OrderTakeProfit(),0,CLR_NONE)){ int err = GetLastError(); Print("SL modify error"); } }// sell order }//orderSelect else{//in case it fails to select an order int err = GetLastError(); Print("Error selecting an Order"); // Why not just use Print("Error selecting an order",GetLasterror()); } }// forloop
i wonder...were do you actually print out the error code itself in journal log using that kind of coding of yours...?
- Better use input in favour of extern because it's deprecated and even will not show up in MT5 inputs.
- TrailStep usually means the step size (increment) to move the trailing stop. I guess you mixed Trailing Step and Trailing Stop somewhere.
- So TrailStop is somewhere missing in the inputs.
- Point is mostly different from a Pip and may vary across brokers for the same security.
- Check out some EAs in the codebase and compare their trailing stop logic to yours.
Thank you so much for looking at the code. However, I implemented your suggestions but the trail function
is still not working:( If anyone have any other ideas im happy to hear. Been going over it so many times myself, and i really cant figure out
where the issue is..
This line:
if(OrderStopLoss()<Ask+TrailStep*Point)
Should be:
if(OrderStopLoss()>Ask+TrailStep*Point)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use