Hello PRC Forum
I have the following problem: my indicator signals me an entry point, but during the backtest the position is opened one bar later. Engulfing requires the evaluation of the 2 past candles, why is no position opened on the 3rd candle but on the 4th candle?
Indicator:
SecondToLastCandleHeight = abs(close[2]-open[2])
LastCandleHeight = abs(close[1]-open[1])
Engulfing = LastCandleHeight > SecondToLastCandleHeight
ShortMarketCondition = DIminus[14](CLOSE[1]) > DIplus[14](CLOSE[1]) AND ADX[14] > 12 AND ADX[14] >= ADX[14][2]
LongMarketCondition = DIplus[14](CLOSE[1]) > DIminus[14](CLOSE[1]) AND ADX[14] > 12 AND ADX[14] >= ADX[14][2]
EngulfingShort = Engulfing AND CLOSE[2] > OPEN[2] AND CLOSE[1] < OPEN[2] AND ShortMarketCondition
IF EngulfingShort THEN
DRAWARROWDOWN(barindex,close) coloured(255,255,0)
ENDIF
EngulfingLong = Engulfing AND CLOSE[2] < OPEN[2] AND CLOSE[1] > OPEN[2] AND LongMarketCondition
IF EngulfingLong THEN
DRAWARROWUP(barindex,close) coloured(0,255,255)
ENDIF
RETURN EngulfingShort as "EngulfingShort", EngulfingLong as "EngulfingLong", LastCandleHeight as "LastCandleHeight", SecondToLastCandleHeight as "SecondToLastCandleHeight"
Strategy:
//Engulfing/DMI
//MainTime: 1H, SubordinateTime: 5M
//Cornel Schmid, 03.09.2022
//TERMS AND CONDITIONS
DEFPARAM PreLoadBars = 300
DEFPARAM CumulateOrders = true
Position=1
//ENGULFING
EngulfingShort, EngulfingLong, LastCandleHeight, SecondToLastCandleHeight = CALL "FR_Engulfing_DMI"
//ENGULFINGRATIO
EngulfingRatio = (LastCandleHeight/SecondToLastCandleHeight) < MaxRatio
//EXECUTION
IF Engulfingshort THEN
SELLSHORT Position PERPOINT AT MARKET
SET STOP PLOSS StopLossShort
ENDIF
IF EngulfingLong THEN
BUY Position PERPOINT AT MARKET
SET STOP PLOSS StopLossLong
ENDIF
[REM Trailing Stop, Exitconditions, etc....]
I have added a picture: Yellow and turquoise arrows = Indicator, Green and red arrows = Strategy
What is the problem?
JSParticipant
Senior
This is the normal course of events, when a condition is true (in your indicator), then a position is opened on the Open of the next bar in your trading system.
It’s because your indicator is lagging by 1 candle.
Replace all occurrences of [1] with [0], then [2] with [1].
Thank you Roberto! It works