Guides How to write a custom indicator
How to write a custom indicator in Python or JavaScript
When none of the 79 built-in indicators says exactly what you want, write your own. AlphaChart’s editor runs AlphaScript, its own language for charts, and JavaScript, Python, C, C++ and Java with the same library. This guide builds a moving average crossover in Python and in JavaScript: two lines, settings for their lengths, marks where they cross, and an alert.
Open the editor
Press AlphaScript in the bottom bar (on a phone, AlphaScript editor under More). The editor opens under the chart. Start a new script, choose its language, and write: Add to chart (Ctrl + S) saves it and runs it on the chart you have open.
Every language reads the chart the same way. close, open, high, low and volume are the current bar’s values, the ta library holds the indicators, and the script runs once for every bar, from the oldest to the newest, like an indicator recalculating along the chart.
The crossover in Python
indicator("EMA crossover", overlay=True)
fast_len = input.int("Fast length", 9, min=1, max=200)
slow_len = input.int("Slow length", 21, min=2, max=500)
fast = ta.ema(close, fast_len)
slow = ta.ema(close, slow_len)
plot(fast, "Fast EMA", color="#2962ff", width=2)
plot(slow, "Slow EMA", color="#ff9800", width=2)
up = ta.crossover(fast, slow)
down = ta.crossunder(fast, slow)
if up:
mark("Up", position="below", shape="triangleUp", color=color.green)
if down:
mark("Down", position="above", shape="triangleDown", color=color.red)
alert("Fast EMA crossed above the slow one", when=up)indicatornames the script and, withoverlay=True, draws it over the price, where moving averages belong. Without it, a script draws in a pane of its own under the chart, as an oscillator does.input.intputs a setting in the indicator’s settings, with a default and limits: the two lengths here.ta.emais an exponential moving average of the close.plotdraws a line, with a title for the legend and a colour.ta.crossoveris true on the bar where the fast line goes from at or below the slow one to above it;ta.crossunderthe other way.markputs a triangle under or over the bar where it happens.alertraises an alert on the newest bar when the fast line crosses up.
Python mode follows Python’s rules: indentation, if and elif, keyword arguments, f-strings. Library names also answer in snake_case.
The same in JavaScript
indicator("EMA crossover", { overlay: true });
const fastLen = input.int("Fast length", 9, { min: 1, max: 200 });
const slowLen = input.int("Slow length", 21, { min: 2, max: 500 });
const fast = ta.ema(close, fastLen);
const slow = ta.ema(close, slowLen);
plot(fast, "Fast EMA", { color: "#2962ff", width: 2 });
plot(slow, "Slow EMA", { color: "#ff9800", width: 2 });
const up = ta.crossover(fast, slow);
const down = ta.crossunder(fast, slow);
if (up) mark("Up", { position: "below", shape: "triangleUp", color: color.green });
if (down) mark("Down", { position: "above", shape: "triangleDown", color: color.red });
alert("Fast EMA crossed above the slow one", { when: up });JavaScript passes options as an object at the end, where Python uses keyword arguments. The script, the library and what reaches the chart are the same: on the same bars, the two versions mark the same crosses.
Use it on any market
- A script is saved to your account, up to 200 of them, and comes with you to any browser you sign in on.
- It runs on every market and interval: switch the chart, and it recalculates.
- Its settings are in the indicator’s settings, like any built-in indicator’s, and its colours and lines can be changed there too.
alertgoes off while the script is on an open chart: a notice in the app, and a notification if this device allows them. For alerts that go off with the app closed, use price alerts.printwrites to the editor’s Output, the place to check a value while you build. Problems lists any mistake, with its line.
Where to go from here
The AlphaScript docs cover each language’s rules and the whole library, with examples: 49 technical analysis functions, labels, lines, boxes, inputs, colours and alerts. The editor’s Examples menu starts you from working scripts.
Questions
Do I need to install anything?
No. The editor and the engine run in the browser, on the chart: write, press Add to chart, and it draws.
Which languages can I use?
AlphaScript, AlphaChart’s own language made for charts, and JavaScript, Python, C, C++ and Java. They all reach the same library and run the same way.
Is it real Python?
It reads Python’s rules (indentation, def, if / elif / else, loops, keyword arguments, f-strings) and runs on the chart. It has no access to files, the network or outside packages: everything it needs is in the library.
Is it free?
Yes, like everything in AlphaChart: up to 200 scripts per account, on every market and interval.