AlphaScript Python
AlphaScript in Python
Python mode reads Python’s rules: indentation, def, if / elif / else, for … in range(…), while, global, keyword arguments, f-strings, //, **, chained comparisons and None, True, False.
A whole indicator
# 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)How the file runs
Without an on_bar() function the module runs on every bar. With one, the module runs once, setup() once if there is one, then on_bar() on every bar.
Keeping values between bars
Top to bottom, count = state(0) keeps a value from bar to bar. With on_bar(), module variables keep theirs; assign them inside on_bar() with global, as in any Python.
# top to bottom on every bar: state() keeps a value
count = state(0)
if close > open:
count += 1
# or with on_bar(): module variables keep their values
highest = 0
def on_bar():
global highest
highest = max(highest, close)
plot(highest, "Highest")Arguments and options
Options are keyword arguments, and names can be written in snake_case:
plot(ta.ema(close, 21), "EMA", color="#ff9800", width=2)
mark("Buy", position="below", shape="triangleUp", color=color.green)
print(f"bar {bar_index}: {close:.2f}")What is read
def f(a, b=1):,return,globalif/elif/else,for i in range(…),for x in values,while,break,continue,pass- lists,
xs[-1]; tuples are read as lists, soreturn a, b,a, b = f()anda, b = b, awork and or not,is None,in,a < b < c,x if c else y- f-strings with formats:
f"{x:.2f}",f"{x:,.0f}",f"{p:.1%}", and"{:.2f}".format(x) abs,min,max,round,len,int,float,str,print,math.*
What is not
- classes, lambdas, comprehensions, generators, decorators, try / except, with
- dictionaries and sets
importof anything outside the library (import lines are allowed and ignored)
Anything not read is pointed out with its line, in plain words.
Names and numbers
Every library name also answers in snake_case: bar_index, is_last_bar, ta.highest_bars. / always divides exactly; // divides down to a whole number; % follows Python’s sign rule.
Example: moving average cross
indicator("MA cross", overlay=True)
fast = ta.ema(close, input.int("Fast", 20, min=1))
slow = ta.ema(close, input.int("Slow", 50, min=1))
f = plot(fast, "Fast", color="#ff9800", width=2)
s = plot(slow, "Slow", color="#2962ff", width=2)
fill(f, s, color=color.alpha(color.green, 0.12), color_down=color.alpha(color.red, 0.12))
if ta.crossover(fast, slow):
mark("Golden cross", position="below", shape="triangleUp", color=color.green)
alert(f"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.