//+------------------------------------------------------------------+
//|                                      Output History ferry678.mq4 |
//+------------------------------------------------------------------+

#property show_inputs

#include <hanover --- function header.mqh>

extern int  NumberOfDays = 9999;

string   ccy, today, lastday, buy, sell;
int      dig, tmf;
double   spr, pnt, tickval;

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  ccy     = Symbol();
  tmf     = Period();
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  } 
  out_hist(ccy,tmf);

  MessageBox("History output complete","Output History script");
  return(0);
}

//+------------------------------------------------------------------+
int out_hist(string ccy, int tf)  {
//+------------------------------------------------------------------+
  lastday = "";
  int h=-99, c=0;
  for (int i=0; i<Bars; i++)  {
    today = DateToStr(Time[i],"_Y-M-D");
    if (today != lastday)  {
      if (h>0) {
        FileWrite(h,buy);
        FileWrite(h,sell);
        FileClose(h);
      }
      c++;
      if (c > NumberOfDays)   break;  
      string fname = ccy + "_" + TFToStr(tf) + today + ".tsv";
      h = FileOpen(fname,FILE_CSV|FILE_WRITE,',');
      buy  = "";
      sell = "";
      if (h<0) {
        MessageBox("Unable to open file "+fname,"Error");  
        return(0);
    } }
    lastday = today;
    int upticks   = (Volume[i]+(Close[i]-Open[i])/Point)/2;
    int downticks = Volume[i]-upticks;
    buy  = NumberToStr(upticks,"T-10'\x09'")     + buy; 
    sell = NumberToStr(-downticks,"T-10'\x09'")  + sell; 
  }  
  if (h>0) {
    FileWrite(h,buy);
    FileWrite(h,sell);
    FileClose(h);
  }  
  return(0);
}

#include <hanover --- extensible functions.mqh>