if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < Bid - Point*TraillingStop )) { int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue); if(ticket <= 0) return; }
You normalize the SL when you set it, but you don't normalize when you check it.
OrderModify returns a bool, not an int.
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
You normalize the SL when you set it, but you don't normalize when you check it.
OrderModify returns a bool, not an int.
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
Hi Thanks,
I just changed it to
if((OrderType()==OP_BUY) && (CheckTotalNumberOfOrders_Buy() == 1)) { if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < NormalizeDouble(Bid-Point*TraillingStop,Digits))) { //Trailing_SL_Point = Bid-Point*TraillingStop; int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue); //Print("modify BUY :", DoubleToString( NormalizeDouble(Bid-Point*TraillingStop,Digits))); if(ticket <= 0) return; } } if((OrderType()==OP_SELL) && (CheckTotalNumberOfOrders_Sell() == 1)) { if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > NormalizeDouble(Ask+Point*TraillingStop,Digits)) || (OrderStopLoss() == 0))) { //Trailing_SL_Point = Ask+Point*TraillingStop; int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red); //Print("modify SELL :", DoubleToString( NormalizeDouble(Ask+Point*TraillingStop,Digits))); if(ticket <= 0) return; } }
but it still same issue.
I can see the order being modified. however in the log it shows OrderModify Error 130. even though it manages to modify the order itself (Backtest).
Hi Thanks,
I just changed it to
but it still same issue.
I can see the order being modified. however in the log it shows OrderModify Error 130. even though it manages to modify the order itself (Backtest).
Please Change this code
if((OrderType()==OP_BUY) && (CheckTotalNumberOfOrders_Buy() == 1)) { // if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < NormalizeDouble(Bid-Point*TraillingStop,Digits))) if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < NormalizeDouble(OrderOpenPrice()-Point*TraillingStop,Digits))) { //Trailing_SL_Point = Bid-Point*TraillingStop; // int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue); int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue); //Print("modify BUY :", DoubleToString( NormalizeDouble(Bid-Point*TraillingStop,Digits))); if(ticket <= 0) return; } } if((OrderType()==OP_SELL) && (CheckTotalNumberOfOrders_Sell() == 1)) { //if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > NormalizeDouble(Ask+Point*TraillingStop,Digits)) || (OrderStopLoss() == 0))) if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > NormalizeDouble(OrderOpenPrice()+Point*TraillingStop,Digits)) || (OrderStopLoss() == 0))) { //Trailing_SL_Point = Ask+Point*TraillingStop; //int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red); int ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red); //Print("modify SELL :", DoubleToString( NormalizeDouble(Ask+Point*TraillingStop,Digits))); if(ticket <= 0) return; } }
I have just made some adjustment , but still seems the trailing sometime try to modify wrong OrderTicket. any idea everyone?
void TrailStops() { for (int i = 0; i < OrdersTotal(); i++) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { continue; } if (OrderSymbol() != Symbol()) { continue; } if (OrderMagicNumber() != MagicNumber) { continue; } if (OrderComment() != Comment) { continue; } if((OrderType()==OP_BUY) && (CheckTotalNumberOfOrders_Buy() == 1)) { if(((Bid - OrderOpenPrice()) > (Point*TrailingStart)) && (OrderStopLoss() < NormalizeDouble(Bid-Point*TraillingStop,Digits))) { if (OrderStopLoss() == 0) { bool ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue); Print("modify BUY :", OrderTicket()); if(ticket) Print("Order Stop Loss Has Been Modified Successfully."); } else { if ((OrderStopLoss() + TrailingStep*Point) < NormalizeDouble(Bid-Point*TraillingStop,Digits)) { bool ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TraillingStop,Digits),OrderTakeProfit(),0,Blue); Print("modify BUY :", OrderTicket()); if(ticket) Print("Order Stop Loss Has Been Modified Successfully."); } } } } if((OrderType()==OP_SELL) && (CheckTotalNumberOfOrders_Sell() == 1)) { if(((OrderOpenPrice() - Ask) > (Point*TrailingStart)) && ((OrderStopLoss() > NormalizeDouble(Ask+Point*TraillingStop,Digits)) || (OrderStopLoss() == 0))) { if (OrderStopLoss() == 0) { bool ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red); Print("modify SELL :", OrderTicket()); if(ticket) Print("Order Stop Loss Has Been Modified Successfully."); } else { if ((OrderStopLoss()-TrailingStep*Point) > NormalizeDouble(Ask+Point*TraillingStop,Digits)) { bool ticket=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TraillingStop,Digits),OrderTakeProfit(),0,Red); Print("modify SELL :", OrderTicket()); if(ticket) Print("Order Stop Loss Has Been Modified Successfully."); } } } } } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I have written the code below for trailing stoploss. it does work but on the other end it seems to keep spamming lots of error in the log too. which makes me confuse.