optimize
Scannednpx machina-cli add skill marketcalls/vectorbt-backtesting-skills/optimize --openclawCreate a parameter optimization script for a VectorBT strategy.
Arguments
Parse $ARGUMENTS as: strategy symbol exchange interval
$0= strategy name (e.g., ema-crossover, rsi, donchian). Default: ema-crossover$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 to optimize.
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}_optimize.py - The script must:
- Load
.envfrom project root usingfind_dotenv()and fetch data via OpenAlgoclient.history() - Use TA-Lib for ALL indicators (never VectorBT built-in)
- Use OpenAlgo ta for specialty indicators (Supertrend, Donchian, etc.)
- Use
ta.exrem()to clean signals (always.fillna(False)before exrem) - Define sensible parameter ranges for the chosen strategy
- Use loop-based optimization to collect multiple metrics per combo
- Track: total_return, sharpe_ratio, max_drawdown, trade_count for each combination
- Use
tqdmfor progress bars - Indian delivery fees:
fees=0.00111, fixed_fees=20for delivery equity - Find best parameters by total return AND by Sharpe ratio
- Print top 10 results for both criteria
- Generate Plotly heatmap of total return across parameter grid (
template="plotly_dark") - Generate Plotly heatmap of Sharpe ratio across parameter grid
- Fetch NIFTY benchmark and compare best parameters vs benchmark
- Print Strategy vs Benchmark comparison table
- Explain results in plain language for normal traders
- Save results to CSV
- Load
- Never use icons/emojis in code or logger output
- For futures symbols, use lot-size-aware sizing:
- NIFTY:
min_size=65, size_granularity=65 - BANKNIFTY:
min_size=30, size_granularity=30
- NIFTY:
Default Parameter Ranges
| Strategy | Parameter 1 | Parameter 2 |
|---|---|---|
| ema-crossover | fast EMA: 5-50 | slow EMA: 10-60 |
| rsi | window: 5-30 | oversold: 20-40 |
| donchian | period: 5-50 | - |
| supertrend | period: 5-30 | multiplier: 1.0-5.0 |
Example Usage
/optimize ema-crossover RELIANCE NSE D
/optimize rsi SBIN
Source
git clone https://github.com/marketcalls/vectorbt-backtesting-skills/blob/master/.claude/skills/optimize/SKILL.mdView on GitHub Overview
Creates a parameter optimization script for a VectorBT strategy. It tests parameter combinations, generates heatmaps, saves results, and benchmarks against NIFTY for trader-ready insights.
How This Skill Works
The script loads environment data via find_dotenv and fetches history with the OpenAlgo client. All indicators are computed with TA-Lib (OpenAlgo ta handles specialty indicators) and signals are cleaned with ta.exrem after fillna(False). It iterates over defined parameter grids per strategy, records total_return, sharpe_ratio, max_drawdown, and trade_count, and outputs top results plus Plotly heatmaps and a CSV file. It also fetches a NIFTY benchmark for comparison and prints a strategy vs benchmark table with plain language explanations.
When to Use It
- You want to optimize a strategy like ema-crossover, rsi, donchian, or supertrend on a specific symbol and interval
- You need a grid search that produces heatmaps for quick visual interpretation of parameter performance
- You want to compare optimized results against a NIFTY benchmark and view a strategy vs benchmark table
- You require a reproducible report with CSV export and plain-language explanations for traders
- You are evaluating futures with lot-size aware sizing using the specified min_size and size_granularity
Quick Start
- Step 1: Run the optimizer with a strategy and market, e.g., /optimize ema-crossover RELIANCE NSE D
- Step 2: Let the script run with the built-in progress bar and wait for top results to print
- Step 3: Review the top 10 results, heatmaps, and the CSV report; compare against NIFTY benchmark
Best Practices
- Start with the default parameter ranges per strategy before expanding
- Ensure all indicators come from TA-Lib as required by the skill
- Always apply fillna(False) before ta.exrem to clean signals
- Use tqdm progress bars and review the top 10 results by total_return and by Sharpe ratio
- Save results to CSV, generate Plotly heatmaps, and compare against the NIFTY benchmark
Example Use Cases
- optimize ema-crossover RELIANCE NSE D
- optimize rsi SBIN NSE D
- optimize donchian NIFTY NSE D
- optimize supertrend RELIANCE NSE 1h
- optimize ema-crossover BANKNIFTY NSE D