#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

double Buffer1[], Buffer2[];
extern int RegressionLength=10;     
double Group1, GBPUSDDeviation, EURGBPDeviation, GBPJPYDeviation, USDCHFDeviation, NZDUSDDeviation, AUDJPYDeviation, EURJPYDeviation;
double Group2, EURUSDDeviation, USDJPYDeviation, AUDUSDDeviation, NZDJPYDeviation, GBPCHFDeviation, CHFJPYDeviation, EURCHFDeviation;
string text;

int init(){

SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,Buffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,Buffer2);
string short_name = "2 Group % Standard Deviation From Regression";
IndicatorShortName(short_name);

return(0);}

int deinit(){
   Comment("");
return(0);}

int start()   {

int counted_bars = IndicatorCounted();
if (counted_bars<0) return(-1);
if (counted_bars>0) counted_bars--;
int offset = Bars - counted_bars;
while(offset >= 0){

   //Group1
   GBPUSDDeviation = Deviation("GBPUSD", RegressionLength,offset);
   EURGBPDeviation = Deviation("EURGBP", RegressionLength,offset);
   GBPJPYDeviation = Deviation("GBPJPY", RegressionLength,offset);
   USDCHFDeviation = Deviation("USDCHF", RegressionLength,offset);
   NZDUSDDeviation = Deviation("NZDUSD", RegressionLength,offset);
   AUDJPYDeviation = Deviation("AUDJPY", RegressionLength,offset);
   EURJPYDeviation = Deviation("EURJPY", RegressionLength,offset);
      
   //Group2   
   EURUSDDeviation = Deviation("EURUSD", RegressionLength,offset);
   USDJPYDeviation = Deviation("USDJPY", RegressionLength,offset);
   AUDUSDDeviation = Deviation("AUDUSD", RegressionLength,offset);
   NZDJPYDeviation = Deviation("NZDJPY", RegressionLength,offset);
   GBPCHFDeviation = Deviation("GBPCHF", RegressionLength,offset);
   CHFJPYDeviation = Deviation("CHFJPY", RegressionLength,offset);
   EURCHFDeviation = Deviation("EURCHF", RegressionLength,offset);
      
   Group1 = GBPUSDDeviation+EURGBPDeviation+GBPJPYDeviation+USDCHFDeviation+NZDUSDDeviation+AUDJPYDeviation+EURJPYDeviation;
   Group2 = EURUSDDeviation+USDJPYDeviation+AUDUSDDeviation+NZDJPYDeviation+GBPCHFDeviation+CHFJPYDeviation+EURCHFDeviation;
                 
   text = "\n--- Standard deviation from linear regression point as a percentage of linear regression ---\n";
   text = text + "\nGroup1:";
   text = text + "\nGBPUSD " + GBPUSDDeviation + "%";
   text = text + "\nEURGBP " + EURGBPDeviation + "%";
   text = text + "\nGBPJPY " + GBPJPYDeviation + "%";
   text = text + "\nUSDCHF " + USDCHFDeviation + "%";
   text = text + "\nNZDUSD " + NZDUSDDeviation + "%";
   text = text + "\nAUDJPY " + AUDJPYDeviation + "%";
   text = text + "\nEURJPY " + EURJPYDeviation + "%";
   
   text = text + "\n\nGroup2:";
   text = text + "\nEURUSD " + EURUSDDeviation + "%";
   text = text + "\nUSDJPY " + USDJPYDeviation + "%";
   text = text + "\nAUDUSD " + AUDUSDDeviation + "%";
   text = text + "\nNZDJPY " + NZDJPYDeviation + "%";
   text = text + "\nGBPCHF " + GBPCHFDeviation + "%";
   text = text + "\nCHFJPY " + CHFJPYDeviation + "%";
   text = text + "\nEURCHF " + EURCHFDeviation + "%";
   
   text = text + "\n\nGroup 1 deviation: " + Group1 + "    Group 2 deviation: " + Group2 + "\n";
   text = text + "Difference: " + MathAbs(Group1 - Group2);
     
   Comment(text);

   Buffer1[offset] = Group1;
   Buffer2[offset] = Group2;
   offset--;
}

return(0);}

// Linear regression & deviation percentage function
double Deviation(string symbol, int RegressionLength,int Offset)
   {
   // Linear Regression
   
   double SumY = 0;
   double Sum1 = 0;
   double Slope = 0;
   double DeviationPercent = 0;
   double Intercept = 0;
   double c;
   for (int x=0; x <= RegressionLength - 1; x++) {
      c = iClose(symbol, 0, x + Offset);
      SumY += c;
      Sum1 += x*c; }
   double SumBars = RegressionLength * (RegressionLength - 1)*0.5;
   double SumSqrBars = (RegressionLength - 1) * RegressionLength * (2 * RegressionLength - 1)/6;
	double Sum2 = SumBars * SumY;
	double Num1 = RegressionLength * Sum1 - Sum2;
	double Num2 = SumBars * SumBars - RegressionLength * SumSqrBars;
	if (Num2 != 0) Slope = Num1 / Num2;
	else Slope = 0;
	if (RegressionLength != 0) Intercept = (SumY - Slope * SumBars) / RegressionLength;
	else Intercept = 0;
	double LinearRegression = Intercept + Slope * (RegressionLength - 1);
	
	// Standard deviation from linear regression as a percentage of linear regression
	
	double StandardDeviation = iStdDev(symbol,0,RegressionLength,0,MODE_EMA,PRICE_CLOSE,0);
   if (LinearRegression + StandardDeviation != 0) DeviationPercent = (1 - (LinearRegression / (LinearRegression + StandardDeviation)))*100;
   else DeviationPercent = 0;
   if (MarketInfo(symbol,MODE_BID) < LinearRegression) DeviationPercent = -DeviationPercent;   
	
	return(DeviationPercent);
}