TriangularMA
Download scriptThis tool is a specialized technical analysis indicator designed to smooth out market price fluctuations, making it easier for traders to identify underlying trends without being distracted by "noise" or minor price spikes. It functions as a weighted moving average, meaning it prioritizes more recent data points over older ones to provide a more responsive and relevant view of the current market direction.
How the Logic Works
The core logic of this script revolves around three main components: Price Selection, Weighted Calculation, and Dynamic Averaging.
1. Flexible Price Selection Rather than simply looking at the final closing price of a candle, the tool allows users to choose which specific data point from the market history should be analyzed. You can select various metrics, including:
- The standard opening or closing prices.
- The highest and lowest prices reached during a period.
- Calculated averages like the "Median" (middle of the high/low range) or "Typical" prices.
2. Weighted Distribution Unlike a standard moving average where every day in a period carries equal importance, this script applies a specific mathematical weight to each price point. It creates a distribution where the most recent price data is given more significance than older data. By "weighting" the values, the indicator becomes more sensitive to what is happening right now while still maintaining the stability of historical context.
3. The Smoothing Process To arrive at the final line you see on your chart:
- The tool looks back over a specific number of periods (which can be customized by the user).
- It retrieves the price for each of those periods based on your chosen selection.
- It multiplies each price by its corresponding weight.
- It then sums these values and divides them by a total divisor to produce a single, smoothed average value for that moment in time.
Summary for the User
The end result is a fluid line on your trading chart that represents a "weighted" consensus of market activity. Because it favors recent price action, it reacts more dynamically to new information than a traditional moving average, helping you visualize the current momentum of the market more clearly.
//+------------------------------------------------------------------+
//| 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