millionchart
فارسی

Write your own scripts

A script is a small program that runs on every bar of the chart. Learn to write one from the first line, with lessons and tested sample programs.

A script is a small program that runs on your chart. It reads the bars (open, high, low, close, volume), does arithmetic on them, and says what to show: a line, an arrow, a coloured background, a level, a table or a label. You write it in Cuprel, the language of the chart, in the script editor.

This section teaches the language from the first line. Every complete program in it is compiled and run on real bars each time the guide is built, so an example that stops working is found before you are.

The smallest script#

A header and one line are a complete script. The header says what kind of script it is; the line under it draws something.

Cuprel
indicator "Close"
    edition = 1

plot(close)

indicator "Close" is the header, and the indented line under it is one of its settings. plot(close) draws the close of every bar as a line (a plot without a name is called Plot 1 in the settings). Without display = price in the header the script gets a pane of its own under the chart. The script runs once for each bar of the chart, from the oldest to the newest, and each run sees only the bars up to that one.

Where scripts live#

  • You open the editor from the Indicators panel: open the On chart tab and look under My scripts. New script opens the editor with a small working example. Save keeps a script with your account and Add to chart puts it on the chart.
  • The editor also has Learn: eight short lessons, each a program to finish, with a check of your work. Each lesson links to the page of this guide that teaches it.
  • Every script is private to your account. Nobody else can open it, and there is no way to share one in this release.
  • The free chart plan keeps 2 scripts; MillionChart Pro keeps up to 100.
  • Put edition = 1 in the header. The edition says which reading of the language the script was written for; a script that says edition = 1 keeps its meaning when a later edition exists. Without it, the current edition is used.
  • A script kept as a text file (.txt) on your computer opens in the editor with Open file, or by dropping the file on the code.

What a script can be#

KindHeaderWhat it is for
Indicatorindicator "Name"Lines, shapes, levels, backgrounds, tables and labels drawn on the chart
Strategystrategy "Name"An indicator that also places simulated orders; the chart marks the trades and shows a table of results, and the Strategy Lab tests it over a longer history
Modulemodule NameFunctions other scripts import. See Modules you reuse

The course#

Eighteen lessons, each with a complete program to run, things to try changing and a checkpoint. Take them in order; each one uses what the earlier ones taught. If you already write scripts for charts in another language, read Already write scripts elsewhere? first.

LessonWhat you learn
1. Your first scriptthe header, a price series, a plot
2. Settings people can changeinputs, limits, choices, groups
3. Lines, colours and levelscolours, styles, levels, zones, the background
4. Signals, arrows and zonesconditions, crossings, marks
5. Values that rememberhistory, state, mut, counters
6. Your own functionsfn, several results, functions that keep a state
7. Lists and mapsarrays, loops, maps
8. Labels, lines and boxesdrawings and their limits
9. Sessions and a small tablenamed sessions, the trading day, tables
10. Other timeframes and symbolsdata.series, earlier days and weeks
11. Alerts that say somethinghow alerts are written (switched off in this release)
12. Modules you reusea module, export and import
13. Finding mistakes with the loglooking at values while you build
14. Your first strategyorders, exits, size and costs
15. Testing in the Strategy Labrunning a strategy over history and reading the report
16. Results that look too goodrepaint, look-ahead and how to avoid them
17. Staying inside the limitsbudgets, history, how to make a script cheaper
18. Reading the editor's messagesthe codes you meet first and their fixes

Look things up#