backtest
marketcalls/vectorbt-backtesting-skills
Generate complete VectorBT backtesting scripts with data fetch, signals, stats, and plots for trading strategies.
What is backtest?
Backtest creates a full Python script that fetches historical data, applies a trading strategy, runs a vectorized backtest, and generates performance statistics and plots. Use it to validate strategy ideas on Indian equities and futures with realistic fees and benchmark comparison.
- Generates complete .py backtest scripts from strategy templates (EMA, RSI, MACD, Supertrend, Donchian, etc.)
- Fetches data via OpenAlgo API or loads from DuckDB for offline analysis
- Applies TA-Lib and OpenAlgo indicators with automatic signal cleaning via exrem()
- Runs vectorized portfolio backtest with Indian delivery/futures fees and lot-size constraints
- Compares strategy performance vs NIFTY benchmark (Total Return, Sharpe, Sortino, Max DD, Win Rate)
- Generates Plotly equity curves, drawdown plots, QuantStats tearsheets, and CSV trade exports
How to install backtest
npx skills add https://github.com/marketcalls/vectorbt-backtesting-skills --skill backtest- VectorBT library installed
- TA-Lib installed for technical indicators
- OpenAlgo API credentials in .env (or DuckDB file path for offline backtesting)
- Python 3.7+
How to use backtest
- 1.Call the skill with strategy name, symbol, exchange, and interval (e.g., `/backtest ema-crossover RELIANCE NSE D`)
- 2.The skill generates a .py script in `backtesting/{strategy_name}/` directory
- 3.Run the generated script: `python backtesting/{strategy_name}/{symbol}_{strategy}_backtest.py`
- 4.Review the printed stats table comparing strategy vs NIFTY benchmark
- 5.Check the generated Plotly HTML plot and QuantStats tearsheet for detailed analysis
- 6.Export trades CSV for further review or optimization
Use cases
- Validate an EMA crossover strategy on RELIANCE equity before live trading
- Backtest a Supertrend strategy on NIFTY futures with correct lot sizing (65 contracts minimum)
- Compare RSI strategy performance across multiple timeframes (daily, hourly, 5-minute)
- Export backtest trades to CSV for manual review and optimization
- Generate benchmark comparison to see if strategy beats NIFTY 50 on risk-adjusted returns
- Retail traders testing strategy ideas on Indian markets
- Quantitative analysts validating trading signals before deployment
- Day traders evaluating intraday strategies on futures
- Portfolio managers comparing strategy performance to benchmarks
backtest FAQ
OpenAlgo API (default, requires .env credentials) or DuckDB files (offline). Auto-detects Historify format (market_data table) or custom format (ohlcv table).
Equity delivery: 0.111% + ₹20 fixed. Futures (NIFTY/BANKNIFTY): 0.018% + ₹20 fixed. Lot sizes enforced (NIFTY min 65, BANKNIFTY min 30).
Yes. Specify interval as 1h, 5m, 15m, etc. The script handles any timeframe supported by your data source.
EMA Crossover, RSI, Donchian, Supertrend, MACD, SDA2, Momentum, Dual Momentum, Buy & Hold, RSI Accumulation.
Fetches NIFTY 50 data (symbol=NIFTY, exchange=NSE_INDEX) and compares Total Return, Sharpe Ratio, Sortino Ratio, Max Drawdown, Win Rate, Trade Count, and Profit Factor.
Full instructions (SKILL.md)
Source of truth, from marketcalls/vectorbt-backtesting-skills.
name: backtest description: Quick backtest a strategy on a symbol. Creates a complete .py script with data fetch, signals, backtest, stats, and plots. argument-hint: "[strategy] [symbol] [exchange] [interval]" allowed-tools: Read, Write, Edit, Bash, Glob, Grep
Create 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 - If user provides a DuckDB path, load data directly via
duckdb.connect(path, read_only=True)instead of OpenAlgo API. Auto-detect format: Historify (market_datatable, epoch timestamps) vs custom (ohlcvtable, date+time). See vectorbt-expertrules/duckdb-data.md. - If
openalgo.tais not importable (standalone DuckDB), use inlineexrem()fallback. - 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
Related skills
More from marketcalls/vectorbt-backtesting-skills and the wider catalog.

optimize
Optimize strategy parameters using VectorBT with heatmap visualization and benchmark comparison.

quick-stats
Quickly fetch data and print key backtest stats for a symbol with a default EMA crossover strategy. No file creation needed - runs inline in a notebook cell or prints to console.

setup
Set up the Python backtesting environment. Detects OS, creates virtual environment, installs dependencies (openalgo, ta-lib, vectorbt, plotly), and creates the backtesting folder structure.

strategy-compare
Compare multiple trading strategies on the same symbol with side-by-side performance metrics and equity curve visualization.

vectorbt-expert
VectorBT backtesting expert for strategy simulation, signal generation, and portfolio analysis.

claude-brainrot
Always-on meme dropper that fires image+sound combos and sound-only hits automatically on every user message.