backtest
Scannednpx machina-cli add skill marketcalls/vectorbt-backtesting-skills/backtest --openclawCreate a complete VectorBT backtest script for the user.
Arguments
Parse $ARGUMENTS as: strategy symbol exchange interval
$0= strategy name (e.g., ema-crossover, rsi, donchian, supertrend, macd, sda2, momentum)$1= symbol (e.g., SBIN, RELIANCE, NIFTY). Default: SBIN$2= exchange (e.g., NSE, NFO). Default: NSE$3= interval (e.g., D, 1h, 5m). Default: D
If no arguments, ask the user which strategy they want.
Instructions
- Read the vectorbt-expert skill rules for reference patterns
- Create
backtesting/{strategy_name}/directory if it doesn't exist (on-demand) - Create a
.pyfile inbacktesting/{strategy_name}/named{symbol}_{strategy}_backtest.py - Use the matching template from
rules/assets/{strategy}/backtest.pyas the starting point - The script must:
- Load
.envfrom the project root usingfind_dotenv()(walks up from script dir automatically) - Fetch data via
client.history()from OpenAlgo - Use TA-Lib for ALL indicators (EMA, SMA, RSI, MACD, BBands, ATR, ADX, STDDEV, MOM)
- Use OpenAlgo ta for specialty indicators (Supertrend, Donchian, Ichimoku, HMA, KAMA, ALMA)
- Use
ta.exrem()to clean duplicate signals (always.fillna(False)before exrem) - Run
vbt.Portfolio.from_signals()withmin_size=1, size_granularity=1 - Indian delivery fees:
fees=0.00111, fixed_fees=20for delivery equity - Fetch NIFTY benchmark via OpenAlgo (
symbol="NIFTY", exchange="NSE_INDEX") - Print full
pf.stats() - Print Strategy vs Benchmark comparison table (Total Return, Sharpe, Sortino, Max DD, Win Rate, Trades, Profit Factor)
- Explain the backtest report in plain language for normal traders
- Generate QuantStats HTML tearsheet if
quantstatsis available - Plot equity curve + drawdown using Plotly (
template="plotly_dark") - Export trades to CSV
- Load
- Never use icons/emojis in code or logger output
- For futures symbols (NIFTY, BANKNIFTY), use lot-size-aware sizing:
- NIFTY:
min_size=65, size_granularity=65(effective 31 Dec 2025) - BANKNIFTY:
min_size=30, size_granularity=30 - Use
fees=0.00018, fixed_fees=20for F&O futures
- NIFTY:
Available Strategies
| Strategy | Keyword | Template |
|---|---|---|
| EMA Crossover | ema-crossover | assets/ema_crossover/backtest.py |
| RSI | rsi | assets/rsi/backtest.py |
| Donchian Channel | donchian | assets/donchian/backtest.py |
| Supertrend | supertrend | assets/supertrend/backtest.py |
| MACD Breakout | macd | assets/macd/backtest.py |
| SDA2 | sda2 | assets/sda2/backtest.py |
| Momentum | momentum | assets/momentum/backtest.py |
| Dual Momentum | dual-momentum | assets/dual_momentum/backtest.py |
| Buy & Hold | buy-hold | assets/buy_hold/backtest.py |
| RSI Accumulation | rsi-accumulation | assets/rsi_accumulation/backtest.py |
Benchmark Rules
- Default: NIFTY 50 via OpenAlgo (
symbol="NIFTY", exchange="NSE_INDEX") - If user specifies a different benchmark, use that instead
- For yfinance: use
^NSEIfor India,^GSPC(S&P 500) for US markets - Always compare: Total Return, Sharpe, Sortino, Max Drawdown
Example Usage
/backtest ema-crossover RELIANCE NSE D
/backtest rsi SBIN
/backtest supertrend NIFTY NFO 5m
Source
git clone https://github.com/marketcalls/vectorbt-backtesting-skills/blob/master/.claude/skills/backtest/SKILL.mdView on GitHub Overview
This skill generates a complete VectorBT backtest script for a chosen strategy and symbol. It automates data fetching from OpenAlgo, indicator calculations with TA-Lib, signal generation, and portfolio execution, plus full reporting. It outputs plots, a trade CSV, and a benchmark comparison to help traders evaluate performance.
How This Skill Works
You specify strategy, symbol, exchange, and interval. The system creates backtesting/{strategy_name}/ and a file named {symbol}_{strategy}_backtest.py using the matching template assets/{strategy}/backtest.py. The script loads environment variables with find_dotenv, fetches data via OpenAlgo client.history, computes indicators with TA-Lib and specialty indicators with OpenAlgo ta, cleans duplicates with ta.exrem() after filling NA with False, and runs vbt.Portfolio.from_signals(min_size=1, size_granularity=1). It prints pf.stats(), shows a Strategy vs Benchmark table, plots the equity curve and drawdown with Plotly (template plotly_dark), exports trades to CSV, and can generate a QuantStats HTML tearsheet if available.
When to Use It
- You want an end-to-end backtest for a specific strategy on a symbol with full data fetch, signals, stats, and plots.
- You need a ready-to-run benchmark comparison against NIFTY (or a custom benchmark) to gauge relative performance.
- You are testing futures (NIFTY or BANKNIFTY) and require lot-size-aware sizing and futures fees settings.
- You want to export the generated trades to CSV for further review or compliance workflows.
- You want an optional QuantStats tearsheet and a clear explanation of the backtest report for non-technical readers.
Quick Start
- Step 1: Pick a strategy and inputs, e.g., /backtest ema-crossover RELIANCE NSE D
- Step 2: The tool creates backtesting/{strategy}/ and {symbol}_{strategy}_backtest.py using the matching template
- Step 3: Run the script to fetch data, run the backtest, view pf.stats, compare to benchmark, plot results, and export trades
Best Practices
- Choose a well-defined strategy in the available list (e.g., ema-crossover, rsi, donchian) and start with a single symbol to validate the script.
- Ensure indicators are computed with TA-Lib for all standard indicators and OpenAlgo ta for specialty indicators, then apply ta.exrem() with a preceding fillna(False).
- Respect Indian delivery and futures sizing rules (fees and fixed_fees) and apply NIFTY or BANKNIFTY sizing as specified.
- Always review the Strategy vs Benchmark table and the plain-language backtest explanation to translate results into actionable insights.
- If quantstats is installed, generate a tearsheet; otherwise rely on pf.stats() and Plotly plots for visualization.
Example Use Cases
- /backtest ema-crossover RELIANCE NSE D
- /backtest rsi SBIN NSE D
- /backtest supertrend NIFTY NFO 5m
- /backtest donchian RELIANCE NSE D
- /backtest macd SBIN NSE 1h