MA_Price_Cross1.1
Download scriptThis tool is designed to help traders identify specific moments when the market price interacts with a Moving Average (MA). Instead of requiring you to constantly watch every tick of the market, this script automatically detects when the price "breaks through" the average and provides instant notifications.
Core Functionality
The script monitors the relationship between the current market price and a user-defined Moving Average. It specifically looks for "crosses"—moments where the price moves from being below the average to above it, or vice versa.
Key Technical Logic:
- Customizable Moving Averages: You can choose from various types of Moving Averages (such as SMA, EMA, SMMA, or LWMA) and set the specific period (the length of time used to calculate the average) to suit your trading style.
- Precision Detection: The script doesn't just check if the price is currently above or below the line; it looks for a three-point confirmation. It analyzes the current price, the previous candle, and the one before it to ensure that a definitive "cross" has occurred. This helps filter out minor fluctuations and identifies actual trend shifts.
- Dynamic Visual Marking: When a crossover is detected, the script places visual markers (arrows) on your chart. It intelligently calculates the distance for these arrows based on the recent price volatility, ensuring they are clearly visible without cluttering the screen.
Notification System:
To ensure you never miss a signal, the script includes a multi-channel alert system that can be customized in the settings:
- Pop-up Alerts: A visual window will appear on your trading platform immediately upon a crossover.
- Audio Signals: The script can trigger a specific sound file to grab your attention.
- Email Notifications: You can configure the tool to send an email directly to your inbox, allowing you to monitor the market even when you are away from your computer.
In summary, this tool acts as an automated sentry, scanning the charts to alert you exactly when the price moves across a significant moving average, providing a clear signal for potential trend changes.
//+------------------------------------------------------------------+
//| MA-Cross/Price Signal.mq4 |
//+------------------------------------------------------------------+
/* Copyright © 2008, GODFREYH
+------------------------------------------------------------------+
| Allows you to enter a ma period and it will then show you and |
| email you at which point price crossed the MA. |
+------------------------------------------------------------------+
*/
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
double CrossUp[];
double CrossDown[];
extern string MA_Type_A = "0=sma, 1=ema";
extern string MA_Type_B = "2=smma, 3=lwma";
extern int MA_Type_selected = 1; //0=sma, 1=ema, 2=smma,
extern int MA_Period =50;
double PRICECLOSE;
extern bool Alert_On_Crossing = true;
extern bool POP_UP_ON = true;
extern bool EMAIL_ON = false;
extern string NameFileSound = "alert.wav";
string MAshort_name;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW,EMPTY);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);
//---- indicator short name
switch(MA_Type_selected)
{
case 1 : MAshort_name="EMA"; break;
case 2 : MAshort_name="SMMA"; break;
case 3 : MAshort_name="LWMA"; break;
default :
MA_Type_selected=0;
MAshort_name="SMA";
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {
static datetime dt = 0;
int limit, i, counter;
double MAnow, Pricenow, MAprevious, Priceprevious, MAafter, Priceafter;
double Range, AvgRange;
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;
static datetime cDT = 0;
for(i = 0; i <= limit; i++) {
counter=i;
Range=0;
AvgRange=0;
CrossUp[i] = 0; CrossDown[i] = 0;
for (counter=i ;counter<=i+9;counter++)
{
AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range=AvgRange/10;
MAnow = iMA(NULL, 0, MA_Period, 0, MA_Type_selected, PRICE_CLOSE, i);
MAprevious = iMA(NULL, 0, MA_Period, 0, MA_Type_selected, PRICE_CLOSE, i+1);
MAafter = iMA(NULL, 0, MA_Period, 0, MA_Type_selected, PRICE_CLOSE, i-1);
Pricenow = iClose(NULL,0,i);
Priceprevious = iClose(NULL,0,i+1);
Priceafter = iClose(NULL,0,i-1);
if ((Pricenow > MAnow) && (Priceprevious < MAprevious) && (Priceafter > MAafter)) {
CrossUp[i] = Low[i] - Range*0.75;
if ((i < 2) && (dt != iTime(NULL,0,0)))
{
if (Alert_On_Crossing ==true)
{
if (POP_UP_ON ==true)
{
Alert(Symbol(), " M", Period(), " Price closed above moving average");
}
PlaySound(NameFileSound);
dt = iTime(NULL,0,0);
}
if (EMAIL_ON ==true)
{
SendMail("Price closed above MA"," Price closed above moving average");
dt = iTime(NULL,0,0);
}
}
}
else if ((Pricenow < MAnow) && (Priceprevious > MAprevious) && (Priceafter < MAafter)){
CrossDown[i] = High[i] + Range*0.75;
if ((i < 2) && (dt != iTime(NULL,0,0)))
{
if (Alert_On_Crossing ==true)
{
if (POP_UP_ON ==true)
{
Alert(Symbol(), " M", Period(), " Price closed below moving average");
}
PlaySound(NameFileSound);
dt = iTime(NULL,0,0);
}
if (EMAIL_ON ==true)
{
SendMail("Price closed below MA"," Price closed below moving average");
dt = iTime(NULL,0,0);
}
}}
}
return(0);
}
Comments