简体   繁体   中英

Not possible to declare variables in a if-condition (PineScript)

im trying to improove my indicator because the loading time is too long when i switch the timeframe. Now im trying to change the code and im in front of a problem.

Look at the code below.

I want to declare values for variables inside an if condition. But when i use the code below, it does not take the declared variables inside the if condition if its "true". It does always use the variables which i declared at the beginning of the code.

Any hints how i can fix that?

 stochCalc = sma(stoch(close, high, low, 14), 6)
    v1 = "1"
    v2 = "1"
    v3 = "1"

        if timeframe.multiplier == 240
            v1 = "15"
            v2 = "30"
            v3 = "60"
        else
            if timeframe.multiplier == 720
                v1 = "60"
                v2 = "120"
                v3 = "240"
            else
                if timeframe.multiplier == 1440
                    v1 = "240"
                    v2 = "720"
                    v3 = "1440"
                else
                    v1 = "1"
                    v2 = "2"
                    v3 = "3"

        k1 = security(syminfo.tickerid, v1, stochCalc)
        k2 = security(syminfo.tickerid, v2, stochCalc)
        k3 = security(syminfo.tickerid, v3, stochCalc)

        plot(k1, color=color.yellow, transp=0, linewidth=2)
        plot(k2, color=color.yellow, transp=0, linewidth=2)
        plot(k3, color=color.yellow, transp=0, linewidth=2)

im trying to improove my indicator because the loading time is too long when i switch the timeframe. Now im trying to change the code and im in front of a problem.

Look at the code below.

I want to declare values for variables inside an if condition. But when i use the code below, it does not take the declared variables inside the if condition if its "true". It does always use the variables which i declared at the beginning of the code.

Any hints how i can fix that?

 stochCalc = sma(stoch(close, high, low, 14), 6)
    v1 = "1"
    v2 = "1"
    v3 = "1"

        if timeframe.multiplier == 240
            v1 = "15"
            v2 = "30"
            v3 = "60"
        else
            if timeframe.multiplier == 720
                v1 = "60"
                v2 = "120"
                v3 = "240"
            else
                if timeframe.multiplier == 1440
                    v1 = "240"
                    v2 = "720"
                    v3 = "1440"
                else
                    v1 = "1"
                    v2 = "2"
                    v3 = "3"

        k1 = security(syminfo.tickerid, v1, stochCalc)
        k2 = security(syminfo.tickerid, v2, stochCalc)
        k3 = security(syminfo.tickerid, v3, stochCalc)

        plot(k1, color=color.yellow, transp=0, linewidth=2)
        plot(k2, color=color.yellow, transp=0, linewidth=2)
        plot(k3, color=color.yellow, transp=0, linewidth=2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM