pine-scriptundeclared-identifier

TradingView's ‘undeclared identifier’ error in pine editor


Have a problem with custom identifier in pine on tradingview:

smma(src, length) =>
   smma = na(smma[1]) ? sma(src, length) : (smma[1] * (length - 1) + src) / length
   smma

Got error in line 2: Undeclared identifiersmma Anybody know how to fix it ? Will be appreciate for any suggestions


Solution

  • Either one of these will work. Need to declare variables before referring to them:

    //@version=4
    study("","",true)
    
    smma(src, length) =>
        float smma = na
        smma := na(smma[1]) ? sma(src, length) : (smma[1] * (length - 1) + src) / length
    
    smma2(src, length) =>
        var float smma = na
        smma := na(smma) ? sma(src, length) : (smma * (length - 1) + src) / length
    
    plot(smma(close, 20))
    plot(smma2(close, 20), "", color.blue, 6, transp = 80)