RSI_Cross_Alert_Stoch





//+------------------------------------------------------------------+
//|                                              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);
  }
  
//-------------------------------------------------------------------+



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


Indicator Curves created:

Implements a curve of type DRAW_LINE


Indicators Used:

Relative strength index
Moving average indicator
Stochastic oscillator


Custom Indicators Used:

Order Management characteristics:

Other Features:

It plays sound alerts
It sends emails
It issuies visual alerts to the screen