//+------------------------------------------------------------------+
//|                                             Sentinel_Revised.mq4 |
//|                                      Copyright © 2008, PZig Inc. |
//+------------------------------------------------------------------+
//Coded by Kevin "Savage Pip Fiend" Eaton





//EMail -- leno_2k2@hotmail.com
//.ALWAYS for hire.





#property copyright "Copyright © 2008, PZig Inc."

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Yellow
#property indicator_color3 Red
#property indicator_color4 White

extern double	EUR = 1.0,	// weights
					JPY = 1.0,
					GBP = 1.0,
					CHF = 1.0,
					CAD = 1.0,
					AUD = 1.0;
extern int MA_Value = 100;//<-- Moving average value set to 336 by default
extern int MA_Shift = 0;//<-- Shift for the MA. Set to 0 by default.
extern int MA_Method = 0;//<-- MA Method (0 is SMA, 1 is EMA, 2 is SMMA, 3 is LWMA). Anything above/below these values is set to SMA (0) automatically.
extern bool Use_2nd_MA = False;
extern int MA2_Value = 0;//<-- 2nd Moving average value
extern int MA2_Shift = 0;//<-- Shift for the MA2. Set to 0 by default.
extern int MA2_Method = 0;//<-- MA2 Method (0 is SMA, 1 is EMA, 2 is SMMA, 3 is LWMA). Anything above/below these values is set to SMA (0) automatically.
extern bool Use_3rd_MA = False;
extern int MA3_Value = 0;//<-- 3rd Moving average value
extern int MA3_Shift = 0;//<-- Shift for the MA3. Set to 0 by default.
extern int MA3_Method = 0;//<-- MA3 Method (0 is SMA, 1 is EMA, 2 is SMMA, 3 is LWMA). Anything above/below these values is set to SMA (0) automatically.

string pairs[6] = { "EURUSD","USDJPY","GBPUSD","USDCHF","USDCAD","AUDUSD","NZDUSD" };
double weights[6];

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];

int totalCount = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator
   int i;
   
   ArrayInitialize(weights, 0.0);
   
   //Fill the array weight values to match the pair values.
   weights[0] = EUR;
   weights[1] = JPY;
   weights[2] = GBP;
   weights[3] = CHF;
   weights[4] = CAD;
   weights[5] = AUD;

   //Check that we have all our pairs..
   for(i = 0; i < 6; i++)
   {
    if(!checkPair(pairs[i]))
    {
      Print("Not able to load pair "+pairs[i]+"... switching to mini mode.");
      if(!checkPair(pairs[i]+"m"))
      {
        Alert("Warning -- Not able to load "+pairs[i]+" in mini or normal mode. Results may be off.");
        weights[i] = 0;
        
      } else {
        pairs[i] = pairs[i] + "m";
        if(iBars(pairs[i],0) < totalCount || totalCount == 0)
          totalCount = iBars(pairs[i],0);
      }
    } else {
       if(iBars(pairs[i],0) < totalCount || totalCount == 0)
          totalCount = iBars(pairs[i],0);
    }
    Print("Pair ("+pairs[i]+") bars = "+iBars(pairs[i],0));
  }

  Print("Number of bars to use -- ",totalCount);
    
  IndicatorBuffers(2);

  SetIndexBuffer(0,ExtMapBuffer1);
  SetIndexLabel(0,"Sentinel");
  SetIndexStyle(0,DRAW_LINE);
  
  SetIndexBuffer(1,ExtMapBuffer2);
  SetIndexLabel(1,"Sentinel MA");
  SetIndexStyle(1,DRAW_LINE);
  
  SetIndexBuffer(2,ExtMapBuffer3);
  SetIndexLabel(2,"Sentinel MA2");
  SetIndexStyle(2,DRAW_LINE);
  
  SetIndexBuffer(3,ExtMapBuffer4);
  SetIndexLabel(3,"Sentinel MA3");
  SetIndexStyle(3,DRAW_LINE);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   double sentCalc;
   int j = 0;
   
   if(totalCount <= 0)
     return(0);
   
   int limit = Bars - counted_bars;
   if(limit > totalCount) limit = totalCount;
   if(limit > 0) limit--;
   if(limit < 0) return(-1);  
   
   for(int i = limit; i >= 0; i--)
   {
     sentCalc = 0;

     for(j = 0; j < 6; j++)
     {
       if(weights[j] > 0)
       {
          if(StringFind(pairs[j],"USDJPY",0) == 0)
          sentCalc += ((iClose(pairs[j],0,iBarShift(pairs[j],0,Time[i])) * weights[j]) /100) * 10000;
          
          if(StringFind(pairs[j],"USDCHF",0) == 0)
          sentCalc += (iClose(pairs[j],0,iBarShift(pairs[j],0,Time[i])) * weights[j]) * 10000;
          
          if(StringFind(pairs[j],"USDCAD",0) == 0)
          sentCalc += (iClose(pairs[j],0,iBarShift(pairs[j],0,Time[i])) * weights[j]) * 10000;
          
          if(StringFind(pairs[j],"EURUSD",0) == 0)
          sentCalc -= (iClose(pairs[j],0,iBarShift(pairs[j],0,Time[i])) * weights[j]) * 10000;
          
          if(StringFind(pairs[j],"GBPUSD",0) == 0)
          sentCalc -= (iClose(pairs[j],0,iBarShift(pairs[j],0,Time[i])) * weights[j]) * 10000;
          
          if(StringFind(pairs[j],"AUDUSD",0) == 0)
          sentCalc -= (iClose(pairs[j],0,iBarShift(pairs[j],0,Time[i])) * weights[j]) * 10000;
       }  
     }
     
     //sentCalc now equals our Sent number for this bar.
     ExtMapBuffer1[i] = sentCalc;
   }
   
   if(limit > MA_Value) limit -= MA_Value;
   //Do the Moving Average
   for(i = limit; i >= 0; i--)
   {
     ExtMapBuffer2[i] = iMAOnArray(ExtMapBuffer1,0,MA_Value,MA_Shift,MA_Method,i);
   }
   
   if (Use_2nd_MA == True) {
   if(limit > MA2_Value) limit -= MA2_Value;
   //Do the Moving Average
   for(i = limit; i >= 0; i--)
   {
     ExtMapBuffer3[i] = iMAOnArray(ExtMapBuffer1,0,MA2_Value,MA2_Shift,MA2_Method,i);
   }
   }
   
   if (Use_3rd_MA == True) {
   if(limit > MA3_Value) limit -= MA3_Value;
   //Do the Moving Average
   for(i = limit; i >= 0; i--)
   {
     ExtMapBuffer4[i] = iMAOnArray(ExtMapBuffer1,0,MA3_Value,MA3_Shift,MA3_Method,i);
   }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

bool checkPair(string pairToCheck)
{
    
  if(iClose(pairToCheck,0,0) <= 0.0)
   return(false);
 else
   return(true);

}