pine-scriptpine-script-v5

filter criteria met, but one candle later as expected / Higher Timeframe / with request.security


I was writing a filter which should filter in a 60 Minutes chart a expected trend change for work in the lower timeframe (5 Minutes).

The actual 60 Minutes Candle should be a long candle, and the previous 4 candels should be short candles.

This I tried with this Filter:

/ Check actual candle is a long candle and check are there the 4 previous candles short candles ? 
FilteractualLongAnd4PreviousCandlesShort = close[4] < open[4] and close[3] < open[3] and close[2] < open[2] and close[1] < open[1] and  close[0] > open[0] 

for checking the higher timeframe I was trying with this code and disabling repainting for the script:

// Check is in higher Timeframe (dev H1) the condition met? Then draw  a backcolor visualisation
res = input.timeframe(title = "Higher TimeFrame to chose", defval = "60")
HTCheck = request.security(syminfo.tickerid, res, FilteractualLongAnd4PreviousCandlesShort[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1] // disable repainting
bgcolor(HTCheck ?  color.new(color.gray, 70) : na) // show candle where all conditions are met. 

But in this constellation not the green (Long) candle is marked. It is the candle after the condition is met. So I'm confused with this result.

Here the picture to the Work of the filter in a 60 Minutes chart to illustrate that the filter is 1 candle behind.

enter image description here

I tried to rewrite the Filter Criteria, with 1 digit higher, but this delivers delay of 1 more candle in stead of visualise the right candle.

My try:

// Check actual candle is a long candle and check are there the 4 previous candles short candles ? 
FilteractualLongAnd4PreviousCandlesShort = close[5] < open[5] and close[4] < open[4] and close[3] < open[3] and close[2] < open[2] and  close[1] > open[1] 

But this delivers completely wrong results.

Here the picture to the results in a 60Minutes Timeframe chart for visualisation is the Filter working accurate or not.

enter image description here


Solution

  • Using an offset will work here - >

    bgcolor(HTCheck[0] ?  color.new(color.lime, 70) : na, offset = -1)