triangulatMA

Download script
Author: Matias Romeo
triangulatMA

This script is designed to provide a specialized moving average on your trading charts, specifically known as the Triangular Moving Average (TMA). Unlike standard moving averages that treat all data points in a sequence with equal importance, this tool uses a weighted calculation to create a smoother, more responsive line that helps traders identify trends while filtering out "noise."

How it Works

The logic behind this script follows three main steps:

1. Weighted Distribution Instead of simply adding up the last few prices and dividing by the total number of bars, the script assigns different "weights" to each data point within your chosen timeframe (the Periods). It prioritizes the middle of the data set more heavily than the edges. By doing this, it creates a "triangular" weight distribution, which results in a smoother curve that is less susceptible to sudden, erratic price spikes.

2. Custom Price Selection The tool is highly flexible regarding what information it looks at. You can choose whether the average should be calculated based on:

  • Standard prices: Such as the Open, High, Low, or Close.
  • Derived prices: Including the Median (average of high/low), Typical (average of high/low/close), or Weighted Close price. This allows you to tailor the indicator to specific trading styles, whether you prefer following the actual closing price or the total range of the candle's movement.

3. Dynamic Calculation The script automatically calculates the "Divisor"—the sum of all weights used—to ensure that the final line on your chart is perfectly balanced and scaled. Every time the market moves, the script looks back over your chosen period, applies the triangular weighting to the selected price types, and plots the resulting smooth line in yellow on your chart window.

Key Takeaway for Traders

The primary goal of this logic is smoothing. Because it uses a weighted approach rather than a simple linear average, it is intended to provide a clearer picture of the current market trend by smoothing out the "zig-zags" of price action while remaining more responsive than a standard moving average.

Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
triangulatMA
//+------------------------------------------------------------------+
//|                                                          TMA.mq4 |
//|                                                     Matias Romeo |
//|                                    mailto:matias.romeo@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Matias Romeo"
#property link      "mailto:matias.romeo@gmail.com"


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Yellow


//Input parameters
extern int       Periods=7;
extern int       ApplyTo=0;


//Apply To like "Applied price enumeration"
//0 Close price. 
//1 Open price. 
//2 High price. 
//3 Low price. 
//4 Median price, (high+low)/2. 
//5 Typical price, (high+low+close)/3. 
//6 Weighted close price, (high+low+close+close)/4


//Indicator buffers
double tma[];


//Global variables
int    weights[];
double divisor;


int init()
{
  SetIndexStyle(0,DRAW_LINE);
  SetIndexBuffer(0,tma);


  IndicatorShortName( "TMA(" + Periods + ")" );


  //Calculate weigths
  ArrayResize(weights,Periods);


  double dPeriods = Periods;
  int to=MathCeil(dPeriods/2.0);


  for(int i=0; i<to; i++)
  {
    weights[i]            = i+1;
    weights[Periods-1-i]  = i+1;
  }


  divisor = 0.0;
  for(int j=0; j<Periods; j++)
    divisor += weights[j];


  return(0);
}


int deinit()
{
  return(0);
}
int start()
{
  int    counted_bars = IndicatorCounted();
  if( counted_bars < 0 )
    return(-1);
  
  //Recalculate last bar
  if( counted_bars > 0 )
    counted_bars++;
  
  int limit = Bars - counted_bars;
  for(int i=limit-1; i>=0; i--)
  {
    double tma_val = 0.0;
    for(int j=0; j<Periods; j++)
    {
      double price = getPrice(ApplyTo, i+Periods-j);
      tma_val += price*weights[j];
    }


    tma[i] = tma_val/divisor;
  }
  
  return(0);
}


double getPrice(int priceType, int index)
{
  double price = 0.0;
  
  switch(priceType)
  {
    case PRICE_OPEN    : price = Open[index];
                         break;
  
    case PRICE_HIGH    : price = High[index];
                         break;
  
    case PRICE_LOW     : price = Low[index];
                         break;
  
    case PRICE_MEDIAN  : price = (High[index]+Low[index])/2.0;
                         break;
  
    case PRICE_TYPICAL : price = (High[index]+Low[index]+Close
[index])/3.0;
                         break;


    case PRICE_WEIGHTED: price = (High[index]+Low[index]+2*Close
[index])/4.0;
                         break;


    
    case PRICE_CLOSE   : 
    default            : price = Close[index];
                         break;


  }
  
  return(price);
}

Comments