How do I correct an error code 0 in MQL4 OrderModify() call?

How do I correct an error code 0 in MQL4 OrderModify() call?

Manage alerts

Loading saved threads...

Peterclaver · External communityPost link
External question — Stack Overflow Stack Exchange Author: Peterclaver Original post: https://stackoverflow.com/questions/70967970 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I have this code below which I am using to test trailing stop function. It works on some currency pairs and doesn't work in others. It's returning error code 0 (zero). I will be grateful if somebody can help me out void modifyTrade() { TrailingStop = MarketInfo(Symbol(), MODE_STOPLEVEL); if(TrailingStop>0) { for(int z = 0; z<OrdersTotal(); z++) { OrderSelect(z,SELECT_BY_POS); if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()<Bid-Point*TrailingStop) { bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue); if(!res) { Print("Error in OrderModify. Error code=",GetLastError()); //if(Symbol()=="EURNZD") Alert("Error in OrderModify. Error code=",GetLastError()," ; ",OrderSymbol()); } else { Print("Order modified successfully."); } } } } } }
Quote
Report
user3666197 · External communityPost link
External answer — Stack Overflow Stack Exchange Author: user3666197 Original post: https://stackoverflow.com/a/71133660 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Error being 0 == ERR_NO_ERROR ... No error returned is rather ok, no need to "correct" it, it means there was nothing to actually improve. There are a few risky points to mention - prices move, STOPLEVEL parameter can either ( be careful on this - a hostile case if appears in live-trading ) and also be informed, that there are other levels, that may prohibit an OrderModify() -call from being Broker-T&C-compliant (and will get rejected for that reason) Somehow improved code may work in this sense : void modifyTrade() { TrailingStop = MarketInfo( Symbol(), MODE_STOPLEVEL ); if ( TrailingStop > 0 ) { for ( int z = 0; z < OrdersTotal(); z++ ) { if !OrderSelect( z, SELECT_BY_POS ) continue; // ______________________________________________________ CRITICAL RefreshRates(); // ______________________________________________________ Bid + Ask valid... if ( TrailingStop != MarketInfo( OrderSymbol(), MODE_STOPLEVEL ) ) { // TrailingStop CHANGED ... LOG, renegotiate such Broker policy TrailingStop = MarketInfo( OrderSymbol(), MODE_STOPLEVEL } // ______________________________________________________ STOPLEVEL valid... if ( Bid - OrderOpenPrice() > Point * TrailingStop && OrderType() == OP_BUY ) { if ( OrderStopLoss() < Bid - Point * TrailingStop ) { bool res = OrderModify( OrderTicket(), OrderOpenPrice(), NormalizeDouble( Bid - Point * TrailingStop, Digits ), OrderTakeProfit(), 0, Blue ); if ( !res ) { Print( "Error in OrderModify. Error code=", _LastError ); // if ( Symbol() == "EURNZD" ) Alert( "Error in OrderModify. Error code=", _LastError, " ; ", OrderSymbol() ); } else { Print( "Order modified successfully." ); } } } if ( OrderOpenPrice() - Ask > Point * TrailingStop && OrderType() == OP_SELL ) { ... } } } }
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange Author: PaulB Original post: https://stackoverflow.com/a/71226029 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. There are a couple of errors in your code. once called, GetLastError() is reset. If you are both printing and alerting the last error, you should store the error code prior to printing/alerting. you should check OrderSelect() succeeds. you should iterate over orders in the opposite direction to ensure your code carried on operating should an order be closed mid-check. You require different code for longs and shorts Your code is prone to errors simply because you have no buffer and are trying to trail price at the minimum stop level. By the time your order modification is received by the trading server it is highly likely price may have moved back inside the stop level area resulting in a modification error. void modifyTrade() { TrailingStop = MarketInfo(Symbol(), MODE_STOPLEVEL); if(TrailingStop>0) { for(int z=OrdersTotal()-1; z>=0; z--) { if(OrderSelect(z,SELECT_BY_POS)) { if(OrderType()==OP_BUY) { if(Bid-OrderStopLoss()>TrailingStop*point) { bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue); if(!res) { int err=GetLastError(); Print("Error in OrderModify. Error code=",err); Alert("Error in OrderModify. Error code=",err); } else Print("Order modified successfully."); } } if(OrderType()==OP_SELL) { if(OrderStopLoss()-Ask>TrailingStop*point) { bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue); if(!res) { int err=GetLastError(); Print("Error in OrderModify. Error code=",err); Alert("Error in OrderModify. Error code=",err); } else Print("Order modified successfully."); } } } } } }
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Peterclaver Source score (net votes, not local likes): 1 Original post: https://stackoverflow.com/questions/70967970 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I have this code below which I am using to test trailing stop function. It works on some currency pairs and doesn't work in others. It's returning error code 0 (zero). I will be grateful if somebody can help me out void modifyTrade() { TrailingStop = MarketInfo(Symbol(), MODE_STOPLEVEL); if(TrailingStop>0) { for(int z = 0; z<OrdersTotal(); z++) { OrderSelect(z,SELECT_BY_POS); if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()<Bid-Point*TrailingStop) { bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue); if(!res) { Print("Error in OrderModify. Error code=",GetLastError()); //if(Symbol()=="EURNZD") Alert("Error in OrderModify. Error code=",GetLastError()," ; ",OrderSymbol()); } else { Print("Order modified successfully."); } } } } } }

Cancel quote

Checking account access…