AlphaScript JavaScript
AlphaScript in JavaScript
JavaScript mode reads the core of the language you know: let and const, functions and arrow functions, if, for, for … of, while, do, switch, template literals, the ?? operator and destructuring.
A whole indicator
// 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 });
}How the file runs
Without an onBar() function the whole file runs on every bar, like AlphaScript. With one, the file runs once (that is where globals get their starting values), then setup() once if there is one, then onBar() on every bar.
Keeping values between bars
Top to bottom, let count = state(0) keeps a value from bar to bar. With onBar(), globals keep theirs.
// top to bottom on every bar: state() keeps a value
let count = state(0);
if (close > open) count++;
// or with onBar(): globals keep their values
let highest = 0;
function setup() { indicator("Highest close", { overlay: true }); }
function onBar() {
highest = Math.max(highest, close);
plot(highest, "Highest");
}Arguments and options
Options are given as an object, the last argument:
plot(ta.ema(close, 21), "EMA", { color: "#ff9800", width: 2 });
mark("Buy", { position: "below", shape: "triangleUp", color: color.green });
console.log(`bar ${barIndex}: ${close.toFixed(2)}`);What is read
let,const,var,const [a, b] = …function f(a, b = 1) { … },const f = (a, b) => …if,for (;;),for (const x of list),while,do … while,switch++ --,+= -= *= /=,? :,??,=== !==- template literals
`…${x}…`,x.toFixed(2),x.toString() Math.*,console.log,NaN,null,undefined(all na)
What is not
- classes; objects as values (an object literal is for options only); functions inside functions
import,require,fetch,window,document, timers, promises- regular expressions
Anything not read is pointed out with its line, in plain words.
Names and numbers
Library names are the same as in AlphaScript (ta.sma, barIndex). Math.max, Math.abs and the rest of Math work, as does console.log.
Example: moving average cross
indicator("MA cross", { overlay: true });
const fast = ta.ema(close, input.int("Fast", 20, { min: 1 }));
const slow = ta.ema(close, input.int("Slow", 50, { min: 1 }));
const f = plot(fast, "Fast", { color: "#ff9800", width: 2 });
const s = plot(slow, "Slow", { color: "#2962ff", width: 2 });
fill(f, s, { color: color.alpha(color.green, 0.12), colorDown: color.alpha(color.red, 0.12) });
if (ta.crossover(fast, slow)) {
mark("Golden cross", { position: "below", shape: "triangleUp", color: color.green });
alert(`Golden cross on ${symbol.name}`);
}
if (ta.crossunder(fast, slow)) {
mark("Death cross", { position: "above", shape: "triangleDown", color: color.red });
}The same indicator in every language: examples.