Navigation´╝ÜHome > Content >

StochRSI_with_alerts-2.mq4

Time: 2010-12-19 | Download file:StochRSI_with_alerts-2.mq4

//+------------------------------------------------------------------+
//|                                                   Stochastic RSI |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers    2
#property indicator_color1     Gold
#property indicator_color2     Red
#property indicator_width1     2
#property indicator_style2     STYLE_DASH
#property indicator_levelcolor DarkSlateGray

//
//
//
//
//

extern int    RsiPeriod                 = 13;
extern int    Price                     = PRICE_TYPICAL;
extern int    StochasticKPeriod         = 10;
extern int    StochasticSlowing         = 3;
extern int    StochasticSlowingMaMethod = MODE_SMA;
extern int    StochasticDPeriod         = 3;
extern int    StochasticDMaMethod       = MODE_SMA;
extern double levelOs                   = 20;
extern double levelOb                   = 80; 
extern string __MaMethods               = "";
extern string __0                       = "SMA";
extern string __2                       = "EMA";
extern string __3                       = "Smoothed MA";
extern string __4                       = "Linear weighted MA";
extern string __5                       = "Triangular MA";
extern string __6                       = "Volume weghted MA";
extern string __7                       = "Sine weighted MA";
extern string __8                       = "EPMA";

extern string note                      = "turn on Alert = true; turn off = false";
extern bool   alertsOn                  = true;
extern bool   alertsOnStoRsiSigCross    = true;
extern bool   alertsOnCurrent           = true;
extern bool   alertsMessage             = true;
extern bool   alertsSound               = false;
extern bool   alertsEmail               = false;
extern string soundFile                 = "alert2.wav";

//
//
//
//
//

double stochRsi[];
double stochRsiSignal[];
double rsi[];
double stoch[];
double trend1[];
double trend2[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0,stochRsi);
   SetIndexBuffer(1,stochRsiSignal);
   SetIndexBuffer(2,rsi);
   SetIndexBuffer(3,stoch);
   SetIndexBuffer(4,trend1);
   SetIndexBuffer(5,trend2);
   SetLevelValue(0,levelOs);
   SetLevelValue(1,levelOb);
   SetLevelValue(2,5);
   SetLevelValue(3,15);
   SetLevelValue(4,50);
   SetLevelValue(5,85);
   SetLevelValue(6,95);

      //
      //
      //
      //
      //
      
      string smooth1 = getAverageName(StochasticSlowingMaMethod);
      string smooth2 = getAverageName(StochasticDMaMethod);
      
      //
      //
      //
      //
      //
      
   IndicatorShortName("StochRsi ("+RsiPeriod+","+StochasticKPeriod+","+StochasticSlowing+","+smooth1+","+StochasticDPeriod+","+smooth2+")");
   return(0);
}
int deinit() { return(0);}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //
   
                  for(i=limit; i>=0; i--)
                  {
                  
                      rsi[i] = iRsi(iMA(NULL,0,1,0,MODE_SMA,Price,i),RsiPeriod,i);
                  double min = rsi[ArrayMinimum(rsi,StochasticKPeriod,i)];
                  double max = rsi[ArrayMaximum(rsi,StochasticKPeriod,i)];
                          stoch[i] = 0.5; if (min!=max) stoch[i] = 100.0*(rsi[i]-min)/(max-min); 
                       stochRsi[i] = iCMAOnArray(stoch   ,stochRsi      ,StochasticSlowing,StochasticSlowingMaMethod,i);
                 stochRsiSignal[i] = iCMAOnArray(stochRsi,stochRsiSignal,StochasticDPeriod,StochasticDMaMethod      ,i); 
                        trend1[i] = trend1[i+1]; 
                        trend2[i] = trend2[i+1]; 
                 if (stochRsi[i] <= levelOs && stochRsi[i] > stochRsi[i+1] && stochRsi[i+1] < stochRsi[i+2]) trend1[i] =  1;
                 if (stochRsi[i] >= levelOb && stochRsi[i] < stochRsi[i+1] && stochRsi[i+1] > stochRsi[i+2]) trend1[i] = -1;
                 if (stochRsi[i] > stochRsiSignal[i])                                                        trend2[i] =  1;
                 if (stochRsi[i] < stochRsiSignal[i])                                                        trend2[i] = -1;
                 
                 }
                 
                 //
	//
	//
	//
	//
	
	if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      
      //
      //
      //
      //
      //
      
      if (alertsOnStoRsiSigCross)
      {
         if (trend2[whichBar] != trend2[whichBar+1])
         {
            if (trend2[whichBar] == 1) doAlert(whichBar,"Stoch crossed Signal up");
            if (trend2[whichBar] ==-1) doAlert(whichBar,"Stoch crossed Signal down");
         }         
       }
       else
       {
         if (trend1[whichBar] != trend1[whichBar+1])
         {
            if (trend1[whichBar] == 1) doAlert(whichBar,"Stoch inside oversold up");
            if (trend1[whichBar] ==-1) doAlert(whichBar,"Stoch inside overbought down");
         }         
       }
    }
   
