JACK_SPARROW
JACK_SPARROW

Reputation: 33

The function should be called on each calculation for consistency. It is recommended to extract the call from the ternary operator or from the scope

I am getting a error in pine script:- The function '#f2' should be called on each calculation for consistency. It is recommended to extract the call from the ternary operator or from the scope. This is the function:- Variance(src,p) => p == 1 ? 0 : Sma(src*src,p) - pow(Sma(src,p),2)

'''
//@version=4
study("My Script",overlay=true,max_bars_back=1000)
//-----------------------------------Trend lines--------------------------------
//Basic
heading11 = input(title="Trend Lines" ,type=input.bool,defval=true)
lengthppl   = input(30,group='Basic Settings'
,tooltip='Pivot length. Use higher values for having lines connected to more significant 
  pivots')
lookbackppl = input(3,minval=1,group='Basic Settings'
,tooltip='Number of lines connecting a pivot high/low to display')
SLOPEPPL    = input(1.,minval=-1,maxval=1,step=0.1,group='Basic Settings'
,tooltip='Allows to multiply the linear regression slopeppl by a number within -1 and 1')

//Style
ph_col = input(#2157f3,'Pivot High Lines Color',group='Line Colors')
pl_col = input(#ff1100,'Pivot Low Lines Color',group='Line Colors')

//──────────────────────────────────────────────────────────────────────────────
Sma (src,p) => a = cum(src), (a - a[max(p,0)])/max(p,0)
Variance(src,p) => p == 1 ? 0 : Sma(src*src,p) - pow(Sma(src,p),2)
Covariance(x,y,p) => Sma(x*y,p) - Sma(x,p)*Sma(y,p)
//──────────────────────────────────────────────────────────────────────────────
nppl = bar_index
ph = pivothigh(lengthppl,lengthppl)
pl = pivotlow(lengthppl,lengthppl)
//──────────────────────────────────────────────────────────────────────────────
varip ph_array = array.new_float(0)
varip pl_array = array.new_float(0)
varip ph_n_array = array.new_int(0)
varip pl_n_array = array.new_int(0)
if ph
    array.insert(ph_array,0,ph)
    array.insert(ph_n_array,0,nppl)
if pl
    array.insert(pl_array,0,pl)
    array.insert(pl_n_array,0,nppl)
//──────────────────────────────────────────────────────────────────────────────
val_ph = valuewhen(ph,nppl-lengthppl,lookbackppl-1)
val_pl = valuewhen(pl,nppl-lengthppl,lookbackppl-1)
val = min(val_ph,val_pl)
k = nppl - val > 0 ? nppl - val : 2
slopeppl = Covariance(close,nppl,k)/Variance(nppl,k)*SLOPEPPL
var line ph_l = na,var line pl_l = na
if barstate.islast
    for i = 0 to lookbackppl-1
        ph_y2 = array.get(ph_array,i),ph_x1 = array.get(ph_n_array,i)-lengthppl
        pl_y2 = array.get(pl_array,i),pl_x1 = array.get(pl_n_array,i)-lengthppl
        ph_l := line.new(ph_x1,ph_y2,ph_x1+1,ph_y2+slopeppl,extend=extend.right,color=ph_col)
        pl_l := line.new(pl_x1,pl_y2,pl_x1+1,pl_y2+slopeppl,extend=extend.right,color=pl_col)
        '''

Upvotes: 0

Views: 1803

Answers (1)

bajaco
bajaco

Reputation: 980

Functions that operate on time-series data should not be in an if statement or ternary. Simply move the functions out as separate variables:


Variance(src,p) => 
    sma_a = sma(src*src,p)
    sma_b = sma(src,p)
    return = p == 1 ? 0 : sma_a - pow(sma_b,2)
    return

This assumes you're calling the function outside of an if or ternary, or some other scope. If not then set a variable for the function:

variance = Variance(a,b)

Then use the variable in your logic as necessary.

Upvotes: 1

Related Questions