AlphaChart

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)
}

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.ema keep their own memory, one for each place they are called from. Call them on every bar, not only inside an if.

What the library has

Languages

LanguageHow a file is written
AlphaScriptMade for charts: the shortest way to write an indicator. Runs top to bottom on every bar.
JavaScriptTop to bottom on every bar, or a function onBar(). Options as { color: … }.
PythonTop to bottom on every bar, or def on_bar():. Keyword arguments, f-strings, snake_case names.
CGlobals, setup() once, onBar() on every bar. static locals, printf.
C++As C, with auto, std::vector, std::cout and options as {.color = …}.
JavaOne 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.