Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Improvement Suggestions

Here is a roadmap for evolving the Adaptive Regime Filter + Divergence (AER-VN) script into a professional-grade quantitative trading system.

Level 1: Parameter Optimization & Dynamic Adaptability

The initial script is an excellent signal generator but lacks the mechanics of a complete trading strategy. Level 1 transforms it from a qualitative indicator into a quantifiable, backtestable system with dynamic risk management, moving it away from static, curve-fit parameters.

Suggested Upgrades:

  1. Strategy Conversion with ATR-Based Risk Management: The first step is to convert the indicator into a strategy. This requires implementing entry and exit logic. We will define risk dynamically based on the market’s current volatility, using the Average True Range (ATR).

    • Stop-Loss (SL): When a regular bearish divergence (regBear) is confirmed, a short entry is triggered. The initial stop-loss will be placed at entry_price + (ATR_multiplier * ta.atr(atrLength)). A typical ATR_multiplier would be between 1.5 and 3.0.

    • Take-Profit (TP): The take-profit will be set based on a fixed Risk-to-Reward (RR) ratio. For example, with a 1:2 RR, the TP would be placed at entry_price - (2 * stop_loss_distance_in_pips). This creates a system with a defined, positive expectancy from the outset.

  2. Adaptive Lookback Period for Efficiency Ratio: The length of the ER is currently a static input (10). This is a prime candidate for curve-fitting. A more robust system adapts this lookback to the market’s tempo. We can use the same atrRatio that powers the dynamic threshold to modulate the ER lookback.

    • Logic: When volatility is high (atrRatio > 1), the market is moving quickly, and a shorter lookback is more responsive. When volatility is low (atrRatio < 1), a longer lookback is needed to capture the trend’s structure.

    • Implementation:

      minErLookback = input.int(7, "Min ER Lookback")
      maxErLookback = input.int(21, "Max ER Lookback")
      // Clamp the ratio to prevent extreme values, e.g., between 0.5 and 2.0
      clampedAtrRatio = math.max(0.5, math.min(2.0, atrRatio)) 
      // Inversely relate lookback to volatility: higher vol -> shorter lookback
      adaptiveLength = math.round(maxErLookback / clampedAtrRatio)
      adaptiveLength := math.max(minErLookback, math.min(maxErLookback, adaptiveLength))
      
      // Use 'adaptiveLength' instead of 'length' in the ER calculation

Quantitative Benefit:

By implementing ATR-based risk management, we immediately introduce discipline and quantify risk on a per-trade basis. This directly impacts risk-adjusted return metrics. The primary benefit is a reduction in Maximum Drawdown (MDD) and an improvement in the Calmar Ratio (Annualized Return / MDD). The adaptive lookback period attacks the problem of curve-fitting. Instead of finding a single “magic number” that works on historical data, the system adapts its core parameter to live volatility, significantly increasing its robustness and likelihood of performing well out-of-sample and across different assets or market conditions.


Level 2: Secondary Confluence & Noise Filtration

With a backtestable strategy in place, Level 2 focuses on improving signal quality. The goal is to add secondary filters that confirm the primary divergence signal, eliminating low-probability setups that often lead to “whipsaws” in choppy markets.

Suggested Upgrades:

  1. Volume-Weighted Confirmation Filter: A divergence signal is significantly more potent if the key price action is supported by a surge in volume. This indicates institutional participation and conviction behind the potential reversal.

    • Logic: For a bearish divergence, we require the volume on the bar that confirms the new swing high to be above its recent average. This suggests an exhaustive “blow-off top.” Conversely, a bullish divergence is stronger if the reversal bar shows capitulation volume.

    • Implementation:

      volLookback = input.int(20, "Volume MA Lookback")
      volAvg = ta.sma(volume, volLookback)
      
      // Add this condition to the entry logic
      bool volumeConfirmation = volume[1] > volAvg[1] // Check volume on the swing confirmation bar
      
      // New entry condition
      if regBear and volumeConfirmation
          strategy.entry(...)
  2. Higher-Timeframe (HTF) Directional Bias: The base strategy is purely counter-trend. This is statistically dangerous in strongly trending markets. A powerful filter is to only take signals that align with the broader market direction. For example, only take bearish (short) divergence signals if the weekly trend is either bearish or neutral/ranging.

    • Logic: Use the script’s own regime classification logic on a higher timeframe. If trading on a 1-hour chart, request the regime state from the 4-hour or Daily chart. A bearish divergence on the 1H is only acted upon if the 4H chart is not in a strong isUptrend state.

    • Implementation:

      htf = input.timeframe("4H", "Higher Timeframe")
      // Request the 'isUptrend' and 'isDowntrend' booleans from the HTF
      [htfIsUptrend, htfIsDowntrend] = request.security(syminfo.tickerid, htf, [isUptrend, isDowntrend], lookahead=barmerge.lookahead_on)
      
      // New entry conditions
      bool allowShorts = not htfIsUptrend
      bool allowLongs  = not htfIsDowntrend
      
      if regBear and volumeConfirmation and allowShorts
          strategy.entry(...)

