tgstl
tgstl

Reputation: 1

How to set the size of the range of historical data in Pine Script?

Tell me please one moment. How to set the size of the calculated range of historical data in Pine? For example, you want the function to be calculated from a certain candle (without sliding). Fot example,

(open + close) / open

for the last 50 candles. Then a new bar appears for the last 51, for the last 52, and so on. Mathematically it:

(open[i] + close[i]) / open[i] ->

(open[i+1] + close [i+1]) / open[i+1] ->

(open[i+2] + close [i+2]) / open[i+2] ->

(open[i+..n] + close [i+..n]) / open[i+..n]

I found two solutions, but it both do not work.

  1. set the candle using timestamp and compare it with if using time. Example:

a= timestamp(...)

b= time > a ? (function) : na

Doesn't work, returns null value of the function. Through the for loop, the same thing - a null value. Although the size of the counter is set in this way, but the function does not count.

  1. max_bars_back I thought it can be set the number of bars that variables can use, but it does not work either through the indicator or as a separate function for the variable. Still counts as if no batch size has been set.

Please tell me how to resolve the issue🙏

Upvotes: 0

Views: 624

Answers (1)

vitruvius
vitruvius

Reputation: 21121

You can use a timestamp as a condition and do your calculations only whenever you are in the correct time window.

You also don't need a for loop. Just use a var variable.

Below example will start its calculations starting from 01 Aug 2022.

//@version=5
indicator("My script")

in_time_start = input.time(timestamp("01 Aug 2022 00:00 +0000"), "Start Time")
in_time_end = input.time(timestamp("31 Dec 2031 00:00 +0000"), "End Time")

time_in_window = (time >= in_time_start and time <= in_time_end)

var float your_variable = 0.0

your_variable := time_in_window ? ((open + close) / open) : your_variable

plot(your_variable)

enter image description here

Upvotes: 1

Related Questions