Get the FREE Ultimate OpenClaw Setup Guide →

backtest

Scanned
npx machina-cli add skill marketcalls/vectorbt-backtesting-skills/backtest --openclaw
Files (1)
SKILL.md
3.5 KB

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

  1. Read the vectorbt-expert skill rules for reference patterns
  2. Create backtesting/{strategy_name}/ directory if it doesn't exist (on-demand)
  3. Create a .py file in backtesting/{strategy_name}/ named {symbol}_{strategy}_backtest.py
  4. Use the matching template from rules/assets/{strategy}/backtest.py as the starting point
  5. The script must:
    • Load .env from the project root using find_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() with min_size=1, size_granularity=1
    • Indian delivery fees: fees=0.00111, fixed_fees=20 for 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 quantstats is available
    • Plot equity curve + drawdown using Plotly (template="plotly_dark")
    • Export trades to CSV
  6. Never use icons/emojis in code or logger output
  7. 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=20 for F&O futures

Available Strategies

StrategyKeywordTemplate
EMA Crossoverema-crossoverassets/ema_crossover/backtest.py
RSIrsiassets/rsi/backtest.py
Donchian Channeldonchianassets/donchian/backtest.py
Supertrendsupertrendassets/supertrend/backtest.py
MACD Breakoutmacdassets/macd/backtest.py
SDA2sda2assets/sda2/backtest.py
Momentummomentumassets/momentum/backtest.py
Dual Momentumdual-momentumassets/dual_momentum/backtest.py
Buy & Holdbuy-holdassets/buy_hold/backtest.py
RSI Accumulationrsi-accumulationassets/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 ^NSEI for 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

  1. Step 1: Pick a strategy and inputs, e.g., /backtest ema-crossover RELIANCE NSE D
  2. Step 2: The tool creates backtesting/{strategy}/ and {symbol}_{strategy}_backtest.py using the matching template
  3. 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

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers