Skip to main content

DSL Grammar

Overview​

Vaanam DSL (Domain Specific Language) is a powerful query language designed for financial data analysis, backtesting, and screening. It provides a natural way to express complex trading conditions using technical indicators, price data, and logical operations.

The Vaanam DSL is a declarative language for expressing market conditions, not a programming language.

Expression Types​

The DSL supports two main expression types:

  • Logical Expressions: Boolean conditions using AND/OR operators
  • Mathematical Expressions: Arithmetic operations and comparisons

Operators​

Logical Operators​

  • AND - Logical AND operation
  • OR - Logical OR operation

Comparison Operators​

  • < - Less than
  • > - Greater than
  • <= - Less than or equal to
  • >= - Greater than or equal to
  • == - Equal to
  • != - Not equal to

Arithmetic Operators​

  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division
  • % - Modulo

Grouping​

  • () - Parentheses for grouping expressions

Data Types​

Numbers​

  • Integer: 14, 20, 100
  • Decimal: 0.02, 2.5, 30.0

Identifiers​

Technical indicators, price data, and special variables follow specific naming conventions.

Asset Data Structure​

All data references follow the pattern: ASSET_[SYMBOL]_[CATEGORY]_[INDICATOR]_[PARAMETERS]

Asset Categories​

Basic Price Data​

  • ASSET_STOCK_OPEN - Opening price
  • ASSET_STOCK_HIGH - High price
  • ASSET_STOCK_LOW - Low price
  • ASSET_STOCK_CLOSE - Closing price
  • ASSET_STOCK_VOLUME - Trading volume

Technical Indicators​

Format: ASSET_STOCK_TECH_[INDICATOR]_[PARAMETERS]

Supported Technical Indicators​

Moving Averages
  • SMA (Simple Moving Average)

    • Format: ASSET_STOCK_TECH_SMA_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_SMA_C_20 (20-period SMA of closing price)
  • EMA (Exponential Moving Average)

    • Format: ASSET_STOCK_TECH_EMA_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_EMA_C_50 (50-period EMA of closing price)
  • WMA (Weighted Moving Average)

    • Format: ASSET_STOCK_TECH_WMA_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_WMA_C_10 (10-period WMA of closing price)
  • HMA (Hull Moving Average)

    • Format: ASSET_STOCK_TECH_HMA_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_HMA_C_20 (20-period HMA of closing price)
  • DEMA (Double Exponential Moving Average)

    • Format: ASSET_STOCK_TECH_DEMA_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_DEMA_C_20 (20-period DEMA of closing price)
  • TEMA (Triple Exponential Moving Average)

    • Format: ASSET_STOCK_TECH_TEMA_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_TEMA_C_20 (20-period TEMA of closing price)
  • T3 (T3 Moving Average)

    • Format: ASSET_STOCK_TECH_T3_[PRICE_TYPE]_[PERIOD]_[VFACTOR]
    • Example: ASSET_STOCK_TECH_T3_C_20_0.7 (20-period T3 with volume factor 0.7)
    • The vfactor parameter controls smoothing (default 0.7)
Oscillators
  • RSI (Relative Strength Index)

    • Format: ASSET_STOCK_TECH_RSI_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_RSI_C_14 (14-period RSI)
  • STOCH (Stochastic Oscillator)

    • Format: ASSET_STOCK_TECH_STOCH_[FASTK]_[SLOWK]_[SLOWD]_[LINE]
    • Example: ASSET_STOCK_TECH_STOCH_14_3_3_K (Stochastic %K line)
    • Lines: K or D
  • CCI (Commodity Channel Index)

    • Format: ASSET_STOCK_TECH_CCI_[PERIOD]
    • Example: ASSET_STOCK_TECH_CCI_20
  • MFI (Money Flow Index)

    • Format: ASSET_STOCK_TECH_MFI_[PERIOD]
    • Example: ASSET_STOCK_TECH_MFI_14
Volatility Indicators
  • ATR (Average True Range)

    • Format: ASSET_STOCK_TECH_ATR_[PERIOD]
    • Example: ASSET_STOCK_TECH_ATR_14
  • BBANDS (Bollinger Bands)

    • Format: ASSET_STOCK_TECH_BBANDS_[PERIOD]_[DEVIATION]_[BAND]
    • Example: ASSET_STOCK_TECH_BBANDS_20_2.0_UPPER
    • Bands: UPPER, MIDDLE, LOWER
  • STDEV (Standard Deviation)

    • Format: ASSET_STOCK_TECH_STDEV_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_STDEV_C_20
Trend Indicators
  • ADX (Average Directional Index)

    • Format: ASSET_STOCK_TECH_ADX_[PERIOD]
    • Example: ASSET_STOCK_TECH_ADX_14
  • ADXR (ADX Rating)

    • Format: ASSET_STOCK_TECH_ADXR_[PERIOD]
    • Example: ASSET_STOCK_TECH_ADXR_14
  • AROON (Aroon Indicator)

    • Format: ASSET_STOCK_TECH_AROON_[PERIOD]_[LINE]
    • Example: ASSET_STOCK_TECH_AROON_14_UP
    • Lines: UP or DOWN
  • SAR (Parabolic SAR)

    • Format: ASSET_STOCK_TECH_SAR_[ACCELERATION]_[MAXIMUM]
    • Example: ASSET_STOCK_TECH_SAR_0.02_0.2
MACD (Moving Average Convergence Divergence)
  • Format: ASSET_STOCK_TECH_MACD_[FAST]_[SLOW]_[SIGNAL]_[LINE]
  • Example: ASSET_STOCK_TECH_MACD_12_26_9_MACD
  • Lines: MACD, SIGNAL, HIST
Volume Indicators
  • OBV (On-Balance Volume)

    • Format: ASSET_STOCK_TECH_OBV_[PERIOD]
    • Example: ASSET_STOCK_TECH_OBV_10
  • ADOSC (Chaikin A/D Oscillator)

    • Format: ASSET_STOCK_TECH_ADOSC_[FAST]_[SLOW]
    • Example: ASSET_STOCK_TECH_ADOSC_3_10
Other Indicators
  • ROC (Rate of Change)

    • Format: ASSET_STOCK_TECH_ROC_[PRICE_TYPE]_[PERIOD]
    • Example: ASSET_STOCK_TECH_ROC_C_10
  • BOP (Balance of Power)

    • Format: ASSET_STOCK_TECH_BOP
    • Example: ASSET_STOCK_TECH_BOP (no parameters required)
  • PIVOTHIGH (Pivot High Detection)

    • Format: ASSET_STOCK_TECH_PIVOTHIGH_[LEFT_BARS]_[RIGHT_BARS]
    • Example: ASSET_STOCK_TECH_PIVOTHIGH_5_2 (5 bars left, 2 bars right)
    • Returns the high price at pivot points, NaN otherwise
  • PIVOTLOW (Pivot Low Detection)

    • Format: ASSET_STOCK_TECH_PIVOTLOW_[LEFT_BARS]_[RIGHT_BARS]
    • Example: ASSET_STOCK_TECH_PIVOTLOW_5_2 (5 bars left, 2 bars right)
    • Returns the low price at pivot points, NaN otherwise
  • VCEIL (Ceiling Function)

    • Format: ASSET_STOCK_TECH_VCEIL_[PERIOD]
    • Example: ASSET_STOCK_TECH_VCEIL_14
  • DONCHIAN (Donchian Channels)

    • Format: ASSET_STOCK_TECH_DONCHIAN_[PERIOD]_[BAND]
    • Example: ASSET_STOCK_TECH_DONCHIAN_20_UPPER
    • Bands: UPPER, MIDDLE, LOWER

Price Type Codes​

  • O - Open price
  • H - High price
  • L - Low price
  • C - Close price
  • V - Volume

Historical Data Modifiers​

Previous Values​

  • PREV_[N]_ - Access data from N periods ago
    • Example: PREV_1_ASSET_STOCK_CLOSE (Previous day's closing price)
    • Example: PREV_5_ASSET_STOCK_TECH_RSI_C_14 (RSI from 5 periods ago)

Statistical Functions​

  • HIGH_[N]_ - Highest value over N periods

    • Example: HIGH_20_ASSET_STOCK_CLOSE (Highest close in last 20 periods)
    • Example: HIGH_252_ASSET_STOCK_CLOSE (52-week high on daily timeframe)
  • LOW_[N]_ - Lowest value over N periods

    • Example: LOW_20_ASSET_STOCK_CLOSE (Lowest close in last 20 periods)
  • AVG_[N]_ - Average value over N periods

    • Example: AVG_10_ASSET_STOCK_VOLUME (Average volume over 10 periods)
    • Example: AVG_20_ASSET_STOCK_VOLUME (20-day average volume)

Consecutive Condition​

  • CONSECUTIVE_[N]_ - Requires a condition to be true for N consecutive bars
    • Example: CONSECUTIVE_3_ASSET_STOCK_TECH_RSI_C_14 < 30 (RSI has been below 30 for 3 bars in a row)
    • Example: CONSECUTIVE_5_ASSET_STOCK_CLOSE > ASSET_STOCK_TECH_SMA_C_50 (Price above 50-SMA for 5 consecutive bars)

Intraday Reference​

  • INTRA_[N]_ - References the Nth intraday candle
    • Example: INTRA_1_ASSET_STOCK_CLOSE (Close price of the first intraday candle)
    • Only meaningful on intraday timeframes (Pro plan required)

Today's Session High / Low​

Reference the session's running high or low up to the current bar. Useful for Opening Range Breakout (ORB) strategies.

  • TODAY_HIGH_[OPERAND] — running max of the operand for the current day
    • Example: ASSET_STOCK_CLOSE > TODAY_HIGH_ASSET_STOCK_HIGH (breakout above today's highest high)
  • TODAY_LOW_[OPERAND] — running min of the operand for the current day
    • Example: ASSET_STOCK_CLOSE < TODAY_LOW_ASSET_STOCK_LOW (breakdown below today's lowest low)

Once the regular market session starts (9:30 AM ET), pre-market bars are excluded from the calculation.

Entry-Time References​

  • ENTRY_ - Captures the value of any indicator at the time a trade was opened

    • Example: ENTRY_ASSET_STOCK_TECH_ATR_14 (ATR value when the trade was entered)
    • Use case: exit when ATR has changed significantly since entry
  • ENTRY_PREV_[N]_ - Value of an indicator N candles before the trade entry

    • Example: ENTRY_PREV_3_ASSET_STOCK_LOW (Low price 3 candles before entry)

Chart Pattern Indicators​

Pattern indicators return numeric codes and can be used in DSL expressions like any other indicator.

PatternFormatReturn Codes
TRIANGLEASSET_STOCK_TECH_TRIANGLE_[BARS]0=none, 1=ascending, 2=descending, 3=symmetric
HNS (Head & Shoulders)ASSET_STOCK_TECH_HNS_[BARS]0=none, 1=bearish, 2=inverse/bullish
DOUBLETOPASSET_STOCK_TECH_DOUBLETOP_[BARS]0=none, 1=detected
DOUBLEBOTASSET_STOCK_TECH_DOUBLEBOT_[BARS]0=none, 1=detected
VCPASSET_STOCK_TECH_VCP_[BARS]0=none, 1=bullish, 2=bearish
STAIRCASEASSET_STOCK_TECH_STAIRCASE_[BARS]0=none, 1=bullish staircase
POWERBASEASSET_STOCK_TECH_POWERBASE_[BARS]0=none, 1=bullish powerbase

The [BARS] parameter is the pivot lookback window (e.g., 6, 8, 10).

Example usage:

ASSET_STOCK_TECH_TRIANGLE_6 == 1

Ascending triangle detected with 6-bar pivot lookback

ASSET_STOCK_TECH_HNS_8 == 2 AND ASSET_STOCK_TECH_RSI_C_14 < 40

Inverse head & shoulders with RSI confirmation

Special Variables​

Trading Context Variables​

  • BUY_PRICE - Entry price of the current open long position (exit queries only)
  • SELL_PRICE - Entry price of the current open short position (exit queries only)

Important: BUY_PRICE and SELL_PRICE are only valid in exit conditions. Using them in entry queries will cause an error, since no position is open yet at the time of entry evaluation.

Expression Examples​

Simple Conditions​

ASSET_STOCK_TECH_RSI_C_14 < 30

RSI below 30 (oversold condition)

ASSET_STOCK_CLOSE > ASSET_STOCK_TECH_SMA_C_20

Price above 20-period moving average

Complex Logical Expressions​

ASSET_STOCK_TECH_RSI_C_14 < 30 AND ASSET_STOCK_VOLUME > AVG_10_ASSET_STOCK_VOLUME

RSI oversold AND volume above 10-day average

(ASSET_STOCK_TECH_MACD_12_26_9_MACD > ASSET_STOCK_TECH_MACD_12_26_9_SIGNAL) OR ASSET_STOCK_TECH_STOCH_14_3_3_K > 80

MACD bullish crossover OR Stochastic overbought

Mathematical Expressions​

ASSET_STOCK_CLOSE / ASSET_STOCK_TECH_SMA_C_50 > 1.05

Price 5% above 50-day moving average

(ASSET_STOCK_HIGH - ASSET_STOCK_LOW) / ASSET_STOCK_CLOSE > 0.02

Daily range greater than 2% of closing price

Advanced Patterns​

ASSET_STOCK_TECH_RSI_C_14 < 30 AND PREV_1_ASSET_STOCK_TECH_RSI_C_14 >= 30

RSI just crossed below 30

ASSET_STOCK_CLOSE > HIGH_20_ASSET_STOCK_CLOSE * 0.98

Price within 2% of 20-day high

Bollinger Bands Strategy​

ASSET_STOCK_CLOSE < ASSET_STOCK_TECH_BBANDS_20_2.0_LOWER AND ASSET_STOCK_TECH_RSI_C_14 < 30

Price below lower Bollinger Band AND RSI oversold

Moving Average Crossover​

ASSET_STOCK_TECH_SMA_C_10 > ASSET_STOCK_TECH_SMA_C_20 AND PREV_1_ASSET_STOCK_TECH_SMA_C_10 <= PREV_1_ASSET_STOCK_TECH_SMA_C_20

10-day SMA crosses above 20-day SMA

Usage in Trading Strategies​

Entry Conditions​

Define when to enter a position:

ASSET_STOCK_TECH_RSI_C_14 < 30 AND ASSET_STOCK_CLOSE > ASSET_STOCK_TECH_SMA_C_50

Exit Conditions​

Define when to exit a position:

ASSET_STOCK_TECH_RSI_C_14 > 70 OR ASSET_STOCK_CLOSE < ASSET_STOCK_TECH_SMA_C_20

Price Execution​

Specify execution prices using basic price data:

  • Entry: ASSET_STOCK_OPEN
  • Exit: ASSET_STOCK_CLOSE

Usage in Stock Screening​

Filter stocks based on criteria:

ASSET_STOCK_TECH_RSI_C_14 < 30 AND ASSET_STOCK_VOLUME > AVG_20_ASSET_STOCK_VOLUME * 1.5

Best Practices​

Performance Optimization​

  1. Use specific periods: Avoid very long lookback periods unless necessary
  2. Combine conditions efficiently: Group related conditions with AND/OR appropriately
  3. Limit complexity: Break complex expressions into simpler components

Common Patterns​

  1. Trend Following: Combine price and moving average conditions
  2. Mean Reversion: Use oscillators like RSI and Bollinger Bands
  3. Momentum: Incorporate volume and rate of change indicators
  4. Confirmation: Use multiple indicators to confirm signals

Tips for Writing Expressions​

  • Ensure sufficient historical data for indicator calculations
  • Use appropriate parameter ranges (e.g., RSI periods > 0)
  • Group complex conditions with parentheses for clarity
  • Test expressions with different market conditions

Important Notes​

  1. Data Requirements: Indicators need sufficient historical data to calculate properly
  2. Calculation Periods: Some indicators require warm-up periods before producing valid values
  3. Market Focus: Currently optimized for equity markets and standard technical indicators

© 2026 Vaanam, Inc. All rights reserved.