Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

  • Forums
  • Pinescript
  • Value NaN in alert message but ok in plot and lable as script

Value NaN in alert message but ok in plot and lable as script

Mar 15, 2022 - 2:19 AM

Viewed 1374 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/56044 COPY
  • Hi,
    I have a function to calculate leverage
    I have a function in Pinescript that returns a value based on several indicators:

        varip a = 0.0
    calculate_() =>
        period = 50
        basis = ta.sma(src, period)
        dev = mult * ta.stdev(src, period)
        upper = basis + dev
        lower = basis - dev
        nATR = ta.atr(period) / src
        hATR = ta.highest(nATR, period)
        lATR = ta.lowest(nATR, period)
        nSD = ta.stdev(src, period) / src
        hSD = ta.highest(nSD, period)
        lSD = ta.lowest(nSD, period)
        MA = ta.wma(nATR, period)
        perm = 100 * math.abs(nATR - MA) / MA
        pers = 100 * (nSD - lSD) / (hSD - lSD)
        pera = 100 * (nATR - lATR) / (hATR - lATR)
        perb = 100 * (src - lower)/(upper - lower)
        per = gear == 4 or gear == 5 ? (perm + pers + pera + perb) / 4 : gear==1 ? math.min(100 , (pers + pera + perb) / 2.5) : (pers + pera + perb) / 3
        EL = (100 - per) / (6-gear)
        float(math.max(1,int(EL + .5)))
    
    a:= calculate_Leverage()
    
    plot(a, 'Leverage')
    label.new(bar_index, high, str.tostring(a))
    

    It plots and labels the right value.

    But when I try to put it in an alert message, I get only NaN on my alert. Tried both ways and same result:

    var msgLongBuy = str.format("{0,number,#.#}", a)
    str.tostring(a,"#.00)")
    

    Any idea why?

    0
  • Maybe use the str.tostring functioin instead?

    0
CONTENTS