triangulatMA_v1
Download scriptThis tool is a specialized visual indicator designed for the MetaTrader platform to help traders identify trends by smoothing out market noise. Unlike standard moving averages that treat all recent price data equally, this script utilizes a "Triangular Moving Average" (TMA) logic, which prioritizes the most recent information while still maintaining a smooth, continuous flow.
Core Logic and Methodology
The indicator operates on a weighted distribution system. Instead of simply adding up prices over a set period and dividing by that number, it assigns specific importance to each data point within that window. The weights are structured so that the most current price has the highest influence, and the influence gradually decreases as you look further back into the past. This creates a "triangular" weighting effect, which is intended to make the resulting line more responsive to immediate market shifts than a traditional moving average while remaining less erratic.
Key Features and Customization
To ensure the tool meets different trading styles, it includes several flexible parameters:
- Time Period Selection: Users can define the "Periods" (the window of time) the indicator looks at. A smaller number makes the indicator more sensitive to rapid price changes, while a larger number provides a smoother, broader view of the trend.
- Price Source Versatility: The tool is highly adaptable regarding which specific data point it tracks. Rather than being locked into just the closing price, you can choose from several different market snapshots, including:
- Standard Open, High, Low, and Close prices.
- Median Price (the midpoint between high and low).
- Typical Price (an average of the high, low, and close).
- Weighted Close Price (a weighted average of the price range).
Visual Output
On your trading chart, the script generates a yellow line. By calculating the weighted average of the chosen price type across the specified period, it provides a smoothed trajectory of market movement. Traders use this visual guide to better understand the underlying direction of the market, filtering out minor fluctuations to identify more significant trends.
/*-----------------------------+
| |
| 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 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