// LindencourtFX Signal
// modified by San May 31, 2010

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
string SignalText;

extern bool       RepeatSignals        = false;
extern int        CountBars            = 2500;
extern bool       EmailON              = true;
extern bool       SoundON              = true;
extern bool       AlertON              = true;
int Alert_Count = 0;
datetime    Prior_Time;
bool hasPriceClosedBelow21MA = false;
bool hasPriceClosedAbove21MA = false;

int init() {
  SetIndexStyle(0, DRAW_ARROW, 3);
  SetIndexArrow(0, 233);
  SetIndexBuffer(0, CrossUp);
  SetIndexStyle(1, DRAW_ARROW, 3);
  SetIndexArrow(1, 234);
  SetIndexBuffer(1, CrossDown);
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
 
   if (iTime(Symbol(),Period(),1) != Prior_Time)
    {
     Prior_Time = iTime(Symbol(),Period(),1);
     Alert_Count = 0;
    }
   else
    {
     return;
    }   
   
   int limit = MathMin(Bars-counted_bars,CountBars);

   for (int i=limit; i>=0; i--)
   {
      double Ema7 = iMA(NULL, 0, 7, 0, MODE_EMA, PRICE_CLOSE, i);
      double Ema21 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, i);
      double Ema84 = iMA(NULL, 0, 84, 0, MODE_EMA, PRICE_CLOSE, i);
      double CCI = iCustom(NULL, 0, "LC-FX-Snipers-T3-CCI_Alert", 5, 5, 0.618, 0, i+1); // Condition must be met at OPEN of bar (which is close to the previous Close)
      double RSI = iRSI(NULL,0,13,0,i) - 50; // I compared the custom RSI to the standard one, and the difference seems to only be a constant -50
      
      if(Close[i]<Ema21)
         hasPriceClosedBelow21MA = true;
      else if(Close[i]>Ema21)
         hasPriceClosedAbove21MA = true;
      
      if(Ema7>Ema21 && Close[i]>Ema84 && CCI > 0 && RSI > 10) { // long conditions
         if(RepeatSignals){
            CrossUp[i]=Low[i]-0.0010;
            if (SoundON == 1 && Alert_Count == 0) PlaySound("alert.wav");
            if (AlertON == 1 && Alert_Count == 0) Alert("LindencourtFX BUY Signal: " + Symbol());
            if (EmailON == 1 && Alert_Count == 0) SendMail("LindencourtFX Signal: BUY", "You have LindencourtFX BUY signal on " + Symbol());}
         else if(hasPriceClosedBelow21MA && CrossUp[i+1] == EMPTY_VALUE){
            CrossUp[i]=Low[i]-0.0010;
            if (SoundON == 1 && Alert_Count == 0) PlaySound("alert.wav");
            if (AlertON == 1 && Alert_Count == 0) Alert("LindencourtFX BUY Signal: " + Symbol());
            if (EmailON == 1 && Alert_Count == 0) SendMail("LindencourtFX Signal: BUY", "You have LindencourtFX BUY signal on " + Symbol());
         hasPriceClosedBelow21MA = false;}
      }
      else if(Ema7<Ema21 && Close[i]<Ema84 && CCI < 0 && RSI < -10) { // short conditions
         if(RepeatSignals){
            CrossDown[i]=High[i]+0.0010;
            if (SoundON == 1 && Alert_Count == 0) PlaySound("alert.wav");
            if (AlertON == 1 && Alert_Count == 0) Alert("LindencourtFX SELL Signal: " + Symbol());
            if (EmailON == 1 && Alert_Count == 0) SendMail("LindencourtFX Signal: SELL", "You have LindencourtFX SELL signal on " + Symbol());}
         else if(hasPriceClosedAbove21MA && CrossUp[i+1] == EMPTY_VALUE){
            CrossDown[i]=High[i]+0.0010;
            if (SoundON == 1 && Alert_Count == 0) PlaySound("alert.wav");
            if (AlertON == 1 && Alert_Count == 0) Alert("LindencourtFX SELL Signal: " + Symbol());
            if (EmailON == 1 && Alert_Count == 0) SendMail("LindencourtFX Signal: SELL", "You have LindencourtFX SELL signal on " + Symbol()); 
         hasPriceClosedAbove21MA = false;}
      }
   }
   return (0);
}

