Quantstrat中自定义指标参数优化代码示例求助
Hey there! I totally get the frustration—most quantstrat examples stick to standard indicators like SMA or MACD, so finding guidance for custom ones like your MACD Forest momentum indicator can feel like searching for a needle in a haystack. Let’s break down how you can implement parameter optimization for your custom indicator step by step, with concrete code examples.
Step 1: Define & Register Your Custom MACD Forest Indicator
First, make sure your indicator function is compatible with quantstrat’s framework. Here’s a sample structure (replace the internal logic with your actual MACD Forest calculation):
# Custom MACD Forest momentum indicator function MACDforest <- function(x, fast_len, slow_len, signal_len, num_trees) { # Example: Generate multiple MACD variants (one per "tree") macd_variants <- lapply(1:num_trees, function(tree_idx) { # Adjust lengths slightly per tree (customize this to your Forest logic) adjusted_fast <- fast_len + (tree_idx - 1) * 2 adjusted_slow <- slow_len + (tree_idx - 1) * 3 fast_ma <- SMA(x, n = adjusted_fast) slow_ma <- SMA(x, n = adjusted_slow) macd_line <- fast_ma - slow_ma signal_line <- SMA(macd_line, n = signal_len) # Return momentum value (MACD - Signal) for this tree macd_line - signal_line }) # Combine tree results (e.g., average them—adjust to your Forest's aggregation rule) forest_momentum <- Reduce("+", macd_variants) / num_trees return(forest_momentum) } # Register the indicator to your quantstrat strategy add.indicator(strategy = "my_macdforest_strategy", name = "MACDforest", arguments = list(x = quote(Cl(mktdata)), # Use closing prices as input fast_len = 12, # Default values slow_len = 26, signal_len = 9, num_trees = 5), label = "MACDforest")
Step 2: Set Up Parameter Optimization Ranges
Use quantstrat’s add.param() function to define which parameters you want to tune, along with their valid ranges:
# Define optimization ranges for your MACD Forest parameters # Optimize fast_len: from 8 to 16, step of 2 add.param(strategy = "my_macdforest_strategy", parameter = "fast_len", label = "MACDforest", component.type = "indicator", component.label = "MACDforest", start = 8, end = 16, step = 2) # Optimize slow_len: from 20 to 32, step of 4 add.param(strategy = "my_macdforest_strategy", parameter = "slow_len", label = "MACDforest", component.type = "indicator", component.label = "MACDforest", start = 20, end = 32, step = 4) # Optimize number of trees: from 3 to 7, step of 2 add.param(strategy = "my_macdforest_strategy", parameter = "num_trees", label = "MACDforest", component.type = "indicator", component.label = "MACDforest", start = 3, end = 7, step = 2)
Step 3: Run the Optimization & Analyze Results
Next, create a parameter set and execute the optimization. Note that full grid search (nsamples = 0) can be slow for large parameter ranges—use random sampling (nsamples = 50) if you need faster results.
# Create a parameter set combining your defined parameters paramset_macdforest <- make.paramset(strategy = "my_macdforest_strategy", params = c("fast_len", "slow_len", "num_trees")) # Execute the optimization (replace portfolio/account names with yours) optimization_results <- apply.paramset(strategy = "my_macdforest_strategy", paramset = paramset_macdforest, portfolio = "my_trading_portfolio", account = "my_trading_account", nsamples = 0, # 0 = full grid search verbose = TRUE) # Extract and sort results by net profit to find optimal parameters optimal_params <- optimization_results$results %>% dplyr::arrange(dplyr::desc(Net.Trading.PL)) %>% head(1) cat("Optimal Parameters:\n") print(optimal_params)
Key Notes for Success
- Indicator Compatibility: Ensure your
MACDforestfunction returns a vector with the same length as the inputx—quantstrat relies on this to integrate the indicator with market data. - Computational Efficiency: If your parameter ranges are large, use random sampling (
nsamples = N) instead of full grid search to save time. - Context Matters: Always optimize parameters alongside your entry/exit rules and risk management (e.g., stop-losses) to ensure results reflect real-world trading conditions.
内容的提问来源于stack exchange,提问作者Olivier.S

