$npx -y skills add marketcalls/vectorbt-backtesting-skills --skill optimizeOptimize strategy parameters using VectorBT. Tests parameter combinations and generates heatmaps.
| 1 | Create a parameter optimization script for a VectorBT strategy. |
| 2 | |
| 3 | ## Arguments |
| 4 | |
| 5 | Parse `$ARGUMENTS` as: strategy symbol exchange interval |
| 6 | |
| 7 | - `$0` = strategy name (e.g., ema-crossover, rsi, donchian). Default: ema-crossover |
| 8 | - `$1` = symbol (e.g., SBIN, RELIANCE, NIFTY). Default: SBIN |
| 9 | - `$2` = exchange (e.g., NSE, NFO). Default: NSE |
| 10 | - `$3` = interval (e.g., D, 1h, 5m). Default: D |
| 11 | |
| 12 | If no arguments, ask the user which strategy to optimize. |
| 13 | |
| 14 | ## Instructions |
| 15 | |
| 16 | 1. Read the vectorbt-expert skill rules for reference patterns |
| 17 | 2. Create `backtesting/{strategy_name}/` directory if it doesn't exist (on-demand) |
| 18 | 3. Create a `.py` file in `backtesting/{strategy_name}/` named `{symbol}_{strategy}_optimize.py` |
| 19 | 4. The script must: |
| 20 | - Load `.env` from project root using `find_dotenv()` and fetch data via OpenAlgo `client.history()` |
| 21 | - If user provides a DuckDB path, load data directly via `duckdb.connect(path, read_only=True)`. See vectorbt-expert `rules/duckdb-data.md`. |
| 22 | - If `openalgo.ta` is not importable (standalone DuckDB), use inline `exrem()` fallback. |
| 23 | - **Use OpenAlgo ta for ALL indicators by default** (never VectorBT built-in). Only switch to TA-Lib if the user explicitly says "talib"/"TA-Lib" |
| 24 | - **Always use OpenAlgo ta** for specialty indicators (Supertrend, Donchian, etc.) - no TA-Lib equivalent exists |
| 25 | - Use `ta.exrem()` to clean signals (always `.fillna(False)` before exrem) |
| 26 | - Define sensible parameter ranges for the chosen strategy |
| 27 | - Use loop-based optimization to collect multiple metrics per combo |
| 28 | - Track: total_return, sharpe_ratio, max_drawdown, trade_count for each combination |
| 29 | - Use `tqdm` for progress bars |
| 30 | - **Indian delivery fees**: `fees=0.00111, fixed_fees=20` for delivery equity |
| 31 | - Find best parameters by total return AND by Sharpe ratio |
| 32 | - Print top 10 results for both criteria |
| 33 | - Generate Plotly heatmap of total return across parameter grid (`template="plotly_dark"`) |
| 34 | - Generate Plotly heatmap of Sharpe ratio across parameter grid |
| 35 | - **Fetch NIFTY benchmark** and compare best parameters vs benchmark |
| 36 | - **Print Strategy vs Benchmark comparison table** |
| 37 | - **Explain results** in plain language for normal traders |
| 38 | - Save results to CSV |
| 39 | 4. Never use icons/emojis in code or logger output |
| 40 | 5. For futures symbols, use lot-size-aware sizing: |
| 41 | - NIFTY: `min_size=65, size_granularity=65` |
| 42 | - BANKNIFTY: `min_size=30, size_granularity=30` |
| 43 | |
| 44 | ## Default Parameter Ranges |
| 45 | |
| 46 | | Strategy | Parameter 1 | Parameter 2 | |
| 47 | |----------|------------|-------------| |
| 48 | | ema-crossover | fast EMA: 5-50 | slow EMA: 10-60 | |
| 49 | | rsi | window: 5-30 | oversold: 20-40 | |
| 50 | | donchian | period: 5-50 | - | |
| 51 | | supertrend | period: 5-30 | multiplier: 1.0-5.0 | |
| 52 | |
| 53 | ## Example Usage |
| 54 | |
| 55 | `/optimize ema-crossover RELIANCE NSE D` |
| 56 | `/optimize rsi SBIN` |