Quantitative Benefit:

These filters are designed to increase the precision of the entries, directly targeting the Win Rate and Profit Factor. By filtering out trades against the macro trend or those lacking volume conviction, we avoid many “whipsaw” losses where a minor divergence fails and the primary trend resumes. While this may decrease the total number of trades, the Expected Value (EV) of each executed trade increases substantially. This leads to a smoother equity curve and a higher Profit Factor (Gross Profits / Gross Losses), which is a key indicator of a strategy’s efficiency.


Level 3: Structural Architecture & Regime Detection

Level 3 elevates the system from a static strategy to a dynamic, intelligent agent that understands and adapts to fundamental shifts in market structure. This involves re-architecting the core engine to handle different market regimes, not just as a filter, but as a state-dependent execution model.

Suggested Upgrades:

  1. Market Regime Filter (Trend vs. Mean-Reversion Mode): The current strategy is exclusively mean-reverting. A truly robust system should also know how to trade trends. By integrating a regime filter, the strategy can switch its entire personality from “mean reversion” to “trend following.”

    • Logic: Use a quantitative measure like the Hurst Exponent or a Gaussian Filter’s derivative to classify the market’s underlying behavior.

      • Hurst < 0.5: The market is mean-reverting. Activate the existing AER-VN divergence logic.

      • Hurst > 0.5: The market is trending (persistent). Deactivate the divergence logic and activate a trend-following module (e.g., enter long on a breakout above the dynamicThreshold when isUptrend is true).

    • Implementation (Conceptual):

      // Assume a function `f_getHurst()` that calculates the Hurst Exponent
      hurst = f_getHurst(close, 100)
      
      isMeanReversionRegime = hurst < 0.45
      isTrendingRegime = hurst > 0.55
      
      // In the execution logic:
      if isMeanReversionRegime
          // Execute Level 2 logic (divergence + filters)
          ...
      else if isTrendingRegime
          // Execute trend-following logic
          // e.g., if ta.crossunder(er, dynamicThreshold) and isUptrend[1]
          //    strategy.close("Short")
          // if ta.crossover(er, dynamicThreshold) and not in_trade
          //    strategy.entry("Trend Long", strategy.long)
          ...
  2. Multi-Timeframe (MTF) Recursive Signal Validation: This goes beyond the simple HTF filter of Level 2. Instead of a binary check, it builds a “confluence score” by validating the signal recursively across multiple timeframes. A signal that appears simultaneously on the 15M, 1H, and 4H is an order of magnitude more powerful than a signal on a single timeframe.

    • Logic: Create a function that detects the regBear or regBull state. Call this function for several key timeframes (e.g., M15, H1, H4). An entry is only triggered if a minimum number of timeframes agree.

    • Implementation (Conceptual):

      // Function to get divergence signal for a given timeframe
      f_getDivergence(tf) =>
          // Use request.security to get 'regBear' and 'regBull' from the specified timeframe 'tf'
          [tfRegBear, tfRegBull] = request.security(syminfo.tickerid, tf, [regBear, regBull])
          [tfRegBear, tfRegBull]
      
      [m15_bear, m15_bull] = f_getDivergence("15")
      [h1_bear, h1_bull]   = f_getDivergence("60")
      [h4_bear, h4_bull]   = f_getDivergence("240")
      
      // Calculate a confluence score
      bearishConfluenceScore = (m15_bear ? 1 : 0) + (h1_bear ? 1 : 0) + (h4_bear ? 1 : 0)
      
      // Entry requires a high degree of confluence
      if bearishConfluenceScore >= 2 and isMeanReversionRegime
          strategy.entry(...)

Quantitative Benefit:

These structural changes provide the ultimate goal: true Robustness. A regime-switching architecture (Upgrade 1) allows the strategy to generate alpha in multiple market types, preventing the long periods of flat P&L or deep drawdowns that occur when a single-paradigm strategy is out of sync with the market. This drastically improves the strategy’s Sharpe Ratio over long periods and enhances its ability to survive “Black Swan” events or secular changes in market behavior. The MTF recursive validation (Upgrade 2) maximizes the Signal-to-Noise Ratio, ensuring the system only deploys capital on the highest conviction setups. This may result in fewer trades, but each trade carries a significantly higher Expected Value (EV), the defining characteristic of a professional quantitative trading system.