#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Tomato

extern bool UseIBFXPairs = True;
extern string Pair_suffix = "";
extern double Lots = 0.1;
extern bool UsePipCurrencyValue = false;
extern int AddWeeksBack = 0; 
extern int MA1_Period = 5;
extern string MA_Type = "0=SMA,1=EMA,2=SMMA,3=LWMA";
extern int MA1_Type = 1;
extern int MA2_Period = 10;
extern int MA2_Type = 1;

string shortname;

double ExtMapBuffer_max_anchors[];

string Pair[14];
int length=14;
double Pair_start[14];
datetime Pair_starttime[14];
int Pair_spread[14];
double Pair_end[14];
double A_Pair[14]; 
double Pair_PipValuePerLot[14];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("T101 BuyAll_pairs_Anchor_spread indi");
//---- indicators
   //IndicatorShortName(shortname);
   
   SetIndexStyle(0,DRAW_LINE,EMPTY,EMPTY);
   SetIndexLabel (0, "Anchor_Distance");
   SetIndexBuffer(0,ExtMapBuffer_max_anchors);

//   SetIndexStyle(4,DRAW_LINE,EMPTY,EMPTY);
//   SetIndexLabel (4, "Anchor_Sell");
//   SetIndexBuffer(4,ExtMapBuffer_max_sells);

   
   if(UseIBFXPairs == False) { // The usual suspects.. NOTE: PLACEMENT OF JPY PAIRS ARE CRITICAL, BECAUSE OF REST OF CODE BELOW
      Pair[0] = "CADJPY"+Pair_suffix; 
      Pair[1] = "AUDUSD"+Pair_suffix; 
      Pair[2] = "USDJPY"+Pair_suffix; 
      Pair[3] = "EURUSD"+Pair_suffix; 
      Pair[4] = "EURCHF"+Pair_suffix;
      Pair[5] = "GBPJPY"+Pair_suffix; 
      Pair[6]= "USDCAD"+Pair_suffix; 
      Pair[7] = "GBPUSD"+Pair_suffix; 
      Pair[8] = "EURGBP"+Pair_suffix; 
      Pair[9] = "GBPCHF"+Pair_suffix;
      Pair[10] = "CHFJPY"+Pair_suffix; 
      Pair[11] = "AUDJPY"+Pair_suffix; 
      Pair[12] = "EURJPY"+Pair_suffix; 
      Pair[13] = "USDCHF"+Pair_suffix;
      
   } else if (UseIBFXPairs == True) { //But these are the IBFX pairs, because IBFX does not have CADJPY
      Pair[0] = "GBPUSD"+Pair_suffix; 
      Pair[1] = "EURGBP"+Pair_suffix; 
      Pair[2] = "GBPJPY"+Pair_suffix; 
      Pair[3] = "USDCHF"+Pair_suffix; 
      Pair[4] = "NZDUSD"+Pair_suffix;
      Pair[5] = "AUDJPY"+Pair_suffix; 
      Pair[6] = "EURJPY"+Pair_suffix; 
      Pair[7] = "EURUSD"+Pair_suffix; 
      Pair[8] = "USDJPY"+Pair_suffix; 
      Pair[9] = "AUDUSD"+Pair_suffix;
      Pair[10] = "NZDJPY"+Pair_suffix; 
      Pair[11] = "GBPCHF"+Pair_suffix; 
      Pair[12] = "CHFJPY"+Pair_suffix; 
      Pair[13] = "EURCHF"+Pair_suffix;
   }   
   return(0);
  }
//---- 
int deinit() {

   return(0);
}
//+------------------------------------------------------------------+
//| Accumulation/Distribution                                        |
//+------------------------------------------------------------------+
int start()
  {
     int counted_bars=IndicatorCounted();
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
     if(counted_bars>0) counted_bars--;
   
   datetime WeekOpenTime =  iTime(NULL,PERIOD_W1,AddWeeksBack);
   int StartBar = iBarShift(NULL,0,WeekOpenTime)-1;
   
//---- main loop
   for(int i=StartBar; i>=0; i--) {
   
      for (int j=0;j<length;j++) {
         Pair_start[j] = iOpen(Pair[j],0,iBarShift(Pair[j],0,WeekOpenTime)-1);
         Pair_starttime[j] = iTime(Pair[j],0,iBarShift(Pair[j],0,WeekOpenTime)-1);
         Pair_end[j] = iClose(Pair[j],0,i);
              
         if(Point == 0.00001 || Point == 0.001) {   
            Pair_spread[j] = MarketInfo(Pair[j],MODE_SPREAD)/10;
         } else if(Point == 0.0001 || Point == 0.01) {
            Pair_spread[j] = MarketInfo(Pair[j],MODE_SPREAD);
         }                
      }    
      
      //For Buy pairs, subtract starting price (artificial Ask from start of week) from ending price (Bid now), then divide by 100 (if JPY pair), then multiply by 10000, to get pips gained or lost as whole numbers.
      //For Sell pairs, subtract ending price (Ask now) from starting price (Bid from start of week), then divide by 100 (if JPY pair), then multiply by 10000, to get pips gained or lost as whole numbers.
      for (j=0;j<length;j++) {
         if( StringFind(Pair[j],"JPY",0)!=-1) {
            A_Pair[j] = (((Pair_end[j]-(Pair_start[j]+(Pair_spread[j]*0.01)))/100)*10000);
         } else {
            A_Pair[j] = ((Pair_end[j]-(Pair_start[j]+(Pair_spread[j]*0.0001)))*10000);
         }      
      }
   
      //Give currency value to the pips if the user chooses to do so (recommended)
      
      if(UsePipCurrencyValue == True) {
         
         for (j=0; j<14;j++) {
            if(Point == 0.00001 || Point == 0.001) {   
               Pair_PipValuePerLot[j] = MarketInfo(Pair[j], MODE_TICKVALUE)/10;
            } else if(Point == 0.0001 || Point == 0.01) {
               Pair_PipValuePerLot[j] = MarketInfo(Pair[j], MODE_TICKVALUE);
            }
         }
        
         for (j=0;j<length;j++) {
            A_Pair[j] = A_Pair[j]*Pair_PipValuePerLot[j]*Lots;
         }
      }
      
      double sum_A_Pair=0.0;
      double min_anchor = 100000000;
      double max_anchor = -1;

      for (j=0;j<length;j++) {
         sum_A_Pair=sum_A_Pair+A_Pair[j];
         if (A_Pair[j]>max_anchor) max_anchor = A_Pair[j];
         if (A_Pair[j]<min_anchor) min_anchor = A_Pair[j];
      }   

      ExtMapBuffer_max_anchors[i]= MathAbs(min_anchor)+MathAbs(max_anchor); 
        
   }
   
   return(0);
  }
//+------------------------------------------------------------------+