0

I want to be able to get the historic 30-min bars for a ticker, starting at yesterday's bars (ie. I don't want to consider today's bars) to find highs and lows.

To do this I (1) detect how many bars I have to go back to when dayofmonth() changes between bars (the latest date-changover), and then (2) use this variable as the limiter when polling request.sequrity(). However, this doesn't produce the expected results as the results of my dayofmonth() changover checker is a series int, not a simple int, so it seems request.security() gets confused.

This is how I detect how many bars I have to go back to exclude today's bars:

t_hist = request.security(syminfo.tickerid, "30", time)

int yesterdayBarNum = 1
int startBarNum1 = 2
int daysAgoCounter = 0
int previous_day_of_month = dayofmonth(t_hist[0])

for i = 0 to math.min(4999, bar_index)
    if dayofmonth(t_hist[i]) != previous_day_of_month
        daysAgoCounter += 1
        previous_day_of_month := dayofmonth(t_hist[i])
        if daysAgoCounter == 1 // number of bars today (which we want to exclude)
            yesterdayBarNum := int(i)
        if daysAgoCounter == 4 // number of days back to start the series - used later in the code
            startBarNum1 := int(i) - 1
        if startBarNum1 != 2
            break

It simply pages through the series of timestamps, one step back at a time, until I detect the change in days. This could be the previous bar (at the start of the day) or 13 bars back (at the end of the day, on a 30-min interval), or really any other value. I just need to know exactly where this date change happens so I can use it when getting my historic data:

h_hist = request.security(syminfo.tickerid, "30", high[yesterdayBarNum])

If I pass yesterdayBarNum = 13 (simple int) everything works great! (if that is the correct number). If I pass the yesterdayBarNum calculated by the for-loop above - not so much. I suspect this is because the yesterdayBarNum detection above creates a series int which the request.sequrity() function doesn't like.

I can use yesterdayBarNum in mathematical calculations without any issues (it only uses the last value then, like yesterdayBarNum + 1 to find the first bar of today)

Any ideas?

Thanks!

PS: The use case is to find the highest (and lowest) 30-min bars between a date that is N days ago, and yesterday. Then using those values drawing a box starting at those highs and lows and stretched to the right. This would create historic high and low zones, with the 30-min bars used to decide the depth of the zone based on the tail of the 30-min candles.

glemmestad
  • 11
  • 2
  • 1
    You are thinking this the wrong way. You cannot use the `security()` function like that. You should do this on the fly. You need to store some information in `var` variables (e.g. price, time, bar_index etc.) and use those to draw your boxes whenever your condition is satisfied. Update those values if needed as new bars form. – vitruvius Jun 22 '23 at 07:06

0 Answers0