Still got an error that the sl is too near.....even the sl is more away than the stop level!
Inserted Code
//*******************************************************************
void trailVisibleSL(double trailingPrice,string symbol) {
color tradecolor;
double stopLevel=(double)MarketInfo(symbol,MODE_STOPLEVEL);
if (OrderType()==OP_BUY){
// distance ok?
if (Bid - trailingPrice < stopLevel * Point)
trailingPrice =Bid-stopLevel * Point;
tradecolor=clrPaleGreen;
// Move only if nearer
if (trailingPrice < OrderStopLoss()&& OrderStopLoss()>0)
return;
}
else if (OrderType()==OP_SELL){
// distance ok?
if (trailingPrice - Ask< stopLevel * Point)
trailingPrice = Ask+stopLevel * Point;
tradecolor=clrLightCoral;
// Move only if nearer
if (OrderStopLoss() < trailingPrice && OrderStopLoss()>0)
return;
}
else{
Alert("trailVisibleSL: Unknown case beneath OP_BUY,OP_SELL!");
return;
}
modifyOrder(OrderTicket(), OrderOpenPrice(), trailingPrice, OrderTakeProfit(), tradecolor, symbol);
}
//*******************************************************************
// ModifyOrder
//*******************************************************************
void modifyOrder(int ticket, double price, double sl, double tp, color mColor,string symbol) {
int CloseCnt, err;
// new sl only bigger than last loss with less than a point?
double diffSL=MathAbs(OrderStopLoss()-sl);
// Rounding problems?
if (diffSL<Point)
return;
CloseCnt = 0;
bool success;
while (CloseCnt < 3) {
if (CloseCnt == 0)
success= OrderModify(ticket, price, NormalizeDouble(sl,Digits), NormalizeDouble(tp,Digits), 0, mColor);
else
success= OrderModify(ticket, price, sl, tp, 0, mColor);
if (success) {
CloseCnt = 3;
} else {
err = GetLastError();
Alert("===============================================================");
Alert("Error modifying order: "+ err+" Symbol:"+OrderSymbol());
Alert("sl old:"+(string)OrderStopLoss()+ " sl new: " +(string)sl);
Alert("Normalized old sl:" +(string) NormalizeDouble(sl,Digits));
Alert("Diff in points:"+(string)(diffSL/Point)+ " stop level:" +(string) MarketInfo(symbol,MODE_STOPLEVEL));
CloseCnt++;
}
}
} Vucking good EA coder... https://t.ly/AZjRM