Create 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
.py file in backtesting/{strategy_name}/ named {symbol}_{strategy}_optimize.py
- The script must:
- Load
.env from project root using find_dotenv() and fetch data via OpenAlgo client.history()
- If user provides a DuckDB path, load data directly via
duckdb.connect(path, read_only=True). See vectorbt-expert rules/duckdb-data.md.
- If
openalgo.ta is not importable (standalone DuckDB), use inline exrem() fallback.
- 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
tqdm for progress bars
- Indian delivery fees:
fees=0.00111, fixed_fees=20 for 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
- 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
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
1---2name: optimize3description: Optimize strategy parameters using VectorBT. Tests parameter combinations and generates heatmaps.4argument-hint: "[strategy] [symbol] [exchange] [interval]"5allowed-tools: Read, Write, Edit, Bash, Glob, Grep6metadata:7 source: https://skillmd.com/skills/levalencia/optimize8---910Create a parameter optimization script for a VectorBT strategy.1112## Arguments1314Parse `$ARGUMENTS` as: strategy symbol exchange interval1516- `$0` = strategy name (e.g., ema-crossover, rsi, donchian). Default: ema-crossover17- `$1` = symbol (e.g., SBIN, RELIANCE, NIFTY). Default: SBIN18- `$2` = exchange (e.g., NSE, NFO). Default: NSE19- `$3` = interval (e.g., D, 1h, 5m). Default: D2021If no arguments, ask the user which strategy to optimize.2223## Instructions24251. Read the vectorbt-expert skill rules for reference patterns262. Create `backtesting/{strategy_name}/` directory if it doesn't exist (on-demand)273. Create a `.py` file in `backtesting/{strategy_name}/` named `{symbol}_{strategy}_optimize.py`284. The script must:29 - Load `.env` from project root using `find_dotenv()` and fetch data via OpenAlgo `client.history()`30 - If user provides a DuckDB path, load data directly via `duckdb.connect(path, read_only=True)`. See vectorbt-expert `rules/duckdb-data.md`.31 - If `openalgo.ta` is not importable (standalone DuckDB), use inline `exrem()` fallback.32 - **Use TA-Lib for ALL indicators** (never VectorBT built-in)33 - **Use OpenAlgo ta** for specialty indicators (Supertrend, Donchian, etc.)34 - Use `ta.exrem()` to clean signals (always `.fillna(False)` before exrem)35 - Define sensible parameter ranges for the chosen strategy36 - Use loop-based optimization to collect multiple metrics per combo37 - Track: total_return, sharpe_ratio, max_drawdown, trade_count for each combination38 - Use `tqdm` for progress bars39 - **Indian delivery fees**: `fees=0.00111, fixed_fees=20` for delivery equity40 - Find best parameters by total return AND by Sharpe ratio41 - Print top 10 results for both criteria42 - Generate Plotly heatmap of total return across parameter grid (`template="plotly_dark"`)43 - Generate Plotly heatmap of Sharpe ratio across parameter grid44 - **Fetch NIFTY benchmark** and compare best parameters vs benchmark45 - **Print Strategy vs Benchmark comparison table**46 - **Explain results** in plain language for normal traders47 - Save results to CSV484. Never use icons/emojis in code or logger output495. For futures symbols, use lot-size-aware sizing:50 - NIFTY: `min_size=65, size_granularity=65`51 - BANKNIFTY: `min_size=30, size_granularity=30`5253## Default Parameter Ranges5455| Strategy | Parameter 1 | Parameter 2 |56|----------|------------|-------------|57| ema-crossover | fast EMA: 5-50 | slow EMA: 10-60 |58| rsi | window: 5-30 | oversold: 20-40 |59| donchian | period: 5-50 | - |60| supertrend | period: 5-30 | multiplier: 1.0-5.0 |6162## Example Usage6364`/optimize ema-crossover RELIANCE NSE D`65`/optimize rsi SBIN`