I'm trying to get a candlestick trailing stoploss working, but have come across a very strange situation...
The trailing stop works fine for Buys, but for Sells, I have to insert either an alert, or Sleep(1);...
Here's the code, (adapted from Steve's shell)...
Code: Select all
void CandlestickTrailingStop()
{
//Trails the stop at the hi/lo of the previous candle shifted by the user choice.
//Only tries to do this once per bar, so an invalid stop error will only be generated once. I could code for
//a too-close sl, but cannot be arsed. Coders, sort this out for yourselves.
if (OldCstBars == iBars(NULL, CstTimeFrame)) return;
OldCstBars = iBars(NULL, CstTimeFrame);
//if (OrderProfit() < 0) return;//Nothing to do
double NewStop;
bool modify=false;
bool result;
for (int i=OrdersTotal()-1 ; i>=0 ; i--)
{
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if (OrderMagicNumber() == MagicNumber)
if (OrderSymbol() == Symbol())
{
int sl = OrderStopLoss();
int ticket = OrderTicket();
if (OrderType() == OP_BUY)
{
if ((BreakEven) && (sl < OrderOpenPrice())) continue;
if (iLow(NULL, CstTimeFrame, CstTrailCandles) > OrderOpenPrice())
if ((!BreakEven) && (sl < OrderOpenPrice())) sl = OrderOpenPrice();
if (iLow(NULL, CstTimeFrame, CstTrailCandles) > sl)
if (iLow(NULL, CstTimeFrame, CstTrailCandles) < (Bid - ((MarketInfo(Symbol(), MODE_STOPLEVEL)/factor)/10)))
{
NewStop = NormalizeDouble(iLow(NULL, CstTimeFrame, CstTrailCandles), Digits);
//Check that the new stop is > the old. Exit the function if not.
if (NewStop < OrderStopLoss() || CloseEnough(NewStop, OrderStopLoss()) ) continue;
modify = true;
}
//if (iLow(NULL, CstTimeFrame, CstTrailCandles) > sl)
}//if (OrderType == OP_BUY)
if (OrderType() == OP_SELL)
{
if ((BreakEven) && (sl > OrderOpenPrice())) continue;
if (iHigh(NULL, CstTimeFrame, CstTrailCandles) < OrderOpenPrice())
if ((!BreakEven) && (sl > OrderOpenPrice())) sl = OrderOpenPrice();
if (iHigh(NULL, CstTimeFrame, CstTrailCandles) < sl)
if (iHigh(NULL, CstTimeFrame, CstTrailCandles) > (Ask + ((MarketInfo(Symbol(), MODE_STOPLEVEL)/factor)/10)))
Alert("If I remove this alert, candlesticktrailingstop only works for Buys"); // Why? Why?? WHY???
{
NewStop = NormalizeDouble(iHigh(NULL, CstTimeFrame, CstTrailCandles), Digits);
//Check that the new stop is < the old. Exit the function if not.
if (NewStop > OrderStopLoss() || CloseEnough(NewStop, OrderStopLoss()) ) continue;
if (NewStop > OrderOpenPrice() ) continue;
modify = true;
}//if (iHigh(NULL, CstTimeFrame, CstTrailCandles) < sl)
}//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 = OrderModify(OrderTicket(), OrderOpenPrice(), NewStop, OrderTakeProfit(), OrderExpiration(), CLR_NONE);
if (!result) ReportError(" CandlestickTrailingStop()", slm);
if (!result)
{
ReportError(" CandlestickTrailingStop()", slm);
OldCstBars = 0;
}//if (!result)
}//if (modify)
}
}
return;
}//End void CandlestickTrailingStop()Your help will be greatly appreciated.
Have fun!
Radar =8^)