MA_Price_Cross1.1m_alert
Download scriptThis script is designed as a monitoring tool for traders who want to track specific interactions between market prices and Moving Averages (MA). Its primary purpose is to automatically identify the exact moment price "breaks" through a moving average line, providing both visual cues on the chart and real-time notifications.
Core Functionality
The logic operates by constantly comparing the current closing price of an asset against its calculated Moving Average. It is programmed to recognize two specific types of events:
- Bullish Crosses: When the price moves from below the moving average to above it.
- Bearish Crosses: When the price moves from above the moving average to below it.
To ensure accuracy, the script looks at the relationship between the current candle and the previous candle. It only triggers a signal if the transition happened across that specific boundary, preventing false signals during periods where the price is simply drifting along the line.
Visual and Audio Feedback
When a crossover is detected, the script performs several actions simultaneously to keep the trader informed:
- Chart Markers: It places visual arrows on the trading chart (Dodger Blue for upward crosses and Red for downward crosses). These markers are positioned slightly away from the candles based on the recent market volatility to ensure they remain clearly visible.
- Pop-up Alerts: A text window appears on the platform screen notifying the user of the specific direction of the price break.
- Audio Signals: The script can trigger a specific sound file (such as a beep or alert tone) to grab the trader's attention immediately.
Remote Notifications
For traders who are not sitting in front of their computers at all times, the script includes an automated communication system:
- Email Alerts: If enabled, the script can automatically send an email notification to the user whenever a price crossover occurs, allowing for real-time monitoring from any location.
Customization Options
The logic is flexible and allows users to define the "rules" of the calculation without needing to change the underlying code:
- Moving Average Type: Users can choose between different calculation methods, such as Simple (SMA), Exponential (EMA), Smoothed (SMMA), or Linear Weighted (LWMA).
- Timeframe Sensitivity: The period (how many past candles are used to calculate the average) is fully adjustable.
- Notification Settings: Users can toggle specific alerts (Pop-ups, Sounds, or Emails) on or off depending on their preferred trading style.
//+------------------------------------------------------------------+
//| 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. |
+------------------------------------------------------------------+
*/
//mod2009fxtsd
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
double CrossUp[];
double CrossDown[];
extern string MA_Type_ = "0sma,1ema,2smma,3lwma";
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 alertsDelay = 1;
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);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);
SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexEmptyValue(1,EMPTY_VALUE);
//---- 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+alertsDelay);
MAprevious = iMA(NULL, 0, MA_Period, 0, MA_Type_selected, PRICE_CLOSE, i+1+alertsDelay);
// MAafter = iMA(NULL, 0, MA_Period, 0, MA_Type_selected, PRICE_CLOSE, i-1);
Pricenow = iClose(NULL,0,i+alertsDelay);
Priceprevious = iClose(NULL,0,i+1+alertsDelay);
// Priceafter = iClose(NULL,0,i-1);
CrossUp[i] = EMPTY_VALUE; CrossDown[i] = EMPTY_VALUE;
if ((Pricenow > MAnow) && (Priceprevious < MAprevious) ) {
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) ){
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