_time_avg_v1.0
Download scriptThis tool is designed to analyze market price movements by tracking how often and how significantly the price trend reverses over a specific period. Instead of just showing you where the price is, it calculates the "volatility" and "frequency" of trend changes based on two moving averages.
How the Logic Works
1. Trend Identification The script uses two different moving averages (short-term and long-term) to determine the current direction of the market. When these two lines cross each other, the script identifies a change in momentum—switching from an upward trend to a downward one, or vice versa.
2. Measuring Momentum Volatility Every time a "cross" occurs, the tool measures the distance between the highest price peak and the lowest price valley reached during that specific movement. By calculating this range, the script determines how much "ground" was covered during each swing.
3. Statistical Averaging Rather than showing you individual spikes, the script performs several mathematical operations to smooth out the data:
- Average Movement: It takes all the price swings identified within a set window and calculates their average size. This tells you, on average, how many points (pips) the market moves in a single trend cycle.
- Frequency of Change: It counts how many times these crossovers happen within the designated timeframe.
4. Visual Output The final result is displayed in a separate window as two distinct lines:
- Primary Line: Represents the average magnitude of the price movement (normalized to your broker's points). This helps you understand the "strength" or "reach" of the market trends.
- Secondary Line: Represents the frequency or density of these changes, helping you see how often the market is flipping between directions.
Summary for Traders
In simple terms, this script filters out the daily noise to show you the average strength of a trend and how frequently the market reverses. It helps you visualize whether the current market behavior is characterized by long, sweeping movements or frequent, choppy changes in direction.
//+------------------------------------------------------------------+
//| time_avg_v1.0.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_buffers 2
#property indicator_separate_window
#property indicator_color1 MediumTurquoise
#property indicator_width1 2
#property indicator_color2 CadetBlue
#property indicator_width2 1
extern int MA1=12;
extern int MA2=24;
extern int Crosses=50;
extern int Bars_=600;
double buff1[],buff2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(2);
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, buff1);
SetIndexStyle(1, DRAW_LINE,STYLE_DOT);
SetIndexBuffer(1, buff2);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int cnt;
int dir,a;
double h_u,h_d,l_u,l_d;
double delta_u,delta_d;
//----
dir=1;
cnt=0;
for(int i=Bars_;i>0;i--)
{
double m1_=iMA(NULL,Period(),MA1,0,MODE_SMA,PRICE_CLOSE,i);
double m2_=iMA(NULL,Period(),MA2,0,MODE_SMA,PRICE_CLOSE,i);
cnt=1;
h_u=0; h_d=0; l_u=100000; l_d=100000;
delta_u=0;
int q=i;
while(cnt<50)
{
double m1=iMA(NULL,Period(),MA1,0,MODE_SMA,PRICE_CLOSE,q);
double m2=iMA(NULL,Period(),MA2,0,MODE_SMA,PRICE_CLOSE,q);
if(m1>m2&&dir==-1)
{
dir=1;
cnt++;
}
if(m1<m2&&dir==1)
{
dir=-1;
cnt++;
}
q++;
}
int aaa=q-i;
cnt=1;
h_u=0; h_d=0; l_u=100000; l_d=100000;
delta_u=0;
while(q>i)
{
m1=iMA(NULL,Period(),MA1,0,MODE_SMA,PRICE_CLOSE,q);
m2=iMA(NULL,Period(),MA2,0,MODE_SMA,PRICE_CLOSE,q);
if(High[q]>h_u)h_u=High[q];
if(Low[q]<l_u)l_u=Low[q];
if(m1>m2&&dir==-1)
{
dir=1;
cnt++;
delta_u=delta_u+(h_u-l_u);
h_u=0; h_d=0; l_u=100000; l_d=100000;
}
if(m1<m2&&dir==1)
{
dir=-1;
cnt++;
delta_u=delta_u+(h_u-l_u);
h_u=0; h_d=0; l_u=100000; l_d=100000;
}
q--;
}
delta_u=delta_u/cnt;
delta_u=delta_u/2;
cnt=aaa/cnt;
buff2[i]=cnt;
buff1[i]=NormalizeDouble(delta_u/Point,0);
}
if (!IsTesting()){
Comment(" Áîêîâûå îòêëîíåíèÿ: "+DoubleToStr((delta_u/Point),0)+" ïóíêòîâ\n Ñðåäíåå êîëè÷åñòâî áàðîâ: "+cnt+"\n Ïåðåñå÷åíèé: "+ Crosses+"\n Èñïîëüçóåòñÿ áàðîâ: "+aaa);
}
return(0);
}
//+------------------------------------------------------------------+
Comments