The StochasticDivergence for Pair Trading
- Indicatori
- Ledi Haryadi
- Versione: 1.421
The StochasticDivergence is a custom indicator for the MetaTrader 4 (MT4) platform, designed to analyze the divergence between the Stochastic Oscillator (%K line) values of two currency pairs. It compares the Stochastic %K of the chart’s symbol (first symbol) with a user-defined second symbol, displaying both %K values and their absolute difference (divergence) in a separate subwindow. The indicator provides visual and alert-based feedback to highlight significant divergence levels, with customizable settings for user flexibility.
Key Features
- Stochastic %K Calculation:
- Computes the Stochastic Oscillator’s %K line for the chart’s symbol (e.g., EURUSD) and a user-defined second symbol (default: USDCHF).
- Configurable parameters include K Period (default: 100), D Period (default: 1), and Slowing (default: 1), using Simple Moving Average (SMA) for smoothing.
- Supports inversion of %K values for either symbol (default: disabled), allowing users to reverse the calculation (100 - %K) for specific analysis needs.
- Divergence Measurement:
- Calculates the absolute difference between the %K values of the two symbols, plotted as a dotted line in the subwindow.
- Displays the current divergence percentage in a label within the subwindow, updated in real-time.
- Visualization:
- Plots %K for the first symbol (default color: aqua) and second symbol (default color: red) as solid lines in a subwindow, scaled from 0 to 100.
- Shows divergence as a dotted line (default color: yellow) in the same subwindow.
- Includes Stochastic levels at 15, 50, and 85 for reference, with a customizable label for divergence display (position, font size, and color adjustable).
- Alerts and Notifications:
- Triggers alerts when divergence exceeds a user-defined threshold (default: 80%), with options for pop-up alerts (default: enabled) and push notifications (default: disabled).
- Prevents repeated alerts on the same bar using timestamp tracking, ensuring one alert per bar.
- Alert messages include the symbols, divergence percentage, threshold, and timestamp.
- Customization:
- Offers input parameters to adjust the second symbol, Stochastic parameters (K Period, D Period, Slowing), inversion settings, colors (for %K lines, divergence line, and label), label position (X, Y coordinates), font size, and alert settings.
- Allows users to enable/disable alerts and notifications based on their preferences.
Usage
- Initialization: Validates the second symbol in Market Watch, accounting for any suffix (e.g., ".m" or ".pro"). Sets up three buffers for %K and divergence, creates a divergence label, and configures the subwindow with a custom name.
- Operation: Calculates %K for both symbols and their divergence for each bar, updating the subwindow plots and divergence label. Checks for divergence exceeding the threshold and triggers alerts if enabled.
- Visualization: Displays %K lines, divergence line, and a label showing the current divergence percentage in the subwindow, with customizable colors and positions.
- Deinitialization: Removes all indicator objects (labels) from the subwindow upon removal.
Notes
- Requires the second symbol to be available in Market Watch, with automatic suffix detection for compatibility.
- Operates on the current chart’s timeframe, processing bars based on the Stochastic parameters (K Period, D Period, Slowing).
- The indicator focuses solely on Stochastic %K divergence, without additional technical indicators or risk management features.
