0

I am not an expert. I wrote an simple code that plots ema based on condition. If intraday is true then value of ema is 9 or value of ema is 21. But there is error on "out = ta.ema(src, len)" there is a red line below len and says that "Undeclared identifier len". can someone guide how to solve this.

//@version=5
indicator(title="Moving Average Exponential", shorttitle="EMA", overlay=true)

intraday=input(defval=true, title="INTRADAY / STANDARD")

if (intraday == true)
    len = input.int(9, title="Length")
else 
    len = input.int(21, title="Length")

src = input(close, title="Source")
out = ta.ema(src, len)
plot(out, title="EMA", color=color.blue)

1 Answers1

0

You are defining len in the local scope. It is not visible in the global scope, hence the error message.

If you want to fix that, you can do:

int len = na

if (intraday == true)
    len := input.int(9, title="Length")
else 
    len := input.int(21, title="Length")

However, you cannot do what you are trying to do. You cannot have a conditional input.

You will have two Length inputs here. If you really want to have different inputs for intraday and standard length, you can do:

//@version=5
indicator(title="Moving Average Exponential", shorttitle="EMA", overlay=true)

intraday=input(defval=true, title="INTRADAY / STANDARD")
len_intraday = input.int(9, title="Intraday Length")
len_standard = input.int(21, title="Standard Length")

len = intraday ? len_intraday : len_standard

src = input(close, title="Source")
out = ta.ema(src, len)
plot(out, title="EMA", color=color.blue)
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • https://stackoverflow.com/users/7209631/vitruvius, Can you help me how to add 3rd input too? like condition 1 is 9ema, condition 2 is 21ema and condition 3 is 55ema – NITIN PATEL Dec 23 '22 at 05:00