/*
   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 int PeriodRSI 	= 8;
extern int PeriodDEV 	= 30;
extern double	KDev		= 1.3185;
extern string s.1 = "Sound Alerts:";
extern bool SoundAlerts = TRUE;
extern string s.2 = "Email Alerts:";
extern bool EmailAlerts = FALSE;
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);
}
//+---------------------------------------------------------------------------------------------------------------------------------+
int start() 
{
	double SL;
	double sum;
	double m[];
	ArrayResize(m, PeriodDEV);
	int CountedBars = IndicatorCounted();
	int limit = Bars - CountedBars - 1;
	for (int i = limit; i >= 0; i--) 
		{
		buf1[i] = iRSI(NULL, 0, PeriodRSI, PRICE_LOW, i);
		sum = 0;
		for (int li_32 = i; li_32 < i + PeriodDEV; li_32++) 
			{
			m[li_32 - i] = buf1[li_32];
			sum += buf1[li_32] / PeriodDEV;
			}
		buf2[i] = sum + KDev * StDev(m, PeriodDEV);
		buf3[i] = sum - KDev * StDev(m, PeriodDEV);
		}
	if (Open[0] == Close[0] && Open[0] == Low[0] && Open[0] == High[0]) 
		{
		if (buf1[1] < buf2[1] && buf1[2] > buf2[2]) 
			{
			SL = High[iHighest(Symbol(), 0, MODE_HIGH, 3, 1)] + 1.0 * Point;
			if (SoundAlerts) Alert("FXCashFormula Signal: SHORT signal at " + Symbol() + " [" + Period() + "], Stop loss at " + SL);
			if (EmailAlerts) SendMail("FXCashFormula Signal at " + Symbol(), "SHORT signal at " + Symbol() + " [" + Period() + "], Stop loss at " + SL);
			}
		if (buf1[1] > buf3[1] && buf1[2] < buf3[2]) 
			{
			SL = Low[iLowest(Symbol(), 0, MODE_LOW, 3, 1)] - 1.0 * Point;
			if (SoundAlerts) Alert("FXCashFormula Signal: LONG signal at " + Symbol() + " [" + Period() + "], Stop loss at " + SL);
			if (EmailAlerts) SendMail("FXCashFormula Signal at " + Symbol(), "LONG signal at " + Symbol() + " [" + Period() + "], Stop loss at " + SL);
			}
		}
	return (0);
}
//+---------------------------------------------------------------------------------------------------------------------------------+
double StDev(double m[], int DPeriod) 
{
	return (MathSqrt(Variance(m, DPeriod)));
}
//+---------------------------------------------------------------------------------------------------------------------------------+
double Variance(double m[], int DPeriod) 
{
	double sum;
	double sum2;
	for (int i = 0; i < DPeriod; i++) 
		{
		sum += m[i];
		sum2 += MathPow(m[i], 2);
		}
	return ((sum2 * DPeriod - sum * sum) / (DPeriod * (DPeriod - 1)));
}