A Coding Error

Author: mladen
A Coding Error

This script is a technical analysis tool designed to help traders visualize market momentum and identify the strength of current price trends. Rather than showing raw price movements, it processes historical data to produce a smoothed indicator that reflects whether the market is leaning toward an upward or downward bias.

Core Logic and Methodology

The indicator operates through a multi-stage mathematical process to filter out "market noise" and highlight significant directional shifts:

1. Price Smoothing The tool begins by taking the closing prices of assets and applying a Simple Moving Average (SMA). This initial step helps stabilize the data, ensuring that minor, erratic price fluctuations do not cause the indicator to react too aggressively or erratically.

2. Momentum and Gradient Analysis Once the basic trend is established, the script calculates "momentum" by comparing current price values against those from a specific period in the past (defined by the user's settings). It then creates a linear gradient to see how much the price deviates from that projected path.

  • Positive Deviations: If the price is performing better than the projected trend, it is added to an "Up" category.
  • Negative Deviations: If the price is underperforming the trend, it is categorized as a "Down" movement.

3. Percentage Trend Calculation The script combines these upward and downward deviations to create a percentage-based trend value. By dividing the strength of the upward movements by the total combined movement (up and down), the tool generates a ratio. This results in a value that tells you what percentage of the current market energy is moving in a specific direction.

4. T3 Smoothing Filter To make the final visual output readable and less "choppy," the script applies a specialized smoothing algorithm known as the "T3." This filter takes the raw trend percentages calculated in the previous steps and rounds off the sharp edges. It ensures that the resulting line on your chart flows smoothly, making it easier for a trader to identify sustained trends without being distracted by momentary spikes.

Visual Presentation

The indicator is displayed in a separate window below the main price chart. It typically moves between defined boundaries (such as 0 and 100), where the position of the line indicates the current trend's strength:

  • High values suggest dominant upward momentum.
  • Low values suggest dominant downward momentum.
  • Middle zones indicate periods of consolidation or neutrality where the market is lacking a clear directional bias.

By using this logic, the tool serves as a "momentum thermometer," allowing users to see not just where the price is going, but how much force is behind the current move.

Indicators Used
Moving average indicator
9 Views
0 Downloads
0 Favorites
A Coding Error
//+------------------------------------------------------------------+
//|                                               A coding error.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers    1
#property indicator_color1     DarkOrange
#property indicator_levelcolor DimGray


//
//
//
//
//

extern int    Length     = 35;
extern int    Price      = PRICE_CLOSE;
extern double LevelHigh  = 80;
extern double LevelLow   = 20;
extern int    T3Period   = 5;
extern double T3Hot      = 0.7;
extern bool   T3Original = false;

//
//
//
//
//

double Trend[];
double values[][7];
double alpha;
double c1;
double c2;
double c3;
double c4;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,Trend); SetIndexLabel(0,"Trend");
   
      //
      //
      //
      //
      //
      
      LevelHigh = MathMin(MathMax(LevelHigh,0),100);
      LevelLow  = MathMin(MathMax(LevelLow,0) ,100);
      if (LevelHigh<LevelLow)
         {
            double temp = LevelHigh;
                   LevelHigh = LevelLow;
                   LevelLow  = temp;
         }

      //
      //
      //
      //
      //
   
      double a  = T3Hot;
             c1 = -a*a*a;
             c2 =  3*(a*a+a*a*a);
             c3 = -3*(2*a*a+a+a*a*a);
             c4 = 1+3*a+a*a*a+3*a*a;

      T3Period = MathMax(1,T3Period);
      if (T3Original)
           alpha = 2.0/(1.0 + T3Period);
      else alpha = 2.0/(2.0 + (T3Period-1.0)/2.0);
         
      //
      //
      //
      //
      //
               
   SetLevelValue(0,LevelHigh);
   SetLevelValue(1,LevelLow);
   SetLevelValue(2,100);
   SetLevelValue(3,0);
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define prices   0
#define T3_start 1

//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-Length);
         if (ArrayRange(values,0) != Bars) ArrayResize(values,Bars);

   //
   //
   //
   //
   //

   for(i=limit, r=Bars-limit-1; i>=0; i--,r++)
   {
      values[r][prices] = iMA(NULL,0,1,0,MODE_SMA,Price,i);

      //
      //
      //
      //
      //

      double momentum = values[r][prices]-values[r-Length][prices];
      double sumUpDi  = 0;
      double sumDnDi  = 0;
      for(int j=1; j<Length; j++)
      {
         double gradient  = values[r][prices]+momentum*j/(Length);
         double deviation = gradient-values[r-j][prices];
            if (deviation > 0)
                  sumUpDi += deviation;
            else  sumDnDi -= deviation;
      }               
            
      //
      //
      //
      //
      //

      if ((sumUpDi+sumDnDi)!=0)
            double tTrend = 100.0*sumUpDi/(sumUpDi+sumDnDi);
      else         tTrend = 0; 
      if (T3Period>1)
           Trend[i] = iT3(tTrend,r,T3_start);
      else Trend[i] =     tTrend;
   }
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iT3(double price,int i,int s)
{
   if (i < 1)
      {
         values[i][s+0] = price;
         values[i][s+1] = price;
         values[i][s+2] = price;
         values[i][s+3] = price;
         values[i][s+4] = price;
         values[i][s+5] = price;
      }
   else
      {
         values[i][s+0] = values[i-1][s+0]+alpha*(price         -values[i-1][s+0]);
         values[i][s+1] = values[i-1][s+1]+alpha*(values[i][s+0]-values[i-1][s+1]);
         values[i][s+2] = values[i-1][s+2]+alpha*(values[i][s+1]-values[i-1][s+2]);
         values[i][s+3] = values[i-1][s+3]+alpha*(values[i][s+2]-values[i-1][s+3]);
         values[i][s+4] = values[i-1][s+4]+alpha*(values[i][s+3]-values[i-1][s+4]);
         values[i][s+5] = values[i-1][s+5]+alpha*(values[i][s+4]-values[i-1][s+5]);
      }
   return(c1*values[i][s+5] + c2*values[i][s+4] + c3*values[i][s+3] + c4*values[i][s+2]);
}

Comments