Validate RealTest .rts script files using the RealTest Lark grammar.
validate_rts.py- Standalone validator scriptrealtest.lark- Complete RealTest language grammarexample_strategy.rts- Sample RealTest script for testingREADME.md- This file
Python 3.7+ with one package:
pip install larkThat's it! No other dependencies.
python validate_rts.py --file example_strategy.rtspython validate_rts.py --samples path/to/scripts/python validate_rts.py --file script.rts --grammar path/to/realtest.larkpython validate_rts.py [OPTIONS]
Options:
--file FILE Validate specific .rts file
--grammar GRAMMAR Path to grammar file (default: realtest.lark)
--samples SAMPLES Path to samples directory (default: samples)
Validate single script:
python validate_rts.py --file my_strategy.rts
# Output:
# [OK] Grammar loaded successfully
# Found 1 file to validate: my_strategy.rts
# [ 1/1] my_strategy.rts [PASS]
# [SUCCESS] All files parsed successfully!Validate directory:
python validate_rts.py --samples my_strategies/
# Output:
# [OK] Grammar loaded successfully
# Found 25 files to validate
# [ 1/25] strategy1.rts [PASS]
# [ 2/25] strategy2.rts [PASS]
# ...
# Total: 25, Successful: 23 (92.0%), Failed: 2 (8.0%)With custom grammar:
python validate_rts.py \
--file script.rts \
--grammar /path/to/my_realtest.larkThe validator checks that your RealTest script:
- ✅ Has valid syntax according to the grammar
- ✅ All sections (Import, Settings, Data, Strategy, etc.) are properly formatted
- ✅ Expressions, identifiers, and operators follow RealTest rules
- ✅ Comments are correctly placed
- ✅ String literals, numbers, and symbols are valid
This is grammar validation only. It doesn't check:
- ❌ Whether variable names conflict with functions (use semantic_validator.py)
- ❌ Whether data files exist
- ❌ Whether strategies make logical sense
- ❌ Whether parameters are in reasonable ranges
0- All files validated successfully1- One or more files failed validation or error occurred
Use in CI/CD pipelines:
# Validate before commit
python validate_rts.py --file strategies/my_strategy.rts || exit 1
# Validate all strategies
python validate_rts.py --samples strategies/ || exit 1The included realtest.lark grammar defines the complete RealTest Script Language including:
- Sections: Import, Settings, Data, Strategy, Parameters, etc.
- Expressions: Arithmetic, logical, comparisons, function calls
- Identifiers: Variables, symbols ($SPY), watchlists (&MyList), parameters (?pos)
- Literals: Numbers, strings, dates
- Operators: +, -, *, /, ^, and, or, not, >, <, =, etc.
- Functions: MA(), RSI(), ATR(), Extern(), etc.
- Comments: // single-line and /* multi-line */
If you have a .rts file that works correctly in RealTest but fails validation with this tool, please help us improve the grammar!
When reporting, please include:
- ✅ Your complete
.rtsfile (or relevant sections) - ✅ The full error message from the validator
- ✅ Confirmation that it works in RealTest (and which version)
Quick Tip: You can run the validator with --file to get detailed error output for a single file:
python validate_rts.py --file your_strategy.rtsWant to suggest a grammar improvement or report another type of issue?
Ensure realtest.lark is in the same directory as validate_rts.py, or use:
python validate_rts.py --grammar path/to/realtest.larkInstall Lark:
pip install larkIf validation fails, the error message shows:
- Line and column number
- What was expected vs what was found
- Context around the error
Example:
Error: Unexpected token Token('NEWLINE', '\n') at line 23, column 5
Expected one of: COLON
Fix the syntax error at the indicated line and re-run.
If you believe the error is incorrect (i.e., your file works in RealTest), please report it as a validation error!
from validate_rts import load_grammar, validate_file
from pathlib import Path
# Load grammar once
parser = load_grammar('realtest.lark')
# Validate files
script = Path('my_strategy.rts')
success, error = validate_file(parser, script)
if success:
print("Valid!")
else:
print(f"Invalid: {error}")from pathlib import Path
from validate_rts import load_grammar, validate_file
parser = load_grammar('realtest.lark')
for script in Path('strategies').glob('*.rts'):
success, error = validate_file(parser, script)
if not success:
print(f"FAIL: {script.name} - {error}")RealTest is a portfolio-level backtesting system for trading strategies. Learn more at the RealTest forum and documentation.
This validator is provided as-is for use with RealTest scripts. The grammar represents the RealTest Script Language syntax.
Compatible with RealTest 2024+ syntax.
Last updated: November 2025