Stochastic_Divergence V2

Download script
Author: Copyright � 2007, FX5
Stochastic_Divergence V2

This tool is designed to help traders identify "divergences"—a specific market phenomenon where the price of an asset moves in one direction, but the underlying momentum (represented by a technical indicator) suggests that the trend is losing strength. By identifying these discrepancies, the script aims to provide early warning signs of potential trend reversals.

How the Logic Works

The script functions by constantly monitoring two main components: the actual price of the market (Highs and Lows) and the momentum values provided by a Stochastic oscillator. It processes this data in real-time to look for specific patterns:

1. Identifying Momentum Extremes The script first calculates two primary lines: a "Main" line and a "Signal" line. It uses these to track the overall speed and direction of the market. To find meaningful signals, it looks for "Peaks" (high points) and "Troughs" (low points) in the momentum data rather than just looking at every single price tick.

2. Detecting Bullish Divergence (Potential Buy Signals) The script searches for instances where the market price makes a new low, but the momentum indicator does not follow suit—meaning it stays higher than its previous trough.

  • The Logic: If the price drops lower than before, but the momentum "bottoms out" at a higher level, it suggests that selling pressure is weakening and a trend reversal to the upside may be coming.
  • Visual Output: When this occurs, the script places a specific visual marker (an arrow) on your chart.

3. Detecting Bearish Divergence (Potential Sell Signals) Conversely, the script monitors for instances where the market price hits a new high, but the momentum indicator fails to reach the same height as its previous peak.

  • The Logic: If the price is moving up to new highs but the internal momentum is lower than it was during the last peak, it indicates that buying strength is exhausting. This suggests the upward trend may be nearing an end and a move downward could follow.
  • Visual Output: Similar to the bullish signal, the script places a distinct visual marker (an arrow) to alert you to this potential reversal.

Summary of Performance

Instead of just following the price blindly, this script acts as a "quality check" on the trend. It filters out "fake" movements by comparing the strength of the move (the indicator) against the actual movement of the price. When these two elements disagree, the script highlights the discrepancy to help you spot high-probability turning points in the market.

Indicators Used
Stochastic oscillator
Miscellaneous
Implements a curve of type %1
6 Views
0 Downloads
0 Favorites
Stochastic_Divergence V2
//+------------------------------------------------------------------+
//|                     edited from     FX5_MACD_Divergence_V1.1.mq4 |
//|                                                              FX5 |
//|Editor: byens (byens@web.de)                        hazem@uk2.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, FX5"
#property link      "hazem@uk2.net"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_level1 80
#property indicator_level2 20
#property indicator_maximum	100
#property indicator_minimum	0

#define arrowsDisplacement 0.0001

extern int KPeriod=8;
extern int DPeriod=3;
extern int Slowing=3;

double bullishDivergence[];
double bearishDivergence[];
double macd[];
double signal[];

static string   indicatorName;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexStyle(3, DRAW_LINE);
//----   
   SetIndexBuffer(0, bullishDivergence);
   SetIndexBuffer(1, bearishDivergence);
   SetIndexBuffer(2, macd);
   SetIndexBuffer(3, signal);   
//----   
   SetIndexArrow(0, 233);
   SetIndexArrow(1, 234);
//----
   indicatorName = "Stochastic_Divergence(" + KPeriod + ", " + 
                                 DPeriod + ", " + Slowing + ")";
   SetIndexDrawBegin(3, Slowing);//signalSMA);
   IndicatorDigits(Digits + 2);
   IndicatorShortName(indicatorName);

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit(){
    for(int i = ObjectsTotal() - 1; i >= 0; i--){
       string label = ObjectName(i);
       if(StringSubstr(label, 0, 25) != "Stochastic_DivergenceLine")continue;ObjectDelete(label);   
    }
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start(){
    int countedBars=IndicatorCounted();
    if(countedBars<0)countedBars=0;CalculateIndicator(countedBars);
    return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CalculateIndicator(int countedBars){
     for(int i=Bars-countedBars;i>=0;i--)
     {
       macd[i]=iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_MAIN,i);
       signal[i]=iStochastic(NULL,0,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_SIGNAL,i);

       CatchBullishDivergence(i+2);
       CatchBearishDivergence(i+2);
     }              
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CatchBullishDivergence(int shift){
   if(!(macd[shift] <= macd[shift+1] && macd[shift] < macd[shift+2] && macd[shift] < macd[shift-1]))return;  
   int lastTrough=GetIndicatorLastTrough(shift);
//----   
   if(macd[shift]>=macd[lastTrough]&&Low[shift]<=Low[lastTrough]){
      bullishDivergence[shift]=macd[shift]-arrowsDisplacement;                        
   }
//----   
   if(macd[shift]<=macd[lastTrough]&&Low[shift]>=Low[lastTrough]){
      bullishDivergence[shift]=macd[shift]-arrowsDisplacement;
    }      
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CatchBearishDivergence(int shift){
     if(!(macd[shift] >= macd[shift+1] && macd[shift] > macd[shift+2] && macd[shift] > macd[shift-1]))return;
     int lastPeak = GetIndicatorLastPeak(shift);  
     if(macd[shift] <= macd[lastPeak] && High[shift] >= High[lastPeak]){
       bearishDivergence[shift] = macd[shift] + arrowsDisplacement;       
     }
     if(macd[shift] >= macd[lastPeak] && High[shift] <= High[lastPeak]){
       bearishDivergence[shift] = macd[shift] + arrowsDisplacement;
     }   
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetIndicatorLastPeak(int shift){
    for(int i = shift + 5; i < Bars; i++){
       if(signal[i] >= signal[i+1] && signal[i] >= signal[i+2] && signal[i] >= signal[i-1] && signal[i] >= signal[i-2]){
          for(int j = i; j < Bars; j++){
             if(macd[j] >= macd[j+1] && macd[j] > macd[j+2] && macd[j] >= macd[j-1] && macd[j] > macd[j-2])
             return(j);
          }
       }
    }
    return(-1);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetIndicatorLastTrough(int shift){
    for(int i = shift + 5; i < Bars; i++){
        if(signal[i] <= signal[i+1] && signal[i] <= signal[i+2] && signal[i] <= signal[i-1] && signal[i] <= signal[i-2]){
           for(int j = i; j < Bars; j++){
                if(macd[j] <= macd[j+1] && macd[j] < macd[j+2] && macd[j] <= macd[j-1] && macd[j] < macd[j-2])
                   return(j);
           }
        }
    }
    return(-1);
}


Comments