/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2011, FX Cash Formula"
#property link      "http://www.FXCashFormula.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Silver
#property indicator_color2 Red
#property indicator_color3 Green

extern string s.0 					= "FX Cash Formula";
extern bool 	UseExtreme 		= true;
extern string ExtremeName 	= "FX Cash Extreme";
extern string s.1 					= "Sound Alerts:";
extern bool SoundAlerts 		= true;
extern string s.2 					= "Email Alerts:";
extern bool EmailAlerts 		= false;

int RSIPeriod = 8;
int BandsPeriod = 30;
double buf1[];
double buf2[];
double buf3[];

int init() {
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(0, buf1);
   SetIndexBuffer(1, buf2);
   SetIndexBuffer(2, buf3);
   return (0);
}

int deinit() {
   return (0);
}

datetime OldTime=0;

int start() 
{
	bool ExtremeSignal=false;
	double ld_0;
	double sum;
	double BandsMas[];
	ArrayResize(BandsMas, BandsPeriod);
	int CountedBars = IndicatorCounted();
	int limit = Bars - CountedBars - 1;
	for (int i = limit; i >= 0; i--) 
		{
		buf1[i] = iRSI(NULL, 0, RSIPeriod, PRICE_LOW, i);
		sum = 0;
		for (int j = i; j < i + BandsPeriod; j++) 
			{
			BandsMas[j - i] = buf1[j];
			sum += buf1[j] / BandsPeriod;
			}
		buf2[i] = sum + 1.3185 * StDev(BandsMas, BandsPeriod);
		buf3[i] = sum - 1.3185 * StDev(BandsMas, BandsPeriod);
		}


	//datetime NewTime=TimeCurrent();
	//if (Open[0] == Close[0] && Open[0] == Low[0] && Open[0] == High[0]) 
	if(OldTime<Time[0])
		{
		if (buf1[1] < buf2[1] && buf1[2] > buf2[2]) 
			{
			ld_0 = High[iHighest(Symbol(), 0, MODE_HIGH, 3, 1)] + 1.0 * Point;
			
			ExtremeSignal=false;
			if(!UseExtreme) ExtremeSignal=true;
			else if(iCustom(NULL,0,ExtremeName,1,1)<0) ExtremeSignal=true;

			if (SoundAlerts && ExtremeSignal) 
				Alert(TimeToStr(Time[0],TIME_DATE) + " " +TimeToStr(Time[0],TIME_MINUTES) + " | "
									+ "FXCashFormula EDU Signal: SHORT signal at " + Symbol() + " [" + Period() + "], Stop loss at " + ld_0);
			if (EmailAlerts && ExtremeSignal) 
				SendMail(TimeToStr(Time[0],TIME_DATE) + " " +TimeToStr(Time[0],TIME_MINUTES) + " | "
									+ "FXCashFormula EDU Signal at " + Symbol(), "SHORT signal at " + Symbol() + " [" + Period() + "], Stop loss at " + ld_0);
			}
		if (buf1[1] > buf3[1] && buf1[2] < buf3[2]) 
			{
			ld_0 = Low[iLowest(Symbol(), 0, MODE_LOW, 3, 1)] - 1.0 * Point;

			ExtremeSignal=false;
			if(!UseExtreme) ExtremeSignal=true;
			else if(iCustom(NULL,0,ExtremeName,0,1)>0) ExtremeSignal=true;
			
			if (SoundAlerts && ExtremeSignal) 
				Alert(TimeToStr(Time[0],TIME_DATE) + " " +TimeToStr(Time[0],TIME_MINUTES) + " | "
								+ "FXCashFormula EDU Signal: LONG signal at " + Symbol() + " [" + Period() + "], Stop loss at " + ld_0);
			if (EmailAlerts && ExtremeSignal) 
				SendMail(TimeToStr(Time[0],TIME_DATE) + " " +TimeToStr(Time[0],TIME_MINUTES) + " | "
								+ "FXCashFormula EDU Signal at " + Symbol(), "LONG signal at " + Symbol() + " [" + Period() + "], Stop loss at " + ld_0);
			}
		OldTime=Time[0];
		}
	return (0);
}

double StDev(double ada_0[], int ai_4) {
   return (MathSqrt(Variance(ada_0, ai_4)));
}

double Variance(double ada_0[], int ai_4) {
   double sum;
   double ld_16;
   for (int l_index_24 = 0; l_index_24 < ai_4; l_index_24++) {
      sum += ada_0[l_index_24];
      ld_16 += MathPow(ada_0[l_index_24], 2);
   }
   return ((ld_16 * ai_4 - sum * sum) / (ai_4 * (ai_4 - 1)));
}