TMA
Download scriptThis tool is designed to provide a smoothed representation of market price action by calculating a specific type of moving average known as the Triangulated Moving Average (TMA). Unlike standard moving averages that give equal weight to all data points in a set period, this script utilizes a unique weighting system to produce a smoother trend line.
How the Logic Works
1. Weighted Price Distribution The core logic of this script lies in how it values historical data. Instead of treating every price point within your chosen time frame (the "Periods") as equally important, the script applies a triangular weight. It assigns higher importance to the most recent prices and lower importance to older prices. This creates a "weighted" calculation where the influence of the price gradually increases as it moves toward the current moment, helping to filter out "market noise" more effectively than a simple average.
2. Flexible Price Input The script is highly adaptable regarding which market data it analyzes. You can choose to base the calculations on various types of price information, including:
- Standard Prices: Open, High, Low, or Close.
- Calculated Mid-points: Median (Average of High and Low), Typical (Average of High, Low, and Close), or Weighted Close prices.
3. Dynamic Calculation Engine Every time the market updates, the script performs a comprehensive calculation across your specified timeframe:
- It identifies the price type you have selected.
- It retrieves the historical data for the defined period.
- It multiplies each historical price by its specific weight based on its age.
- Finally, it divides the total sum by a constant divisor to produce a single, smooth trend line visible on your chart.
Summary of User Experience
For the trader, this means the resulting green line on your chart represents a smoothed "consensus" of the market's direction. Because it prioritizes recent data through its triangular weighting system, it aims to provide a clearer picture of the current trend by reducing the erratic spikes often seen in standard price charts.
//+------------------------------------------------------------------+
//| 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