return(0);
}
//+------------------------------------------------------------------+


void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," StochRsi ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," StochRsi "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}
           
//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//
//

double workRsi[][3];
#define _price  0
#define _change 1
#define _changa 2

//
//
//
//

double iRsi(double price, double period, int shift, int forz=0)
{
   if (ArrayRange(workRsi,0)!=Bars) ArrayResize(workRsi,Bars);
      int    z     = forz*3; 
      int    i     = Bars-shift-1;
      double alpha = 1.0/period; 

   //
   //
   //
   //
   //
   
   workRsi[i][_price+z] = price;
   if (i=0; k++) sum += MathAbs(workRsi[i-k][_price+z]-workRsi[i-k-1][_price+z]);
            workRsi[i][_change+z] = (workRsi[i][_price+z]-workRsi[0][_price+z])/MathMax(k,1);
            workRsi[i][_changa+z] =                                         sum/MathMax(k,1);
      }
   else
      {
         double change = workRsi[i][_price+z]-workRsi[i-1][_price+z];
                         workRsi[i][_change+z] = workRsi[i-1][_change+z] + alpha*(        change  - workRsi[i-1][_change+z]);
                         workRsi[i][_changa+z] = workRsi[i-1][_changa+z] + alpha*(MathAbs(change) - workRsi[i-1][_changa+z]);
      }
   if (workRsi[i][_changa+z] != 0)
         return(50.0*(workRsi[i][_change+z]/workRsi[i][_changa+z]+1));
   else  return(0);
}

//
//
//
//
//

double iCMAOnArray(double& source[], double& result[], int period, int method, int i)
{
   switch(method)
   {
      case 0: return(iSmaOnArray(source,period,i));
      case 1: return(iEmaOnArray(source,result,period,i));
      case 2: return(iSmmaOnArray(source,result,period,i));
      case 3: return(iLwmaOnArray(source,period,i));
      case 4: return(iTmaOnArray(source,period,i));
      case 5: return(iSineWMAOnArray(source,period,i));
      case 6: return(iVolumeWMAOnArray(source,period,i));
      case 7: return(iEPMAOnArray(source,period,i));
   }
   return(EMPTY_VALUE);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iSmaOnArray(double& array[], double period, int i)
{
   double sum = 0; 
   for(int k=0; k=(Bars-period))
   {
      double sum = 0; 
         for(int k=0; k half) weight = period-k;
             sumw  += weight;
             sum   += weight*array[i+k];  
   }             
   if (sumw!=0)
         return(sum/sumw);
   else  return(EMPTY_VALUE);
}

//
//
//
//
//

#define Pi 3.14159265358979323846
double iSineWMAOnArray(double& array[], int period, int i)
{
   double sum  = 0;
   double sumw = 0;
  
   for(int k=0; k        

Recommend