THV3_Trix_v4.01_Div






#property copyright "convert FinGeR Alex orginal by Luis Damiani"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Green
#property indicator_color2 Crimson
#property indicator_color3 Lime
#property indicator_color4 Red
#property indicator_color5 Lime
#property indicator_color6 Red

int gi_76 = 20;
int gi_80 = 35;
extern string note1 = "Trix level colors";
extern color HighLine_Color = FireBrick;
extern color ZeroLine_Color = DimGray;
extern color LowLine_Color = DarkGreen;
extern int Line_Style = 2;
extern string note2 = "Cobra Label colors";
extern color text1Color = C'0x77,0x77,0x00';
extern color text2Color = C'0x77,0x77,0x00';
extern color text3Color = Green;
string gs_unused_128 = "===== Alert Settings =====";
extern bool MsgAlerts = FALSE;
extern bool SoundAlerts = FALSE;
extern bool eMailAlerts = FALSE;
extern bool AlertOnTrixCross = FALSE;
extern bool AlertOnTrixSigCross = TRUE;
extern bool AlertOnSlopeChange = TRUE;
extern string TrixCrossSound = "trixcross.wav";
extern int AnalyzeLabelWindow = 1;
extern bool AnalyzeLabelonoff = TRUE;
int gi_176 = 0;
int gi_180 = 0;
double gd_184 = 0.0;
extern int Trixnum_bars = 750;
int gi_196 = 0;
int gi_200 = 0;
double gd_204 = 0.7;
double g_ibuf_212[];
double g_ibuf_216[];
double g_ibuf_220[];
double g_ibuf_224[];
double g_ibuf_228[];
double g_ibuf_232[];
double g_x_236;
double g_x_244;
int g_bars_252;
string gs_256;
string gs_264;
datetime g_time_272;
datetime g_time_276;
extern string separator2 = "*** Divergence Settings ***";
extern int NumberOfDivergenceBars = 500;
extern bool drawPriceTrendLines = TRUE;
extern bool drawIndicatorTrendLines = FALSE;
extern bool ShowIn1MChart = FALSE;
extern string _ = "--- Divergence Alert Settings ---";
extern bool EnableAlerts = TRUE;
extern string _Info1 = "";
extern string _Info2 = "------------------------------------";
extern string _Info3 = "SoundAlertOnDivergence only works";
extern string _Info4 = "when EnableAlerts is true.";
extern string _Info5 = "";
extern string _Info6 = "If SoundAlertOnDivergence is true,";
extern string _Info7 = "then sound alert will be generated,";
extern string _Info8 = "otherwise a pop-up alert will be";
extern string _Info9 = "generated.";
extern string _Info10 = "------------------------------------";
extern string _Info11 = "";
extern bool SoundAlertOnDivergence = TRUE;
extern bool EmailDivergenceAlerts = FALSE;
extern string __ = "--- Divergence Color Settings ---";
extern color BullishDivergenceColor = DodgerBlue;
extern color BearishDivergenceColor = FireBrick;
extern string ___ = "--- Divergence Sound Files ---";
extern string ClassicBullDivSound = "CBullishDiv.wav";
extern string ReverseBullDivSound = "RBullishDiv.wav";
extern string ClassicBearDivSound = "CBearishDiv.wav";
extern string ReverseBearDivSound = "RBearishDiv.wav";
double g_ibuf_468[];
double g_ibuf_472[];

int init() {
   IndicatorBuffers(8);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, g_ibuf_216);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, g_ibuf_220);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, g_ibuf_228);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexBuffer(3, g_ibuf_232);
   SetIndexBuffer(4, g_ibuf_468);
   SetIndexBuffer(5, g_ibuf_472);
   SetIndexStyle(4, DRAW_ARROW);
   SetIndexStyle(5, DRAW_ARROW);
   SetIndexArrow(4, 233);
   SetIndexArrow(5, 234);
   SetIndexDrawBegin(5, 9);
   IndicatorDigits(Digits + 2);
   SetIndexBuffer(6, g_ibuf_224);
   SetIndexBuffer(7, g_ibuf_212);
   gs_256 = "THVTrix v" + "4" + "." + "01";
   gs_264 = gs_256;
   IndicatorShortName(gs_256);
   return (0);
}

