
#property copyright "GVC"
#property link      ""
#property indicator_chart_window

#define pair.count 5


extern string Buy_pair_1 = "AUDJPYm";

extern string Buy_pair_2 = "EURUSDm";  

extern string Buy_pair_3 = "EURJPYm"; 

extern string Buy_pair_4 = "GBPJPYm" ;

extern string Buy_pair_5 = "GBPUSDm" ;


double AUDJPYm, EURUSDm, EURJPYm, GBPJPYm, GBPUSDm;

int blue, red;

double AUDJPYm_now,EURUSDm_now,EURJPYm_now,GBPJPYm_now,GBPUSDm_now;

double AUDJPYm_pips, EURUSDm_pips, EURJPYm_pips, GBPJPYm_pips,GBPUSDm_pips;


string short_name;
int wnd;

string PairsNames1[pair.count];   
string PairsNames2[pair.count];   
string PairsNames3[pair.count];   
string PairsNames4[pair.count];
string PairsNames5[pair.count];   

int init()
{
      
   short_name="C5 Signal";
   IndicatorShortName(short_name);

   return(0);
}

int deinit()
{
   delete_objects(1);
   delete_objects(2);
   delete_objects(3);
   delete_objects(4);
   delete_objects(5);
   delete_objects(6);
   delete_objects(7);
    
   return(0);
}

int start()
{
   int counted_bars = IndicatorCounted();

   if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) counted_bars--;


   run_statistics_longs(1);   
   draw_objects(1, PairsNames1);

   run_statistics_longs(2);    
   draw_objects(2, PairsNames2);

   run_statistics_longs(3);   
   draw_objects(3, PairsNames3);

   run_statistics_longs(4);    
   draw_objects(4, PairsNames4);
   

return(0);
}


void run_statistics_longs(int week_back)
{

bool i_Open = false;

int tf=0;
int bars_back = 0;

AUDJPYm =0; EURUSDm=0; EURJPYm=0; GBPJPYm=0; GBPUSDm=0;

AUDJPYm_now =0; EURUSDm_now = 0; EURJPYm_now=0; GBPJPYm_now=0; GBPUSDm_now=0;

AUDJPYm_pips = 0; EURUSDm_pips=0; EURJPYm_pips = 0; GBPJPYm_pips=0; GBPUSDm_pips=0;


 if (week_back==1) // this week
 {
     tf = PERIOD_W1;
     bars_back = 0;
     i_Open = true;
 }    
 else if (week_back==2) // this day
 {
     tf = PERIOD_D1;
     bars_back = 0;
     i_Open = true;
 }
else if (week_back==3) // 4 hours ago
 {
     tf = PERIOD_M1;
     bars_back = 240;
     i_Open = true;
 }
else if (week_back==4) // hour ago
 {
     tf = PERIOD_M1;
     bars_back = 60;
     i_Open = true;
 }
 
  
{
   AUDJPYm      = iOpen(Buy_pair_1, tf, bars_back);
   AUDJPYm_now  = MarketInfo(Buy_pair_1, MODE_ASK);
   AUDJPYm_pips = (AUDJPYm_now-AUDJPYm)/dPoint(Buy_pair_1);
}

{
   EURUSDm= iOpen(Buy_pair_2, tf, bars_back);
   EURUSDm_now= MarketInfo(Buy_pair_2, MODE_ASK);
   EURUSDm_pips = (EURUSDm_now-EURUSDm)/dPoint(Buy_pair_2);

}

{
   EURJPYm= iOpen(Buy_pair_3, tf, bars_back);
   EURJPYm_now= MarketInfo(Buy_pair_3, MODE_ASK);
   EURJPYm_pips = (EURJPYm_now-EURJPYm)/dPoint(Buy_pair_3);
   
   //Print (GBPJPY,GBPJPY_now, GBPJPY_pips);
}

{
   GBPJPYm= iOpen(Buy_pair_4, tf, bars_back);
   GBPJPYm_now= MarketInfo(Buy_pair_4, MODE_ASK);
   GBPJPYm_pips = (GBPJPYm_now-GBPJPYm)/dPoint(Buy_pair_4);
}

{
   GBPUSDm= iOpen(Buy_pair_5, tf, bars_back);
   GBPUSDm_now= MarketInfo(Buy_pair_5, MODE_ASK);
   GBPUSDm_pips = (GBPUSDm_now-GBPUSDm)/dPoint(Buy_pair_5);
}

   return;
}


