Pinescript- How to start calculate with a given timeframe?

Viewed 28

I'm having an interest to learn creating volume profile. But I'm quite confused of how to start the script to calculate every given timeframe. Example: if the timeframe period is "D", start calculating, else if it's the next day... break. You guys got any idea? Please help.

Truly no idea what to do. Need to specify a price...

range_ = ta.highest(high, timeframe.period) - ta.lowest(low, timeframe.period)

But it's not working cause in needs simple int and not simple string. So how can I convert the string to int?

1 Answers

You need to implement your own algorithm. You cannot cast simple string to simple int.

You can use ta.change() together with time() to figure out if it is a new "timeframe".

Below example will track highest high and lowest low of the day. It will reset everytime a new day starts.

//@version=5
indicator("My script", overlay=true)

is_new_day = ta.change(time("D"))

var float highest_high = na
var float lowest_low = na

if (is_new_day)
    highest_high := high
    lowest_low := low
else
    if (high > highest_high)
        highest_high := high
    if (low < lowest_low)
        lowest_low := low

plot(highest_high, color=color.green, style=plot.style_circles)
plot(lowest_low, color=color.red, style=plot.style_circles)
bgcolor(is_new_day ? color.new(color.blue, 85) : na)

enter image description here

Related