RSI_Cross_Alert_Stoch
Download scriptThis tool is designed for traders who use the Relative Strength Index (RSI) to identify momentum shifts in the market. It works by monitoring the relationship between two different RSI lines—a "Fast" one and a "Slow" one—and providing real-time notifications when they cross each other.
Core Functionality
The primary logic of the script revolves around momentum crossover. By comparing a shorter-term RSI (Fast) with a longer-term RSI (Slow), the tool helps identify when short-term price momentum is beginning to pull away from the long-term trend.
- Buy Signals: When the Fast RSI line crosses above the Slow RSI line, the script identifies this as an upward momentum shift.
- Sell Signals: When the Fast RSI line crosses below the Slow RSI line, the script identifies this as a downward momentum shift.
Key Features and Customization
The logic includes several layers of sophistication to ensure the signals are as clear as possible:
- Smoothing Options: Users have the ability to "smooth" the RSI lines using various moving averages (like Simple, Exponential, or Linear Weighted). This helps filter out "noise" or minor price fluctuations that might cause false signals in a choppy market.
- Stochastic Filtering: For traders who want extra confirmation, there is an optional filtering system. If enabled, the script won't trigger an alert based solely on the RSI cross; it will also check the Stochastic Oscillator to ensure the momentum move is supported by that secondary indicator.
- Visual and Audible Alerts: To ensure traders don't miss a movement, the script provides multiple ways to notify you:
- Pop-up Windows: Visual alerts directly on your trading screen.
- Sound Notifications: Audio cues (different sounds for Buy vs. Sell).
- Email Alerts: Direct notifications sent to your registered email address.
- Chart Marking: The script can automatically draw vertical lines on your chart (Blue for Buys, Red for Sells) at the exact moment the crossover occurs.
Operational Logic
The script is built to be highly controllable regarding timing:
- Real-time vs. Closed Bar: You can choose whether the tool alerts you the very instant a crossover happens or wait until the current candle closes to confirm the move. This prevents "repainting" issues where a signal might appear and then disappear before the price action is finalized.
- Automatic Cleanup: The script manages its own workspace, ensuring that when you remove it from your chart, it cleans up the vertical lines it drew, keeping your workspace tidy.
//+------------------------------------------------------------------+
//| 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(" with Smooth ",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(" with Smooth ",smooth_slow,SlowRSI_MASmooth_Period,")");
}
if(!UseFilterStochastic) string stoch = "";
else stoch = StringConcatenate(" & filter Stoch(",STOCH_Kperiod,",",STOCH_Dperiod,",",STOCH_slowing,")");
short_name = StringConcatenate("RSI_Cross_Alert[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