Navigation:Home > Content >

diver_MAX_StochasticRSI_Osc.mq4

Time: 2010-05-01 | Download file:diver_MAX_StochasticRSI_Osc.mq4

/*

*/
#property copyright "Copyright ©"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 Magenta
#property indicator_color4 Magenta 
#property indicator_color5 Lime
#property indicator_color6 Red
#property indicator_level1 80
#property indicator_level2 20

extern string separator1 = "*** MAX_StochasticRSI_Osc ***";
extern string ind_name   = "MAX_StochasticRSI_Osc";
extern int RSIPeriod = 8;
extern int PeriodK = 8;
extern int SlowPeriod = 3;
extern int up = 80;
extern int dn = 20;
extern string separator2 = "*****************************";
extern bool drawIndicatorTrendLines = TRUE;
extern bool drawPriceTrendLines = TRUE;
extern bool displayAlert = TRUE;
double g_ibuf_116[];
double g_ibuf_120[];
double g_ibuf_124[];
double g_ibuf_128[];
double H[];
double L[];
datetime g_time_132;
string gs_136;

int init() {
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexBuffer(0, g_ibuf_116);
   SetIndexBuffer(1, g_ibuf_120);
   SetIndexBuffer(2, g_ibuf_124);
   SetIndexBuffer(3, g_ibuf_128);
   SetIndexArrow(0, 233);
   SetIndexArrow(1, 234);
//-----------------------------------   
   SetIndexStyle(4, DRAW_LINE, STYLE_SOLID, 3);
   SetIndexBuffer(4, H);
   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID, 3);
   SetIndexBuffer(5, L);
  
//-----------------------------------   
   gs_136 = "MAX_StochasticRSI_Osc(" + RSIPeriod + ", " + PeriodK + ", " + SlowPeriod + ")";
   SetIndexDrawBegin(3, RSIPeriod);
   IndicatorDigits(Digits + 2);
   IndicatorShortName(gs_136);
   return (0);
}

int deinit() {
   string l_name_4;
   for (int li_0 = ObjectsTotal() - 1; li_0 >= 0; li_0--) {
      l_name_4 = ObjectName(li_0);
      if (StringSubstr(l_name_4, 0, 21) == "MAX_StochasticRSI_Osc") ObjectDelete(l_name_4);
   }
   return (0);
}

int start() {
   int l_ind_counted_0 = IndicatorCounted();
   if (l_ind_counted_0 < 0) l_ind_counted_0 = 0;
   CalculateIndicator(l_ind_counted_0);
   return (0);
}

void CalculateIndicator(int ai_0) {
   for (int li_4 = Bars - ai_0; li_4 >= 0; li_4--) {
      CalculateMACD(li_4);
      CatchBullishDivergence(li_4 + 2);
      CatchBearishDivergence(li_4 + 2);
   }
}

void CalculateMACD(int a_shift_0) {
   g_ibuf_124[a_shift_0] = iCustom(NULL, 0, ind_name,RSIPeriod,PeriodK,SlowPeriod,0,a_shift_0);
   g_ibuf_128[a_shift_0] = iCustom(NULL, 0, ind_name,RSIPeriod,PeriodK,SlowPeriod,0,a_shift_0);
    if(g_ibuf_124[a_shift_0]>up){H[a_shift_0]=g_ibuf_124[a_shift_0];}else{H[a_shift_0]=EMPTY_VALUE;}
    if(g_ibuf_124[a_shift_0] g_ibuf_124[li_8] && Low[li_4] < Low[li_8]) {
         g_ibuf_116[li_4] = g_ibuf_124[li_4] - 0.0001;
         if (drawPriceTrendLines == TRUE) DrawPriceTrendLine(Time[li_4], Time[li_8], Low[li_4], Low[li_8], Green, STYLE_SOLID);
         if (drawIndicatorTrendLines == TRUE) DrawIndicatorTrendLine(Time[li_4], Time[li_8], g_ibuf_124[li_4], g_ibuf_124[li_8], Green, STYLE_SOLID);
         if (displayAlert == TRUE) DisplayAlert("Classical bullish divergence on: ", li_4);
      }
      if (g_ibuf_124[li_4] < g_ibuf_124[li_8] && Low[li_4] > Low[li_8]) {
         g_ibuf_116[li_4] = g_ibuf_124[li_4] - 0.0001;
         if (drawPriceTrendLines == TRUE) DrawPriceTrendLine(Time[li_4], Time[li_8], Low[li_4], Low[li_8], Green, STYLE_DOT);
         if (drawIndicatorTrendLines == TRUE) DrawIndicatorTrendLine(Time[li_4], Time[li_8], g_ibuf_124[li_4], g_ibuf_124[li_8], Green, STYLE_DOT);
         if (displayAlert == TRUE) DisplayAlert("Reverse bullish divergence on: ", li_4);
      }
   }
}

