0

I am writing a script that detects the highest highs and lowest lows in the previous N days and plots them as horizontal zones. I want to use the OHLC data from 30-min bars to be able to use the tail size to plot the depth of the zone/box, regardless of whether I'm on a smaller timeframe. For this I'm using

tohlc_hist = request.security(syminfo.tickerid, "30", array.from(time,open,high,low,close))

I want to ignore today's price movements, and as such I need to detect the first previous bar (counting back from today) that is a bar from yesterday.

tf1 = 3 // days to look back, starting with yesterday

// initialize values
yesterdayBarNum = 1
startBarNum = 2
daysAgoCounter = 0
previous_bar_day_of_month = dayofmonth(int(array.get(tohlc_hist, 0)))

for i = 0 to math.min(4999, bar_index)
    if dayofmonth(int(array.get(tohlc_hist, 0)[i])) != previous_bar_day_of_month
        daysAgoCounter += 1
        previous_bar_day_of_month := dayofmonth(int(array.get(tohlc_hist, 0)[i]))
        if daysAgoCounter == 1 // first bar from yesterday found
            yesterdayBarNum := int(i) // number of bars today (which we want to exclude when searching for highs and lows)
        if daysAgoCounter == tf1 + 1 // this bar is one PAST the day we want to start searching
            startBarNum := int(i) - 1 // start searching for highs/lows one bar previous
            break

startBarNum := startBarNum - yesterdayBarNum + 1

So far so good. During debugging (using on chart labels) I can see that startBarNum and yesterdayBarNum are calculating as expected. However, when actually using yesterdayBarNum in the array statement, it doesn't work. If I hard code the number, it behaves exactly as intended.

    highestHigh := ta.highest(array.get(tohlc_hist, 2)[yesterdayBarNum],startBarNum)
    highestHighOffset := (ta.highestbars(array.get(tohlc_hist, 2)[yesterdayBarNum],startBarNum) * -1) + yesterdayBarNum
    highestHighTail := math.max(((array.get(tohlc_hist, 2)[highestHighOffset] - math.max(array.get(tohlc_hist, 1)[highestHighOffset], array.get(tohlc_hist, 4)[highestHighOffset])) * tailDepth), 0.1)
    highestHighCenter := f_truncate(highestHigh - (highestHighTail / 2),2)

    lowestLow := ta.lowest(array.get(tohlc_hist, 3)[yesterdayBarNum],startBarNum)
    lowestLowOffset := (ta.lowestbars(array.get(tohlc_hist, 3)[yesterdayBarNum],startBarNum) * -1) + yesterdayBarNum
    lowestLowTail := math.max(((math.min(array.get(tohlc_hist, 1)[lowestLowOffset], array.get(tohlc_hist, 4)[lowestLowOffset1]) - array.get(tohlc_hist, 3)[lowestLowOffset]) * tailDepth),0.1)
    lowestLowCenter := f_truncate(lowestLow + (lowestLowTail / 2),2)

This doesn't work. Regardless of what yesterdayBarNum is ta.highest, ta.highestbars, ta.lowest, and ta.lowestbars acts in unexpected ways. It does not correctly find the highs and lows in the previous time period.

As an example: On the last bar (during market hours or not) if the value of yesterdayBarNum is 13 (I can see this in debugging), it doesn't work right. But if I add a line before the highestHigh code above saying yesterdayBarNum := 13 it works flawlessly as expected. It starts searching from the 13th bar back, for another 38 bars back (3 days worth of bars when on the 30-min chart).

Any idea or suggestions why this is happening? Am I doing something wrong, or am I just pushing Pinescript further than it will go?

Thank you!

PS: I tried hardcoding the value to 13 (for example) and it worked flawlessly everywhere that the value 13 was expected (end of day). I can also do this intraday by setting the value to 8, for example, when the debugger is printing that the current value should be 8.

This is how it looks when the number is hardcoded:

correct high/low identified

This is how it looks when I don't have it hardcoded. Notice how the debug value is identical but the zones are all messed up, like it starts searching from the latest bar - rather than from yesterday's bar and further back.

messed up zones

glemmestad
  • 11
  • 2

0 Answers0