OPEN-SOURCE SCRIPT

Divergence Strategy [Trendoscope®]

766
🎲 Overview

The Divergence Strategy [Trendoscope®] is a sophisticated TradingView strategy that enhances the Divergence Screener [Trendoscope®] by adding automated trade signal generation, risk management, and trade visualization. It leverages the screener’s robust divergence detection to identify bullish, bearish, regular, and hidden divergences, then executes trades with precise entry, stop-loss, and take-profit levels. Designed for traders seeking automated trading solutions, this strategy offers customizable trade parameters and visual feedback to optimize performance across various markets and timeframes.

For core divergence detection features, including oscillator options, trend detection methods, zigzag pivot analysis, and visualization, refer to the Divergence Screener [Trendoscope®] documentation. This description focuses on the strategy-specific enhancements for automated trading and risk management.
快照
🎲 Strategy Features
🎯Automated Trade Signal Generation
  • Trade Direction Control: Restrict trades to long-only or short-only to align with market bias or strategy goals, preventing conflicting orders.
  • Divergence Type Selection: Choose to trade regular divergences (bullish/bearish), hidden divergences, or both, targeting reversals or trend continuations.
  • Entry Type Options:
    Cautious: Enters conservatively at pivot points and exits quickly to minimize risk exposure.
    Confident: Enters aggressively at the latest price and holds longer to capture larger moves.
    Mixed: Combines conservative entries with delayed exits for a balanced approach.
  • Market vs. Stop Orders: Opt for market orders for instant execution or stop orders for precise price entry.

🎯 Enhanced Risk Management
  • Risk/Reward Ratio: Define a risk-reward ratio (default: 2.0) to set profit targets relative to stop-loss levels, ensuring consistent trade sizing.
  • Bracket Orders: Trades include entry, stop-loss, and take-profit levels calculated from divergence pivot points, tailored to the entry type and risk-reward settings.
  • Stop-Loss Placement: Stops are strategically set (e.g., at recent pivot or last price point) based on entry type, balancing risk and trade validity.
  • Order Cancellation: Optionally cancel pending orders when a divergence is broken (e.g., price moves past the pivot in the wrong direction), reducing invalid trades. This feature is toggleable for flexibility.

🎯 Trade Visualization
  • Target and Stop Boxes: Displays take-profit (lime) and stop-loss (orange) levels as boxes on the price chart, extending 10 bars forward for clear visibility.
  • Dynamic Trade Updates: Trade visualizations are added, updated, or removed as trades are executed, canceled, or invalidated, ensuring accurate feedback.
  • Overlay Integration: Trade levels overlay the price chart, complementing the screener’s oscillator-based divergence lines and labels.

🎯 Strategy Default Configuration
  • Capital and Sizing: Set initial capital (default: $1,000,000) and position size (default: 20% of equity) for realistic backtesting.
  • Pyramiding: Allows up to 4 concurrent trades, enabling multiple divergence-based entries in trending markets.
  • Commission and Margin: Accounts for commission (default: 0.01%) and margin (100% for long/short) to reflect trading costs.
  • Performance Optimization: Processes up to 5,000 bars dynamically, balancing historical analysis and real-time execution.

🎲 Inputs and Configuration

🎯Trade Settings
  • Direction: Select Long or Short (default: Long).
  • Divergence: Trade Regular, Hidden, or Both divergence types (default: Both).
  • Entry/Exit Type: Choose Cautious, Confident, or Mixed (default: Cautious).
  • Risk/Reward: Set the risk-reward ratio for profit targets (default: 2.0).
  • Use Market Order: Enable market orders for immediate entry (default: false, uses limit orders).
  • Cancel On Break: Cancel pending orders when divergence is broken (default: true).

快照
🎯Inherited Settings
The strategy inherits all inputs from the Divergence Screener, including:
  • Oscillator Settings: Oscillator type (e.g., RSI, CCI), length, and external oscillator option.
  • Trend Settings: Trend detection method (Zigzag, MA Difference, External), MA type, and length.
  • Zigzag Settings: Zigzag length (fixed repaint = true).

快照
🎲 Entry/Exit Types for Divergence Scenarios

The Divergence Strategy [Trendoscope®] offers three Entry/Exit Type options—Cautious, Confident, and Mixed—which determine how trades are entered and exited based on divergence pivot points. This section explains how these settings apply to different divergence scenarios, with placeholders for screenshots to illustrate each case.
The divergence pattern forms after 3 pivots. The stop and entry levels are formed on one of these levels based on Entry/Exit types.

🎯Bullish Divergence (Reversal)

A bullish divergence occurs when price forms a lower low, but the oscillator forms a higher low, signaling a potential upward reversal.

