#property indicator_chart_window

extern int RegressionLength=96;     
double Group1, GBPUSDDeviation, EURGBPDeviation, GBPJPYDeviation, USDCHFDeviation, NZDUSDDeviation, AUDJPYDeviation, EURJPYDeviation;
double Group2, EURUSDDeviation, USDJPYDeviation, AUDUSDDeviation, NZDJPYDeviation, GBPCHFDeviation, CHFJPYDeviation, EURCHFDeviation;
string text;

int init(){
return(0);}

int deinit(){
   Comment("");
return(0);}

int start()   {
//Group1
   GBPUSDDeviation = Deviation("GBPUSD", RegressionLength,0);
   EURGBPDeviation = Deviation("EURGBP", RegressionLength,0);
   GBPJPYDeviation = Deviation("GBPJPY", RegressionLength,0);
   USDCHFDeviation = Deviation("USDCHF", RegressionLength,0);
   NZDUSDDeviation = Deviation("NZDUSD", RegressionLength,0);
   AUDJPYDeviation = Deviation("AUDJPY", RegressionLength,0);
   EURJPYDeviation = Deviation("EURJPY", RegressionLength,0);
      
//Group2   
   EURUSDDeviation = Deviation("EURUSD", RegressionLength,0);
   USDJPYDeviation = Deviation("USDJPY", RegressionLength,0);
   AUDUSDDeviation = Deviation("AUDUSD", RegressionLength,0);
   NZDJPYDeviation = Deviation("NZDJPY", RegressionLength,0);
   GBPCHFDeviation = Deviation("GBPCHF", RegressionLength,0);
   CHFJPYDeviation = Deviation("CHFJPY", RegressionLength,0);
   EURCHFDeviation = Deviation("EURCHF", RegressionLength,0);
      
   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";
   if (Group1 > Group2) text = text + "Sell Group 1, Buy Group 2";
   if (Group2 > Group1) text = text + "Buy Group 1, Sell Group 2";
     
   Comment(text);

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, PERIOD_M15, 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,PERIOD_M15,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);
}