Learn

Write your first script

A moving average of the close, from an empty editor to a line on the chart.

Put it on the chart

Scripts are written in the chart under Indicators, On chart, My scripts: New script opens the editor, Save keeps the script with your account, and Add to chart runs it on the chart.

  1. Sign in and open the chart at millionchart.com/chart.
  2. Open Indicators, go to the On chart tab and, under My scripts, choose New script. The editor opens with a short starting text.
  3. Click in the code and press Cmd + A (Ctrl + A on Windows) to select the starting text, then paste the program below over it. A script has one header, so do not keep the old one.
  4. Type a name in the name field at the top of the editor, for example Simple moving average.
  5. Press Save. The script is kept with your account.
  6. Press Add to chart. The average is drawn over the price.
Simple moving average
// A simple moving average: the mean of the last `length` closes.
// This is a complete indicator; the header says what kind of script it is.
indicator "Simple moving average"
    edition = 1
    display = price

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

The name on the chart. The chart's legend and the list of your scripts show the script name from the name field at the top of the editor, not the name in the header line. A new script is called My indicator until you type a name there and press Save.

The settings. After Add to chart the settings do not open by themselves: open them from the gear next to the indicator's name in the chart's legend.

Typing instead of pasting. When you type a script by hand, the editor does not indent the line after the header for you. The header lines such as edition and display must be indented by four spaces; without them the editor reports E3102 and its suggested fix does not help. Pasting a sample avoids this.

If the editor lists a problem, it names the line and a code. Every code has a page in the error index.

Read it line by line

  • A line that starts with // is a comment. The engine ignores it.
  • indicator "Simple moving average" is the header: the kind of script and its name. The indented lines under it belong to the header.
  • edition = 1 says which edition of the language the script is written for. Write it in every script: a later edition can change the language without changing what this script means.
  • display = price draws on the price chart. An oscillator uses display = pane and gets a pane of its own.
  • input length: int = 20, name = "Length", min = 1 makes the length a setting of the indicator: a whole number with a first value, the name the settings show, and a smallest allowed value.
  • let average = tech.sma(close, length) gives a name to the simple moving average of the close. A let name never changes once it is set.
  • plot(average, name = "SMA", color = color.orange, width = 2) draws the value of every bar as a line. Arguments written with a name can come in any order.

The script runs once for each bar, from the oldest bar to the newest. On each bar it can read that bar and the bars before it.

Next

The language tour explains the rest of the language through complete programs, and the sample programs are ready to paste.

In the MillionChart wiki: Your first script and The editor.