void CatchBearishDivergence(int ai_0) {
   int li_4;
   int li_8;
   if (IsIndicatorPeak(ai_0) != 0) {
      li_4 = ai_0;
      li_8 = GetIndicatorLastPeak(ai_0);
      if (g_ibuf_124[li_4] < g_ibuf_124[li_8] && High[li_4] > High[li_8]) {
         g_ibuf_120[li_4] = g_ibuf_124[li_4] + 0.0001;
         if (drawPriceTrendLines == TRUE) DrawPriceTrendLine(Time[li_4], Time[li_8], High[li_4], High[li_8], Red, STYLE_SOLID);
         if (drawIndicatorTrendLines == TRUE) DrawIndicatorTrendLine(Time[li_4], Time[li_8], g_ibuf_124[li_4], g_ibuf_124[li_8], Red, STYLE_SOLID);
         if (displayAlert == TRUE) DisplayAlert("Classical bearish divergence on: ", li_4);
      }
      if (g_ibuf_124[li_4] > g_ibuf_124[li_8] && High[li_4] < High[li_8]) {
         g_ibuf_120[li_4] = g_ibuf_124[li_4] + 0.0001;
         if (drawPriceTrendLines == TRUE) DrawPriceTrendLine(Time[li_4], Time[li_8], High[li_4], High[li_8], Red, STYLE_DOT);
         if (drawIndicatorTrendLines == TRUE) DrawIndicatorTrendLine(Time[li_4], Time[li_8], g_ibuf_124[li_4], g_ibuf_124[li_8], Red, STYLE_DOT);
         if (displayAlert == TRUE) DisplayAlert("Reverse bearish divergence on: ", li_4);
      }
   }
}

int IsIndicatorPeak(int ai_0) {
   if (g_ibuf_124[ai_0] >= g_ibuf_124[ai_0 + 1] && g_ibuf_124[ai_0] > g_ibuf_124[ai_0 + 2] && g_ibuf_124[ai_0] > g_ibuf_124[ai_0 - 1]) return (1);
   return (0);
}

int IsIndicatorTrough(int ai_0) {
   if (g_ibuf_124[ai_0] <= g_ibuf_124[ai_0 + 1] && g_ibuf_124[ai_0] < g_ibuf_124[ai_0 + 2] && g_ibuf_124[ai_0] < g_ibuf_124[ai_0 - 1]) return (1);
   return (0);
}

int GetIndicatorLastPeak(int ai_0) {
   for (int li_4 = ai_0 + 5; li_4 < Bars; li_4++) {
      if (g_ibuf_128[li_4] >= g_ibuf_128[li_4 + 1] && g_ibuf_128[li_4] >= g_ibuf_128[li_4 + 2] && g_ibuf_128[li_4] >= g_ibuf_128[li_4 - 1] && g_ibuf_128[li_4] >= g_ibuf_128[li_4 - 2]) {
         for (int li_ret_8 = li_4; li_ret_8 < Bars; li_ret_8++)
            if (g_ibuf_124[li_ret_8] >= g_ibuf_124[li_ret_8 + 1] && g_ibuf_124[li_ret_8] > g_ibuf_124[li_ret_8 + 2] && g_ibuf_124[li_ret_8] >= g_ibuf_124[li_ret_8 - 1] && g_ibuf_124[li_ret_8] > g_ibuf_124[li_ret_8 - 2]) return (li_ret_8);
      }
   }
   return (-1);
}

int GetIndicatorLastTrough(int ai_0) {
   for (int li_4 = ai_0 + 5; li_4 < Bars; li_4++) {
      if (g_ibuf_128[li_4] <= g_ibuf_128[li_4 + 1] && g_ibuf_128[li_4] <= g_ibuf_128[li_4 + 2] && g_ibuf_128[li_4] <= g_ibuf_128[li_4 - 1] && g_ibuf_128[li_4] <= g_ibuf_128[li_4 - 2]) {
         for (int li_ret_8 = li_4; li_ret_8 < Bars; li_ret_8++)
            if (g_ibuf_124[li_ret_8] <= g_ibuf_124[li_ret_8 + 1] && g_ibuf_124[li_ret_8] < g_ibuf_124[li_ret_8 + 2] && g_ibuf_124[li_ret_8] <= g_ibuf_124[li_ret_8 - 1] && g_ibuf_124[li_ret_8] < g_ibuf_124[li_ret_8 - 2]) return (li_ret_8);
      }
   }
   return (-1);
}

void DisplayAlert(string as_0, int ai_8) {
   if (ai_8 <= 2 && Time[ai_8] != g_time_132) {
      g_time_132 = Time[ai_8];
      Alert(as_0, Symbol(), " , ", Period(), " minutes chart");
   }
}

void DrawPriceTrendLine(int a_datetime_0, int a_datetime_4, double a_price_8, double a_price_16, color a_color_24, double a_style_28) {
   string l_name_36 = "MAX_StochasticRSI_Osc 0# " + DoubleToStr(a_datetime_0, 0);
   ObjectDelete(l_name_36);
   ObjectCreate(l_name_36, OBJ_TREND, 0, a_datetime_0, a_price_8, a_datetime_4, a_price_16, 0, 0);
   ObjectSet(l_name_36, OBJPROP_RAY, FALSE);
   ObjectSet(l_name_36, OBJPROP_COLOR, a_color_24);
   ObjectSet(l_name_36, OBJPROP_STYLE, a_style_28);
}

void DrawIndicatorTrendLine(int a_datetime_0, int a_datetime_4, double a_price_8, double a_price_16, color a_color_24, double a_style_28) {
   string l_name_40;
   int l_window_36 = WindowFind(gs_136);
   if (l_window_36 >= 0) {
      l_name_40 = "MAX_StochasticRSI_Osc 0$# " + DoubleToStr(a_datetime_0, 0);
      ObjectDelete(l_name_40);
      ObjectCreate(l_name_40, OBJ_TREND, l_window_36, a_datetime_0, a_price_8, a_datetime_4, a_price_16, 0, 0);
      ObjectSet(l_name_40, OBJPROP_RAY, FALSE);
      ObjectSet(l_name_40, OBJPROP_COLOR, a_color_24);
      ObjectSet(l_name_40, OBJPROP_STYLE, a_style_28);
   }
}

Recommend