MAcrosses
Download scriptThis tool is designed to measure the overall momentum and trend strength of a financial instrument by analyzing the relationship between multiple Moving Averages (MAs) simultaneously. Instead of looking at just one line, it evaluates the "hierarchy" of various averages to determine if the market is in a state of strong agreement.
How the Logic Works
1. The Multi-Layer Analysis The script generates a series of moving averages based on a maximum period you define (e.g., 50). Rather than looking at these in isolation, the logic compares every faster moving average against every slower one. For example, it checks if the shortest averages are positioned above the medium ones, and the medium ones above the longest ones.
2. The "Cross" Calculation The core of the script is a counting mechanism:
- Bullish Alignment: Every time a faster moving average is higher than a slower one, the script registers a "cross."
- Bearish Alignment: If a faster average drops below a slower one, it is not counted toward the positive score.
3. The Percentage Score The script then converts these counts into a percentage scale from 0% to 100%:
- 100% Score: This indicates perfect bullish alignment. It means every single moving average in the set is positioned above the ones that follow it, suggesting a very strong upward trend.
- 0% Score: This indicates perfect bearish alignment. It means every moving average is positioned below the ones following it, suggesting a dominant downward trend.
- Intermediate Percentages: These represent "mixed" market conditions where some averages are crossing over others, indicating indecision or a transitioning trend.
4. Trend Smoothing To make the data easier for a human to read, the script applies an additional secondary moving average to the percentage results. This smooths out minor fluctuations (noise) in the data, allowing you to see the broader trend of the market's sentiment more clearly without being distracted by every tiny price tick.
Summary for the User
Think of this indicator as a "Market Consensus Meter." While a single moving average might give you a false signal during a choppy market, this script looks at the "big picture" by ensuring that the entire stack of averages is moving in harmony. A high percentage suggests a unified bullish move, while a low percentage suggests a unified bearish move.
//+-------------------------------------------------------------------------------------------------------+
//| MAcrosses.mq4 |
//| Copyright © 2011, Matus German |
//| http://www.MTexperts.net/ |
//| Counts crosses off MAs, it shows % that all MAs are above slower MAs |
//| If an MA crosses above another, the indicator rises |
//| If an MA crosses under another, the indicator falls |
//| If the indicator rises to 100%, every MA is above MAs with higher period |
//| If the indicator falls to 0%, every MA is under MAs with higher period |
//+-------------------------------------------------------------------------------------------------------+
#property copyright "Copyright © 2011, Matus German"
#property link "http://www.MTexperts.net/"
#property indicator_separate_window
#property indicator_buffers 2 // Number of buffers
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_color1 Red
#property indicator_color2 Blue
double MAcrs[];
double MAcrsAvg[];
extern int MAmax = 50; // the maximal MA
extern int Step = 5; // the increse between MAs, optimal MA/10
extern int MAavg = 20; // parameter to calculate moving average from MAcrosses
extern int MAmethod = 1; // iMA moving average method:
// 0 Simple moving average,
// 1 Exponential moving average,
// 2 Smoothed moving average,
// 3 Linear weighted moving average.
extern int AppliedPrice = 0; // Applied to price:
// 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.
double maxCrosses;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(2);
SetIndexBuffer(0,MAcrs);
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
SetIndexBuffer(1,MAcrsAvg);
SetIndexStyle (1,DRAW_LINE,STYLE_DASH,1);
//----
SetLevelValue(0,10);
SetLevelValue(1,90);
maxCrosses=max(MAmax,Step);
return;
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
// Function counts how many crosses could be made by MAs
double max(int MA,int MAstep)
{
/*
int max=0;
int MAcount=MA/MAstep;
while(MAcount>0)
{
MAcount=MAcount-1;
max=max+MAcount;
}
*/
int i=MA;
int MAs=0;
while(i>0)
{
i-=MAstep;
MAs++;
}
double max=((MAs*MAs)-MAs)/2;
return (max);
}
////////////////////////////////////////////////////////////////////
int start()
{
int counted_bars,i,j,k;
double crosses;
if(Bars<=MAmax) return(0);
counted_bars=IndicatorCounted(); // Number of counted bars
i=Bars-counted_bars-1; // Index of the first uncounted
while(i>=0)
{
crosses=0;
k=MAmax;
while(k>0)
{
j=MAmax;
while(j>k)
{
if(iMA(NULL,0,j,0,MAmethod,AppliedPrice,i)<iMA(NULL,0,k,0,MAmethod,AppliedPrice,i))
{
crosses=crosses+1; // count crosses
}
j=j-Step;
}
k=k-Step;
}
MAcrs[i]=(crosses/maxCrosses)*100; // % that all MAs are abowe slower MAs
double MAcrsSum=0;
int l;
for(l=i; l<i+MAavg;l++)
{
MAcrsSum = MAcrsSum + MAcrs[l];
}
MAcrsAvg[i]=MAcrsSum/MAavg; // MAcrosses average
i--;
}
return;
}
//+------------------------------------------------------------------+
Comments