This seems to work well...
// when an order is closed stopMoved is set to 0 void stopBreakeven() { int err; if ( OrderType() == OP_BUY && (OrderProfit()*Point*10)+0.0010 && stopMoved == 0 ) { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop up to break-even +1 err = GetLastError(); Print("OrderModify Buy Error:",err); if (err == 0) stopMoved = 1; } if ( OrderType() == OP_SELL && (OrderProfit()*Point*10)+0.0010 && stopMoved == 0 ) { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop down to break-even +1 err = GetLastError(); Print("OrderModify Sell Error:",err); if (err == 0) stopMoved = 1; } return; } // end stopBreakeven
This seems to work well...
Really ?
This is not a bool . . . what does it do ?
OrderProfit()*Point*10)+0.0010
Why do you print the error even if there isn't one ?
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop up to break-even +1 err = GetLastError(); Print("OrderModify Buy Error:",err);
Why not make stopmoved a bool ? and use the function to return it ?
Does you code ensure that you do not try to set the same SL a second or more times ? if not you will get error 1
Oops, the "OrderProfit()*Point*10)+0.0010" is intended to make sure there are 10 pips profit before moving the stop. But you're right that piece of code is not going to do it.
OrderProfit is a Account Currency (dollar) amount.
//++++ These are adjusted for 5 digit brokers. int pips2points; // slippage 3 pips 3=points 30=points double pips2dbl; // Stoploss 15 pips 0.015 0.0150 int Digits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips) int init(){ OptInitialization(); if (Digits % 2 == 1){ // DE30=1/JPY=3/EURUSD=5 https://www.mql5.com/en/forum/135345 pips2dbl = Point*10; pips2points = 10; Digits.pips = 1; } else { pips2dbl = Point; pips2points = 1; Digits.pips = 0; } // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //---- These are adjusted for 5 digit brokers. /* On ECN brokers you must open first and THEN set stops int ticket = OrderSend(..., 0,0,...) if (ticket < 0) Alert("OrderSend failed: ", GetLastError()); else if (!OrderSelect(ticket, SELECT_BY_TICKET)) Alert("OrderSelect failed: ", GetLastError()); else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0) Alert("OrderModify failed: ", GetLastError()); */Might try (buy) OrderClosePrice() - OrderOpenPrice() >= 10*pips2dbl
This seems to work well...
does this code works "to take 1 pip as takeprofit"??
the code below..
void stopBreakeven() { int err; if ( OrderType() == OP_BUY && (OrderProfit()*Point*10)+0.0010 && stopMoved == 0 ) { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop up to break-even +1 err = GetLastError(); Print("OrderModify Buy Error:",err); if (err == 0) stopMoved = 1; } if ( OrderType() == OP_SELL && (OrderProfit()*Point*10)+0.0010 && stopMoved == 0 ) { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-0.0001,OrderTakeProfit(),0,Turquoise); // moved the stop down to break-even +1 err = GetLastError(); Print("OrderModify Sell Error:",err); if (err == 0) stopMoved = 1; } return; } // end stopBreakeven
does this code works "to take 1 pip as takeprofit"??
the code below..
void breakEvenStopLoss() { for (int i = 0; i < OrdersTotal(); i++){ int order = OrderSelect(i, SELECT_BY_POS); if(OrderMagicNumber() != MAGIC || OrderSymbol() != Symbol()) continue; if (OrderSymbol() == Symbol()){ if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL)) { double sl = 0; if ((OrderType() == OP_BUY) && (OrderStopLoss() < OrderOpenPrice()) ){ sl = OrderOpenPrice() ; } if ((OrderType() == OP_SELL) && (OrderStopLoss() > OrderOpenPrice()) ){ sl = OrderOpenPrice() ; } if (sl != 0){ OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0); } } } } }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'd like to learn more about open orders -- getting information and modifying. Moving the stop to breakeven should be a good project. I found plenty of discussion on the subject but no finished code illustrating the details of how it's really done. Here's a few references if anyone wants to see what's out there:
And here's a rough draft of what I have in mind: