RSI_Cross_Alert
Download scriptThis script is designed as a technical analysis tool for the MetaTrader 4 platform. Its primary purpose is to monitor price momentum and provide visual and audible notifications when specific mathematical conditions are met on a trading chart.
Core Logic Overview
The tool operates by comparing two different versions of the Relative Strength Index (RSI)—a popular momentum oscillator. It tracks a "Fast" RSI (which reacts quickly to recent price changes) and a "Slow" RSI (which follows broader price trends). The core logic relies on identifying the exact moment these two lines cross each other.
Key Functional Components
1. Dual RSI Tracking
- Fast RSI: Tracks short-term momentum.
- Slow RSI: Tracks long-term momentum.
- Smoothing Options: The script allows for "smoothing" of these lines (using moving averages). This helps filter out minor market "noise," ensuring that the alerts are triggered by meaningful trends rather than small, insignificant price fluctuations.
2. Stochastic Filtering (Optional) For users who want extra precision, the script includes an optional filtering mechanism using a Stochastic Oscillator. If enabled, the script won't just look for an RSI cross; it will also check if the Stochastic momentum is moving in the same direction as the desired trade, acting as a second layer of confirmation.
3. Signal Detection The script constantly scans the most recent price data to detect two specific events:
- Buy Signal: Triggered when the Fast RSI line crosses from below to above the Slow RSI line.
- Sell Signal: Triggered when the Fast RSI line crosses from above to below the Slow RSI line.
4. Notifications and Visual Aids When a signal is detected, the script provides several ways to alert the user:
- Visual Markers: It can automatically draw vertical lines on the chart (Blue for Buy, Red for Sell) to mark exactly where the crossover occurred.
- Pop-up Alerts: A message box appears on the trading platform screen.
- Audio Alerts: The script can play specific sound files when a crossover happens.
- Email Notifications: If configured, the script can send an automated email with the specific details of the trade signal.
User Controls
The script is highly customizable, allowing the user to define:
- The speed and sensitivity of both RSI lines.
- Whether to use "Smoothing" to stabilize the data.
- How to receive alerts (Sound, Email, or Pop-up).
- A "Close Bar" setting, which ensures that a signal is only confirmed once a candle has finished closing, helping to avoid "fake" signals caused by flickering prices during an active candle.
//+------------------------------------------------------------------+
//| RSI_Cross_Alert.mq4 |
//| Copyright © 2009, Serega Lykov |
//| http://mtexperts.narod.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Serega Lykov"
#property link "http://mtexperts.narod.ru/"
//---- property of indicator ----------------------------------------+
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 Gold
#property indicator_color3 LightSeaGreen
#property indicator_color4 Red
//---- external parameters ------------------------------------------+
extern string _____Fast_RSI___________;
extern int FastRSI_Period = 7;
extern int FastRSI_Price = 0;
extern bool FastRSI_UseSmooth = false;
extern int FastRSI_MASmooth_Period = 4;
extern int FastRSI_MASmooth_Method = 0;
extern string _____Slow_RSI___________;
extern int SlowRSI_Period = 12;
extern int SlowRSI_Price = 0;
extern bool SlowRSI_UseSmooth = false;
extern int SlowRSI_MASmooth_Period = 7;
extern int SlowRSI_MASmooth_Method = 0;
extern string _____Filter_Stochastic__;
extern bool UseFilterStochastic = false;
extern int STOCH_Kperiod = 5;
extern int STOCH_Dperiod = 3;
extern int STOCH_slowing = 3;
extern int STOCH_method = 0; // (0-3)
extern int STOCH_price_field = 0; // (0-1)
extern int STOCH_mode = 0; // (0-1)
extern string _____Alerts_____________;
extern bool SignalOnlyAfterCloseBar = false;
extern bool EmailAlert = false;
extern bool PopUpAlert = true;
extern bool SoundAlert = false;
extern string SoundFile_Buy = "alert.wav";
extern string SoundFile_Sell = "alert2.wav";
extern bool DrawVertLine = true;
extern color VLineColor_Buy = Blue;
extern color VLineColor_Sell = Red;
//---- buffers ------------------------------------------------------+
static double FastRSIBuffer[];
static double SlowRSIBuffer[];
static double STOCHMainBuffer[];
static double STOCHSignalBuffer[];
static double FastRSIBufferSmooth[];
static double SlowRSIBufferSmooth[];
//---- global variables ---------------------------------------------+
static datetime prevtime;
static datetime prevtime_dl;
static string buy_message = "The line FastRSI to cross a line SlowRSI from below to above.";
static string sell_message = "The line FastRSI to cross a line SlowRSI from above to below.";
static string short_name;
static int shift;
static int draw_begin;
//-------------------------------------------------------------------+
//---- initialization of indicator ----------------------------------+
//-------------------------------------------------------------------+
int init()
{
//---- the 2 additional buffer -----------------------------------+
IndicatorBuffers(6);
//---- set a "short" name of the indicator -----------------------+
if(!FastRSI_UseSmooth) string smooth_fast = "";
else
{
switch(FastRSI_MASmooth_Method)
{
case 1: smooth_fast = "EMA("; break;
case 2: smooth_fast = "SMMA("; break;
case 3: smooth_fast = "LWMA("; break;
default: smooth_fast = "SMA("; FastRSI_MASmooth_Method = 0;
}
smooth_fast = StringConcatenate(",",smooth_fast,FastRSI_MASmooth_Period,")");
}
if(!SlowRSI_UseSmooth) string smooth_slow = "";
else
{
switch(SlowRSI_MASmooth_Method)
{
case 1: smooth_slow = "EMA("; break;
case 2: smooth_slow = "SMMA("; break;
case 3: smooth_slow = "LWMA("; break;
default: smooth_slow = "SMA("; SlowRSI_MASmooth_Method = 0;
}
smooth_slow = StringConcatenate(",",smooth_slow,SlowRSI_MASmooth_Period,")");
}
if(!UseFilterStochastic) string stoch = "";
else stoch = StringConcatenate(" & Stoch(",STOCH_Kperiod,",",STOCH_Dperiod,",",STOCH_slowing,")");
short_name = StringConcatenate("FastRSI(",FastRSI_Period,smooth_fast,")",",SlowRSI(",SlowRSI_Period,smooth_slow,")",stoch);
IndicatorShortName(short_name);
//---- set a accuracy of values of the indicator -----------------+
IndicatorDigits(4);
//---- set a style for line --------------------------------------+
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID);
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID);
SetIndexStyle(3,DRAW_LINE,STYLE_DOT);
//---- set a arrays for line -------------------------------------+
SetIndexBuffer(0,FastRSIBuffer);
SetIndexBuffer(1,SlowRSIBuffer);
SetIndexBuffer(2,STOCHMainBuffer);
SetIndexBuffer(3,STOCHSignalBuffer);
SetIndexBuffer(4,FastRSIBufferSmooth);
SetIndexBuffer(5,SlowRSIBufferSmooth);
//---- set a first bar for drawing the line ----------------------+
if(!FastRSI_UseSmooth) int draw_begin_fast = FastRSI_Period;
else draw_begin_fast = FastRSI_Period + FastRSI_MASmooth_Period;
if(!SlowRSI_UseSmooth) int draw_begin_slow = SlowRSI_Period;
else draw_begin_slow = SlowRSI_Period + SlowRSI_MASmooth_Period;
draw_begin = MathMax(draw_begin_fast,draw_begin_slow);
SetIndexDrawBegin(0,draw_begin);
SetIndexDrawBegin(1,draw_begin);
SetIndexDrawBegin(2,draw_begin);
SetIndexDrawBegin(3,draw_begin);
//---- set a names for lines -------------------------------------+
SetIndexLabel(0,"FastRSI");
SetIndexLabel(1,"SlowRSI");
SetIndexLabel(2,"Stoch Main");
SetIndexLabel(3,"Stoch Signal");
//---- set a constant --------------------------------------------+
if(SignalOnlyAfterCloseBar) shift = 1;
else shift = 0;
//---- initialization of parameters ------------------------------+
prevtime = Time[0];
//---- finish of initialization ----------------------------------+
return(0);
}
//-------------------------------------------------------------------+
//---- deinitialization of indicator --------------------------------+
//-------------------------------------------------------------------+
int deinit()
{
//---- delete objects of indicator -------------------------------+
int obj_total = ObjectsTotal() - 1;
for(int i=obj_total; i>=0; i--)
{
string name = ObjectName(i);
if(ObjectType(name) != OBJ_VLINE) continue;
if(StringFind(name,"rsi_vline",0) < 0) continue;
ObjectDelete(name);
}
return(0);
}
//-------------------------------------------------------------------+
//---- Crossing two RSI with Alert ----------------------------------+
//-------------------------------------------------------------------+
int start()
{
//---- if on the chart there are not enough bars the indicator is not calculate
if(Bars <= draw_begin) return(0);
//---- amount not changed bars after last call of the indicator --+
int counted_bars = IndicatorCounted();
if(counted_bars < 0) return(-1);
//---- last counted bar will be counted --------------------------+
if(counted_bars > 0) counted_bars--;
int limit = Bars - counted_bars;
//---- get values of fast RSI ------------------------------------+
if(!FastRSI_UseSmooth)
{
for(int i=0; i<limit; i++)
{
FastRSIBuffer[i] = iRSI(NULL,0,FastRSI_Period,FastRSI_Price,i);
FastRSIBufferSmooth[i] = EMPTY_VALUE;
}
}
else
{
for(i=0; i<limit; i++) FastRSIBufferSmooth[i] = iRSI(NULL,0,FastRSI_Period,FastRSI_Price,i);
for(i=0; i<limit; i++) FastRSIBuffer[i] = iMAOnArray(FastRSIBufferSmooth,0,FastRSI_MASmooth_Period,0,FastRSI_MASmooth_Method,i);
}
//---- get values of slow RSI ------------------------------------+
if(!SlowRSI_UseSmooth)
{
for(i=0; i<limit; i++)
{
SlowRSIBuffer[i] = iRSI(NULL,0,SlowRSI_Period,SlowRSI_Price,i);
SlowRSIBufferSmooth[i] = EMPTY_VALUE;
}
}
else
{
for(i=0; i<limit; i++) SlowRSIBufferSmooth[i] = iRSI(NULL,0,SlowRSI_Period,SlowRSI_Price,i);
for(i=0; i<limit; i++) SlowRSIBuffer[i] = iMAOnArray(SlowRSIBufferSmooth,0,SlowRSI_MASmooth_Period,0,SlowRSI_MASmooth_Method,i);
}
//---- get values of Stochastic ----------------------------------+
if(UseFilterStochastic)
{
for(i=0; i<limit; i++)
{
STOCHMainBuffer[i] = iStochastic(NULL,0,STOCH_Kperiod,STOCH_Dperiod,STOCH_slowing,STOCH_method,STOCH_price_field,0,i);
STOCHSignalBuffer[i] = iStochastic(NULL,0,STOCH_Kperiod,STOCH_Dperiod,STOCH_slowing,STOCH_method,STOCH_price_field,1,i);
}
}
else
{
for(i=0; i<limit; i++)
{
STOCHMainBuffer[i] = EMPTY_VALUE;
STOCHSignalBuffer[i] = EMPTY_VALUE;
}
}
//---- alert when lines FastRSI and SlowRSI are crossed ----------+
for(i=limit-shift-2; i>=0; i--)
{
if(STOCH_mode == 0)
{
double curr_stoch = STOCHMainBuffer[i+shift];
double prev_stoch = STOCHMainBuffer[i+shift+1];
}
else
{
curr_stoch = STOCHSignalBuffer[i+shift];
prev_stoch = STOCHSignalBuffer[i+shift+1];
}
int signal = 0;
if((FastRSIBuffer[i+shift+1] <= SlowRSIBuffer[i+shift+1] && FastRSIBuffer[i+shift] > SlowRSIBuffer[i+shift]) &&
(!UseFilterStochastic || (UseFilterStochastic && prev_stoch < curr_stoch)))
{
if(DrawVertLine && prevtime_dl != Time[i])
{
string name = StringConcatenate("rsi_vline BUY ",Symbol()," ",TimeToStr(Time[i]));
ObjectCreate(name,OBJ_VLINE,0,Time[i],0);
ObjectSet(name,OBJPROP_COLOR,VLineColor_Buy);
prevtime_dl = Time[i];
}
signal++;
}
if((FastRSIBuffer[i+shift+1] >= SlowRSIBuffer[i+shift+1] && FastRSIBuffer[i+shift] < SlowRSIBuffer[i+shift]) &&
(!UseFilterStochastic || (UseFilterStochastic && prev_stoch > curr_stoch)))
{
if(DrawVertLine && prevtime_dl != Time[i])
{
name = StringConcatenate("rsi_vline SELL ",Symbol()," ",TimeToStr(Time[i]));
ObjectCreate(name,OBJ_VLINE,0,Time[i],0);
ObjectSet(name,OBJPROP_COLOR,VLineColor_Sell);
prevtime_dl = Time[i];
}
signal--;
}
}
if(prevtime != Time[0])
{
if(signal > 0)
{
if(SoundAlert) PlaySound(SoundFile_Buy);
if(EmailAlert) SendMail(StringConcatenate("Alert from indicator \"",short_name,"\""),StringConcatenate("BUY Signal on ",Symbol()," at ",TimeToStr(TimeCurrent()),buy_message));
if(PopUpAlert) Alert(StringConcatenate("Indicator \"",short_name,"\":\nBUY Signal on ",Symbol()," at ",TimeToStr(TimeCurrent()),"\n",buy_message));
prevtime = Time[0];
}
if(signal < 0)
{
if(SoundAlert) PlaySound(SoundFile_Sell);
if(EmailAlert) SendMail(StringConcatenate("Alert from indicator \"",short_name,"\""),StringConcatenate("SELL Signal on ",Symbol()," at ",TimeToStr(TimeCurrent()),sell_message));
if(PopUpAlert) Alert(StringConcatenate("Indicator \"",short_name,"\":\nSELL Signal on ",Symbol()," at ",TimeToStr(TimeCurrent()),"\n",sell_message));
prevtime = Time[0];
}
}
//---- finish of iteration ---------------------------------------+
return(0);
}
//-------------------------------------------------------------------+
Comments