//+------------------------------------------------------------------+
//|                                               LoadDataScotty.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, zznbrm"
#property show_inputs

#import "user32.dll"
   int PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int RegisterWindowMessageA(string lpString);
#import

extern string estrSymbol = "XYZ";
extern int    eintDigits = 2;
extern string estrInputFile = "Data.txt";

#define WM_COMMAND      0x0111
#define cDATA_TIME      0
#define cDATA_OPEN      1
#define cDATA_LOW       2
#define cDATA_HIGH      3
#define cDATA_CLOSE     4
#define cDATA_VOL       5
#define cHISTORY_HEADER_SIZE 148 
#define cHISTORY_RECORD_SIZE 44  

int gintOutput = -1;
int gintInput = -1;

//+------------------------------------------------------------------+
//| deinit                                                           |
//+------------------------------------------------------------------+
int deinit()
{
   if ( gintOutput >= 0 )     FileClose( gintOutput );   
   if ( gintInput >= 0 )      FileClose( gintInput ); 
   
   return( 0 );
}

//+------------------------------------------------------------------+
//| start                                                            |
//+------------------------------------------------------------------+
int start()
{     
   string strRow;
   
   // Open Input file 
   gintInput = FileOpen( estrInputFile, FILE_BIN|FILE_READ );
   
   if ( gintInput < 0 )    return( -1 );
   
   // Open output file
   gintOutput = hsGetHistFileHandle( estrSymbol, PERIOD_D1, false );
   
   if ( gintOutput < 0 )   return( -1 );
   
   if( !hsWriteHdrRecord( estrSymbol, gintOutput, PERIOD_D1, eintDigits ) )    return( -1 );
           
   // Read the header row and discard
   strRow = FileReadString( gintInput, 127 );
   
   while( !IsStopped() && !FileIsEnding( gintInput ) )
   {
      strRow = FileReadString( gintInput, 138 );
      
      if ( StringLen( strRow ) < 10 )   break;
      
      if ( !processData( strRow ) )    break;
   }
   
   hsUpdateChartWindow( estrSymbol, PERIOD_D1 );  
   return( 0 );
}

//+------------------------------------------------------------------+
//| processData                                                      |
//+------------------------------------------------------------------+
bool processData( string strData )
{
   double adblData[6];
   string strTemp, strTemp2;
   
   // Get the date
   strTemp = StringTrimLeft( StringSubstr( strData, 0, 17 ) );
   strTemp2 = StringSubstr( strTemp, 0, 4 ) + "." + StringSubstr( strTemp, 4, 2 ) + "." +
              StringSubstr( strTemp, 6 );
   adblData[cDATA_TIME] = StrToTime( strTemp2 );
      
   // Get the Open 
   strTemp = StringTrimLeft( StringSubstr( strData, 17, 17 ) );
   adblData[cDATA_OPEN] = StrToDouble( strTemp );
      
   // Get the High 
   strTemp = StringTrimLeft( StringSubstr( strData, 34, 17 ) );
   adblData[cDATA_HIGH] = StrToDouble( strTemp );
   
   // Get the Low 
   strTemp = StringTrimLeft( StringSubstr( strData, 51, 17 ) );
   adblData[cDATA_LOW] = StrToDouble( strTemp );
      
   // Get the Close 
   strTemp = StringTrimLeft( StringSubstr( strData, 68, 17 ) );
   adblData[cDATA_CLOSE] = StrToDouble( strTemp );
   
   // Get the Volume 
   strTemp = StringTrimLeft( StringSubstr( strData, 85, 17 ) );
   adblData[cDATA_VOL] = StrToDouble( strTemp );
      
   return( hsWriteHistRecord( adblData, gintOutput, 0 ) );
}

//+------------------------------------------------------------------+
//| hsGetHistFileHandle                                              |
//+------------------------------------------------------------------+
int hsGetHistFileHandle( string strSymbol, int intTF, bool blnAppend )
{
   string strFileName = strSymbol + intTF + ".hst";
   int intHandle, intDelimiter;
   
   if ( blnAppend )   intDelimiter = FILE_BIN | FILE_READ | FILE_WRITE;
   else               intDelimiter = FILE_BIN | FILE_WRITE;
   
   intHandle = FileOpenHistory( strFileName, intDelimiter );
   
   if ( intHandle < 0 )   
      Print( "Error opening file: " + strFileName );
   
   return( intHandle );
}

