Author: Grzegorz Szczerba
realvalue

This script is a custom technical indicator designed to create a smooth, adaptive trend line on your trading chart. Instead of simply following the price directly, which can often lead to "choppy" or jagged movements, this tool uses a sophisticated calculation to filter out minor market noise while maintaining the overall direction of the price action.

How the Logic Works

The indicator functions by analyzing three primary components of market data:

1. Volatility Measurement The tool looks back at a specific number of previous candles (defined by the "Period" setting) to measure how much the price is swinging. By calculating the difference between high and low prices over this timeframe, it establishes a baseline for how "active" or "volatile" the current market environment is.

2. Dynamic Smoothing & Filtering Rather than jumping instantly to every new price movement, the script calculates the distance between the current price and the previous trend value. It then compares this movement against the volatility measured in step one. If a price move is too small compared to the surrounding volatility, the indicator treats it as "noise" and smooths it out. This ensures that the line only reacts significantly to meaningful trends rather than tiny, insignificant fluctuations.

3. Adaptive Trend Following The logic evaluates whether the current price is moving up or down relative to its previous position. It then applies a mathematical adjustment (the "Variation" factor) to the trend line. If the price confirms a move in a specific direction, the indicator adjusts the line accordingly; if the move lacks sufficient momentum or deviates from the established trend, the script dampens the change to keep the line steady.

Final Smoothing

After calculating the new position for each point on the line, the script performs a final averaging step. This blends the new calculated value with the previous value, resulting in a fluid, continuous curve that provides a clearer visual representation of the prevailing market trend.

Summary for Traders

In simple terms, this tool acts as a sophisticated "filter." It identifies the underlying momentum of the market by weighing current price action against historical volatility. The result is a red trend line that seeks to ignore minor market "hiccups" and highlight the actual direction in which the price is moving.

Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
realvalue
//+------------------------------------------------------------------+
//| Copyright (C) Grzegorz Szczerba                                  |
//| grzegorz.szczerba@gmail.com                                      |
//+------------------------------------------------------------------+
#property copyright "Grzegorz Szczerba"
#property link      "grzegorz.szczerba@gmail.com"
//---
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---
extern double Alpha=1;
extern double Variation=3.0;
extern int Period=20;
//---
double buffer[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,buffer);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=2;
//---
   for(int i=limit;i>=0;i--)
     {
      double h0 = iHigh(NULL, 0, i);
      double l0 = iLow(NULL, 0, i);
      double h1 = iHigh(NULL, 0, i + 1);
      double l1 = iLow(NULL, 0, i + 1);
      double var= 0;
      for(int j = 1; j<= Period; j++)
        {
         double hv1 = iHigh(NULL, 0, i + j);
         double lv1 = iLow(NULL, 0, i + j);
         double hv2 = iHigh(NULL, 0, i + j + 1);
         double lv2 = iLow(NULL, 0, i + j + 1);
         //---
         var+=MathAbs((hv1-lv1) -(hv2-lv2));
        }
      var/=(double)Period;
      //---
      double val=(h0+l0)/2.0;
      //---
      if(buffer[i+1]>1000000 || buffer[i+2]>1000000)
        {
         //FIRST VALUES
         buffer[i]=val;
         continue;
        }
      else
        {
         double diffB= buffer[i+1]-buffer[i+2];
         double diff = val -(h1+l1)/2.0;
         if(diffB>Alpha*var)
           {
            diffB=Alpha*var;
           }
         if(diffB<-Alpha*var)
           {
            diffB=-Alpha*var;
           }
         //---
         double c=var/Variation;
         if(val>buffer[i+1])
           {
            if(diff<0)
              {
               c=0;
               diffB=0;
              }
            buffer[i]=buffer[i+1]+diffB+c;
           }
         else
           {
            if(diff>0)
              {
               c=0;
               diffB=0;
              }
            buffer[i]=buffer[i+1]+diffB-c;
           }
        }
      buffer[i]=(buffer[i]+buffer[i+1])/2.0;
     }
   return(0);
  }
//+------------------------------------------------------------------+

Comments