Need help to complete code , Once breakeven hit then trailing stop should start.

 
Hi folks,
I am a newbie need help for my code. I copied all the stuff from here and there. Till now everything is working as expected.
Trail & Breakeven both working individually. But if I activate them with "if else" both start working , SL moves to BE then TS and viceversa rapidly. i.e. SL simply dances on every price change.

Any help will be appriciated.

Trailing code I took from below thread.
https://www.forexfactory.com/thread/...code-for-an-ea




if (OrderStopLoss() == OrderOpenPrice())

{
if(trail) trail(); //Trailing Stop Start
}

else //Break Even

{
int digits = MarketInfo(Symbol(),MODE_DIGITS);
double value = OrderOpenPrice();
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderSymbol()!=Symbol())
continue;

RefreshRates();

if(OrderType()==OP_BUY)
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),OrderExpiration());
if(OrderType()==OP_SELL)
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),OrderExpiration());

}
}




void trail()
{
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ( OrderSymbol()==Symbol() )
{
if (OrderType() == OP_BUY) {
if (Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
}
}
} else if (OrderType() == OP_SELL) {
if (OrderOpenPrice() - Ask > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
if ((OrderStopLoss() > Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) ||
(OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(),
Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
}
}
}
}
}
}


--------------------------------------------------------------------------------------------


 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 
  1. Just write your trailing code. Don't modify the SL unless the new SL is above/below OOP (break even) and above/below the current SL.
  2. Run the posted code through the styler so you can see what is inside what.
 
William Roeder:
  1. Just write your trailing code. Don't modify the SL unless the new SL is above/below OOP (break even) and above/below the current SL.
  2. Run the posted code through the styler so you can see what is inside what.

But breakeven still running.