This technical tool is designed to help traders identify potential trend reversals by monitoring the interaction between two different Moving Averages (MA) on a price chart. It simplifies market analysis by automating the detection of "crossovers," which are moments when short-term price momentum shifts relative to long-term trends.
Core Functionality
The script calculates and displays two distinct moving averages on your chart simultaneously:
- Fast Moving Average: A quicker indicator that reacts rapidly to recent price fluctuations.
- Slow Moving Average: A steadier indicator that tracks the broader, long-term trend of the asset.
By visualizing these two lines, users can see whether the current price momentum is accelerating or slowing down compared to the historical average.
Detection Logic and Visual Alerts
The primary purpose of this script is to automatically identify the exact moment these two lines cross each other. Rather than requiring a trader to watch the screen constantly, the logic performs the following:
- Bearish Signals (Downside): If the Fast Moving Average was previously above the Slow Moving Average but moves below it, the script recognizes a potential downward shift in momentum. It automatically places a visual arrow above the price candle and triggers an audible alert on your platform.
- Bullish Signals (Upside): Conversely, if the Fast Moving Average was previously below the Slow Moving Average but crosses above it, the script identifies a potential upward trend. It places a visual arrow below the price candle and triggers an audible alert.
Smart Features
- Real-Time Monitoring: The script is designed to track live price action. It includes a safeguard to ensure that alerts are only triggered once per bar, preventing your platform from sending multiple repetitive notifications for the same single crossover event.
- Dynamic Timeframe Awareness: When an alert is triggered, the script identifies which timeframe you are currently viewing (e.g., M15, H1, D1) and includes that information in the notification message, providing immediate context for your trading decision.
- Customizable Parameters: Users can adjust the "speed" of both moving averages (the period length), as well as the calculation method and price source, allowing the tool to be tailored to different types of assets or trading styles.
//+------------------------------------------------------------------+
//| 2xMA.mq4 |
//| Yuriy Tokman |
//| yuriytokman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Yuriy Tokman"
#property link "yuriytokman@gmail.com"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Yellow
#property indicator_color4 Red
extern string __1__ = "Fast MA";
extern int PeriodMA_1 = 5;
extern int ma_method_1 = 1;//0-3
extern int applied_price_1 = 6;//0-6
extern string __2__ = "Slow MA";
extern int PeriodMA_2 = 12;
extern int ma_method_2 = 1;//0-3
extern int applied_price_2 = 6;//0-6
extern string __3__ = "Another";
extern int alertBar = 0;
extern int arrow_code_up = 218;
extern int arrow_code_down = 217;
extern int width = 3;
double ExtMapBuffer0[];
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
datetime LastTime=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer0);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer1);
SetIndexStyle(2,DRAW_ARROW,EMPTY,width);
SetIndexArrow(2,arrow_code_up);
SetIndexBuffer(2,ExtMapBuffer2);
SetIndexEmptyValue(2,0.0);
SetIndexStyle(3,DRAW_ARROW,EMPTY,width);
SetIndexArrow(3,arrow_code_down);
SetIndexBuffer(3,ExtMapBuffer3);
SetIndexEmptyValue(3,0.0);
IndicatorShortName("2xMA ("+PeriodMA_1+")");
SetIndexLabel(0," ISQ#481971287 ");
SetIndexLabel(1," yuriytokman@gmail.com ");
SetIndexLabel(2," ISQ#481971287 ");
SetIndexLabel(3," yuriytokman@gmail.com ");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
//----
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int i=limit; i>=0; i--)
{
double MA_1_0 = iMA(NULL,0,PeriodMA_1,0,ma_method_1,applied_price_1,i+alertBar);
double MA_1_1 = iMA(NULL,0,PeriodMA_1,0,ma_method_1,applied_price_1,i+alertBar+1);
double MA_2_0 = iMA(NULL,0,PeriodMA_2,0,ma_method_2,applied_price_2,i+alertBar);
double MA_2_1 = iMA(NULL,0,PeriodMA_2,0,ma_method_2,applied_price_2,i+alertBar+1);
ExtMapBuffer0[i] = MA_1_0;
ExtMapBuffer1[i] = MA_2_0;
if (MA_1_1>=MA_2_1 && MA_1_0<MA_2_0 && Time[i]!= LastTime)
{
ExtMapBuffer2[i]=High[i]+5*Point;
LastTime = Time[i];
Alert("MAcrossDown"," TF",GetNameTF());
}
if (MA_1_1<=MA_2_1 && MA_1_0>MA_2_0 && Time[i]!= LastTime)
{
ExtMapBuffer3[i]=Low[i]-5*Point;
LastTime = Time[i];
Alert("MAcrossUp "," TF",GetNameTF());
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
//| Àâòîð : Êèì Èãîðü Â. aka KimIV, http://www.kimiv.ru |
//+----------------------------------------------------------------------------+
//| Âåðñèÿ : 01.09.2005 |
//| Îïèñàíèå : Âîçâðàùàåò íàèìåíîâàíèå òàéìôðåéìà |
//+----------------------------------------------------------------------------+
//| Ïàðàìåòðû: |
//| TimeFrame - òàéìôðåéì (êîëè÷åñòâî ñåêóíä) (0 - òåêóùèé ÒÔ) |
//+----------------------------------------------------------------------------+
string GetNameTF(int TimeFrame=0) {
if (TimeFrame==0) TimeFrame=Period();
switch (TimeFrame) {
case PERIOD_M1: return("M1");
case PERIOD_M5: return("M5");
case PERIOD_M15: return("M15");
case PERIOD_M30: return("M30");
case PERIOD_H1: return("H1");
case PERIOD_H4: return("H4");
case PERIOD_D1: return("D1");
case PERIOD_W1: return("W1");
case PERIOD_MN1: return("MN");
default: return("UnknownPeriod");
}
}
Comments