//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#property  copyright ""
#property  link      ""
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 20
#property  indicator_color1  Blue
#property  indicator_color2  Cyan
#property  indicator_color3  Green
#property  indicator_color4  Yellow
#property  indicator_color5  Orange
#property  indicator_color6  Magenta
#property  indicator_color7  White
#property  indicator_color8  Red
#property  indicator_width1  3
#property  indicator_width2  3
#property  indicator_width3  5
//---- indicator parameters
extern int FastMA=21;
extern int SlowMA=55;
extern int MA_Method=3;
extern int Price=0;
extern int SignalSMA=24;
extern int Slowing=5;
extern int BuySellSignalMA=3;
extern double BuySellSignalSize=0.025;
extern color Color_AUD = Yellow;
extern color Color_CAD = White;
extern color Color_CHF = DarkGoldenrod;
extern color Color_EUR = Cyan;
extern color Color_GBP = Red;
extern color Color_JPY = Magenta;
extern color Color_NZD = SteelBlue;
extern color Color_USD = LimeGreen;
extern color Color_Trend = Blue;
extern color Color_Velocity = Orange;
extern color Color_Acceleration = Maroon;
extern color Color_AvgTrendVelocity = Coral;
extern color Color_AvgTrendVelocityMA = Plum;
extern color Color_BuySellSignal = Red;

extern int Line_Thickness = 3;

//---- indicator buffers

double AUD;
double CAD;
double CHF;
double EUR;
double GBP;
double JPY;
double NZD;
double USD;

double FirstCurrency[];
double SecondCurrency[];
double Trend[];
double Velocity[];
double Acceleration[];
double AvgTrendVelocity[];
double AvgTrendVelocityMA[];
double BuySellSignal[];

color FirstCurrencyColor;
color SecondCurrencyColor;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
     RefreshRates();
     if(Symbol()=="AUDCAD")
     {
       FirstCurrencyColor=Color_AUD;
       SecondCurrencyColor=Color_CAD;
     } 
     else if(Symbol()=="AUDCHF")
     {
       FirstCurrencyColor=Color_AUD;
       SecondCurrencyColor=Color_CHF;
      }
     else if(Symbol()=="AUDJPY")
     {
       FirstCurrencyColor=Color_AUD;
       SecondCurrencyColor=Color_JPY;
      }
     else if(Symbol()=="AUDNZD")
     {
       FirstCurrencyColor=Color_AUD;
       SecondCurrencyColor=Color_NZD;
      }
     else if(Symbol()=="AUDUSD")
     {
       FirstCurrencyColor=Color_AUD;
       SecondCurrencyColor=Color_USD;
      }
  
    IndicatorBuffers(20); 
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE, DRAW_LINE, Line_Thickness,FirstCurrencyColor);
   SetIndexStyle(1,DRAW_LINE, DRAW_LINE, Line_Thickness,SecondCurrencyColor);
   SetIndexStyle(2,DRAW_LINE, DRAW_LINE, 1, Color_Trend);
   SetIndexStyle(3,DRAW_LINE, DRAW_LINE, 1, Color_Velocity);
   SetIndexStyle(4,DRAW_LINE, DRAW_LINE, 1, Color_Acceleration);
   SetIndexStyle(5,DRAW_LINE, DRAW_LINE, 1, Color_AvgTrendVelocity);
   SetIndexStyle(6,DRAW_LINE, STYLE_DOT, 1, Color_AvgTrendVelocityMA);
   SetIndexStyle(7,DRAW_LINE, DRAW_LINE, 1, Color_BuySellSignal);
   //SetIndexDrawBegin(0,SignalSMA);
   //SetIndexDrawBegin(1,SignalSMA);
   //SetIndexDrawBegin(2,SignalSMA);
   //SetIndexDrawBegin(3,SignalSMA);
   //SetIndexDrawBegin(4,SignalSMA);
   IndicatorDigits(Digits+1);

//---- indicator buffers
   SetIndexBuffer(0,FirstCurrency);
   SetIndexBuffer(1,SecondCurrency);
   SetIndexBuffer(2,Trend);
   SetIndexBuffer(3,Velocity);
   SetIndexBuffer(4,Acceleration);
   SetIndexBuffer(5,AvgTrendVelocity);
   SetIndexBuffer(6,AvgTrendVelocityMA);
   SetIndexBuffer(7,BuySellSignal);
   
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("CCP_1pair_TVA_1HR");
   SetIndexLabel(0,"FirstCurrency");
   SetIndexLabel(1,"SecondCurrency");
   SetIndexLabel(2,"Trend");
   SetIndexLabel(3,"Velocity");
   SetIndexLabel(4,"Acceleration");
   SetIndexLabel(5,"AvgTrendVelocity");
   SetIndexLabel(6,"AvgTrendVelocityMA");
   SetIndexLabel(7,"BuySellSignal");