//+------------------------------------------------------------------+
//| hsWriteHdrRecord()                                               |
//+------------------------------------------------------------------+
bool hsWriteHdrRecord( string strSymbol, int intHnd, int intTF, int intDigits = 5 )
{
   int    intBytes = 0;
   int    i_unused[13];
   string c_copyright="(C)opyright 2010, MetaQuotes Software Corp.";
   
   intBytes += FileWriteInteger( intHnd, 400, LONG_VALUE );
   intBytes += FileWriteString( intHnd, c_copyright, 64 );
   intBytes += FileWriteString( intHnd, strSymbol, 12 );
   intBytes += FileWriteInteger( intHnd, intTF, LONG_VALUE );
   intBytes += FileWriteInteger( intHnd, intDigits, LONG_VALUE );
   intBytes += FileWriteInteger( intHnd, 50, LONG_VALUE );      
   intBytes += FileWriteInteger( intHnd, 0, LONG_VALUE );       
   intBytes += FileWriteArray( intHnd, i_unused, 0, 13 );
   
   return( true ); 
}

//+------------------------------------------------------------------+
//| hsWriteHistRecord                                                |
//+------------------------------------------------------------------+
bool hsWriteHistRecord( double& adblD[], int intHnd, int intBack )
{
   int intBytes = 0;
   int intPos = intBack * cHISTORY_RECORD_SIZE * (-1);
   
   if ( !FileSeek( intHnd, intPos, SEEK_END ) )
   {
      Print( "writeHistRecord() - File Seek Failed: " + GetLastError() );
      return( false );
   }
   
   if ( FileTell( intHnd ) < cHISTORY_HEADER_SIZE )
   {
      Print( "writeHistRecord() - File Pointer invalid: " + FileTell( intHnd ) );
      return( false );
   }
   
   intBytes += FileWriteInteger( intHnd, adblD[cDATA_TIME], LONG_VALUE );
   intBytes += FileWriteDouble( intHnd, adblD[cDATA_OPEN], DOUBLE_VALUE );
   intBytes += FileWriteDouble( intHnd, adblD[cDATA_LOW], DOUBLE_VALUE );
   intBytes += FileWriteDouble( intHnd, adblD[cDATA_HIGH], DOUBLE_VALUE );
   intBytes += FileWriteDouble( intHnd, adblD[cDATA_CLOSE], DOUBLE_VALUE );
   intBytes += FileWriteDouble( intHnd, adblD[cDATA_VOL], DOUBLE_VALUE );
   
   FileFlush( intHnd );
   
   if ( intBytes != cHISTORY_RECORD_SIZE )   
   {
      Print( "writeHistRecord() - Error writing to Hist File: " + GetLastError() );
      return( false );
   }
   
   return( true );
}

//+------------------------------------------------------------------+
//| hsUpdateChartWindow                                              |
//+------------------------------------------------------------------+
bool hsUpdateChartWindow( string strSymbol, int intTF ) 
{
   bool blnResult = false;   
   int intWindow = WindowHandle( strSymbol, intTF );
   static int intMT4MsgID = 0;
		
   if( intWindow > 0 ) 
   {
      // Send update (refresh) command to offline chart
      if ( PostMessageA( intWindow, WM_COMMAND, 33324, 0 ) == 0 )
         Print( strSymbol + "-" + intTF + " Chart window update failed!!!!" );
      else
         blnResult = true;
            
      // Send simulated tick to offline chart
      if ( intMT4MsgID == 0 )
         intMT4MsgID = RegisterWindowMessageA("MetaTrader4_Internal_Message");         
         
      if ( PostMessageA( intWindow, intMT4MsgID, 2, 1 ) == 0 )
      {
         Print( strSymbol + "-" + intTF + " Chart window sim tick failed!!!!" );
         intMT4MsgID = 0;
      }
   }
   
   return( blnResult );
}


