简体   繁体   中英

How to show an Indicator with key On/Off (Pine Script)

//code

//@version=4
study("Enable test indicator ")

// in setup panel, I can enable or disable with instruction below

habilitaRSI = input(true, "Enable", type = input.bool, group = "RSI", inline = "RSI Period")

rsiLength = input (14, "RSI Comprimento", type = input.integer, group="RSI", inline = "RSI Period")
rsiVal = rsi(close, rsiLength)

// below I need Plot with display.all ou display.none  - depends on (if check is enable or not in setup panel)

if habilitaRSI
plot(rsi(close,14),display=display.all)
else
plot(rsi(close,14),display=display.none)

but I got this error on "IF" command

Error: Processando script... line 18: Cannot use 'plot' in local scope.;

So, how can I Plot using "IF"?

I need Plot many indicators in only one script and use" check enable" or "check disable" in setup panel

To do this I am using a parameter(option) in Plot command that are (display.all and/or display.none)

like: plot(rsi(close,14),display=display.none)

It's depends on (if check is enable or not in setup panel)

The plot() function should be called in the global scope only and could not be executed from the local block. Use the ternary ?: operator directly in the series= argument of the plot() function, a simplified version:

//@version=4 
study("Enable test indicator", overlay = false)
habilitaRSI = input(true, "Enable", type = input.bool, group = "RSI", inline = "RSI Period")
rsiLength = input (14, "RSI Comprimento", type = input.integer, group="RSI", inline = "RSI Period") 
rsiVal = rsi(close, rsiLength)
plot(habilitaRSI ? rsiVal : na) 

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