I cannot debug the error message.Please help and many thanks.
Previously I used pivothigh() and valuewhen() to code, it worked perfect,but I got warning message when setting alerts.
So that I use Dif[2]>Dif[1] and Dif[2]>Dif and so on to replace pivothigh().
But it could be compiled but cannot work properly and shows that references too many candles.
//@version=4
maxbarsback=1000
study(title="MACD v3",max_bars_back=maxbarsback)
dh1=0.0,dh2=0.0,dh3=0.0,dph1=0,dph2=0,dph3=0
var line ll=na
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00
dif = ema(close,9)-ema(close,26)
signal = ema(dif, 9)
hist = dif - signal
condition1=dif[2]>dif[1] and dif[2]>dif and dif[2]>dif[3] and dif[2]>dif[4]
if condition1
dh3:=dh2[1]
dh2:=dh1[1]
dh1:=dif[2]
dph3:=dph2[1]
dph2:=dph1[1]
dph1:=bar_index-2
condition2=dph3!=0 and dh2>=dh1 and high[bar_index-dph1]>high[bar_index-dph2]
if condition2 and not condition2[1]
ll:=line.new(dph2,dif[bar_index-dph2],dph1,dif[bar_index-dph1],extend=extend.none,color=color.red,width=2,style=line.style_dotted)
plotshape(title='+DC', series=condition2 and not condition2[1] ?dif[2] : na, text='+DC', style=shape.labeldown, location=location.absolute, color=color.maroon, textcolor=color.white,offset=-2)
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
plot(dif, title="DIF", color=color.white, transp=0)
plot(signal, title="MACD", color=color.gray, transp=0)
I experienced and resolved the same error today, so I will explain.
Tradingview has limits on how many bars back you can reference, which isn't usually an issue as it handles getting rid of historical bars, and only calculating on what is available. However this behavior can easily be contradicted by using values based on bar_index
. In this case it seems to be by passing the bar_index to a variable, and then continuing to pass it to other variables. You can do that, but if the difference between the current bar_index and the one used to reference is greater than the amount of bars available, you will get this error.
In my scenario, I was collecting bar_index
s in arrays to draw lines between, and drawing more lines connecting those lines. As the program progressed there were cases where the amount of bars was more than is available.
The solution is relatively simple, any calculation that depends on a variable that originally uses bar_index
as its source should be used in an if statement. In my code doing
if bar_index - storedBarIndex < 5000
// Do something that depends on the storedBarIndex to set or access a value
I don't remember how many bars are generally available, and it may vary from account to account.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With