AlphaScript Docs
AlphaScript
AlphaScript is how you write your own indicators on AlphaChart: averages, oscillators, signals, levels, labels and alerts. Write them in AlphaScript, AlphaChart’s own language made for charts, or in a language you already know: JavaScript, Python, C, C++ or Java. Every language reaches the same library and runs the same way.
Your first indicator
A 20-bar moving average with a mark where the price crosses above it. Pick your language:
// AlphaScript runs this file on every bar, from the oldest to the newest.
// "state" keeps a value from bar to bar; x[1] is x on the previous bar.
// Docs: alphachart.dev/docs/alphascript
indicator("My indicator", overlay: true)
let length = input.int("Length", 20, min: 1)
let average = ta.sma(close, length)
plot(average, "Average", color: #2962ff, width: 2)
if ta.crossover(close, average) {
mark("Up", position: "below", shape: "triangleUp", color: color.green)
}// JavaScript: this file runs on every bar, from the oldest to the newest.
// let x = state(0) keeps a value from bar to bar; x[1] is x on the previous bar.
// Docs: alphachart.dev/docs/alphascript/javascript
indicator("My indicator", { overlay: true });
const length = input.int("Length", 20, { min: 1 });
const average = ta.sma(close, length);
plot(average, "Average", { color: "#2962ff", width: 2 });
if (ta.crossover(close, average)) {
mark("Up", { position: "below", shape: "triangleUp", color: color.green });
}# Python: this file runs on every bar, from the oldest to the newest.
# count = state(0) keeps a value from bar to bar; x[1] is x on the previous bar.
# Docs: alphachart.dev/docs/alphascript/python
indicator("My indicator", overlay=True)
length = input.int("Length", 20, min=1)
average = ta.sma(close, length)
plot(average, "Average", color="#2962ff", width=2)
if ta.crossover(close, average):
mark("Up", position="below", shape="triangleUp", color=color.green)// C: globals keep their values; setup() runs once, onBar() on every bar.
// Docs: alphachart.dev/docs/alphascript/c
void setup(void) {
indicator("My indicator", true);
}
void onBar(void) {
int length = input.int("Length", 20, 1);
double average = ta.sma(close, length);
plot(average, "Average", color.blue, 2);
if (ta.crossover(close, average)) {
mark("Up", "below", "triangleUp", color.green);
}
}// C++: globals keep their values; setup() runs once, onBar() on every bar.
// Docs: alphachart.dev/docs/alphascript/cpp
void setup() {
indicator("My indicator", true);
}
void onBar() {
const int length = input.int("Length", 20, {.min = 1});
const double average = ta::sma(close, length);
plot(average, "Average", {.color = color::blue, .width = 2});
if (ta::crossover(close, average)) {
mark("Up", {.position = "below", .shape = "triangleUp", .color = color::green});
}
}// Java: fields keep their values; setup() runs once, onBar() on every bar.
// Docs: alphachart.dev/docs/alphascript/java
import alphachart.*;
public class MyIndicator extends Indicator {
public void setup() {
indicator("My indicator", true);
}
public void onBar() {
int length = input.integer("Length", 20, 1);
double average = ta.sma(close, length);
plot(average, "Average", Color.BLUE, 2);
if (ta.crossover(close, average)) {
mark("Up", "below", "triangleUp", Color.GREEN);
}
}
}Open the editor with the AlphaScript button at the bottom of the chart (Alt Shift E, on a Mac ⌥ ⇧ E), pick a language, and press Ctrl S (⌘ S): the script is saved and drawn on the chart. Change it, press it again, and the chart follows.
How a script runs
A chart is a row of bars, oldest first. A script runs once on every bar, from the oldest to the newest, and each time it sees that bar: open, high, low, close, volume, time, barIndex. What it draws on a bar stays on that bar. That is all there is to the model:
- Values of the past are one step away.
close[1]is the previous bar’s close,x[3]any variable three bars ago. - Some values must be kept from bar to bar, like a counter. In AlphaScript they are declared with
state; the other languages have their own way (globals,static, fields,state(0)). - Calculations such as
ta.emakeep their own memory, one for each place they are called from. Call them on every bar, not only inside anif.
What the library has
Languages
| Language | How a file is written |
|---|---|
| AlphaScript | Made for charts: the shortest way to write an indicator. Runs top to bottom on every bar. |
| JavaScript | Top to bottom on every bar, or a function onBar(). Options as { color: … }. |
| Python | Top to bottom on every bar, or def on_bar():. Keyword arguments, f-strings, snake_case names. |
| C | Globals, setup() once, onBar() on every bar. static locals, printf. |
| C++ | As C, with auto, std::vector, std::cout and options as {.color = …}. |
| Java | One class: fields, setup(), onBar(), List, Math, System.out.println. |
Safe by design
Scripts run inside AlphaChart’s own script engine. A script cannot reach the page, your account, the network, files or the device; it only sees the chart’s bars and the library. Whatever the language, nothing in it is ever handed to the browser to run as code. Every run has limits on work, time and memory, so a runaway loop stops with a clear message instead of freezing the chart. The limits.