void draw_objects(int week_back, string& PairsNames[])
{

int x_coord;
string loc_date;

 
 if (week_back==1)
     {x_coord = 5; loc_date="W1";}
 else if (week_back==2)
     {x_coord = 97; loc_date="D1";}
 else if (week_back==3)
     {x_coord = 190; loc_date="H4";}
 else if (week_back==4)
     {x_coord = 283; loc_date="H1";}

int companion =WindowFind(short_name);
if (companion == -1)companion=0;

blue=0; red=0;                                                                                  

   string PairsDisplay[pair.count];   

   string PairsSearch[pair.count];   
   
   color  PairsColor[pair.count];              
   double PLArray[pair.count];

   bool ShowDirections[pair.count];

   if (StringLen(PairsNames[0])>0)
  // {
      for(int m=0;m<pair.count;m++)
      { 
         PairsSearch[m] = PairsNames[m];   
      }
  // }

   PLArray[0]=AUDJPYm_pips;
   PLArray[1]=EURUSDm_pips;
   PLArray[2]=EURJPYm_pips;
   PLArray[3]=GBPJPYm_pips;      
   PLArray[4]=GBPUSDm_pips;
     
   PairsNames[0]=Buy_pair_1;
   PairsNames[1]=Buy_pair_2;
   PairsNames[2]=Buy_pair_3;
   PairsNames[3]=Buy_pair_4;   
   PairsNames[4]=Buy_pair_5;
    
   PairsColor[0] = Red;
   PairsColor[1] = Red;
   PairsColor[2] = Red;
   PairsColor[3] = Red;  
   PairsColor[4] = Red;
     

    
   // custom sorting function
   
   int plarr_size = ArraySize(PLArray);
   
   double temp_var;
   string temp_str;
   color temp_color;
   //bool temp_direction;
   
   for(int i=0;i < plarr_size;i++) //Hold the first element
	{
		for(int j=i+1;j<plarr_size;j++) //Hold the next element from the first element
		{
			if(PLArray[i] < PLArray[j])	//comparing first and next element
			{
				temp_var = PLArray[j];	
				PLArray[j] = PLArray[i];
				PLArray[i] = temp_var;
				
				temp_str = PairsNames[j];	
				PairsNames[j] = PairsNames[i];
				PairsNames[i] = temp_str;

				temp_color  = PairsColor[j];	
				PairsColor[j] = PairsColor[i];
				PairsColor[i] = temp_color;
				
				

					}
		}
	}
   
   for(i=0;i<pair.count;i++)
    {        
      if(PLArray[i]>0) PairsColor [i]= Lime;
     
       {
         PairsDisplay[i] = ""+PairsNames[i];
         blue = blue + PLArray[i];
       }
       
     
    }

    // Print(red,"...",blue);    

if (ObjectFind(week_back+"sig_tit") == -1 ){
   ObjectCreate(week_back+"sig_tit", OBJ_LABEL, companion, 0, 0);
   ObjectSet(week_back+"sig_tit", OBJPROP_COLOR, Red);
   ObjectSet(week_back+"sig_tit", OBJPROP_XDISTANCE, x_coord+25);
   ObjectSet(week_back+"sig_tit", OBJPROP_YDISTANCE, 15);
   
   ObjectSetText(week_back+"sig_tit",loc_date,10,"Arial",White);


int y_coord = 30;
}
if (StringLen(PairsDisplay[0])>0) //StringLen(StringTrimLeft(StringSubstr(PairsDisplay[0],4)))>0
{
   if (ObjectFind("W"+week_back+"00") == -1 ){
      ObjectCreate("W"+week_back+"00", OBJ_LABEL, companion, 0, 0);
      ObjectSet("W"+week_back+"00", OBJPROP_COLOR, Red);
      ObjectSet("W"+week_back+"00", OBJPROP_XDISTANCE, x_coord);
      ObjectSet("W"+week_back+"00", OBJPROP_YDISTANCE, y_coord);
   }

   ObjectSetText("W"+week_back+"00",PairsDisplay[0]+"  "+DoubleToStr(PLArray[0],0), 9,
                     "Arial",PairsColor[0]);

     

      y_coord = y_coord + 20;
}


if (StringLen(PairsDisplay[1])>0)
{
   if (ObjectFind("W"+week_back+"01") == -1 ){
      ObjectCreate("W"+week_back+"01", OBJ_LABEL, companion, 0, 0);
      ObjectSet("W"+week_back+"01", OBJPROP_COLOR, Red);
      ObjectSet("W"+week_back+"01", OBJPROP_XDISTANCE, x_coord);
      ObjectSet("W"+week_back+"01", OBJPROP_YDISTANCE, y_coord-5);
   }

   ObjectSetText("W"+week_back+"01",PairsDisplay[1]+"  "+DoubleToStr(PLArray[1],0), 9,
         "Arial",PairsColor[1]);

     
         
      y_coord = y_coord + 20;
}

if (StringLen(PairsDisplay[2])>0)
{
   if (ObjectFind("W"+week_back+"02") == -1 ){
      ObjectCreate("W"+week_back+"02", OBJ_LABEL, companion, 0, 0);
      ObjectSet("W"+week_back+"02", OBJPROP_COLOR, Red);
      ObjectSet("W"+week_back+"02", OBJPROP_XDISTANCE, x_coord);
      ObjectSet("W"+week_back+"02", OBJPROP_YDISTANCE, y_coord-10);
   }

   ObjectSetText("W"+week_back+"02",PairsDisplay[2]+"  "+DoubleToStr(PLArray[2],0), 9,
      "Arial",PairsColor[2]);

     
         
      y_coord = y_coord + 20;

}

if (StringLen(PairsDisplay[3])>0)
{
   if (ObjectFind("W"+week_back+"03") == -1 ){
      ObjectCreate("W"+week_back+"03", OBJ_LABEL, companion, 0, 0);
      ObjectSet("W"+week_back+"03", OBJPROP_COLOR, Red);
      ObjectSet("W"+week_back+"03", OBJPROP_XDISTANCE, x_coord);
      ObjectSet("W"+week_back+"03", OBJPROP_YDISTANCE, y_coord-15);
   }

   ObjectSetText("W"+week_back+"03",PairsDisplay[3]+"  "+DoubleToStr(PLArray[3],0), 9,
       "Arial",PairsColor[3]);

    
         
      y_coord = y_coord + 20;
}

if (StringLen(PairsDisplay[4])>0)
{
   if (ObjectFind("W"+week_back+"04") == -1 ){
      ObjectCreate("W"+week_back+"04", OBJ_LABEL, companion, 0, 0);
      ObjectSet("W"+week_back+"04", OBJPROP_COLOR, Red);
      ObjectSet("W"+week_back+"04", OBJPROP_XDISTANCE, x_coord);
      ObjectSet("W"+week_back+"04", OBJPROP_YDISTANCE, y_coord-20);
   }

   ObjectSetText("W"+week_back+"04",PairsDisplay[4]+"  "+DoubleToStr(PLArray[4],0), 9,
      "Arial",PairsColor[4]);

              
      y_coord = y_coord + 20;

}

if (ObjectFind(week_back+"lin") == -1 ){
    ObjectCreate(week_back+"lin",OBJ_LABEL,companion,0, 0);
    ObjectSet(week_back+"lin",OBJPROP_COLOR,White);
    ObjectSet(week_back+"lin", OBJPROP_XDISTANCE, x_coord);
    ObjectSet(week_back+"lin", OBJPROP_YDISTANCE, y_coord-32);
    ObjectSetText(week_back+"lin","--------------", 8,
         "Arial",DarkViolet);
    y_coord = y_coord+20;
}


/*if (ObjectFind(week_back+"tot") == -1 ){
   ObjectCreate(week_back+"tot", OBJ_LABEL, companion, 0, 0);
   ObjectSet(week_back+"tot", OBJPROP_COLOR, Yellow);
   ObjectSet(week_back+"tot", OBJPROP_XDISTANCE, x_coord);
   ObjectSet(week_back+"tot", OBJPROP_YDISTANCE, y_coord);


   ObjectSetText(week_back+"tot","TOTAL:"+DoubleToStr(blue,0), 13,
         "Courier New",Yellow);
   y_coord = y_coord +20;         

}*/

if (ObjectFind(week_back+"sig") == -1 ){
   ObjectCreate(week_back+"sig", OBJ_LABEL, companion, 0, 0);
   ObjectSet(week_back+"sig", OBJPROP_COLOR, Red);
   ObjectSet(week_back+"sig", OBJPROP_XDISTANCE, x_coord);
   ObjectSet(week_back+"sig", OBJPROP_YDISTANCE, y_coord-42);
}

  string trend = "NONE";
   color col = White;
   
   if (blue < 0 && blue < -100)
      {trend = "SELL (Strong)"; col = Red;}
   else if (blue < 0 && blue > -100)
      {trend = "SELL (Weak)"; col = Orange;}
   else if (blue > 0 && blue > 100)
      {trend = "BUY (Strong)"; col = Lime;}
   else if (blue > 0 && blue < 100 )
      {trend = "BUY (Weak)"; col = Goldenrod;}



   ObjectSetText(week_back+"sig",trend, 10,"Arial",col);

    WindowRedraw();
   
}


