3MA_Cross_w_Alert_v2GS2
Download scriptThis script is a technical analysis tool designed to help traders identify potential trend shifts by monitoring the interaction between three different Moving Averages (MAs). Instead of simply looking for one line to cross another, this logic requires a more coordinated "agreement" between all three averages before it signals an opportunity.
Core Logic Overview
The tool tracks three distinct averages: a Fast one, a Medium one, and a Slow one. The primary goal of the script is to identify specific moments where the two faster averages (Fast and Medium) both move into position relative to the slowest average (Slow).
How the Signals Work
The script looks for "confluence"—meaning it waits for multiple conditions to be met simultaneously rather than relying on a single crossover.
For an Upward Signal: A signal is triggered if the following conditions are met:
- The Fast average crosses above the Slow average.
- The Medium average is already positioned above the Slow average.
- The script also verifies the momentum of these moves by checking the previous price positions to ensure the crossover is a distinct "breakout" rather than just a lingering overlap.
For a Downward Signal: A signal is triggered if the following conditions are met:
- The Fast average crosses below the Slow average.
- The Medium average is already positioned below the Slow average.
- Similar to the upward signal, it validates that this move represents a clear shift in direction based on previous data points.
Visual and Audio Features
To make this information easy for a trader to digest, the script provides several visual aids:
- Dynamic Line Plotting: It draws all three Moving Averages directly on your chart so you can see the current trend flow.
- Visual Markers: When the specific "confluence" logic described above is met (where both faster averages agree with the direction of the cross), the script places a visual arrow or icon on the chart at that specific price point.
- Real-Time Alerts: The script includes an automated notification system. When a valid signal is detected, it triggers an audible alert and a pop-up message on your trading platform to notify you immediately of the trend change.
Summary for the User
In simple terms, this tool filters out "noise." By requiring both the Fast and Medium averages to align with the Slow average before giving a signal, it attempts to identify higher-probability trend changes while ignoring minor fluctuations that might occur if you were only watching one moving average at a time.
//+--------------------------------------------------------------------------+
//| 3 MA Cross w_Alert v2.mq4 |
//| Copyright © 2005, Jason Robinson (jnrtrading) |
//| http://www.jnrtading.co.uk |
//| 3 ma conversion and Alert , David Honeywell , transport.david@gmail.com |
//| http://finance.groups.yahoo.com/group/MetaTrader_Experts_and_Indicators/ |
//+--------------------------------------------------------------------------+
/*
+-------------------------------------------------------------------------------+
| Allows you to enter 3 ma periods and it will then show you and alert you at |
| which point the 2 faster ma's "OPEN" are both above or below the Slowest ma . |
+-------------------------------------------------------------------------------+
*/
// mod. 2008fxtsd eachMAShift taken off; 1GeneralShift 4 all MAs &Sigs; signals after bar close only// ki
// 3MA_Cross_w_Alert_v2GS2 displacement shift for lines and signals; actual cross MAshift = 0!
#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)"
#property link "http://www.jnrtrading.co.uk"
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Aquamarine
#property indicator_color2 Magenta
#property indicator_color3 RoyalBlue
#property indicator_color4 Red
#property indicator_color5 Yellow
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 1
extern int FastMA = 5;
extern int FastMAMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
//extern int FasterShift = 0;
extern int MediumMA = 13;
extern int MediumMAMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
//extern int MediumShift = -3;
extern int SlowMA = 21;
extern int SlowMAMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
//extern int SlowerShift = -1;
extern int FastMADisplacement = 0; // 4 display only // calculated shift = 0!
extern int MediumMADisplacement = 0;
extern int SlowMADisplacement = 0;
//extern int GeneralShift = 0;
extern int SignalDisplacement = 0;
extern bool MADisplacementON = true;
extern bool SignalDisplacementON = true;
extern string note_Displacement = "calculated MAshift = 0!";
extern bool Drawlines = true;
extern bool SoundAlertON = true;
extern string note_MA_Mode = "SMA0 EMA1 SMMA2 LWMA3";
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double CrossUp[];
double CrossDown[];
double prevtime;
double Range, AvgRange;
double fastMAnow, fastMAprevious, fastMAafter;
double mediumMAnow, mediumMAprevious, mediumMAafter;
double slowMAnow, slowMAprevious, slowMAafter;
double alertTag;
double control=2147483647;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 233);//221
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 234);//222
SetIndexBuffer(1, CrossDown);
if (SignalDisplacementON)
{
SetIndexShift(0,SignalDisplacement);
SetIndexShift(1,SignalDisplacement);
}
else
{
SetIndexShift(0,0);
SetIndexShift(1,0);
}
//---- indicators
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,ExtMapBuffer1);
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,ExtMapBuffer2);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,ExtMapBuffer3);
if (MADisplacementON)
{
SetIndexShift(4,FastMADisplacement);
SetIndexShift(3,MediumMADisplacement);
SetIndexShift(2,SlowMADisplacement);
}
else
{
SetIndexShift(4,0);
SetIndexShift(3,0);
SetIndexShift(2,0);
}
SetIndexLabel(0,"3MA("+FastMA+","+MediumMA+","+SlowMA+")xUp");
SetIndexLabel(1,"3MA("+FastMA+","+MediumMA+","+SlowMA+")xDn");
SetIndexLabel(2,"3MAxFast("+FastMA+","+MediumMA+","+SlowMA+")");
SetIndexLabel(3,"3MAxMid("+FastMA+","+MediumMA+","+SlowMA+")");
SetIndexLabel(4,"3MAxSlow("+FastMA+","+MediumMA+","+SlowMA+")");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, i, counter;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(i = 0; i <= limit; i++)
{
counter=i;
Range=0;
AvgRange=0;
for (counter=i ;counter<=i+9;counter++)
{
AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range=AvgRange/10;
fastMAnow = iMA(NULL, 0, FastMA, 0, FastMAMode, PRICE_CLOSE, i+1);
fastMAprevious = iMA(NULL, 0, FastMA, 0, FastMAMode, PRICE_CLOSE, i+2);
fastMAafter = iMA(NULL, 0, FastMA, 0, FastMAMode, PRICE_OPEN, i);
mediumMAnow = iMA(NULL, 0, MediumMA, 0, MediumMAMode, PRICE_CLOSE, i+1);
mediumMAprevious = iMA(NULL, 0, MediumMA, 0, MediumMAMode, PRICE_CLOSE, i+2);
mediumMAafter = iMA(NULL, 0, MediumMA, 0, MediumMAMode, PRICE_OPEN, i);
slowMAnow = iMA(NULL, 0, SlowMA, 0, SlowMAMode, PRICE_CLOSE, i+1);
slowMAprevious = iMA(NULL, 0, SlowMA, 0, SlowMAMode, PRICE_CLOSE, i+2);
slowMAafter = iMA(NULL, 0, SlowMA, 0, SlowMAMode, PRICE_OPEN, i);
if ((fastMAnow > slowMAnow &&
fastMAprevious <= slowMAprevious &&
fastMAafter > fastMAprevious &&
mediumMAnow > slowMAnow ) // fast x slow up; med>slow
||
(fastMAnow > slowMAnow &&
mediumMAnow > slowMAnow &&
mediumMAprevious <= slowMAprevious &&
mediumMAafter > mediumMAprevious )) // med x sl up; fast >slow
{
CrossUp[i] = Low[i] - Range*0.5;
}
if ((fastMAnow < slowMAnow &&
fastMAprevious >= slowMAprevious &&
fastMAafter < fastMAprevious &&
mediumMAnow < slowMAnow ) // fast x slow dn; mid<slow
||
(fastMAnow < slowMAnow &&
mediumMAnow < slowMAnow &&
mediumMAprevious >= slowMAprevious &&
mediumMAafter < mediumMAprevious )) // mid x slow dn; fast<slow
{
CrossDown[i] = High[i] + Range*0.5;
}
if (SoundAlertON==true && i==1 && CrossUp[i] > CrossDown[i] && alertTag!=Time[0])
{
Alert("3MACross2GS going Down on ",Symbol()," M",Period()," @ ",Bid);
alertTag = Time[0];
}
if (SoundAlertON==true && i==1 && CrossUp[i] < CrossDown[i] && alertTag!=Time[0])
{
Alert("3MACross2GS going Up on ",Symbol()," M",Period()," @ ",Bid);
alertTag = Time[0];
}
//Comment(" CrossUp[0] ",CrossUp[0]," , CrossDown[0] ",CrossDown[0]," , prevtime ",prevtime);
//Comment("");
if (Drawlines)
{
ExtMapBuffer1[i]= iMA(NULL,0,FastMA, 0, FastMAMode, PRICE_CLOSE,i);
ExtMapBuffer2[i]= iMA(NULL,0,MediumMA,0, MediumMAMode,PRICE_CLOSE,i);
ExtMapBuffer3[i]= iMA(NULL,0,SlowMA, 0, SlowMAMode, PRICE_CLOSE,i);
}
}
return(0);
}
Comments