millionchart
فارسی

Lesson 1: Your first script

Write a moving average in five lines, give it an input, make it look the way you want, and put it on your chart.

Lesson 1 of 18. In this lesson you write an indicator that draws a moving average of the close, then change it three times. Each program is complete: you can copy it into the script editor as it is.

A line on the chart#

Cuprel
// A moving average of the close.
indicator "My first average"
    edition = 1
    display = price

plot(tech.sma(close, 20))

Read it from the top.

  • A line that starts with // is a comment. The engine ignores it.
  • indicator "My first average" is the header. The text after indicator is the name. The indented lines under it are the header's settings: display = price draws on the price chart; without it the script gets a pane of its own under the chart, which is right for things like an oscillator.
  • edition = 1 says which reading of the language the script uses. Put it in every header; a script that says edition = 1 keeps its meaning when a later edition comes.
  • close is the closing price of the bar. Every price name (open, high, low, close, volume) is a series: one value for every bar. Anywhere you write close you mean the close of the bar the script is running on.
  • tech.sma(close, 20) is the simple moving average of the last 20 closes. tech is the group of technical functions; sma is one of them. The first 19 bars have no average yet, so the line starts on bar 20.
  • plot(...) draws a number on every bar as a line.

The script runs once for each bar, from the oldest to the newest. The number of the average on a bar uses only bars up to that one.

An input you can change#

A fixed 20 is a guess. Make it a setting:

Cuprel
indicator "My first average"
    edition = 1
    display = price

input length: int = 20, name = "Length", min = 1
plot(tech.sma(close, length))

input length: int = 20 declares a setting called length. Its type is int (a whole number) and its first value is 20. name is what the settings panel shows; min = 1 stops anyone from setting it to zero. Inside the script length is a plain number. When you change the setting on the chart, the script runs again with the new value.

The name length is also the setting's id: a saved chart finds the value again by it, even if you change the title later. Ids explains this.

Name it and colour it#

Cuprel
indicator "My first average"
    edition = 1
    display = price

input length: int = 20, name = "Length", min = 1
let average = tech.sma(close, length)
plot(average, name = "Average", color = color.orange, width = 2)

let average = ... gives the result a name. A let name never changes after it is set; use it whenever you want to read a value more than once or give it a meaning. name, color and width are arguments written by name. The order of named arguments does not matter.

Two averages, one script#

Cuprel
indicator "Two averages"
    edition = 1
    display = price

input fast_length: int = 10, name = "Fast length", min = 1
input slow_length: int = 30, name = "Slow length", min = 2
let fast = tech.ema(close, fast_length)
let slow = tech.ema(close, slow_length)
plot(fast, name = "Fast", color = color.green)
plot(slow, name = "Slow", color = color.red)

tech.ema is the exponential moving average. A script can have as many inputs and plots as it needs. Each plot has its own line colour and width in the settings.

Run it on the chart#

  1. Open the Indicators panel, go to the On chart tab, and under My scripts choose New script.
  2. A new script starts with a small example text. Select all of it and replace it with the program (two headers in one script are an error). The editor underlines mistakes and lists them under Problems.
  3. Press Save, then Add to chart. The line appears and the settings open from the indicator's name.
  4. Open the indicator's settings and change a value. The chart draws the line again with the new value.

Try changing#

  • In the first program, replace tech.sma with tech.ema. The line follows the price more closely, because the exponential average gives the newest bars more weight.
  • In the second program, change min = 1 to min = 5, save, and try to set the length to 2 in the settings: the field does not go below 5.
  • In "Name it and colour it", change color.orange to color.blue and width = 2 to width = 4.
  • In "Two averages, one script", read hl2 (the middle of each bar) instead of close in both averages: tech.ema(hl2, fast_length).
  • Delete the line display = price from a header and add the script again: it moves to a pane of its own under the chart.

Checkpoint#

  • You can write a header with edition = 1 and display = price.
  • You can draw a built-in average with plot and give it a name, a colour and a width.
  • You can turn a fixed number into an input.
  • You can save a script and put it on the chart from My scripts.

See also: Syntax and layout, Inputs, Plots and drawings.

Next: Lesson 2: Settings people can change