int deinit() {
   DeleteObjects("Trix_");
   Comment("");
   return (0);
}

void drawLine(string a_name_0, double a_price_8, color a_color_16, int a_style_20) {
   ObjectDelete(a_name_0);
   ObjectCreate(a_name_0, OBJ_HLINE, WindowFind(gs_256), Time[0], a_price_8);
   ObjectSet(a_name_0, OBJPROP_STYLE, a_style_20);
   ObjectSet(a_name_0, OBJPROP_COLOR, a_color_16);
   ObjectSet(a_name_0, OBJPROP_WIDTH, 1);
}

void DeleteObjects(string as_0) {
   string l_name_12;
   for (int li_8 = ObjectsTotal() - 1; li_8 >= 0; li_8--) {
      l_name_12 = ObjectName(li_8);
      if (StringFind(l_name_12, as_0) > -1) ObjectDelete(l_name_12);
   }
}

int start() {
   double ld_0;
   string ls_404;
   string l_text_428;
   ObjectCreate("text1", OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
   ObjectSetText("text1", "THV V 3", 14, "Arial Black", text1Color);
   ObjectSet("text1", OBJPROP_CORNER, 1);
   ObjectSet("text1", OBJPROP_XDISTANCE, gi_180 + 13);
   ObjectSet("text1", OBJPROP_YDISTANCE, gi_176 + 1);
   ObjectCreate("text2", OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
   ObjectSetText("text2", "System", 9, "Batang", text2Color);
   ObjectSet("text2", OBJPROP_CORNER, 1);
   ObjectSet("text2", OBJPROP_XDISTANCE, gi_180 + 32);
   ObjectSet("text2", OBJPROP_YDISTANCE, gi_176 + 26);
   ObjectCreate("text3", OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
   ObjectSetText("text3", "© By Cobraforex", 8, "Arial", text3Color);
   ObjectSet("text3", OBJPROP_CORNER, 1);
   ObjectSet("text3", OBJPROP_XDISTANCE, gi_180 + 18);
   ObjectSet("text3", OBJPROP_YDISTANCE, gi_176 + 41);
   if (Period() == PERIOD_M1) ld_0 = 0.0002;
   if (Period() == PERIOD_M5) ld_0 = 0.0003;
   if (Period() == PERIOD_M15) ld_0 = 0.0005;
   if (Period() == PERIOD_M30) ld_0 = 0.008;
   if (Period() == PERIOD_H1) ld_0 = 0.0012;
   if (Period() == PERIOD_H4) ld_0 = 0.003;
   if (Period() == PERIOD_D1) ld_0 = 0.005;
   if (Period() == PERIOD_W1) ld_0 = 0.08;
   if (Period() == PERIOD_MN1) ld_0 = 0.015;
   if (gd_184 > 0.0) ld_0 = gd_184;
   double ld_8 = ld_0;
   double ld_16 = -1.0 * ld_0;
   drawLine("Trix_" + "line_HL", ld_8, HighLine_Color, Line_Style);
   drawLine("Trix_" + "line_ZL", 0, ZeroLine_Color, Line_Style);
   drawLine("Trix_" + "line_LL", ld_16, LowLine_Color, Line_Style);
   int li_24 = 0;
   double ld_28 = 0;
   double ld_36 = 0;
   double ld_unused_44 = 0;
   double ld_52 = 0;
   double ld_60 = 0;
   double ld_68 = 0;
   double ld_76 = 0;
   double ld_84 = 0;
   double ld_92 = 0;
   double ld_100 = 0;
   double ld_108 = 0;
   double l_x_116 = 0;
   double ld_124 = 0;
   double ld_132 = 0;
   double ld_140 = 0;
   double ld_148 = 0;
   double ld_156 = 0;
   double ld_164 = 0;
   double ld_172 = 0;
   double ld_180 = 0;
   double ld_188 = 0;
   double ld_196 = 0;
   double ld_204 = 0;
   double ld_212 = 0;
   double ld_220 = 0;
   double ld_228 = 0;
   double ld_236 = 0;
   double ld_244 = 0;
   double ld_252 = 0;
   double ld_260 = 0;
   double ld_268 = 0;
   double ld_276 = 0;
   double ld_284 = 0;
   double ld_292 = 0;
   double ld_300 = 0;
   double ld_308 = 0;
   double ld_316 = 0;
   double ld_324 = 0;
   double ld_332 = 0;
   double ld_340 = 0;
   double ld_348 = 1;
   double ld_356 = 0;
   double l_bars_364 = 0;
   double ld_372 = 0;
   double ld_380 = 0;
   double ld_388 = 0;
   double ld_396 = 0;
   string ls_412 = "nonono";
   int l_ind_counted_420 = IndicatorCounted();
   bool li_424 = TRUE;
   ld_380 = Trixnum_bars + gi_76 + gi_196 + gi_80 + gi_200 + gd_204;
   if (ld_380 == ld_388 && ls_412 == Symbol() && ld_396 == Time[4] - Time[5] && Bars - l_bars_364 < 2.0) ld_372 = Bars - l_bars_364;
   else ld_372 = -1;
   ls_412 = Symbol();
   ld_396 = Time[4] - Time[5];
   l_bars_364 = Bars;
   ld_388 = ld_380;
   if (ld_372 == 1.0 || ld_372 == 0.0) ld_356 = ld_372;
   else ld_348 = 1;
   if (!ShowIn1MChart && Period() == PERIOD_M1) li_424 = FALSE;
   if (ld_348 == 1.0) {
      ld_332 = gd_204 * gd_204;
      ld_340 = ld_332 * gd_204;
      l_x_116 = -ld_340;
      ld_124 = 3.0 * (ld_332 + ld_340);
      ld_132 = -3.0 * (2.0 * ld_332 + gd_204 + ld_340);
      ld_140 = 3.0 * gd_204 + 1.0 + ld_340 + 3.0 * ld_332;
      ld_292 = gi_76;
      if (ld_292 < 1.0) ld_292 = 1;
      ld_292 = (ld_292 - 1.0) / 2.0 + 1.0;
      ld_300 = 2 / (ld_292 + 1.0);
      ld_308 = 1 - ld_300;
      ld_292 = gi_80;
      if (ld_292 < 1.0) ld_292 = 1;
      ld_292 = (ld_292 - 1.0) / 2.0 + 1.0;
      ld_316 = 2 / (ld_292 + 1.0);
      ld_324 = 1 - ld_316;
      g_ibuf_212[Trixnum_bars - 1] = 0;
      ld_244 = 0;
      ld_252 = 0;
      ld_260 = 0;
      ld_268 = 0;
      ld_276 = 0;
      ld_284 = 0;
      g_ibuf_224[Trixnum_bars - 1] = 0;
      ld_148 = 0;
      ld_156 = 0;
      ld_164 = 0;
      ld_172 = 0;
      ld_180 = 0;
      ld_188 = 0;
      ld_356 = Trixnum_bars - 2;
      ld_348 = 0;
   }
   for (li_24 = ld_356; li_24 >= 0; li_24--) {
      if (gi_196 == 1) ld_196 = ld_300 * Open[li_24] + ld_308 * ld_244;
      else ld_196 = ld_300 * Close[li_24] + ld_308 * ld_244;
      ld_204 = ld_300 * ld_196 + ld_308 * ld_252;
      ld_212 = ld_300 * ld_204 + ld_308 * ld_260;
      ld_220 = ld_300 * ld_212 + ld_308 * ld_268;
      ld_228 = ld_300 * ld_220 + ld_308 * ld_276;
      ld_236 = ld_300 * ld_228 + ld_308 * ld_284;
      ld_28 = l_x_116 * ld_236 + ld_124 * ld_228 + ld_132 * ld_220 + ld_140 * ld_212;
      if ((ld_372 == 1.0 && li_24 == 1) || ld_372 == -1.0) {
         ld_244 = ld_196;
         ld_252 = ld_204;
         ld_260 = ld_212;
         ld_268 = ld_220;
         ld_276 = ld_228;
         ld_284 = ld_236;
      }
      ld_68 = ld_316 * Close[li_24] + ld_324 * ld_148;
      ld_76 = ld_316 * ld_68 + ld_324 * ld_156;
      ld_84 = ld_316 * ld_76 + ld_324 * ld_164;
      ld_92 = ld_316 * ld_84 + ld_324 * ld_172;
      ld_100 = ld_316 * ld_92 + ld_324 * ld_180;
      ld_108 = ld_316 * ld_100 + ld_324 * ld_188;
      ld_52 = l_x_116 * ld_108 + ld_124 * ld_100 + ld_132 * ld_92 + ld_140 * ld_84;
      if (gi_200 == 1) {
         g_ibuf_212[li_24] = (ld_28 - ld_36) / ld_36 + (ld_52 - ld_60) / ld_60;
         g_ibuf_224[li_24] = (ld_28 - ld_36) / ld_36;
         g_x_244 = g_ibuf_224[li_24];
      } else {
         if (ld_60 > 0.0 && ld_36 > 0.0) {
            g_ibuf_212[li_24] = (ld_52 - ld_60) / ld_60;
            g_ibuf_224[li_24] = (ld_28 - ld_36) / ld_36;
            g_x_244 = g_ibuf_224[li_24];
         }
      }
      g_ibuf_216[li_24] = EMPTY_VALUE;
      g_ibuf_220[li_24] = EMPTY_VALUE;
      if (g_ibuf_212[li_24 + 1] < g_ibuf_212[li_24]) {
         if (g_ibuf_216[li_24 + 1] == EMPTY_VALUE) g_ibuf_216[li_24 + 1] = g_ibuf_212[li_24 + 1];
         g_ibuf_216[li_24] = g_ibuf_212[li_24];
      } else {
         if (g_ibuf_212[li_24 + 1] > g_ibuf_212[li_24]) {
            if (g_ibuf_220[li_24 + 1] == EMPTY_VALUE) g_ibuf_220[li_24 + 1] = g_ibuf_212[li_24 + 1];
            g_ibuf_220[li_24] = g_ibuf_212[li_24];
         }
      }
      g_ibuf_228[li_24] = EMPTY_VALUE;
      g_ibuf_232[li_24] = EMPTY_VALUE;
      if (g_ibuf_224[li_24 + 1] < g_ibuf_224[li_24]) {
         if (g_ibuf_228[li_24 + 1] == EMPTY_VALUE) g_ibuf_228[li_24 + 1] = g_ibuf_224[li_24 + 1];
         g_ibuf_228[li_24] = g_ibuf_224[li_24];
      } else {
         if (g_ibuf_224[li_24 + 1] > g_ibuf_224[li_24]) {
            if (g_ibuf_232[li_24 + 1] == EMPTY_VALUE) g_ibuf_232[li_24 + 1] = g_ibuf_224[li_24 + 1];
            g_ibuf_232[li_24] = g_ibuf_224[li_24];
         }
      }
      if ((ld_372 == 1.0 && li_24 == 1) || ld_372 == -1.0) {
         ld_36 = ld_28;
         ld_60 = ld_52;
         ld_148 = ld_68;
         ld_156 = ld_76;
         ld_164 = ld_84;
         ld_172 = ld_92;
         ld_180 = ld_100;
         ld_188 = ld_108;
      }
      if (li_24 <= NumberOfDivergenceBars && li_424) {
         CatchBullishDivergence(li_24 + 2);
         CatchBearishDivergence(li_24 + 2);
      }
   }
   if (g_x_244 > 0.0) {
      if (AnalyzeLabelonoff) l_text_428 = "Bull cross";
      ObjectDelete("Trix_Crossing_Label");
      ObjectCreate("Trix_Crossing_Label", OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
      ObjectSet("Trix_Crossing_Label", OBJPROP_CORNER, 3);
      ObjectSet("Trix_Crossing_Label", OBJPROP_XDISTANCE, 7);
      ObjectSet("Trix_Crossing_Label", OBJPROP_YDISTANCE, 95);
      ObjectSet("Trix_Crossing_Label", OBJPROP_COLOR, LimeGreen);
      ObjectSetText("Trix_Crossing_Label", l_text_428, 14, "Arial Black", LimeGreen);
   }
   if (g_x_244 < 0.0) {
      if (AnalyzeLabelonoff) l_text_428 = "Bear cross";
      ObjectDelete("Trix_Crossing_Label");
      ObjectCreate("Trix_Crossing_Label", OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
      ObjectSet("Trix_Crossing_Label", OBJPROP_CORNER, 3);
      ObjectSet("Trix_Crossing_Label", OBJPROP_XDISTANCE, 2);
      ObjectSet("Trix_Crossing_Label", OBJPROP_YDISTANCE, 95);
      ObjectSet("Trix_Crossing_Label", OBJPROP_COLOR, Red);
      ObjectSetText("Trix_Crossing_Label", l_text_428, 14, "Arial Black", Red);
   }
   if (g_x_236 < 0.0 && g_x_244 > 0.0) {
      if (AlertOnTrixCross)
         if (SoundAlerts) PlaySound(TrixCrossSound);
   }
   g_x_236 = g_x_244;
   if (AlertOnTrixSigCross) {
      if (g_ibuf_224[2] < g_ibuf_212[2] && g_ibuf_224[1] > g_ibuf_212[1] && g_bars_252 < Bars) {
         if (AnalyzeLabelonoff) l_text_428 = "Analyze Buy";
         ObjectDelete("Order_Crossing_Label");
         ObjectCreate("Order_Crossing_Label", OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
         ObjectSet("Order_Crossing_Label", OBJPROP_CORNER, 3);
         ObjectSet("Order_Crossing_Label", OBJPROP_XDISTANCE, 2);
         ObjectSet("Order_Crossing_Label", OBJPROP_YDISTANCE, 25);
         ObjectSet("Order_Crossing_Label", OBJPROP_COLOR, Red);
         ObjectSetText("Order_Crossing_Label", l_text_428, 12, "Arial Black", Lime);
         ls_404 = gs_256 + " " + Symbol() + " " + TF2Str(Period()) + " BUY ALARM @ " + TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES);
         DoAlerts(ls_404, ls_404);
         g_bars_252 = Bars;
         if (SoundAlerts) PlaySound("analyze buy.wav");
      } else {
         if (g_ibuf_224[2] > g_ibuf_212[2] && g_ibuf_224[1] < g_ibuf_212[1] && g_bars_252 < Bars) {
            if (AnalyzeLabelonoff) l_text_428 = "Analyze Sell";
            ObjectDelete("Order_Crossing_Label");
            ObjectCreate("Order_Crossing_Label", OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
            ObjectSet("Order_Crossing_Label", OBJPROP_CORNER, 3);
            ObjectSet("Order_Crossing_Label", OBJPROP_XDISTANCE, 2);
            ObjectSet("Order_Crossing_Label", OBJPROP_YDISTANCE, 25);
            ObjectSet("Order_Crossing_Label", OBJPROP_COLOR, Red);
            ObjectSetText("Order_Crossing_Label", l_text_428, 12, "Arial Black", Red);
            ls_404 = gs_256 + " " + Symbol() + " " + TF2Str(Period()) + " SELL ALARM @ " + TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES);
            DoAlerts(ls_404, ls_404);
            g_bars_252 = Bars;
            if (SoundAlerts) PlaySound("analyze sell.wav");
         }
      }
   }
   if (AlertOnSlopeChange) {
      if (g_ibuf_224[1] > g_ibuf_212[1] && g_ibuf_228[2] != EMPTY_VALUE && g_ibuf_228[1] == EMPTY_VALUE && g_bars_252 < Bars) {
         if (AnalyzeLabelonoff) l_text_428 = "Analyze Exit";
         if (ObjectFind("Alarm_Crossing_Label" + Time[0]) == -1) {
            ObjectDelete("Alarm_Crossing_Label");
            ObjectCreate("Alarm_Crossing_Label" + Time[0], OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
            ObjectSet("Alarm_Crossing_Label" + Time[0], OBJPROP_CORNER, 3);
            ObjectSet("Alarm_Crossing_Label" + Time[0], OBJPROP_XDISTANCE, 1);
            ObjectSet("Alarm_Crossing_Label" + Time[0], OBJPROP_YDISTANCE, 43);
            ObjectSet("Alarm_Crossing_Label" + Time[0], OBJPROP_COLOR, Gold);
            ObjectSetText("Alarm_Crossing_Label" + Time[0], l_text_428, 12, "Arial Black", Gold);
         }
         ls_404 = gs_256 + " " + Symbol() + " " + TF2Str(Period()) + " TRIX EXIT ALARM @ " + TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES);
         DoAlerts(ls_404, ls_404);
         g_bars_252 = Bars;
         if (SoundAlerts) PlaySound("analyze exit.wav");
      } else {
         if (g_ibuf_224[1] < g_ibuf_212[1] && g_ibuf_232[2] != EMPTY_VALUE && g_ibuf_232[1] == EMPTY_VALUE && g_bars_252 < Bars) {
            if (AnalyzeLabelonoff) l_text_428 = "Analyze Exit";
            if (ObjectFind("Alarm_Crossing_Label" + Time[0]) == -1) {
               if (AnalyzeLabelonoff) {
                  ObjectDelete("Alarm_Crossing_Label");
                  ObjectCreate("Alarm_Crossing_Label" + Time[0], OBJ_LABEL, AnalyzeLabelWindow, 0, 0);
                  ObjectSet("Alarm_Crossing_Label" + Time[0], OBJPROP_CORNER, 3);
                  ObjectSet("Alarm_Crossing_Label" + Time[0], OBJPROP_XDISTANCE, 1);
                  ObjectSet("Alarm_Crossing_Label" + Time[0], OBJPROP_YDISTANCE, 43);
                  ObjectSet("Alarm_Crossing_Label" + Time[0], OBJPROP_COLOR, Gold);
                  ObjectSetText("Alarm_Crossing_Label" + Time[0], l_text_428, 12, "Arial Black", Gold);
               }
            }
            ls_404 = gs_256 + " " + Symbol() + " " + TF2Str(Period()) + " TRIX EXIT ALARM @ " + TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES);
            DoAlerts(ls_404, ls_404);
            g_bars_252 = Bars;
            if (SoundAlerts) PlaySound("analyze exit.wav");
         }
      }
      if (ObjectFind("Alarm_Crossing_Label" + Time[1]) != -1) ObjectDelete("Alarm_Crossing_Label" + Time[1]);
   }
   return (0);
}

void DoAlerts(string as_0, string as_8) {
   if (MsgAlerts) Alert(as_8);
   if (eMailAlerts) SendMail(as_0, as_8);
}

string TF2Str(int ai_0) {
   switch (ai_0) {
   case 1:
      return ("M1");
      break;
   case 5:
      return ("M5");
      break;
   case 15:
      return ("M15");
      break;
   case 30:
      return ("M30");
      break;
   case 60:
      return ("H1");
      break;
   case 240:
      return ("H4");
      break;
   case 1440:
      return ("D1");
      break;
   case 10080:
      return ("W1");
      break;
   case 43200:
      return ("MN");
   }
   return (Period());
}

void CatchBullishDivergence(int ai_0) {
   int li_4;
   int li_8;
   if (IsIndicatorTrough(ai_0) != 0) {
      li_4 = ai_0;
      li_8 = GetIndicatorLastTrough(ai_0);
      if (g_ibuf_224[li_4] > g_ibuf_224[li_8] && Low[li_4] < Low[li_8]) {
         g_ibuf_468[li_4] = g_ibuf_224[li_4] - 0.0001;
         if (drawPriceTrendLines == TRUE) DrawPriceTrendLine(Time[li_4], Time[li_8], Low[li_4], Low[li_8], BullishDivergenceColor, STYLE_SOLID);
         if (drawIndicatorTrendLines == TRUE) DrawIndicatorTrendLine(Time[li_4], Time[li_8], g_ibuf_224[li_4], g_ibuf_224[li_8], BullishDivergenceColor, STYLE_SOLID);
         if (EnableAlerts == TRUE) {
            if (SoundAlertOnDivergence == TRUE) SoundAlert(ClassicBullDivSound, li_4);
            else DisplayAlert("Classical bullish divergence on: ", li_4);
         }
      }
      if (g_ibuf_224[li_4] < g_ibuf_224[li_8] && Low[li_4] > Low[li_8]) {
         g_ibuf_468[li_4] = g_ibuf_224[li_4] - 0.0001;
         if (drawPriceTrendLines == TRUE) DrawPriceTrendLine(Time[li_4], Time[li_8], Low[li_4], Low[li_8], BullishDivergenceColor, STYLE_DOT);
         if (drawIndicatorTrendLines == TRUE) DrawIndicatorTrendLine(Time[li_4], Time[li_8], g_ibuf_224[li_4], g_ibuf_224[li_8], BullishDivergenceColor, STYLE_DOT);
         if (EnableAlerts == TRUE) {
            if (SoundAlertOnDivergence == TRUE) {
               SoundAlert(ReverseBullDivSound, li_4);
               return;
            }
            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_224[li_4] < g_ibuf_224[li_8] && High[li_4] > High[li_8]) {
         g_ibuf_472[li_4] = g_ibuf_224[li_4] + 0.0001;
         if (drawPriceTrendLines == TRUE) DrawPriceTrendLine(Time[li_4], Time[li_8], High[li_4], High[li_8], BearishDivergenceColor, STYLE_SOLID);
         if (drawIndicatorTrendLines == TRUE) DrawIndicatorTrendLine(Time[li_4], Time[li_8], g_ibuf_224[li_4], g_ibuf_224[li_8], BearishDivergenceColor, STYLE_SOLID);
         if (EnableAlerts == TRUE) {
            if (SoundAlertOnDivergence == TRUE) SoundAlert(ClassicBearDivSound, li_4);
            else DisplayAlert("Classical bearish divergence on: ", li_4);
         }
      }
      if (g_ibuf_224[li_4] > g_ibuf_224[li_8] && High[li_4] < High[li_8]) {
         g_ibuf_472[li_4] = g_ibuf_224[li_4] + 0.0001;
         if (drawPriceTrendLines == TRUE) DrawPriceTrendLine(Time[li_4], Time[li_8], High[li_4], High[li_8], BearishDivergenceColor, STYLE_DOT);
         if (drawIndicatorTrendLines == TRUE) DrawIndicatorTrendLine(Time[li_4], Time[li_8], g_ibuf_224[li_4], g_ibuf_224[li_8], BearishDivergenceColor, STYLE_DOT);
         if (EnableAlerts == TRUE) {
            if (SoundAlertOnDivergence == TRUE) {
               SoundAlert(ReverseBearDivSound, li_4);
               return;
            }
            DisplayAlert("Reverse bearish divergence on: ", li_4);
         }
      }
   }
}

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

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

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 = "THVTrix v" + "4" + "_Trix_DivergenceLine_# " + 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_264);
   if (l_window_36 >= 0) {
      l_name_40 = "THVTrix v" + "4" + "_Trix_DivergenceLine_$# " + 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);
   }
}

void DisplayAlert(string as_0, int ai_8) {
   string ls_unused_12;
   string ls_20;
   if (ai_8 <= 2 && Time[ai_8] != g_time_272) {
      g_time_272 = Time[ai_8];
      Alert(as_0, Symbol(), " , ", TF2Str(Period()), " minutes chart");
      ls_20 = "Divergence on " + TF2Str(Period());
      if (EmailDivergenceAlerts) SendMail(ls_20, as_0);
   }
}

void SoundAlert(string as_0, int ai_8) {
   if (ai_8 <= 2 && Time[ai_8] != g_time_276) {
      g_time_276 = Time[ai_8];
      PlaySound(as_0);
   }
}

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

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



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains open prices of each bar
Series array that contains close prices for each bar
Series array that contains the lowest prices of each bar
Series array that contains the highest prices of each bar


Indicator Curves created:

Implements a curve of type DRAW_LINE

Implements a curve of type DRAW_ARROW

Indicators Used:



Custom Indicators Used:

Order Management characteristics:

Other Features:

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