This script is designed to serve as a visual and audible monitoring tool for traders who use Moving Averages (MA) to identify trends. It specifically tracks the interactions between three different moving averages: a Fast, a Medium, and a Slow average.
Core Functionality
The primary purpose of this tool is to detect "crosses." In trading, a cross occurs when a faster-moving average moves from below a slower one to above it (a bullish signal), or vice versa (a bearish signal). Because this script tracks three different averages, it identifies three distinct types of interactions:
- Fast vs. Medium: Captures rapid shifts in short-term momentum.
- Fast vs. Slow: Captures very immediate price movements relative to long-term trends.
- Medium vs. Slow: Captures intermediate trend changes.
How the Logic Works
- Movement Tracking: The script constantly calculates the mathematical distance between these averages. It looks for the exact moment the relationship between two lines flips (e.g., when the gap changes from a negative value to a positive value).
- Visual Signals: When a cross is detected, the script places an arrow on your chart.
- The direction of the arrow depends on whether the faster average is moving "Up" over the slower one or "Down" under it.
- The placement of these arrows is adjusted dynamically based on the recent price range (volatility) to ensure they are clearly visible and not overlapping with the price bars.
- Smart Alerting: To prevent your workspace from being flooded with repetitive notifications, the script includes a verification system. It ensures that an alert is only triggered once per candle for any specific type of cross.
- Notification Options: You can choose how you receive these signals. The script is capable of providing:
- On-Screen Pop-ups: Visual alerts within the trading platform.
- Email Notifications: Sending a summary of the cross, the asset name, and the current price to your registered email.
- Audio Alerts: Playing a sound file to notify you of a change immediately.
Key User Features
- Customizable Speed: You can define exactly how many periods each moving average covers (e.g., 5, 13, and 34) and which type of calculation method to use (SMA, EMA, etc.).
- Real-Time vs. Confirmed Crosses: You can toggle the settings to see alerts as they happen in real-time (on the current forming candle) or wait until the candle closes to confirm the cross has officially happened.
- Dynamic Range Adjustment: The script automatically calculates the recent "Range" of price movement to ensure that the visual arrows are placed at a professional distance from the price candles.
//+--------------------------------------------------------------------------+
//| 2008forextsd mladen MA_Cross_3MACross_AlertWarnSig |
//+--------------------------------------------------------------------------+
#property copyright ""
#property link ""
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Yellow
#property indicator_color4 Gold
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
//
//
//
//
//
extern int FasterMA = 5;
extern int FasterMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
extern int MediumMA = 13;
extern int MediumMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
extern int SlowerMA = 34;
extern int SlowerMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma
extern bool crossesOnCurrent = true;
extern bool alertsOn = true;
extern bool alertsMessage = true;
extern bool alertsSound = false;
extern bool alertsEmail = false;
extern string MA_Shift ="all MA_Shift = 0";
extern string MA_Mode = "SMA0 EMA1 SMMA2 LWMA3";
//
//
//
//
//
double CrossUp[];
double CrossDown[];
double CrossWUp[];
double CrossWDown[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
SetIndexBuffer(0, CrossUp); SetIndexStyle(0, DRAW_ARROW); SetIndexArrow(0, 233);
SetIndexBuffer(1, CrossDown); SetIndexStyle(1, DRAW_ARROW); SetIndexArrow(1, 234);
SetIndexBuffer(2, CrossWUp); SetIndexStyle(2, DRAW_ARROW); SetIndexArrow(2, 233);
SetIndexBuffer(3, CrossWDown); SetIndexStyle(3, DRAW_ARROW); SetIndexArrow(3, 234);
return(0);
}
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
int limit, i;
int counted_bars=IndicatorCounted();
double fasterMAnow, fasterMAprevious, fasterMAafter;
double mediumMAnow, mediumMAprevious, mediumMAafter;
double slowerMAnow, slowerMAprevious, slowerMAafter;
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//
//
//
//
//
//
for(i = limit; i >=0; i--)
{
double Range = 0.0;
for (int counter=i ;counter<=i+9;counter++) Range += MathAbs(High[counter]-Low[counter]);
Range /= 10;
//
//
//
//
//
fasterMAnow = iMA(NULL, 0, FasterMA, 0,FasterMode, PRICE_CLOSE, i);
fasterMAprevious = iMA(NULL, 0, FasterMA, 0,FasterMode, PRICE_CLOSE, i+1);
mediumMAnow = iMA(NULL, 0, MediumMA, 0,MediumMode, PRICE_CLOSE, i);
mediumMAprevious = iMA(NULL, 0, MediumMA, 0,MediumMode, PRICE_CLOSE, i+1);
slowerMAnow = iMA(NULL, 0, SlowerMA, 0,SlowerMode, PRICE_CLOSE, i);
slowerMAprevious = iMA(NULL, 0, SlowerMA, 0,SlowerMode, PRICE_CLOSE, i+1);
//
//
// avoid current bar if not allowed to check crosses on current (i==0)
//
//
if (crossesOnCurrent==false && i==0) continue;
//
//
//
//
//
int crossID=0;
double curr;
double prev;
double point;
while (true)
{
curr = fasterMAnow - mediumMAnow;
prev = fasterMAprevious - mediumMAprevious;
point = (fasterMAnow + fasterMAprevious)/2;
if (curr*prev<=0) { crossID=1; break; }
curr = fasterMAnow - slowerMAnow;
prev = fasterMAprevious - slowerMAprevious;
if (curr*prev<=0) { crossID=2; break; }
curr = mediumMAnow - slowerMAnow;
prev = mediumMAprevious - slowerMAprevious;
point = (mediumMAnow + mediumMAprevious)/2;
if (curr*prev<=0) { crossID=3; break; }
break;
}
//
//
// the interesting thing is the direction of the crossing
// which MA is the primary and which one the secondary
// if you do not know that you can not determine the "direction"
// of the cross
//
//
//
CrossUp[i] = EMPTY_VALUE;
CrossDown[i] = EMPTY_VALUE;
CrossWUp[i] = EMPTY_VALUE;
CrossWDown[i] = EMPTY_VALUE;
if (crossID>0)
{
if (alertsOn)
if ((i==0 && crossesOnCurrent==true) || (i==1 && crossesOnCurrent==false))
{
switch (crossID)
{
case 1:
if(curr>0) doAlert(" 3MACross: Fast MA crossed Medium MA UP");
else doAlert(" 3MACross: Fast MA crossed Medium MA DOWN");
break;
case 2:
if(curr>0) doAlert(" 3MACross: Fast MA crossed Slow MA UP");
else doAlert(" 3MACross: Fast MA crossed Slow MA DOWN");
break;
case 3:
if(curr>0) doAlert(" 3MACross: Medium MA crossed Slow MA UP");
else doAlert(" 3MACross: Medium MA crossed Slow MA DOWN");
break;
}
}
//
//
//
//
//
if (i==0)
{
if (curr>0)
CrossWUp[i] = point - Range*0.5;
else CrossWDown[i] = point + Range*0.5;
}
else
{
if (curr>0)
CrossUp[i] = point - Range*0.5;
else CrossDown[i] = point + Range*0.5;
}
}
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
void doAlert(string doWhat)
{
static string previousAlert="nothing";
static datetime previousTime;
string message;
if (previousAlert != doWhat || previousTime != Time[0]) {
previousAlert = doWhat;
previousTime = Time[0];
//
//
//
//
//
// if time needed :
// message = StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," @",Bid," ", doWhat);
//
message = StringConcatenate(Symbol()," at ",Bid," ", doWhat);
if (alertsMessage) Alert(message);
if (alertsEmail) SendMail(StringConcatenate(Symbol()," 3MACross:"," M",Period()),message);
if (alertsSound) PlaySound("alert2.wav");
}
}
Comments