//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Start
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   RefreshRates();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- 
   for(int i=0; i<limit; i++)
   {
     double EURUSD_Fast = iMA("EURUSD", 0, FastMA, 0,  MA_Method, Price, i);
     double EURUSD_Slow = iMA("EURUSD", 0, SlowMA, 0, MA_Method, Price, i);
   
     double GBPUSD_Fast = iMA("GBPUSD", 0, FastMA, 0,  MA_Method, Price, i);
     double GBPUSD_Slow = iMA("GBPUSD", 0, SlowMA, 0, MA_Method, Price, i);

     double AUDUSD_Fast = iMA("AUDUSD", 0, FastMA, 0,  MA_Method, Price, i);
     double AUDUSD_Slow = iMA("AUDUSD", 0, SlowMA, 0, MA_Method, Price, i);

     double NZDUSD_Fast = iMA("NZDUSD", 0, FastMA, 0,  MA_Method, Price, i);
     double NZDUSD_Slow = iMA("NZDUSD", 0, SlowMA, 0, MA_Method, Price, i);

     double USDCAD_Fast = iMA("USDCAD", 0, FastMA, 0,  MA_Method, Price, i);
     double USDCAD_Slow = iMA("USDCAD", 0, SlowMA, 0, MA_Method, Price, i);

     double USDCHF_Fast = iMA("USDCHF", 0, FastMA, 0,  MA_Method, Price, i);
     double USDCHF_Slow = iMA("USDCHF", 0, SlowMA, 0, MA_Method, Price, i);

     double USDJPY_Fast = iMA("USDJPY", 0, FastMA, 0,  MA_Method, Price, i);
     double USDJPY_Slow = iMA("USDJPY", 0, SlowMA, 0, MA_Method, Price, i);

     //TempOne[i]= AUDUSD_Fast;
     //TempTwo[i]= USDJPY_Slow;
     //TempThree[i]= USDJPY_Slow+AUDUSD_Fast;
     //TempFour[i]= 2*(TempOne[i]+TempTwo[i]);

     AUD = 0;
     AUD += AUDUSD_Fast/AUDUSD_Slow - 1
     + (EURUSD_Slow/AUDUSD_Slow)/(EURUSD_Fast/AUDUSD_Fast) - 1
     + (GBPUSD_Slow/AUDUSD_Slow)/(GBPUSD_Fast/AUDUSD_Fast) - 1
     + (AUDUSD_Fast/NZDUSD_Fast)/(AUDUSD_Slow/NZDUSD_Slow) - 1
     + (AUDUSD_Fast*USDCHF_Fast)/(AUDUSD_Slow*USDCHF_Slow) - 1
     + (AUDUSD_Fast*USDCAD_Fast)/(AUDUSD_Slow*USDCAD_Slow) - 1
     + (AUDUSD_Fast*USDJPY_Fast)/(AUDUSD_Slow*USDJPY_Slow) - 1;

     CAD = 0;
     CAD += USDCAD_Slow/USDCAD_Fast - 1
     + (EURUSD_Slow*USDCAD_Slow) / 
       (EURUSD_Fast*USDCAD_Fast) - 1
     + (GBPUSD_Slow*USDCAD_Slow) / 
       (GBPUSD_Fast*USDCAD_Fast) - 1
     + (AUDUSD_Slow*USDCAD_Slow) / 
       (AUDUSD_Fast*USDCAD_Fast) - 1
     + (NZDUSD_Slow*USDCAD_Slow) / 
       (NZDUSD_Fast*USDCAD_Fast) - 1
     + (USDCHF_Fast/USDCAD_Fast) / 
       (USDCHF_Slow/USDCAD_Slow) - 1
     + (USDJPY_Fast/USDCAD_Fast) / 
       (USDJPY_Slow/USDCAD_Slow) - 1;

     CHF = 0;
     CHF += USDCHF_Slow/USDCHF_Fast - 1
     + (EURUSD_Slow*USDCHF_Slow) / 
       (EURUSD_Fast*USDCHF_Fast) - 1
     + (GBPUSD_Slow*USDCHF_Slow) / 
       (GBPUSD_Fast*USDCHF_Fast) - 1
     + (AUDUSD_Slow*USDCHF_Slow) / 
       (AUDUSD_Fast*USDCHF_Fast) - 1
     + (NZDUSD_Slow*USDCHF_Slow) / 
       (NZDUSD_Fast*USDCHF_Fast) - 1
     + (USDCHF_Slow/USDCAD_Slow) / 
       (USDCHF_Fast/USDCAD_Fast) - 1
     + (USDJPY_Fast/USDCHF_Fast) / 
       (USDJPY_Slow/USDCHF_Slow) - 1; 

     EUR = 0;
     EUR += EURUSD_Fast/EURUSD_Slow - 1
     + (EURUSD_Fast/GBPUSD_Fast) / 
       (EURUSD_Slow/GBPUSD_Slow) - 1
     + (EURUSD_Fast/AUDUSD_Fast) / 
       (EURUSD_Slow/AUDUSD_Slow) - 1
     + (EURUSD_Fast/NZDUSD_Fast) / 
       (EURUSD_Slow/NZDUSD_Slow) - 1
     + (EURUSD_Fast*USDCHF_Fast) / 
       (EURUSD_Slow*USDCHF_Slow) - 1
     + (EURUSD_Fast*USDCAD_Fast) / 
       (EURUSD_Slow*USDCAD_Slow) - 1
     + (EURUSD_Fast*USDJPY_Fast) / 
       (EURUSD_Slow*USDJPY_Slow) - 1;

     GBP = 0;
     GBP += GBPUSD_Fast/GBPUSD_Slow - 1
      + (EURUSD_Slow/GBPUSD_Slow) / 
        (EURUSD_Fast/GBPUSD_Fast) - 1
      + (GBPUSD_Fast/AUDUSD_Fast) / 
        (GBPUSD_Slow/AUDUSD_Slow) - 1
      + (GBPUSD_Fast/NZDUSD_Fast) / 
        (GBPUSD_Slow/NZDUSD_Slow) - 1
      + (GBPUSD_Fast*USDCHF_Fast) / 
        (GBPUSD_Slow*USDCHF_Slow) - 1
      + (GBPUSD_Fast*USDCAD_Fast) / 
        (GBPUSD_Slow*USDCAD_Slow) - 1
      + (GBPUSD_Fast*USDJPY_Fast) / 
        (GBPUSD_Slow*USDJPY_Slow) - 1;   

     JPY = 0;
     JPY += USDJPY_Slow/USDJPY_Fast - 1
     + (EURUSD_Slow*USDJPY_Slow) / 
       (EURUSD_Fast*USDJPY_Fast) - 1
     + (GBPUSD_Slow*USDJPY_Slow) / 
       (GBPUSD_Fast*USDJPY_Fast) - 1 
     + (AUDUSD_Slow*USDJPY_Slow) / 
       (AUDUSD_Fast*USDJPY_Fast) - 1
     + (NZDUSD_Slow*USDJPY_Slow) / 
       (NZDUSD_Fast*USDJPY_Fast) - 1
     + (USDJPY_Slow/USDCAD_Slow) / 
       (USDJPY_Fast/USDCAD_Fast) - 1 
     + (USDJPY_Slow/USDCHF_Slow) / 
       (USDJPY_Fast/USDCHF_Fast) - 1;

     NZD = 0;
     NZD += NZDUSD_Fast/NZDUSD_Slow - 1
     + (EURUSD_Slow/NZDUSD_Slow) / 
       (EURUSD_Fast/NZDUSD_Fast) - 1
     + (GBPUSD_Slow/NZDUSD_Slow) / 
       (GBPUSD_Fast/NZDUSD_Fast) - 1
     + (AUDUSD_Slow/NZDUSD_Slow) / 
       (AUDUSD_Fast/NZDUSD_Fast) - 1
     + (NZDUSD_Fast*USDCHF_Fast) / 
       (NZDUSD_Slow*USDCHF_Slow) - 1 
     + (NZDUSD_Fast*USDCAD_Fast) / 
       (NZDUSD_Slow*USDCAD_Slow) - 1
     + (NZDUSD_Fast*USDJPY_Fast) / 
       (NZDUSD_Slow*USDJPY_Slow) - 1;

     USD = 0;
     USD += (EURUSD_Slow/EURUSD_Fast)-1
     + (GBPUSD_Slow/GBPUSD_Fast)-1
     + (AUDUSD_Slow/AUDUSD_Fast)-1
     + (NZDUSD_Slow/NZDUSD_Fast)-1
     + (USDCHF_Fast/USDCHF_Slow)-1
     + (USDCAD_Fast/USDCAD_Slow)-1
     + (USDJPY_Fast/USDJPY_Slow)-1;
   
     if(Symbol()=="AUDCAD")
     {
       FirstCurrency[i]=AUD;
       SecondCurrency[i]=CAD;
     } 
     else if(Symbol()=="AUDCHF")
     {
       FirstCurrency[i]=AUD;
       SecondCurrency[i]=CHF;
      }
     else if(Symbol()=="AUDJPY")
     {
       FirstCurrency[i]=AUD;
       SecondCurrency[i]=JPY;
      }
     else if(Symbol()=="AUDNZD")
     {
       FirstCurrency[i]=AUD;
       SecondCurrency[i]=NZD;
      }
     else if(Symbol()=="AUDUSD")
     {
       FirstCurrency[i]=AUD;
       SecondCurrency[i]=USD;
      }
     else if(Symbol()=="CADCHF")
     {
       FirstCurrency[i]=CAD;
       SecondCurrency[i]=CHF;
      }
     else if(Symbol()=="CADJPY")
     {
       FirstCurrency[i]=CAD;
       SecondCurrency[i]=JPY;
      }
     else if(Symbol()=="EURAUD")
     {
       FirstCurrency[i]=EUR;
       SecondCurrency[i]=AUD;
      }
     else if(Symbol()=="EURCAD")
     {
       FirstCurrency[i]=EUR;
       SecondCurrency[i]=CAD;
      }
     else if(Symbol()=="EURCHF")
     {
       FirstCurrency[i]=EUR;
       SecondCurrency[i]=CHF;
      }
     else if(Symbol()=="EURGBP")
     {
       FirstCurrency[i]=EUR;
       SecondCurrency[i]=GBP;
      }
     else if(Symbol()=="EURJPY")
     {
       FirstCurrency[i]=EUR;
       SecondCurrency[i]=JPY;
      }
     else if(Symbol()=="EURNZD")
     {
       FirstCurrency[i]=EUR;
       SecondCurrency[i]=NZD;
      }
     else if(Symbol()=="EURUSD")
     {
       FirstCurrency[i]=EUR;
       SecondCurrency[i]=USD;
      }
     else if(Symbol()=="GBPAUD")
     {
       FirstCurrency[i]=GBP;
       SecondCurrency[i]=AUD;
      }
     else if(Symbol()=="GBPCAD")
     {
       FirstCurrency[i]=GBP;
       SecondCurrency[i]=CAD;
      }
     else if(Symbol()=="GBPCHF")
     {
       FirstCurrency[i]=GBP;
       SecondCurrency[i]=CHF;
      }
     else if(Symbol()=="GBPJPY")
     {
       FirstCurrency[i]=GBP;
       SecondCurrency[i]=JPY;
      }
     else if(Symbol()=="GBPNZD")
     {
       FirstCurrency[i]=GBP;
       SecondCurrency[i]=NZD;
      }
     else if(Symbol()=="GBPUSD")
     {
       FirstCurrency[i]=GBP;
       SecondCurrency[i]=USD;
      }
     else if(Symbol()=="NZDJPY")
     {
       FirstCurrency[i]=NZD;
       SecondCurrency[i]=JPY;
      }
     else if(Symbol()=="NZDUSD")
     {
       FirstCurrency[i]=NZD;
       SecondCurrency[i]=USD;
      }
     else if(Symbol()=="USDCAD")
     {
       FirstCurrency[i]=USD;
       SecondCurrency[i]=CAD;
      }
     else if(Symbol()=="USDCHF")
     {
       FirstCurrency[i]=USD;
       SecondCurrency[i]=CHF;
      }
     else if(Symbol()=="USDJPY")
     {
       FirstCurrency[i]=USD;
       SecondCurrency[i]=JPY;
      }

      Trend[i]=FirstCurrency[i]-SecondCurrency[i];
      Velocity[i]=(Trend[i]-Trend[i+Slowing])*Slowing;
      Acceleration[i]=(Velocity[i]-Velocity[i+Slowing]);
      AvgTrendVelocity[i]=(Trend[i]+Velocity[i])/2;

     //TODO: fix this
     if (false)
     {
		   double temp=0;
		   for(int j=0; j<BuySellSignalMA; j++)
		   {
		     temp=temp+AvgTrendVelocity[j];
		   }
		   AvgTrendVelocityMA[i]=temp/j;
     }

	   AvgTrendVelocityMA[i]=(AvgTrendVelocity[i]+AvgTrendVelocity[i+1]+AvgTrendVelocity[i+2])/3;

     BuySellSignal[i]=0;
     if(AvgTrendVelocity[i]>AvgTrendVelocityMA[i])
     {
       BuySellSignal[i]=BuySellSignalSize;
     }
     if(AvgTrendVelocity[i]<AvgTrendVelocityMA[i])
     {
       BuySellSignal[i]=-1*BuySellSignalSize;
     }

   }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+