Limit number of bars in your study

Viewed 565

I have a complex study I'm working on and when I update and save my code it takes a long time for it to plot/load the results. Is there a way to limit the number of bars the study will analyze I need a couple of month's worth of data not years?

I tried the max_bars_back in the study function but it doesn't seem to do anything.

//30 minute 2 months worth of bars - 2880
study(title="MyScriptStudy", overlay=true, max_bars_back=2880)

I'm also hoping a solution will work when I use the "Replay bar" tool so I'll get 2 months of bars from where I slice off the starting bar. So, if I slice starting at March 1, I'll get bars going back to Jan 1 which I'm thinking will help load my study much faster.

2 Answers

You can add date filtering to script with function like f_tradeDateIsAllowed()

//@version=4
study("Date Filtering", "", true)
i_dateFilter    = input(false,  "═════ Date Range Filtering ═════")
i_fromYear      = input(2020,   "From Year",    minval = 1900)
i_fromMonth     = input(1,      "From Month",   minval = 1, maxval = 12)
i_fromDay       = input(1,      "From Day",     minval = 1, maxval = 31)
i_toYear        = input(2999,   "To Year",      minval = 1900)
i_toMonth       = input(1,      "To Month",     minval = 1, maxval = 12)
i_toDay         = input(1,      "To Day",       minval = 1, maxval = 31)

fromDate        = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)
toDate          = timestamp(i_toYear, i_toMonth, i_toDay, 23, 59)

f_tradeDateIsAllowed() => not i_dateFilter or (time >= fromDate and time <= toDate)


enterLong       = f_tradeDateIsAllowed() and crossover(rsi(close, 14), 50)
plotchar(enterLong, "enterLong", "▲", location.belowbar, color.lime, size = size.tiny)

max_bars_back meaning is different. With max_bars_back=2880 your script will store more data and load slower.

Also you can implement something like this: https://www.pinecoders.com/faq_and_code/#how-can-i-keep-only-the-last-x-labels-or-lines

I found a solution to my particular situation that might help others. This code can be used to only add plots if within a certain time range, 2 months worth of bars

// Bar limiter
i_startTime = input(defval = timestamp("1 Feb 2020 04:30 +0000"), title = "Start Time", type = input.time)
i_endTime = input(defval = timestamp("1 April 2020 04:30 +0000"), title = "End Time", type = input.time)

bar_range(data) => 
    allow = time >= i_startTime and time <= i_endTime ? data : na
    
ema_200 = ema(close, 200)
plot(bar_range(ema_200), title="EMA 200", color=#fff176, linewidth=3)
Related