3

Resources are still Pine Script 3 heavy, so I think I'm mixing something up:

I tried to create an Average Daily Range indicator, which is basically the ATR that takes a "D" input no matter what the time frame of the current chart is. My code works perfectly fine on Pine Script 3, but Pine Script 4 throws out the following errors:

line 4: Undeclared identifier `resolution`;
line 6: Undeclared identifier `tickerid`;
line 6: Undeclared identifier `dRange`;
line 8: Undeclared identifier `adRange`

The docs indicate resolution is still an input() argument, and I'm not sure why anything else is called "undeclared".

My full code is:

//@version=4
study(title="Average Daily Range", shorttitle="ADR", overlay=false)

dRange = input(defval="D", title="Daily Range", type=resolution)

adRange = security(tickerid, dRange, rma(tr, 5))

plot(adRange, title = "ADR", color=#000000, transp=0)

What are these "Undeclared identifiers"? And what must I do differently in Pine Script 4 so that I'm getting the same result?

Thank you.

alexeix
  • 77
  • 1
  • 1
  • 8

2 Answers2

3

I solved it, but still don't know what "undeclared identifiers" were. I assume it's just Pine's way of saying that dRange violated the new rules with its variable resolution, and that adRange was creating problems with the un-used tickerid, which has been replaced by syminfo.tickerid in PineScript 4.

//@version=4
study(title="Average Daily Range", shorttitle="ADR", overlay=false)

adRange = security(syminfo.tickerid, "D", rma(tr, 5))

plot(adRange, title="ADR", color=#000000, transp=0)
alexeix
  • 77
  • 1
  • 1
  • 8
  • An identifier is any text that names a "thing" (variable, function, whatever). So, for example: `study` is an identifier, `adRange` is an identifier, `security` is an identifier, `rma` is an identifier and so on. "Undeclared identifier" means that you tried to use an identifier that wasn't declared: that is, an identifier that it doesn't know about. In pine, when you declare a variable like your `adRange = ...` that also declares the identifier. If you tried to use `adRange` before that line, you would get the undeclared error, but after that line you do not. Basically, pine must know about... –  Sep 19 '19 at 19:25
  • ...an identifier before you can use it. If you tried to use it before its declared, pine doesn't know what it is, so it complains. In your code, `resolution` was used but it's not a built in variable and not a variable you defined, so you got the error. Ok, with that explanation out of the way, the reason `resolution` gave you an error is that it was built in for pine v3, but pine v4 moved a bunch of things to avoid name clashes. You need to use `input.resolution` instead now. –  Sep 19 '19 at 19:27
2

v4 RefMan is here: https://www.tradingview.com/pine-script-reference/v4/

v4 UserMan is here: https://www.tradingview.com/pine-script-docs/en/v4/index.html

Since compiler is returning an error on line 4 which uses input(), makes sense to look up the function in refman, where you will find that the proper argument to type= parameter for what you want is now input.resolution. Being unable to initialize variable dRange because of the error, the compiler also gives an error on that variable. Same goes with adRange, which you've fixed.

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21