millionchart
فارسی

Lesson 18: Reading the editor's messages

What the editor tells you when a script is wrong, and the mistakes beginners make most often, each with the message, the cause and the fix.

Lesson 18 of 18. Everyone writes scripts with mistakes in them. The editor finds most of them while you type, before the script ever runs. This lesson shows how to read what it says, then walks through the mistakes that are made most often.

Where the messages are#

  • A moment after you stop typing, the editor checks the script and underlines the place: a wavy red line for a problem, amber for a note.
  • The status beside the script's name says Ready when there is nothing to fix, Ready · 2 notes when the script works but has warnings to read, and 1 problem when it does not compile yet.
  • The Problems (or Notes) list beside the code names the line and the message's code, such as E3171. An E code is an error: the script cannot run until it is fixed. A W- code is a warning: the script runs, but something deserves a second look.
  • Where the mistake can be mended mechanically, the entry offers Fix: .... Press it to apply the change.

Fix the first problem first. One mistake often causes the messages after it, and they go away with it.

A name that is not declared#

Cuprel · error E3171
indicator "Misspelled name"
    edition = 1
    display = price

let average = tech.sma(close, 20)
plot(averge, name = "SMA")

The message says that averge is not declared and, when it can, names the one you probably meant. Every name must be declared before it is used (with let, input, state and the like) or be a built-in name. Check the spelling, and check upper and lower case: Average and average are two different names.

A text that is not closed#

Cuprel · error E1001
indicator "Open quote"
    edition = 1

let label_text = "Close
plot(close)

A text starts and ends with a double quote on the same line. Add the closing ".

Two statements on one line#

Cuprel · error E2005
indicator "Two on a line"
    edition = 1

let a = 1; let b = 2
plot(a + b)

Each statement has a line of its own; there is no ; between statements. A long statement can go on over several lines when a line ends with an operator or a comma, or inside brackets.

An if that gives a value needs an else#

Cuprel · error E2001
indicator "Missing else"
    edition = 1

let tint = if close > open then color.green
plot(close)

if ... then ... used as a value must say what the value is on the bars where the condition is false. Add else:

Cuprel
indicator "Else added"
    edition = 1
    display = price

let tint = if close > open then color.green else color.red
plot(close, name = "Close", color = tint)

Write else na when there should be nothing on those bars.

An argument the function does not have#

Cuprel · error E3162
indicator "Wrong argument"
    edition = 1
    display = price

plot(close, title = "Close")

The message names the function and lists the arguments it takes. Here the label of a plot is name, not title: write plot(close, name = "Close").

Comparing with na#

This one is a note: the script runs, but the test is never true.

Cuprel · warning W-NA-COMPARE
indicator "Compare with na"
    edition = 1
    display = pane

let average = tech.sma(close, 20)
plot(if average == na then 1 else 0, name = "Missing")

na means "no value", and it is not equal to anything, not even to itself. Ask is_na(x) instead:

Cuprel
indicator "Test for na"
    edition = 1
    display = pane

let average = tech.sma(close, 20)
plot(if is_na(average) then 1 else 0, name = "Missing")

A function that keeps a memory, inside a condition#

Cuprel · error E3302
indicator "Stateful call in an operand"
    edition = 1
    display = price

let rising = close > open
let strong = rising and close > tech.sma(close, 20)
plot(if strong then close else na, name = "Strong bars", style = circles)

Functions such as tech.sma, tech.ema or tech.crosses_above remember the bars they have seen. The right side of and runs only when the left side is true, so the average would skip bars and give wrong numbers; the checker refuses it. Work the value out on a line of its own first, then use the name:

Cuprel
indicator "Stateful call bound first"
    edition = 1
    display = price

let average = tech.sma(close, 20)
let rising = close > open
let strong = rising and close > average
plot(if strong then close else na, name = "Strong bars", style = circles)

The same rule holds for the right side of or, for ??, and for the two branches of if ... then ... else.

When the script stops on the chart#

A script that compiles can still stop while it runs, and the editor then says Stopped on the chart with the engine's message, which names the bar. Each bar may do a limited amount of work, so a loop that never ends is stopped:

Cuprel · error E-LIMIT-OPS
indicator "Endless loop"
    edition = 1
    display = pane

mut n = 0
while true
    n += 1
plot(n)

Make sure every while loop has a condition that becomes false. Another common stop: prior(day) (yesterday's values) needs a chart of bars shorter than a day, so a script that reads it stops on a daily chart with E-PERIOD-TF; open it on an intraday chart, for example 15 minutes or 1 hour. Limits and budgets lists what the engine bounds.

Try changing#

  • Copy "Misspelled name" into a new script and find its entry in the Problems list: it names the line and the code. Fix the spelling and see the status turn to Ready.
  • Write plot(close, title = "Close") in any script and read the list of arguments the message gives; then change title to name.
  • In "Compare with na", replace average == na with is_na(average) and see the note leave the Notes list.
  • Look up one code you met in this lesson in Diagnostics, which explains every code with its cause and fix.

Checkpoint#

  • You can read the status, the Problems and Notes lists, and a code such as E3171.
  • You can tell an error (E) from a warning (W-), and you fix the first problem first.
  • You can mend the mistakes beginners make most: a misspelled name, an open text, a missing else, a wrong argument, a test against na, a stateful call inside a condition.
  • You know what Stopped on the chart means and where the limits are listed.

See also: The editor, na, errors and arithmetic.

Next: you have finished the course. Keep going with the sample programs.