PHP:
bool HaveLongPosition;
bool HaveShortPosition;
/////////////////////////////////
// Sell entry condition
// Bullish HA candle, and body is longer than previous body, previous also bullish, current has no lower wick
if ((HAOpenLatest < HACloseLatest) && (HACloseLatest - HAOpenLatest > MathAbs(HAClosePrevious - HAOpenPrevious)) && (HAOpenPrevious < HAClosePrevious) && (HALowLatest == HAOpenLatest))
{
Bullish = true;
Bearish = false;
}
// Buy entry condition
// Bearish HA candle, and body is longer than previous body, previous also bearish, current has no upper wick
else if ((HAOpenLatest > HACloseLatest) && (HAOpenLatest - HACloseLatest > MathAbs(HAClosePrevious - HAOpenPrevious)) && (HAOpenPrevious > HAClosePrevious) && (HAHighLatest == HAOpenLatest))
{
Bullish = false;
Bearish = true;
}
else
{
Bullish = false;
Bearish = false;
}
GetPositionStates();
if (Bullish)
{
if (!HaveLongPosition) fBuy();
}
else if (Bearish)
{
if (!HaveShortPosition) fSell();
}
return(0);
}
//////////////////////////////////////////////////////////////////
//+------------------------------------------------------------------+
//| Check what position is currently open |
//+------------------------------------------------------------------+
void GetPositionStates()
{
int total = OrdersTotal();
for (int cnt = 0; cnt < total; cnt++)
{
if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) continue;
if (OrderMagicNumber() != Magic) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderType() == OP_BUY)
{
HaveLongPosition = true;
HaveShortPosition = false;
return;
}
else if (OrderType() == OP_SELL)
{
HaveLongPosition = false;
HaveShortPosition = true;
return;
}
}
HaveLongPosition = false;
HaveShortPosition = false;
}