Author: Matias Romeo
TMA_v1

This tool is a specialized visual indicator designed to help traders identify trends by smoothing out price movements on a trading chart. It utilizes a specific mathematical approach known as a Triangular Moving Average (TMA), which differs from standard moving averages by distributing weight more evenly across the data points.

How the Logic Works

1. Weight Distribution Unlike many standard indicators that give equal importance to all previous prices, this script assigns a "weight" to each price point within a specific timeframe. It creates a triangular weighting system where the most recent and older data points are balanced in a way that produces a smoother, more rounded line. This helps reduce the "noise" or erratic jumps often seen in live markets, making it easier to see the underlying trend.

2. Dynamic Price Selection The indicator is flexible regarding which data point it tracks. Users can choose which part of the price action the logic should focus on. The script can calculate the average based on:

  • The standard Closing price.
  • The Opening, High, or Low prices.
  • Calculated averages such as the Median (Middle of the high/low range), Typical (Average of high, low, and close), or Weighted Close.

3. Calculation Process For every candle on the chart, the script looks back over a user-defined number of periods (the "Periods" setting). It multiplies each price by its corresponding weight, adds them together, and then divides the total by a fixed sum (the divisor). By performing this calculation continuously across the chart, it generates a smooth green line.

Summary of Behavior

The primary goal of this logic is trend smoothing. Because it uses a triangular weighting system rather than a simple linear one, the resulting line reacts more gradually to price changes. This allows traders to better visualize the momentum of the market without being distracted by minor, temporary fluctuations in price.

Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
TMA_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                          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 Green

//Input parameters
extern int       Periods=30;
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