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.
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 = 1in the header. The edition says which reading of the language the script was written for; a script that saysedition = 1keeps 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#
| Kind | Header | What it is for |
|---|---|---|
| Indicator | indicator "Name" | Lines, shapes, levels, backgrounds, tables and labels drawn on the chart |
| Strategy | strategy "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 |
| Module | module Name | Functions 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.
| Lesson | What you learn |
|---|---|
| 1. Your first script | the header, a price series, a plot |
| 2. Settings people can change | inputs, limits, choices, groups |
| 3. Lines, colours and levels | colours, styles, levels, zones, the background |
| 4. Signals, arrows and zones | conditions, crossings, marks |
| 5. Values that remember | history, state, mut, counters |
| 6. Your own functions | fn, several results, functions that keep a state |
| 7. Lists and maps | arrays, loops, maps |
| 8. Labels, lines and boxes | drawings and their limits |
| 9. Sessions and a small table | named sessions, the trading day, tables |
| 10. Other timeframes and symbols | data.series, earlier days and weeks |
| 11. Alerts that say something | how alerts are written (switched off in this release) |
| 12. Modules you reuse | a module, export and import |
| 13. Finding mistakes with the log | looking at values while you build |
| 14. Your first strategy | orders, exits, size and costs |
| 15. Testing in the Strategy Lab | running a strategy over history and reading the report |
| 16. Results that look too good | repaint, look-ahead and how to avoid them |
| 17. Staying inside the limits | budgets, history, how to make a script cheaper |
| 18. Reading the editor's messages | the codes you meet first and their fixes |
Look things up#
- The sample programs are complete indicators and strategies to copy, run and change.
- Syntax and layout and the other pages under The language say exactly what you can write.
- The function reference has every function and value with its parameters; the function index lists them on one page.
- Diagnostics explains every message code, with the cause and the fix.
- The cheat sheet, the glossary and the questions people ask.