I think that the trailing stop is not working as expected.
These 2 lines adding or subtracting from the current StopLoss rather than the Bid.
3683
Code: Select all
NewStop = NormalizeDouble(sl + (TrailingStopPips / factor), Digits)
Code: Select all
NewStop = NormalizeDouble(sl - (TrailingStopPips / factor), Digits);
Code: Select all
void TrailingStopLoss(int ticket)
{
//Security check
if (!BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
return;
if (OrderProfit() < 0) return;//Nothing to do
string LineName = SlPrefix + DoubleToStr(OrderTicket(), 0);
double sl = ObjectGet(LineName, OBJPROP_PRICE1);
//if (CloseEnough(sl, 0) ) return;//No line, so nothing to do
if (CloseEnough(sl, 0) ) sl = OrderStopLoss();
double NewStop;
bool modify=false;
bool result;
if (OrderType()==OP_BUY)
{
if (sl < OrderOpenPrice() ) return;//Not at breakeven yet
// Increment sl by sl + TrailingStopPips.
if (CloseEnough(sl, 0) ) sl = MathMax(OrderStopLoss(), OrderOpenPrice());
//Check that the market is sufficiently in profit to continue
if (Bid < OrderOpenPrice() + (trailStartsAtProfit / factor) )
return;
if (Bid >= sl + (TrailingStopPips / factor) )//George
{
NewStop = NormalizeDouble(Bid - (TrailingStopPips / factor), Digits); //FourXXX
if (HiddenPips > 0) ObjectMove(LineName, 0, Time[0], NewStop);
if (NewStop - OrderStopLoss() >= Point) modify = true;//George again. What a guy
}//if (Bid >= MathMax(sl,OrderOpenPrice()) + (TrailingStopPips / factor) )//George
}//if (OrderType()==OP_BUY)
if (OrderType()==OP_SELL)
{
if (sl > OrderOpenPrice() ) return;//Not at breakeven yet
// Decrement sl by sl - TrailingStopPips.
if (CloseEnough(sl, 0) ) sl = MathMin(OrderStopLoss(), OrderOpenPrice());
if (CloseEnough(sl, 0) ) sl = OrderOpenPrice();
//Check that the market is sufficiently in profit to continue
if (Bid > OrderOpenPrice() - (trailStartsAtProfit / factor) )
return;
if (Bid <= sl - (TrailingStopPips / factor))//George
{
NewStop = NormalizeDouble(Bid + (TrailingStopPips / factor), Digits); //FourXXXX
if (HiddenPips > 0) ObjectMove(LineName, 0, Time[0], NewStop);
if (OrderStopLoss() - NewStop >= Point || OrderStopLoss() == 0) modify = true;//George again. What a guy
}//if (Bid <= MathMin(sl, OrderOpenPrice() ) - (TrailingStopPips / factor) )//George
}//if (OrderType()==OP_SELL)
//Move 'hard' stop loss whether hidden or not. Don't want to risk losing a breakeven through disconnect.
if (modify)
{
while (IsTradeContextBusy() ) Sleep(100);
result = ModifyOrder(OrderTicket(), OrderOpenPrice(), NewStop, OrderTakeProfit(), OrderExpiration(), clrNONE, __FUNCTION__, slm);
}//if (modify)
} // End of TrailingStopLoss sub