v0.5.0Apache 2.0

OpenScript

Also called OpenAlgo Script.

Write a study or a strategy once.Plot it, backtest it, trade it.

The open trading language of OpenAlgo. One short script draws on the chart in /trading, backtests in the browser, and trades through OpenAlgo, starting in sandbox trading (analyzer mode in OpenAlgo).

New to OpenScript? Start with Language basics

// HalfTrend: a trend level that holds flat through noise and turns only when
// the other side of the range gives way. Blue while the trend is up, red while
// it is down, with a shaded channel and a label on every flip.
version 1

study("HalfTrend", overlay = true, precision = 2)

amplitude = input(2, "Amplitude", min = 1, max = 100)
channelDev = input(2, "Channel deviation", min = 0, max = 20)
atrLen = input(100, "ATR length", min = 1, max = 500)

half = atr(atrLen) / 2
dev = channelDev * half
rollHigh = highest(high, amplitude)
rollLow = lowest(low, amplitude)
meanHigh = sma(high, amplitude)
meanLow = sma(low, amplitude)
prevHigh = bar.isFirst ? high : high[1]
prevLow = bar.isFirst ? low : low[1]

// trend is 0 while up and 1 while down; armed is the flip being watched for.
var trend = 0
var armed = 0
var maxLow = low
var minHigh = high
var upLevel = low
var downLevel = high

wasTrend = bar.isFirst ? -1 : trend

if armed == 1
    maxLow = max(orElse(rollLow, maxLow), maxLow)
    if not isNone(meanHigh) and meanHigh < maxLow and close < prevLow
        trend = 1
        armed = 0
        minHigh = orElse(rollHigh, high)
else
    minHigh = min(orElse(rollHigh, minHigh), minHigh)
    if not isNone(meanLow) and meanLow > minHigh and close > prevHigh
        trend = 0
        armed = 1
        maxLow = orElse(rollLow, low)

flipUp = trend == 0 and wasTrend == 1
flipDown = trend == 1 and wasTrend == 0

// On a flip the new level starts where the other side ended, so it steps.
if trend == 0
    upLevel = flipUp ? downLevel : wasTrend == -1 ? maxLow : max(maxLow, upLevel)
else
    downLevel = flipDown ? upLevel : wasTrend == -1 ? minHigh : min(minHigh, downLevel)

ht = trend == 0 ? upLevel : downLevel

upLine = plot(trend == 0 ? ht : none, "Up trend", #2962ff, width = 2)
downLine = plot(trend == 1 ? ht : none, "Down trend", #ef5350, width = 2)
upEdge = plot(trend == 0 ? ht - dev : none, "Channel low", fade(#2962ff, 55), width = 1)
downEdge = plot(trend == 1 ? ht + dev : none, "Channel high", fade(#ef5350, 55), width = 1)
fill(upLine, upEdge, fade(#2962ff, 82))
fill(downLine, downEdge, fade(#ef5350, 82))

if flipUp
    signal("Buy", #2962ff, at = "below", shape = "label")

if flipDown
    signal("Sell", #ef5350, at = "above", shape = "label")
Study66 linesLoading the editor

In /trading

The first tab, halftrend.oscript, running on a BHEL 15 minute NSE chart in the /trading page of OpenAlgo.

In this playground

  • Hover a name such as atr or orElse to read its signature and what it does.
  • Start typing a name on a new line and pick from the library, with each signature beside it.
  • Misspell close. The compiler marks it with an error code and a fix.

What it does

From an idea on a chartto an order, in one language

study() strategy()

Studies and strategies in one language

A study draws plots, fills, markers and tables on the chart. A strategy adds orders, exits and position sizing. The declaration line is the only difference, so an idea moves from the chart to a backtest without a rewrite.

no eval

Compiles in milliseconds

A script compiles to a versioned data program that an engine runs. Nothing is built with eval or new Function, and a typical script compiles in a few milliseconds: the playground above times every keystroke.

diagnose() hover()

Checks as you type

The language ships highlight, complete, hover, signature, diagnose and format as plain functions, so any editor can complete, explain and check a script while it is written. Every error has a code, a message and a fix.

Backtest panel

Backtests in the browser

Pick a strategy, a date range and its inputs in the Backtest panel of /trading, and run it in the browser. The report shows net profit, drawdown and win rate, with every trade and the equity curve they add up to.

  1. Pick a strategy
  2. Set the range and inputs
  3. Run it in the browser
  4. Read the report and trades

analyzer mode

Sandbox first, then live

Deploy a strategy to sandbox trading (analyzer mode in OpenAlgo) and watch its orders on real market data before you switch it to live trading.

alert()

Alerts from scripts

Call alert() on the bar a condition is met. The Alerts panel lists every active alert and logs each time one fires.

spec + 2 engines

An open specification

The language is written down as a specification with a conformance suite. The JavaScript and Python engines run the same compiled programs and are held to the same results.

In /trading

OpenScript is built into the /trading page of OpenAlgo: write in the Scripts panel, draw on the chart, backtest, and deploy, on Indian market instruments.

Libraries

Two libraries, one language

Put OpenScript inside any financial portal. Both are Apache 2.0 with zero dependencies.

openalgo-script

npm · JavaScript and TypeScript

The compiler, the engine, the six editor functions and a charts adapter, as ES modules for the browser and Node.

npm install openalgo-script

openscript

PyPI · Python

Runs compiled OpenScript programs in pure Python, on a server next to your data and your order routing.

pip install openscript

Write your first study

The quickstart takes five minutes: open the Scripts panel in /trading, write a study, and put it on an NSE chart.