Cipher AI / TestAlgoBlazeScript documentationInteractive reference

BLAZESCRIPT / LANGUAGE DOCUMENTATION

BlazeScript Strategy Structure

Getting started

A project uses entry_logic.ta, exit_logic.ta and canvas.ta. Entry logic defines a signal and registers an Entry call. Exit logic can use entry variables and registers Exit. Canvas code can use entry and exit variables to draw results. The examples below reproduce the current website examples without modifying their syntax.

entry_logic.ta

# --- entry_logic.ta ---
# Pythonic execution. No imports required.
fast_ema = EMA(close, 9)
slow_ema = EMA(close, 21)

# Simple crossing over strategy
long_condition = crossover(fast_ema, slow_ema)

# Keyword vars for action, string for order_type
Entry(index=1, logic=long_condition, timeframe="5m", script=banknifty_spot, action=Buy, order_type="MARKET")

exit_logic.ta

# --- exit_logic.ta ---
# Variables declared in entry_logic (fast_ema, slow_ema) are natively accessible here.

# Multiple Exit Conditions
stop_loss = open_pnl < -500
take_profit = open_pnl > 1500
trend_reversal = crossunder(fast_ema, slow_ema)

# Combine logic
exit_signal = stop_loss | take_profit | trend_reversal

# Top-level exit call
Exit(index=1, logic=exit_signal, exit_type="MARKET")

canvas.ta

# --- canvas.ta ---
# Variables from entry & exit logic flow directly into canvas scope.

# Paint the chart background green while Fast EMA is above Slow EMA
debug_paint_background(fast_ema > slow_ema, frame=0, color="rgba(0, 255, 0, 0.1)")

# Draw standard indicators on the main chart (frame=0)
draw(fast_ema, title="Fast 9 EMA", frame=0, color="blue")
draw(slow_ema, title="Slow 21 EMA", frame=0, color="orange")

# Display trade execution visuals natively
marker_trades(frame=0, show_pnl=True, show_signal_line=True)

Strategy & Execution

Entry #

(index, logic, timeframe, script, action, order_type, limit_price)

Registers strategy entry signals. Must be a top-level call.

Mandatory parameters

index
Unique trade identifier.
logic
Boolean condition that triggers the entry.
timeframe
Strategy execution timeframe.
script
Instrument to trade.
action
Trade direction (Buy or Sell).
order_type
Order execution method ("MARKET" or "LIMIT").

Optional parameters

limit_price
Required only for LIMIT orders. Specifies the exact entry price.

Parameter example

Entry(
    index=1,
    logic=buy_signal,
    timeframe=15,
    script=RELIANCE,
    action=Buy,
    order_type="MARKET"
)

Example

# Keyword vars for action, string for order_type
Entry(index=1, logic=long_condition, timeframe=5, script=banknifty_spot, action=Buy, order_type="MARKET")

EMA Crossover

Entry triggers when EMA 9 crosses above EMA 21 — a classic trend-following setup.

# Fast EMA for short-term trend
fast_ema = EMA(close, 9)
# Slow EMA for overall trend direction
slow_ema = EMA(close, 21)
# Entry triggers when EMA 9 crosses above EMA 21
long_condition = crossover(fast_ema, slow_ema)
# Register a BUY entry order
Entry(
    index=1,
    logic=long_condition,
    timeframe=15,
    script=RELIANCE,
    action=Buy,
    order_type="MARKET"
)

RSI Momentum

Entry triggers when RSI rises above 60, indicating bullish momentum.

# Calculate RSI using a 14-period lookback
rsi_val = RSI(close, 14)
# Entry triggers when RSI moves above 60,
# indicating strong bullish momentum
long_condition = rsi_val > 60
# Register a BUY entry order
Entry(
    index=1,
    logic=long_condition,
    timeframe=15,
    script=RELIANCE,
    action=Buy,
    order_type="MARKET"
)

Trend + Momentum

Combines EMA 20/50 for trend direction and RSI 14 for momentum confirmation. Both must agree before entry.

# EMA 20 is used to identify the short-to-medium term trend
ema20 = EMA(close, 20)
# EMA 50 is used to identify the overall market direction
ema50 = EMA(close, 50)
# RSI measures bullish and bearish momentum
rsi_val = RSI(close, 14)
# Entry triggers only when:
# - EMA 20 is above EMA 50 (Bullish Trend)
# - RSI is above 55 (Bullish Momentum)
long_condition = ema20 > ema50 and rsi_val > 55
# Register a BUY entry order
Entry(
    index=1,
    logic=long_condition,
    timeframe=15,
    script=RELIANCE,
    action=Buy,
    order_type="MARKET"
)

Exit #

(index, logic, exit_price, exit_type)

Registers strategy exit signals. Must be a top-level call.

Mandatory parameters

index
Trade identifier to close.
logic
Boolean condition that triggers the exit.
exit_type
Exit execution method ("MARKET" or "LIMIT").

Optional parameters

exit_price
Required only for LIMIT exits. Specifies the exact exit price.

Parameter example

Exit(
    index=1,
    logic=exit_signal,
    exit_type="MARKET"
)

Example

# Top-level exit call
Exit(index=1, logic=exit_signal, exit_type="MARKET")

EMA Crossover

Exit triggers when EMA 9 crosses below EMA 21, signalling the trend has reversed.

# Fast EMA for short-term trend
fast_ema = EMA(close, 9)
# Slow EMA for overall trend direction
slow_ema = EMA(close, 21)
# Exit triggers when EMA 9 crosses below EMA 21
long_exit = crossunder(fast_ema, slow_ema)
# Close the open position
Exit(
    index=1,
    logic=long_exit,
    exit_type="MARKET"
)

RSI Momentum

Exit triggers when RSI falls below 50, indicating momentum is weakening.

# Calculate RSI using a 14-period lookback
rsi_val = RSI(close, 14)
# Exit triggers when RSI falls below 50,
# indicating momentum is weakening
long_exit = rsi_val < 50
# Close the open position
Exit(
    index=1,
    logic=long_exit,
    exit_type="MARKET"
)

Trend + Momentum

Exits when either EMA 20 falls below EMA 50 OR RSI drops below 45 — whichever weakens first.

# EMA 20 is used to monitor trend strength
ema20 = EMA(close, 20)
# EMA 50 is used as the trend filter
ema50 = EMA(close, 50)
# RSI is used to monitor momentum weakness
rsi_val = RSI(close, 14)
# Exit triggers when:
# - EMA 20 falls below EMA 50 (Trend Reversal)
# OR
# - RSI falls below 45 (Momentum Weakness)
long_exit = ema20 < ema50 or rsi_val < 45
# Close the open position
Exit(
    index=1,
    logic=long_exit,
    exit_type="MARKET"
)

Common mistakes and documentation limits

  • Check a function name, capitalization and argument list against this reference before generating code. A grouped reference entry may describe multiple names.
  • Resolve missing names using the built-in variable reference or the strategy setup. Instrument names such as RELIANCE and banknifty_spot are example context, not user-defined strings.
  • The source examples are inconsistent about Entry timeframe: the introductory project uses "5m", while library examples use numbers such as 5 and 15. Confirm the accepted format in your installed Cipher AI runtime; this page does not silently choose a format.
  • Some snippets are partial demonstrations with inputs defined elsewhere. Do not present every snippet as a standalone program.
  • The current website reference does not provide a compiler error-code catalog or a version compatibility matrix. Do not invent error codes or claim examples were executed merely because they appear here.