//+------------------------------------------------------------------+
//|                                                     Basket14.mq4 |
//|                                       Copyright © 2011, PatPatel |
//|                                       forexfactory.pat1@gmail.com|
//+------------------------------------------------------------------+

// Modified by GVC....

#property copyright "Copyright © 2011, PatPatel"
#property link      "forexfactory.pat1@gmail.com"
#include <WinUser32.mqh>
#include <stdlib.mqh>



#property indicator_chart_window

//-------------------------------------------------------------------
extern int NumOfPairs = 3;
extern int MaxBars = 1000;
extern int TF = 60;
extern string BasketName = "TEST";
string Pair_suffix;
string shortname;

string Pair[14];
double Factor,multiplier[14];
int j,StartBar;
string Currency;
static int ExtHandle=-1;
static int tf = 0;
int MN=0;
int MT4InternalMsg;


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#import "user32.dll"
   int RegisterWindowMessageW(string lpString);
   int PostMessageW(int hWnd,int Msg,int wParam,int lParam);
#import

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()


{
   
   Pair_suffix=StringTrimLeft(StringTrimRight(StringSubstr(Symbol(),6,StringLen(Symbol())-6)));
   
   Get_Trade_Pairs();
   Currency= BasketName; 
   GlobalVariableSet(Currency+"_"+TF+"_Fact",Factor);
   
   for(int kj=0;kj<NumOfPairs;kj++)
   {
   if(StringSubstr(Pair[kj],3,3)=="JPY") multiplier[kj]=1.0; else multiplier[kj]=100.0;
   }
   IndicatorShortName(Currency);


   if (TF==0) tf = Period();
   else tf = TF;
   
//Create initial file
   int    version=401;
   string c_copyright;
   string c_symbol=Currency;
   int    i_period=tf;
   int    i_digits=3;
   int    i_unused[13];


//DNK - indentify
   i_unused[0]=3; i_unused[1]=3; i_unused[2]=3;

//----  
		ExtHandle = FileOpenHistory(c_symbol+i_period+".hst", FILE_BIN|FILE_WRITE|FILE_ANSI);
		FileClose(ExtHandle);
		ExtHandle = FileOpenHistory(c_symbol+i_period+".hst", FILE_BIN|FILE_READ|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_ANSI);

//   ExtHandle=FileOpenHistory(c_symbol+i_period+".hst", FILE_BIN|FILE_WRITE|FILE_ANSI|FILE_SHARE_WRITE);
   if(ExtHandle < 0) return(-1);
//---- write history file header
   c_copyright="Copyright © 2011, PatPatel";
   FileWriteInteger(ExtHandle, version, LONG_VALUE);
   FileWriteString(ExtHandle, c_copyright, 64);
   FileWriteString(ExtHandle, c_symbol, 12);
   FileWriteInteger(ExtHandle, i_period, LONG_VALUE);
   FileWriteInteger(ExtHandle, i_digits, LONG_VALUE);
   FileWriteInteger(ExtHandle, 0, LONG_VALUE);       //timesign
   FileWriteInteger(ExtHandle, 0, LONG_VALUE);       //last_sync
   FileWriteArray(ExtHandle, i_unused, 0, 13);
   FileFlush(ExtHandle);
   if(ObjectFind(Currency+shortname+tf)==0)ObjectDelete(Currency+shortname+tf);
   
   return(0);
  }
  
//---- 
int deinit() {
   FileFlush(ExtHandle);
   FileClose(ExtHandle);
   ObjectDelete (Currency+shortname+tf);
   return(0);
}

//####################################################################
//+------------------------------------------------------------------+
//| Main program
//+------------------------------------------------------------------+
static datetime lastbar = 0;
static int last_fpos = 0;
static datetime last_time=0;

int start()

