Search code examples
pine-scriptpine-script-v5tradingview-api

Trading View Pine Script:


So, below is a section of code pared down to just what is needed to demonstrate the error message. The code originally did NOT have lines that extended away from the end of a plotted value. This is what is being attempted to add to the existing code base. The code runs fine WITHOUT the timeframe in the indicator line - So, it will work with whatever the timeframe the user has selected in the TradingView chart. HOWEVER, once the timeframe='' is added so the user can show a different timeframe with this indicator in the chart the error "The 'timeframe' argument is incompatible with functions that have side effects." propogates. I understand enough about the error to know that it is a multi-timeframe issue. But, not enough to determine how to fix it. I have been beating my head and trying different things/various searches/workarounds for a few days without success.

What this intended to do: plots a dynamic moving average with a label and line that extends to the right from the last completed bar with a user selected timeframe to be displayed on the current (of a different time frame).

What happens: the function needs to call time related data and it causes an error message. Without the user being able to choose the timeframe it works as intended. Once the user can select the timeframe the error occurs.

What I'm hoping for is that someone can assist me in finding a way to make this work. I have removed a LOT of code and other values/plots/fills/labels/etc to pare this down in an attempt to show ONLY the part that needs to be fixed.

NOTE: There is too much code above the function to just NOT use the timeframe. So, I need to figure a way out how to, hopefully, alter the function/function parameters.

'//@version=5
indicator('test',overlay=true)
//timeframe=''

//DATA
WMAsource = close
WMAlength = input.int(56, minval=1, title='WMA Length')
atrlen1 = input.int(100, minval=1, title='ATR Length')
mult1 = 2
ma1 = ta.wma(request.security("", "", WMAsource), WMAlength)
WMArange = ta.tr
rangema1 = ta.wma(WMArange, atrlen1)

up1 = ma1 + rangema1 * mult1

//--------------------------------

//PLOTS

lbullt_tit = '1hr Light Bull Threshold'

//WMA Plots
u4 = plot(up1, color=color.new(color.gray, 0), title=lbullt_tit)

//-----------------------------------
//plot extend right lines

var bool show_hlines = input(true, 'Show horizontal lines')
var bool show_extlines = not show_hlines

var color_lbullt = color.gray

var line_lbullt = line.new(x1=na, y1=na, x2=na, y2=na,xloc=xloc.bar_time, extend=extend.right, color=show_hlines ? color_lbullt : na, style=line.style_dashed)

f_moveLine(_id, _x, _y) =>
    line.set_xy1(_id, _x, _y)
    line.set_xy2(_id, _x +1, _y)

if barstate.islast
    f_moveLine(line_lbullt, time, up1)

plot(up1, color=show_extlines ? color_lbullt : na,linewidth=1, title=lbullt_tit)`

I tried replacing various values for the "x" value in the function code. I tried to figure out a way to incorporate request.security into the lower code area. Tried to just use "time" for all x values in the function area of the code. I found this function someplace else and just don't understand enough about how it works and what is being called from the upper formula to know how to troubleshoot it. Also, tried writing/using security() into the 330 lines of code (above the dashed lines in this example).

What I'm hoping to find is a way to remove the "time" component from the function so the "timeframe" setting works for the entire code - while still allowing the function to work at moving the lines.


Solution

  • Use the f_secureSecurity function from this library

    Something like this:

    i_htf = input.timeframe ('', 'Select Higher Timeframe')
    f_secureSecurity(_symbol, _res, _src) =>
        request.security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)
    ma1 = f_secureSecurity(syminfo.tickerid, i_htf, ta.wma(WMAsource, WMAlength))//Non Repainting