Search code examples
pine-scriptpine-script-v5tradingalgorithmic-trading

Converting a study into a strategy on pinescript


Im trying to build a strategy with an indicator from lazy bear on pinescript. The code looks like this:


//@version=5

strategy(title='Volume Flow Indicator [LazyBear]', shorttitle='VFI_LB', overlay = true)

length = input(130, title='VFI length')
coef = input(0.2)
vcoef = input(2.5, title='Max. vol. cutoff')
signalLength = input(5)
smoothVFI = input(false)

if ta.sma(x, y) >= smoothVFI
    ta.sma(x, y)
else 
    X
   // sma_1 = ta.sma(x, y)
   // smoothVFI ? sma_1 : x

typical = hlc3
inter = math.log(typical) - math.log(typical[1])
vinter = ta.stdev(inter, 30)
cutoff = coef * vinter * close
vave = ta.sma(volume, length)[1]
vmax = vave * vcoef
vc = if volume < vmax
    volume
else
    vmax  //min( volume, vmax )

mf = typical - typical[1]

vcp = if mf > cutoff
    vc
if mf < -cutoff
    vc * -1
else
    0

vfi = ta.sma(math.sum(vcp, length) / vave, 3)
vfima = ta.ema(vfi, signalLength)
d = vfi - vfima

plot(0, color=color.new(color.gray, 0), style=plot.style_cross)
showHisto = input(false)
plot(showHisto ? d : na, style=plot.style_histogram, color=color.new(color.gray, 50), linewidth=3)
plot(vfima, title='EMA of vfi', color=color.new(color.orange, 0))
plot(vfi, title='vfi', color=color.new(color.green, 0), linewidth=2)

These are my current mistakes and I do not know how to fix them. Fehler in 11:11 Undeclared identifier 'x' Fehler in 11:14 Undeclared identifier 'y' Fehler in 14:5 Undeclared identifier 'X' Fehler in 11:20 Cannot call 'operator >=' with argument 'expr1'='smoothVFI'. An argument of 'input bool' type was used but a 'simple float' is expected.

My idea is that I just want to do some backtesting on how it works to simply enter a long trade when the vfi crosses the zero above. Its not a standalone idea, I just want to implement that indicator into other strategies Im currently trading. Does anybody know how to fix these errors? Thanks for the help!


Solution

  • Below line in the original code is actually a function and not a call to ta.sma() (in v5).

    ma(x,y) => smoothVFI ? sma(x,y) : x
    

    So, you should write this also as a function.

    Below code is the correct way of upgrading this code to v5:

    //@version=5
    indicator(title = "Volume Flow Indicator [LazyBear]", shorttitle="VFI_LB")
    
    length = input(130, title="VFI length")
    coef = input(0.2)
    vcoef = input(2.5, title="Max. vol. cutoff")
    signalLength=input(5)
    smoothVFI=input.bool(false)
    
    ma(x,y) => smoothVFI ? ta.sma(x,y) : x
    
    typical=hlc3
    inter = math.log( typical ) - math.log( typical[1] )
    vinter = ta.stdev(inter, 30 )
    cutoff = coef * vinter * close
    vave = ta.sma( volume, length )[1]
    vmax = vave * vcoef
    
    vc = if (volume < vmax)
        volume
    else
        vmax //min( volume, vmax )
    
    mf = typical - typical[1]
    
    vcp = if (mf > cutoff)
        vc
    else
        if (mf < -cutoff)
            -vc
        else
            0
    
    vfi = ma(math.sum( vcp , length )/vave, 3)
    vfima=ta.ema( vfi, signalLength )
    d=vfi-vfima
    
    plot(0, color=color.gray, style=plot.style_circles)
    showHisto=input.bool(false)
    plot(showHisto ? d : na, style=plot.style_histogram, color=color.new(color.gray, 50), linewidth=3)
    plot( vfima , title="EMA of vfi", color=color.orange)
    plot( vfi, title="vfi", color=color.green,linewidth=2)