Extremum Filter 2

Download script
Author: Copyright 2017, Tor

This tool is designed to help traders identify significant price movements by filtering out minor market fluctuations and highlighting potential trend reversals. It works by scanning historical price data to detect "extremes"—specifically, high and low price points that stand out over a defined period of time.

How the Logic Works

1. Data Scanning (The Lookback) The script examines the market by looking back at a specific number of previous candles (defined by the user as "before" and "before2"). Instead of reacting to every tiny wiggle in the price, it searches for the highest peaks and lowest valleys within these set windows. This ensures that the tool only focuses on significant structural movements rather than "noise."

2. Peak and Valley Identification The logic processes two distinct signals:

  • High Extremes: When the script identifies a peak that is significantly higher than its surroundings, it flags a potential downward pressure or a "sell" zone.
  • Low Extremes: When the script identifies a valley that is significantly lower than its surroundings, it flags a potential upward pressure or a "buy" zone.

3. Conflict Resolution To keep the signals clean and usable, the tool includes a built-in logic filter: if both a high extreme and a low extreme are detected simultaneously (which can happen in very choppy markets), the script ignores them. This prevents contradictory signals from appearing at the same time, ensuring that only clear, distinct price actions are highlighted.

4. Trend Tracking The tool maintains an internal memory of the current market trend. Once it identifies a valid extreme:

  • If a High Extreme is confirmed first, the script enters a "downward" state. While in this state, it continues to highlight downward pressure.
  • If a Low Extreme is confirmed first, the script enters an "upward" state and highlights upward momentum.

By following this logic, the indicator acts as a visual filter that helps traders see the broader flow of the market, marking out zones where the price has reached a temporary limit and may be preparing to change direction.

Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
Extremum Filter 2
//+------------------------------------------------------------------+
//|                                              Extremum Filter.mq4 |
//|                                              Copyright 2017, Tor |
//|                                             http://einvestor.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Tor"
#property link      "http://einvestor.ru/"
#property version   "2.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2

input int before=15; //Previous bars 1
input int before2=5; //Previous bars 2

double Extremum[],Extremum2[];
static int nowTrend=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorShortName("Extremum Filter 2");
   IndicatorDigits(Digits);
//--- indicator lines
   SetIndexBuffer(0,Extremum);
   SetIndexBuffer(1,Extremum2);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrRed);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrBlue);
   SetIndexLabel(0,"Extremum");
   SetIndexLabel(1,"Extremum2");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   int limit;
//---
   if(rates_total<=1)
      return(0);
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit=limit+1;

   for(int x=limit-1; x>=0; x--)
     {
      Extremum[x]=0; Extremum2[x]=0;
      if(iHighest(_Symbol,_Period,MODE_HIGH,before,x)>(x+before2))
        {
         Extremum[x]=1;
        }
      if(iLowest(_Symbol,_Period,MODE_LOW,before,x)>(x+before2))
        {
         Extremum2[x]=1;
        }
      if(Extremum[x]>0 && Extremum2[x]>0){ Extremum[x]=0; Extremum2[x]=0; }
      if(Extremum[x]>0 && nowTrend>-1)
        { // Sell
         nowTrend=-1;
        }
      if(Extremum2[x]>0 && nowTrend<1)
        { // Buy
         nowTrend=1;
        }
      if(nowTrend>0){ Extremum2[x] = 1; }
      if(nowTrend<0){ Extremum[x] = 1; }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments