Cipher AI / TestAlgoBlazeScript documentationInteractive reference

BLAZESCRIPT / LANGUAGE DOCUMENTATION

BlazeScript Syntax

Syntax used in the examples

  • Assign values with =. Use # for comments. Names and spelling follow the individual reference entries.
  • The platform provides price data such as open, high, low, close and volume. You do not need to import indicator functions in the introductory example.
  • Examples pass positional arguments, such as EMA(close, 9), and keyword arguments, such as index=1. Use the argument list of the specific function.
  • Examples use comparisons, arithmetic, and both and/or and &/| conditions. Parenthesize comparisons when combining them with & or |. Follow the documented example for the relevant series operation.
  • Entry must be a top-level call. The introductory Exit call is also top-level. Buy and Sell are keyword variables in the Entry reference; order_type uses a quoted string.
  • This documentation does not define a complete grammar, type system or supported Python subset. Do not assume that arbitrary Python libraries or Pine Script functions are supported.

Built-in Variables

Price Data #

open, high, low, close, volume, oi, hl2, hlc3, ohlc4

Core OHLCV data series.

Example

# Access current bar data
current_close = close
current_volume = volume
# Calculate typical price manually
my_hlc3 = (high + low + close) / 3

Trade State #

position_size, position_avg_price, open_pnl, bars_in_trade, max_runup, max_drawdown

Real-time in-trade metrics.

Example

# Check if we are in a trade
in_trade = position_size != 0
# Stop loss based on PnL
panic_exit = open_pnl < -1000

Daily OHLC #

PDH, PDL, PDC, PDO, CDH, CDL, CDO

Previous and Current Day's Open, High, Low, and Close.

Example

# Breakout of Previous Day High
breakout = close > PDH
# Gap up condition
gap_up = CDO > PDC

Time & Date #

time, Date, DayOfMonth, DayOfWeek, DayOfYear, DaysSince1900, DayFirstBar, DayLastBar

Time and daily boundary booleans.

Example

# 1. Time-based trading window (Format HHMM)
# Example: 9:30 AM to 11:00 AM
trading_window = (time >= 930) & (time <= 1100)
# 2. Day of Week (1=Monday, ..., 5=Friday)
is_friday = DayOfWeek == 5
# 3. Session Boundaries
# Reset state on first bar of day
if DayFirstBar:
    my_var = 0
# Close positions on last bar
exit_on_close = DayLastBar
# 4. Specific Date (YYYYMMDD)
is_budget_day = Date == 20230201

Math & Structure

Basic Math #

abs, round, min, max, avg, sum

Standard mathematical functions.

Example

# Calculate average price
avg_price = avg(open, high, low, close)
# Round to 2 decimal places
rounded_rsi = round(rsi_14, 2)
# Sum of volume over 10 bars
vol_sum = sum(volume, 10)

Advanced Math #

pow, sqrt, square, log, exp, sign, ceil, floor

Algebraic and logarithmic operations.

Example

# Logarithmic return
log_ret = log(close / close[1])
# Standard deviation calculation steps
variance = sum(square(close - avg(close, 20)), 20) / 20
std_dev = sqrt(variance)

Trigonometry & Constants #

sin, cos, tan, asin, acos, atan, e_const, pi_const, phi_const

Trig functions and constants.

Example

# Sine wave based on bar index
cycle = sin(BarIndex * pi_const / 10)
# Use constants
circumference = 2 * pi_const * radius

peak / trough #

(source, leftbars, rightbars)

Identifies local pivot points.

Example

# Find a pivot high (5 bars left, 2 bars right)
pivot_high = peak(high, 5, 2)
# Find a pivot low
pivot_low = trough(low, 5, 2)

peakbars / troughbars #

(source, leftbars, rightbars, n=1)

Bar offset to local pivot points.

Example

# Bars since last pivot high
bars_since_peak = peakbars(high, 5, 2)
# Get the value of that peak
peak_val = high[bars_since_peak]

HigherHigh / LowerLow / HigherLow / LowerHigh #

(source, leftbars, rightbars)

Trend structure identification.

Example

# Check for Higher High
is_hh = HigherHigh(high, 5, 2)
# Check for Lower Low
is_ll = LowerLow(low, 5, 2)

Array Ops #

BarIndex, BeginValue, EndValue, median

Array position indexing and statistical utilities.

Example

# Current bar number
current_bar = BarIndex
# Median price over 20 bars
med_price = median(close, 20)