Home > Net >  can't extract value from Function
can't extract value from Function

Time:11-24

function takes for example rsi and from declared for me historical bar 1 to 12 finds me maximum value ( integer or '00,0') of rsi.

//@version=5
indicator("loop", shorttitle="loop")
len = input.int(14, title="RSI Length")
src = input.source(close, "RSI Source")

Rsi = ta.rsi(src, len)
plot(Rsi*10, "RSI", color=#673ed8)
hline(65,linestyle= hline.style_dashed , color=color.new(color.red, 0))

minF = 1, maxF = 12, RsiFunction = Rsi
ff_loopMax(RsiFunction,minF, maxF) =>
    var float Max = na
    var float MaxOdpCena = na
    var int MaxNrBar = na
    for i=minF to maxF       
        if i == minF
            Max := RsiFunction[i]
            //MaxOdpCena := high[i]
            //MaxNrBar := i
        else
            //MaxOdpCena := Max > RsiFunction[i] ? MaxOdpCena : high[i]
            //MaxNrBar := Max > RsiFunction[i] ? MaxNrBar : i
            Max := math.max(Max,RsiFunction[i])
    

// I want Max result from function

plot(Max,         "rsiMax", color=#b63253)
Result = ff_loopMax(Rsi,5,15)

The code extracted from function should work alone. But I can't cope with function.

CodePudding user response:

A function in pine script return the value of the last expression. In your case, the last expression is Max, but you can't access this directly. You call the function, and set this return to a variable.

You should change the last two lines to:

Result = ff_loopMax(Rsi,5,15)       // Result is now equal to Max
plot(Result, "rsiMax", color=#b63253)
  • Related