I have written a partially functional code that is intended to wait until the last bar to plot a signal line.
I am using this in a larger script where I need to plot this line based on two values that are available only at the last bar of the run (the line all-time high and low). So, I had to find a solution that would allow me to wait until this point, and still be able to plot the data. As you probably know, in Pine you cannot plot on past (non-current) bars, and the offset function was the only way I found to overcome this obstacle.
A few key points I would like to mention:
internally, i need the script to run using the daily timeframe (notice the
timeframe="D"option). Still, I will be using this to check monthly charts. Therefore, the script should get its information from the daily timeframe, but show it on higher timeframes;it only plots 64 bars (Pine's
plotinstruction limit), but that is enough for me since I am using it on monthly charts;at the last bar, the script saves information about previous daily bars. I thought that this way the plot functions can match the bars using their dates, regardless of the timeframe used. I am not sure this is the right approach though;
since the code below should serve as a raw example, I used a simple
closeas data source. To simplify things, I have stripped the code to the minimum.
Now, the problem... the code seems to work good on the daily timeframe, but fails when using weekly, or monthly timeframes (it does not plot anything!). If you take out the timeframe="D" option, it will work ok on all timeframes, but the code will be integrated into a larger script that was specifically written to get its signals from the daily timeframe, regardless of the user-selected timeframe. So, keeping that option in place is crucial for my needs.
Can someone help me please, I spent hours trying to find a solution, but without any success. Any improvement without losing its main functionality (plot at the last bar) would be welcome too!
Thank you!
Alex
//@version=5
ln = 1280 // 64 monthly bars x 20 trading days per month
indicator("Plot at the last bar", max_bars_back=ln, timeframe="D")
var int date_index = na
dates = array.new_string(ln)
values = array.new_float(ln)
plot_values = array.new_float(ln)
if (barstate.islast)
for i = 0 to ln - 1
// set the date string
date_string = "|" + str.tostring(dayofmonth[i]) + "|" + str.tostring(month[i]) + "|" + str.tostring(year[i]) + "|"
// save the date string and its corresponding value
array.insert(dates, i, date_string)
array.insert(values, i, close[i])
// search for the date string and get its index location
date_index := array.indexof(dates, date_string)
// get the value at the index location found above
array.insert(plot_values, i, (date_index >= 0) ? array.get(values, date_index) : na)
plot(array.get(plot_values,5), color=#000000, linewidth=3, style=plot.style_circles, offset=-5)
plot(array.get(plot_values,4), color=#000000, linewidth=3, style=plot.style_circles, offset=-4)
plot(array.get(plot_values,3), color=#000000, linewidth=3, style=plot.style_circles, offset=-3)
plot(array.get(plot_values,2), color=#000000, linewidth=3, style=plot.style_circles, offset=-2)
plot(array.get(plot_values,1), color=#000000, linewidth=3, style=plot.style_circles, offset=-1)
plot(array.get(plot_values,0), color=#000000, linewidth=3, style=plot.style_circles)
1 Answer 1
If you're going to use monthly charts with daily calculations, you're going to need to look into using request.security_lower_tf() instead of request.security(), which is typically used for retrieving higher timeframe values.
You can read more about request.security_lower_tf() on the TradingView blog in the article Request more data from your scripts.
timeframe="D"and use request.security(). Without that, you'll never be able to achieve what you're describing.