Reputation: 6331
I learned about the security
function which is able to take data from other tickers or time frames. My idea is to use RSI from two time frames - 30m and 4h, but the code below is drawing only 294 bars of that higher time frame. Is it possible to take those candles for the entire period, so I can actually backtest my strategy? Basically to make that 294 candles restriction unlimited. Any ideas?
//@version=4
study(title="RSI Multi Time Frame", shorttitle="RSI", max_lines_count = 500, format=format.price, precision=2)
TimeframeU = input(defval = 'Auto', title ="Higher Time Frame", options = ['Auto', '1', '3', '5', '10', '15', '30', '60', '120', '180', '240', '360', '480', '720', 'D', 'W', '2W', 'M', '3M', '6M', '12M'])
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
rsi_col = input(color.blue, title = "Indicator Color", type = input.color)
rsi_width = input(defval = 2, title = "Indicator Line Width", minval = 1, maxval = 4)
rsi_h_col = input(color.green, title = "Indicator HTF Color", type = input.color)
rsi_h_width = input(defval = 2, title = "Indicator HTF Line Width", minval = 1, maxval = 4)
num_bars = input(defval = 294, title = "Number of Bars for RSI HTF", minval = 10, maxval = 495)
htf = TimeframeU == 'Auto' ?
timeframe.period == '1' ? '60' :
timeframe.period == '2' ? '60' :
timeframe.period == '3' ? '60' :
timeframe.period == '5' ? '60' :
timeframe.period == '10' ? '120' :
timeframe.period == '15' ? '120' :
timeframe.period == '30' ? 'D' :
timeframe.period == '45' ? 'D' :
timeframe.period == '60' ? 'D' :
timeframe.period == '120' ? 'D' :
timeframe.period == '180' ? 'D' :
timeframe.period == '240' ? 'D' :
timeframe.period == 'D' ? 'W' :
timeframe.period == 'W' ? 'M' : timeframe.period : TimeframeU
rsi = rsi(src, len)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")
plot(rsi, color = rsi_col, linewidth = rsi_width)
// RSI HTF
var rsi_htf_lines = array.new_line(0)
var float last_rsi = na
new_htf_bar = change(time(htf)) != 0
rsi_htf = security(syminfo.tickerid, htf, rsi(src, len), lookahead = false)
var line rsi_line = na
if new_htf_bar
last_rsi := rsi_htf[1]
line.delete(rsi_line)
rsi_line := line.new(x1 = bar_index , y1 = rsi_htf, x2 = bar_index - 1, y2 = last_rsi, color = rsi_h_col, width = rsi_h_width)
else
line.set_x1(rsi_line, bar_index)
line.set_x2(rsi_line, bar_index - 1)
line.set_y1(rsi_line, rsi_htf)
rsi_line
if new_htf_bar
if array.size(rsi_htf_lines) >= num_bars
line.delete(array.get(rsi_htf_lines, array.size(rsi_htf_lines) - 1))
array.pop(rsi_htf_lines)
array.unshift(rsi_htf_lines, line.new(x1 = bar_index - 1 , y1 = last_rsi, x2 =bar_index - 2, y2 = last_rsi[1], color = rsi_h_col, width = rsi_h_width))
else
if array.size(rsi_htf_lines) > 0
for x = 0 to array.size(rsi_htf_lines) - 1
line.set_x1(array.get(rsi_htf_lines, x), line.get_x1(array.get(rsi_htf_lines, x)) + 1)
line.set_x2(array.get(rsi_htf_lines, x), line.get_x2(array.get(rsi_htf_lines, x)) + 1)
// show value as indicator values
plot(rsi_htf, color = rsi_h_col, show_last = 1)
Upvotes: 0
Views: 669
Reputation: 1368
You can directly plot the rsi_htf variable as that is the correct rsi of higher timeframe (can be verified easily by adding separate rsi indicator in same pane on same scale) , example below
//@version=4
study(title="RSI Multi Time Frame", shorttitle="RSI", max_lines_count = 500, format=format.price, precision=2)
TimeframeU = input(defval = 'Auto', title ="Higher Time Frame", options = ['Auto', '1', '3', '5', '10', '15', '30', '60', '120', '180', '240', '360', '480', '720', 'D', 'W', '2W', 'M', '3M', '6M', '12M'])
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
rsi_col = input(color.blue, title = "Indicator Color", type = input.color)
rsi_width = input(defval = 2, title = "Indicator Line Width", minval = 1, maxval = 4)
rsi_h_col = input(color.green, title = "Indicator HTF Color", type = input.color)
rsi_h_width = input(defval = 2, title = "Indicator HTF Line Width", minval = 1, maxval = 4)
num_bars = input(defval = 294, title = "Number of Bars for RSI HTF", minval = 10, maxval = 495)
htf = TimeframeU == 'Auto' ?
timeframe.period == '1' ? '60' :
timeframe.period == '2' ? '60' :
timeframe.period == '3' ? '60' :
timeframe.period == '5' ? '60' :
timeframe.period == '10' ? '120' :
timeframe.period == '15' ? '120' :
timeframe.period == '30' ? 'D' :
timeframe.period == '45' ? 'D' :
timeframe.period == '60' ? 'D' :
timeframe.period == '120' ? 'D' :
timeframe.period == '180' ? 'D' :
timeframe.period == '240' ? 'D' :
timeframe.period == 'D' ? 'W' :
timeframe.period == 'W' ? 'M' : timeframe.period : TimeframeU
rsi = rsi(src, len)
band1 = hline(70, "Upper Band", color=#C0C0C0)
band0 = hline(30, "Lower Band", color=#C0C0C0)
fill(band1, band0, color=#9915FF, transp=90, title="Background")
plot(rsi, color = rsi_col, linewidth = rsi_width)
// RSI HTF
var rsi_htf_lines = array.new_line(0)
var float last_rsi = na
new_htf_bar = change(time(htf)) != 0
var float rsi_htf=0.0
rsi_htf := security(syminfo.tickerid, htf, rsi(src, len), lookahead = false)
if new_htf_bar
last_rsi := rsi_htf[1]
else
rsi_htf:=na
plot(rsi_htf, color = rsi_h_col)
Upvotes: 1
Reputation: 1
Default Value should be 294
num_bars = input(defval = 294, title = "Number of Bars for RSI HTF", minval = 10, maxval = 495)
Upvotes: 0
Reputation: 6875
The number of historical bars available depends on your subscription.
Historical bars available for:
See Compare plans
Upvotes: 1