Depending on whether your condition is fit for whether the trade is in a loss or in a profit you can use this simple function for when your trade is actively in a loss with a condition.
One of the reasons why your stoploss is further away could be because you are looping through the previos order and not current order.
double breakevenlevel() { int i, hstTotal=OrdersHistoryTotal(); for(i=0; i<hstTotal; i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderType() == OP_BUY) if(OrderMagicNumber() == "MN") return(OrderOpenPrice()); } // condition to break even void OnTick() { if(condition) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), breakevenlevel(),clrNONE); // to modify the stoploss you could either return NULL (no stoploss) or you can just accept the loss. // Takeprofit is breakeven level, if this was in the orderstoploss command, the trade will terminate immediately after condition is met depending on the condition. // if trade is actively in profit with condition then you would switch the breakevenlevel from ordertakeprofit to orderstoploss }
Depending on whether your condition is fit for whether the trade is in a loss or in a profit you can use this simple function for when your trade is actively in a loss with a condition.
One of the reasons why your stoploss is further away could be because you are looping through the previos order and not current order.
double breakevenlevel() { int i, hstTotal=OrdersHistoryTotal(); for(i=0; i<hstTotal; i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderType() == OP_BUY) if(OrderMagicNumber() == "MN") return(OrderOpenPrice()); } // condition to break even
Why are you using OrdersHistoryTotal() ?
Why are you returning the OrderOpenPrice() for the first buy found in the loop? How is that supposed to be the break-even level if there are multiple trades open?
Why are you checking OrderMagicNumber() against a string?
This is where organisation comes into play imo. If you re read what I just wrote I've emphasised simple function for when your trade is actively in a loss with a condition. The code above is just a simple guidance on how to achieve something similar, theres no wrong answer keith.
int i, hstTotal=OrdersHistoryTotal() for(i=0; i<hstTotal; i++) helps me focus on the current trade
Returning OrderOpenPrice() helps identify the breakeven level per trade.
For multiple trades I would genuinely just put a magic number on each trade, apply it onto multiple functions and run it through each for loop returning that specific orderprices.
Very simple, I have a similar EA to what this person is trying to accomplish and it runs ok.
void ClassicTrail() //1 Вариант: Классический тралл { for(int i=OrdersTotal()-1; i > 0; --i) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); { Print(OrderTicket()); Print(OrderStopLoss()); Print("BreakevenPrice:",BreakevenBuy()); { if((NormalizeDouble(OrderStopLoss(),Digits) <= NormalizeDouble(BreakevenBuy(),Digits)) && ((OrderType() == OP_BUY) && (CountBuyPositions()>1))) { OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(BreakevenBuy(),Digits), OrderTakeProfit(),0,clrNONE); if(GetLastError()==1) { break; } } } } } }Are there possible workaround simply to catch if
(GetLastError()==1)we skip this one and go to next order (i)?
How to properly code it?
This is where organisation comes into play imo. If you re read what I just wrote I've emphasised simple function for when your trade is actively in a loss with a condition. The code above is just a simple guidance on how to achieve something similar, theres no wrong answer keith.
There are wrong answers and your answer is definitely one of them.
You haven't answered the questions
Why are you using OrdersHistoryTotal() ?
Why are you returning the OrderOpenPrice() for the first buy found in the loop? How is that supposed to be the break-even level if there are multiple trades open?
Why are you checking OrderMagicNumber() against a string?
Breakeven price 1.276
OrderStopLoss()!=BreakevenBuy()
why stop loss for #2 order 1.1233 can't be changed to 1.276. Losing my mind here
img attached
void OnTick() { .. ClassicTrail(); .. } ... double BreakevenBuy() { double SummBuyLots_Prices=0; double SummLots=0; double AvereageBuyLevel=0; for(int i=0; i<OrdersTotal(); i++) { if(!OrderSelect(i,SELECT_BY_POS)) continue; if(OrderType()!=OP_BUY || OrderSymbol()!=Symbol() || OrderMagicNumber()=="") continue; { SummLots+=OrderLots(); SummBuyLots_Prices+=OrderLots()*OrderOpenPrice(); } } if(SummLots>0) { AvereageBuyLevel=SummBuyLots_Prices/SummLots; AvereageBuyLevel=NormalizeDouble(AvereageBuyLevel,4); } return (AvereageBuyLevel); } ... void ClassicTrail() { for(int i=OrdersTotal()-1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); { { if(OrderStopLoss()!=BreakevenBuy() && (OrderType() == OP_BUY) && (CountBuyPositions()>1)) { Print (OrderTicket()); Print (OrderStopLoss()); OrderModify(OrderTicket(), OrderOpenPrice(), BreakevenBuy(), OrderTakeProfit(),clrNONE); } } } } }
There are wrong answers and your answer is definitely one of them.
You haven't answered the questions
Why are you using OrdersHistoryTotal() ?
Why are you returning the OrderOpenPrice() for the first buy found in the loop? How is that supposed to be the break-even level if there are multiple trades open?
Why are you checking OrderMagicNumber() against a string?
Well everyone is entitled to an opinion but in coding there aren’t any wrong answers which clearly depicts your ego. I’ve also answered your questions so you can go back to what I’ve wrote and read it again. I’m not going to entertain your rhetorical quizzes which are just considered rude.
Suit yourself.
if(OrderMagicNumber() == "MN")
What do you think comparing a number to a string does? It is obviously wrong. That is a fact, not an opinion.
It is clearly your ego doesn't want help from people trying to help you. Live in ignorance.
What do you think comparing a number to a string does? It is obviously wrong. That is a fact, not an opinion.
It is clearly your ego doesn't want help from people trying to help you. Live in ignorance.

- 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 can't understand why i can't properly move all active orders SL's to BreakevenPrice()
Function simply not looping thru all SL's and return "Error 1" which means i try change SL with same SL, but BreakevenBuy() already moved father
(ELI5 On attached image: I want to move SL's from 2 to 7 onto 7 level, how?)