TMAcentered

Download script
Author: mladen
TMAcentered

This tool is designed to provide a specialized technical analysis visual for traders, specifically by creating a "Triangular Moving Average." Unlike standard moving averages that treat all data points in a set equally, this script applies a sophisticated weighting system to prioritize data based on its proximity to the current price point.

Core Logic and Mechanism

The primary objective of this indicator is to create a smoothed trend line that reacts more dynamically to recent price action while maintaining a "centered" perspective. It achieves this through three main steps:

1. Dynamic Weight Distribution The script calculates a specific weight for every data point within a designated window (determined by the "HalfLength" setting). Instead of a flat average, it builds a triangular distribution. In this model, prices closer to the center of the calculation are given more significance than those at the edges. This creates a balanced curve that filters out minor market "noise" while remaining sensitive to meaningful trend shifts.

2. Multi-Layered Calculation The logic processes the data in two distinct phases:

  • Primary Calculation: It first establishes a baseline by calculating weights across the full length of the observation window. It ensures that the influence of historical data tapers off as it moves further away from the current candle.
  • Refined Centering: The script then performs a secondary pass to ensure the average is perfectly centered. By analyzing prices both ahead and behind the current position (where applicable), it calculates a weighted sum that balances the "pull" of past performance against future potential.

3. Visual Output The final result is rendered as a continuous line on your chart. Because it uses triangular weighting, the resulting line appears smoother and more fluid than a standard Simple Moving Average (SMA). It is designed to help traders identify the underlying direction of the market by highlighting the "true" average price without the erratic jaggedness often found in basic indicators.

Key Takeaways for the User

  • Smoothing: It reduces market volatility visually, making it easier to spot long-term trends.
  • Weighted Importance: By using a triangular logic, the indicator focuses on the most relevant price data points while still considering the broader historical context.
  • Balanced Response: The "centered" nature of the logic means the indicator aims to provide a balanced view of where the price is heading, rather than lagging significantly behind the current candle.
Indicators Used
Moving average indicator
13 Views
0 Downloads
0 Favorites
TMAcentered
//+------------------------------------------------------------------+
//|                                        TriangularMA centered.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 LimeGreen

//
//
//
//
//

extern int HalfLength = 12;
extern int Price      = PRICE_CLOSE;

//
//
//
//
//

double buffer1[];
double weights[];
double sumWeights=0;
int    fullLength;

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

int init()
{
   HalfLength=MathMax(HalfLength,1);
      fullLength = HalfLength*2+1;
      ArrayResize(weights,fullLength);
      
      //
      //
      //
      //
      //
            
      sumWeights          = HalfLength+1;
      weights[HalfLength] = HalfLength+1;
      for (int i=0; i<HalfLength; i++)
      {
         weights[i]              = i+1;
         weights[fullLength-i-1] = i+1;
         sumWeights             += i+i+2;
      }
   SetIndexBuffer(0,buffer1); SetIndexDrawBegin(0,HalfLength);
   return(0);
}
int deinit() { return(0); }




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

int start()
{
   double sum,sumw;
   int counted_bars=IndicatorCounted();
   int i,j,k,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;

   //
   //
   //
   //
   //
   
   for (i=limit;i>=0;i--)
   {
      for(sum=0.0, j=0; j<fullLength; j++) sum += iMA(NULL,0,1,0,MODE_SMA,Price,i+j)*weights[j]; buffer1[i+HalfLength] = sum/sumWeights;
   }
   for (i=HalfLength-1;i>=0;i--)
   {
      sum  = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i);
      sumw = (HalfLength+1);

      //
      //
      //
      //
      //
      
      for(j=1,k=HalfLength; j<HalfLength; j++,k--)
      {
         sum  += iMA(NULL,0,1,0,MODE_SMA,Price,i+j)*k;
         sumw += k;
         if (j<=i)
         {
               sum  += iMA(NULL,0,1,0,MODE_SMA,Price,i-j)*k;
               sumw += k;
         }
      }
      buffer1[i] = sum/sumw;
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}

Comments