💎 Cautious:
  • Entry: At the pivot high point for a conservative entry.
  • Exit: Stop-loss at the last pivot point (previous low that is higher than the current pivot low); take-profit at risk-reward ratio. Canceled if price breaks below the pivot (if Cancel On Break is enabled).
  • Behavior: Enters after confirmation and exits quickly to limit downside risk.

💎Confident:
  • Entry: At the last pivot low, (previous low which is higher than the current pivot low) for an aggressive entry.
  • Exit: Stop-loss at recent pivot low, which is the lowest point; take-profit at risk-reward ratio. Canceled if price breaks below the pivot. (lazy exit)
  • Behavior: Enters early to capture trend continuation, holding longer for gains.

💎Mixed:
  • Entry: At the pivot high point (conservative).
  • Exit: Stop-loss at the recent pivot point that has resulted in lower low (lazy exit). Canceled if price breaks below the pivot.
  • Behavior: Balances entry caution with extended holding for trend continuation.

快照
🎯Bearish Divergence (Reversal)

A bearish divergence occurs when price forms a higher high, but the oscillator forms a lower high, indicating a potential downward reversal.

💎Cautious:
  • Entry: At the pivot low point (lower high) for a conservative short entry.
  • Exit: Stop-loss at the previous pivot high point (previous high); take-profit at risk-reward ratio. Canceled if price breaks above the pivot (if Cancel On Break is enabled).
  • Behavior: Enters conservatively and exits quickly to minimize risk.

💎Confident:
  • Entry: At the last price point (previous high) for an aggressive short entry.
  • Exit: Stop-loss at the pivot point; take-profit at risk-reward ratio. Canceled if price breaks above the pivot.
  • Behavior: Enters early to maximize trend continuation, holding longer.

💎Mixed:
  • Entry: At the previous piot high point (conservative).
  • Exit: Stop-loss at the last price point (delayed exit). Canceled if price breaks above the pivot.
  • Behavior: Combines conservative entry with extended holding for downtrend gains.

快照
🎯Bullish Hidden Divergence (Continuation)

A bullish hidden divergence occurs when price forms a higher low, but the oscillator forms a lower low, suggesting uptrend continuation. In case of Hidden bullish divergence, b]Entry is always on the previous pivot high (unless it is a market order)

💎Cautious:
  • Exit: Stop-loss at the recent pivot low point (higher than previous pivot low); take-profit at risk-reward ratio. Canceled if price breaks below the pivot (if Cancel On Break is enabled).
  • Behavior: Enters after confirmation and exits quickly to limit downside risk.

💎Confident:
  • Exit: Stop-loss at previous pivot low, which is the lowest point; take-profit at risk-reward ratio. Canceled if price breaks below the pivot. (lazy exit)
  • Behavior: Enters early to capture trend continuation, holding longer for gains.

快照
🎯Bearish Hidden Divergence (Continuation)

A bearish hidden divergence occurs when price forms a lower high, but the oscillator forms a higher high, suggesting downtrend continuation. In case of Hidden Bearish divergence, b]Entry is always on the previous pivot low (unless it is a market order)

💎Cautious:
  • Exit: Stop-loss at the latest pivot high point (which is a lower high); take-profit at risk-reward ratio. Canceled if price breaks above the pivot (if Cancel On Break is enabled).
  • Behavior: Enters conservatively and exits quickly to minimize risk.

💎Confident/Mixed:
  • Exit: Stop-loss at the previous pivot high point; take-profit at risk-reward ratio. Canceled if price breaks above the pivot.
  • Behavior: Uses the late exit point to hold longer.

快照
🎲 Usage Instructions
🎯Add to Chart:
  • Add the Divergence Strategy [Trendoscope®] to your TradingView chart.
  • The oscillator and divergence signals appear in a separate pane, with trade levels (target/stop boxes) overlaid on the price chart.

🎯Configure Settings:
  • Adjust trade settings (direction, divergence type, entry type, risk-reward, market orders, cancel on break).
  • Modify inherited Divergence Screener settings (oscillator, trend method, zigzag length) as needed.
  • Enable/disable alerts for divergence notifications.

🎯Interpret Signals:
  • Long Trades: Triggered on bullish or bullish hidden divergences (if allowed), shown with green/lime lines and labels.
  • Short Trades: Triggered on bearish or bearish hidden divergences (if allowed), shown with red/orange lines and labels.
  • Monitor lime (target) and orange (stop) boxes for trade levels.
  • Review strategy performance metrics (e.g., profit/loss, win rate) in the strategy tester.

🎯Backtest and Optimize:
  • Use TradingView’s strategy tester to evaluate performance on historical data.
  • Fine-tune risk-reward, entry type, position sizing, and cancellation settings to suit your market and timeframe.


For questions, suggestions, or support, contact Trendoscope via TradingView or official support channels. Stay tuned for updates and enhancements to the Divergence Strategy!

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。