void delete_objects(int week_back)
{
  for(int m=0;m<pair.count;m++)
  { 
   if (ObjectFind("WA"+week_back+PairsNames1[m]) > -1 ){
      ObjectDelete("WA"+week_back+PairsNames1[m]);
   }

   if (ObjectFind("W"+week_back+PairsNames1[m]) > -1 ){
      ObjectDelete("W"+week_back+PairsNames1[m]);
   }
  }
  
   if (ObjectFind(week_back+"b_tot") > -1 ){
      ObjectDelete(week_back+"b_tot");
   }

   if (ObjectFind(week_back+"tot") > -1 ){
      ObjectDelete(week_back+"tot");
   }

   if (ObjectFind(week_back+"s_tot") > -1 ){
      ObjectDelete(week_back+"s_tot");
   }

   if (ObjectFind(week_back+"lin") > -1 ){
      ObjectDelete(week_back+"lin");
   }

   if (ObjectFind(week_back+"lin_med") > -1 ){
      ObjectDelete(week_back+"lin_med");
   }

   if (ObjectFind(week_back+"sig_tit") > -1 ){
      ObjectDelete(week_back+"sig_tit");
   }
   
   if (ObjectFind(week_back+"sig") > -1 ){
      ObjectDelete(week_back+"sig");
   }

   if (ObjectFind("W"+week_back+"00") > -1 ){
      ObjectDelete("W"+week_back+"00");
   }

   if (ObjectFind("W"+week_back+"01") > -1 ){
      ObjectDelete("W"+week_back+"01");
   }

   if (ObjectFind("W"+week_back+"02") > -1 ){
      ObjectDelete("W"+week_back+"02");
   }

   if (ObjectFind("W"+week_back+"03") > -1 ){
      ObjectDelete("W"+week_back+"03");
   }


   if (ObjectFind("W"+week_back+"04") > -1 ){
      ObjectDelete("W"+week_back+"04");
   }

 
}

double dPoint(string sym)
{
  double _Point = MarketInfo(sym, MODE_POINT);
  if (StringFind(sym, "JPYm")>=0)
      _Point=0.01;
  
  if (_Point==0) _Point=0.0001; 
  
  return (_Point);
}