{

   
   if (ExtHandle==-1) Print ("Error");
   
   int counted_bars=IndicatorCounted();
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   
   StartBar = MaxBars;
   
   if (counted_bars>0) StartBar=Bars-counted_bars;

   //---- main loop
   for(int i=StartBar; i>=0; i--)
   {  
      if (lastbar < iTime(Symbol(),tf,i)) 
      {
         last_fpos=FileTell(ExtHandle);
         lastbar=iTime(Symbol(),tf,i);
      }
      else
      {
         FileSeek(ExtHandle,last_fpos,SEEK_SET);  
      }
      

      datetime timebar=iTime(Symbol(),tf,i);
      
      // Change kk to -1300 if HLOC values are less than 0. 
      double kk=-Factor;
      double sum_A_Pair_C=kk;
      double sum_A_Pair_H=kk;
      double sum_A_Pair_L=kk;
      double sum_A_Pair_O=kk;
      double sum_A_Pair_V=0.0;
      
      for (j=0;j<NumOfPairs;j++)
      {
        int shift=iBarShift(Pair[j],tf,timebar);
         if (iTime(Pair[j],tf,i)>iTime(Pair[j],tf,shift)) {
            shift++;         
         } 
         sum_A_Pair_C=sum_A_Pair_C  +iClose(Pair[j],tf,shift)*multiplier[j];
         sum_A_Pair_H=sum_A_Pair_H  + iHigh(Pair[j],tf,shift)*multiplier[j];
         sum_A_Pair_L=sum_A_Pair_L  +  iLow(Pair[j],tf,shift)*multiplier[j];
         sum_A_Pair_O=sum_A_Pair_O  + iOpen(Pair[j],tf,shift)*multiplier[j];
         sum_A_Pair_V=sum_A_Pair_V  +iVolume(Pair[j],tf,shift);
      }
      
      //Update file with current values
   
      MqlRates rates;
         rates.time  = lastbar;
         rates.open  = sum_A_Pair_O;
         rates.close = sum_A_Pair_C;
         rates.high  = sum_A_Pair_H;
         rates.low   = sum_A_Pair_L;
         rates.tick_volume = sum_A_Pair_V;
         rates.real_volume = sum_A_Pair_V;
         FileWriteStruct(ExtHandle,rates);
      //FileWriteInteger(ExtHandle, lastbar, LONG_VALUE);
      //FileWriteDouble(ExtHandle, sum_A_Pair_O, DOUBLE_VALUE);
      //FileWriteDouble(ExtHandle, sum_A_Pair_L, DOUBLE_VALUE);
      //FileWriteDouble(ExtHandle, sum_A_Pair_H, DOUBLE_VALUE);
      //FileWriteDouble(ExtHandle, sum_A_Pair_C, DOUBLE_VALUE);
      //FileWriteDouble(ExtHandle, sum_A_Pair_V, DOUBLE_VALUE);
      //FileFlush(ExtHandle); 
      }
      FileFlush(ExtHandle); 
      
      // Update Basket Chart if Available
      
      static int hwnd = 0; if (hwnd == 0)  hwnd = WindowHandle(Currency, tf);

      if(MT4InternalMsg == 0) 
        MT4InternalMsg = RegisterWindowMessageW("MetaTrader4_Internal_Message");

      if(hwnd != 0)                        PostMessageW(hwnd, WM_COMMAND, 0x822c, 0);
      if(hwnd != 0 && MT4InternalMsg != 0) PostMessageW(hwnd, MT4InternalMsg, 2, 1);
      
      
   return(0);
}
//+------------------------------------------------------------------+


int Get_Trade_Pairs()
  {
   switch (NumOfPairs)
      {
      case 3:  Pair[0] = "EURUSD" + Pair_suffix;
               Pair[1] = "AUDUSD" + Pair_suffix;
               Pair[2] = "USDCHF" + Pair_suffix;
               Factor=300.0;
               break;     
       default: break;
       }
       Currency=Currency+Pair_suffix;
//----
   return(0);
  }
  
  string PadValINT(int Val,  int PadSpc)
{
  string S = Val;
  
  while ( StringLen(S) < PadSpc ) S = "0" + S;
  
  return(S); 
}