WPR_hist_div_signal

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
WPR_hist_div_signal

This script is designed as a custom technical indicator for the MetaTrader trading platform. Its primary purpose is to analyze price action over a specific period to identify whether the market is currently in an overbought or oversold state, while simultaneously highlighting potential trend reversals through specific momentum triggers.

Core Logic and Functionality

1. Range Analysis (Williams' Percent Range) The indicator begins by looking at a user-defined window of recent price data (the "Period"). Within this window, it identifies the highest high and the lowest low reached by the market. It then calculates where the current closing price sits relative to that total range.

  • If the price is trading near the bottom of its recent range, the indicator interprets this as a sign of downward pressure or an "oversold" condition.
  • If the price is trading near the top of its recent range, it suggests upward pressure or an "overbought" condition.

2. Visual Representation The tool translates these calculations into a visual histogram. It centers the data around a zero line to provide a clear distinction between market sentiment:

  • Positive/Negative Shifts: By subtracting the calculated value from a midpoint of 50, the script creates a mirrored histogram effect. This allows traders to see at a glance whether the current price action is leaning toward the upper or lower boundaries of the recent history.
  • Dynamic Levels: The script utilizes specific boundary lines (set at 40 and -40) to help users identify extreme market zones where prices might be stretched too far in one direction.

3. Momentum & Reversal Triggers Beyond simply measuring the range, the script includes advanced logic to detect specific "turning points" in the market. It monitors four specific conditions simultaneously to determine if a reversal is likely:

  • For Potential Upside (Long signals): The logic looks for a scenario where the price hits a new low within the current period, combined with an increase in upward momentum and specific candlestick "shadows" (the wicks on the candles) that suggest buyers are stepping in.
  • For Potential Downside (Short signals): Conversely, it looks for a scenario where the price reaches a new high within the period, but the momentum begins to shift downward, combined with "upper shadows" that indicate sellers are pushing back against the price.

Summary of Use

For a user, this script acts as a dual-purpose tool: it provides a continuous visual map of where the price sits relative to its recent extremes (the histogram), while also providing specific highlights (long and short buffers) when the market displays the specific structural characteristics of a trend reversal.

Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
WPR_hist_div_signal
//+------------------------------------------------------------------+
//|                                      Williams’ Percent Range.mq4 |
//|                                       Copyright © 2009, Ulterior |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
//----
#property indicator_separate_window
#property indicator_minimum -55
#property indicator_maximum 55
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 DodgerBlue
#property indicator_color3 DarkGreen
#property indicator_color4 FireBrick
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
#property indicator_width4 3

#property indicator_level1 0
#property indicator_level2 40
#property indicator_level3 -40
#property indicator_levelcolor Black
//---- input parameters
extern int ExtWPRPeriod = 30;
//---- buffers
double ExtWPRBuffer1[];
double ExtWPRBuffer2[];
double ExtWPRLongBuffer1[];
double ExtWPRShortBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string sShortName;
//---- indicator buffer mapping
   SetIndexBuffer(0, ExtWPRBuffer1);
   SetIndexBuffer(1, ExtWPRBuffer2);
   SetIndexBuffer(2, ExtWPRLongBuffer1);
   SetIndexBuffer(3, ExtWPRShortBuffer1);
//---- indicator line
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   SetIndexStyle(3, DRAW_HISTOGRAM);
//---- name for DataWindow and indicator subwindow label
   sShortName="%Rhist(" + ExtWPRPeriod + ")";
   IndicatorShortName(sShortName);
   SetIndexLabel(0, StringConcatenate(sShortName, "up" ));
   SetIndexLabel(1, StringConcatenate(sShortName, "down" ));
   SetIndexLabel(2, StringConcatenate(sShortName, "long" ));
   SetIndexLabel(3, StringConcatenate(sShortName, "short" ));
//---- first values aren't drawn
   SetIndexDrawBegin(0, ExtWPRPeriod);
   SetIndexDrawBegin(1, ExtWPRPeriod);
   SetIndexDrawBegin(2, ExtWPRPeriod);
   SetIndexDrawBegin(3, ExtWPRPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Williams’ Percent Range                                          |
//+------------------------------------------------------------------+
int start()
  {
   int i, nLimit, nCountedBars;  
//---- insufficient data
   if(Bars <= ExtWPRPeriod) 
       return(0);
//---- bars count that does not changed after last indicator launch.
   nCountedBars = IndicatorCounted();
//----Williams’ Percent Range calculation
   i = Bars - ExtWPRPeriod - 1;
   if(nCountedBars > ExtWPRPeriod) 
       i = Bars - nCountedBars - 1;  
       
   double upperShadow, lowerShadow, body;    
       
   while(i >= 0)
     {
       double dMaxHigh = High[Highest(NULL, 0, MODE_HIGH, ExtWPRPeriod, i)];
       double dMinLow = Low[Lowest(NULL, 0, MODE_LOW, ExtWPRPeriod, i)];      
       if(!CompareDouble((dMaxHigh - dMinLow), 0.0))
       {
           double val = 100*(dMaxHigh - Close[i]) / (dMaxHigh - dMinLow);
           
           ExtWPRLongBuffer1[i] = 0;
           
           if( val <= 50 ) { ExtWPRBuffer1[i] = -val+50; ExtWPRBuffer2[i] = 0; } else { ExtWPRBuffer1[i] = 0; ExtWPRBuffer2[i] = -val+50; }

           if(Close[i]<=Open[i] )
           {
            lowerShadow = Close[i]-Low[i]; 
            upperShadow = High[i]-Open[i]; 
            body = Open[i]-Close[i];
           }

           if(Close[i]>=Open[i] )
           {
            lowerShadow = Open[i]-Low[i]; 
            upperShadow = High[i]-Close[i]; 
            body = Close[i]-Open[i];
           }
           
           if( (ExtWPRBuffer2[i+1] < ExtWPRBuffer2[i]) && (Low[i] <= dMinLow) && ((Low[i] < Low[i+1]) || (Close[i] > Open[i])) && (lowerShadow >= 0)  )
           {
            ExtWPRLongBuffer1[i] = ExtWPRBuffer2[i];
           }
           else              
           if( (ExtWPRBuffer1[i+1] > ExtWPRBuffer1[i]) && (High[i] >= dMaxHigh) && (High[i] > High[i+1]) && (upperShadow >= 0) )
           {
            ExtWPRShortBuffer1[i] = ExtWPRBuffer1[i];
           }
           else
           {
            ExtWPRLongBuffer1[i] = 0;
            ExtWPRShortBuffer1[i] = 0;
           }
       }    
       i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Ôóíêöèÿ ñðàíåíèÿ äâóõ âåùåñòâåííûõ ÷èñåë.                        |
//+------------------------------------------------------------------+
bool CompareDouble(double Number1, double Number2)
  {
    bool Compare = NormalizeDouble(Number1 - Number2, 8) == 0;
    return(Compare);
  } 
//+------------------------------------------------------------------+ 

Comments