
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window    // #Chaos Entry
#property indicator_buffers 4
#property indicator_color1 CLR_NONE
#property indicator_color2 CLR_NONE
#property indicator_color3 Lime
#property indicator_color4 Yellow

extern bool UseSoundBuy=true;
extern bool AlertSoundBuy=true;
extern bool UseSoundSell=true;
extern bool AlertSoundSell=true;
extern string SoundFileBuy ="tada.wav";
extern string SoundFileSell="email.wav";
extern bool SendMailPossible = false;
extern int SIGNAL_BAR= 1; 
 bool SoundBuy  = False;
 bool SoundSell = False;

int g_period_76 = 5;
int g_period_80 = 21;
int g_period_84 = 17;
bool gi_88 = TRUE;
double g_ibuf_92[];
double g_ibuf_96[];
double g_ibuf_100[];
double g_ibuf_104[];
int gi_108 = 0;
int gi_112 = 0;
double gd_116 = 0.0;
double gd_unused_124 = 0.0;

int init() {
   SetIndexStyle(0, DRAW_NONE);
   SetIndexBuffer(0, g_ibuf_92);
   SetIndexStyle(1, DRAW_NONE, STYLE_DASH, 3);
   SetIndexBuffer(1, g_ibuf_96);
   SetIndexStyle(2, DRAW_ARROW, STYLE_DASH, 3);
   SetIndexArrow(2, 233);
   SetIndexBuffer(2, g_ibuf_100);
   SetIndexEmptyValue(2, 0.0);
   SetIndexStyle(3, DRAW_ARROW, STYLE_DASH, 3);
   SetIndexArrow(3, 234);
   SetIndexBuffer(3, g_ibuf_104);
   SetIndexEmptyValue(3, 0.0);
   return (0);
}

int deinit() {
   ObjectsDeleteAll(0, OBJ_ARROW);
   return (0);
}

int start() {
   int li_4 = IndicatorCounted();
   double l_irsi_8 = 0;
   bool li_16 = FALSE;
   double l_price_20 = 0;
   if (li_4 < 0) return (-1);
   if (li_4 > 0) li_4--;
   int li_0 = Bars - li_4;
   for (int li_28 = 0; li_28 < li_0; li_28++) {
      g_ibuf_92[li_28] = iMA(NULL, 0, g_period_76, 0, MODE_EMA, PRICE_CLOSE, li_28);
      g_ibuf_96[li_28] = iMA(NULL, 0, g_period_80, 0, MODE_EMA, PRICE_CLOSE, li_28);
      l_irsi_8 = iRSI(NULL, 0, g_period_84, PRICE_CLOSE, li_28);
      gd_116 = g_ibuf_92[li_28] - g_ibuf_96[li_28];
      if (gd_116 > 0.0 && l_irsi_8 > 50.0) gi_108 = 1;
      else
         if (gd_116 < 0.0 && l_irsi_8 < 50.0) gi_108 = 2;
      if (gi_108 == 1 && gi_112 == 2) {
         g_ibuf_104[li_28 - 1] = High[li_28 - 1] - 5.0 * Point;
         li_16 = TRUE;
         l_price_20 = Ask;
      } else {
         if (gi_108 == 2 && gi_112 == 1) {
            g_ibuf_100[li_28 - 1] = Low[li_28 - 1] - 5.0 * Point;
            li_16 = TRUE;
            l_price_20 = Bid;
         }
      }
      gi_112 = gi_108;
      gd_unused_124 = gd_116;
   }
   if (gi_88 && li_16) {
    //  PlaySound("alert2.wav");
    //  if (gi_112 == 1) MessageBox("Entry point: buy at " + l_price_20 + "!!", "Entry Point", 0);
    //  else
       //  if (gi_112 == 2) MessageBox("Entry point: sell at " + l_price_20 + "!!", "Entry Point", 0);
      li_16 = FALSE;
   }
   RefreshRates();
  //+------------------------------------------------------------------+
    string  message  =  StringConcatenate("#Chaos Entry (", Symbol(), ", ", Period(), ")  -  BUY!!!" ,"-" ,TimeToStr(TimeLocal(),TIME_SECONDS)); 
    string  message2 =  StringConcatenate("#Chaos Entry (", Symbol(), ", ", Period(), ")  -  SELL!!!","-"  ,TimeToStr(TimeLocal(),TIME_SECONDS)); 
               
        if (g_ibuf_100[SIGNAL_BAR] != EMPTY_VALUE && g_ibuf_100[SIGNAL_BAR] != 0 && SoundBuy)
         {
         SoundBuy = False;
            if (UseSoundBuy) PlaySound (SoundFileBuy);
               if(AlertSoundBuy){         
               Alert(message);                             
               if (SendMailPossible) SendMail(Symbol(),message); 
            }              
         } 
      if (!SoundBuy && (g_ibuf_100[SIGNAL_BAR] == EMPTY_VALUE || g_ibuf_100[SIGNAL_BAR] == 0)) SoundBuy = True;  
            
  
     if (g_ibuf_104[SIGNAL_BAR] != EMPTY_VALUE && g_ibuf_104[SIGNAL_BAR] != 0 && SoundSell)
         {
         SoundSell = False;
            if (UseSoundSell) PlaySound (SoundFileSell); 
             if(AlertSoundSell){                    
             Alert(message2);             
             if (SendMailPossible) SendMail(Symbol(),message2); 
             }            
         } 
      if (!SoundSell && (g_ibuf_104[SIGNAL_BAR] == EMPTY_VALUE || g_ibuf_104[SIGNAL_BAR] == 0)) SoundSell = True; 
   return (0);
}