cdb M1 to M5,H1,H4

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <sstream>

#include <algorithm>

#include <stdexcept>

#include <cstdio>

#include <ctime>

#include "cdbxx/db.hh"

#include "cdbxx/map.hh"

#include "mt4.hh"

using namespace std;

string  _Symbol_;

int     _Period_;

string  _db_;

string  _history_;

string  _hstfiles_;

string  _csvfiles_;

string  _marketinfo_;

string Sym[NR_OF_SYMBOLS];

int    Per[NR_OF_PERIODS];

int main()

{

  int Period=PERIOD_M1;

  int Period1=PERIOD_M5;

  init_mt4();

  for(int i=0;i<20;i++)

    cdb_to_cdb(Sym[i],Period,Period1);

  Period1=PERIOD_H1;

  for(int i=0;i<20;i++)

    cdb_to_cdb(Sym[i],Period,Period1);

  Period1=PERIOD_H4;

  for(int i=0;i<20;i++)

    cdb_to_cdb(Sym[i],Period,Period1);

    return 0;

}

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void cdb_to_cdb(std::string symbol, int Period, int Period1)

{

   static double d_open, d_low, d_high, d_close, d_volume;

   static int i_time;

   //static int last_fpos, csv_fpos;

   int ps;

   int cnt = 0;

   int LastBarTime;

   int      NewPeriod = 0;

   int PeriodMultiplier=(int)(Period1/Period);

   if (PeriodMultiplier <= 1)

   {

      PeriodMultiplier = 1;

   }

   //std::stringstream ss1;

   //ss1 << Period;

   //std::string min1 = ss1.str();

   //std::stringstream ss2;

   //ss2 << PeriodMultiplier;

   //std::string min2 = ss2.str();

   std::string db_name1=symbol_filename(symbol, Period);//"/home/barnix/mt4db/cdb/"+symbol+min1+".cdb";

   std::string db_name2=symbol_filename(symbol, Period1);//"/home/barnix/mt4db/cdb/"+symbol+min2+".cdb";

   //std::cout << db_name1 << std::endl;

   //std::cout << db_name2 << std::endl;

   cdbxx::in_db open_database(db_name1);

   cdbxx::out_db new_database(db_name2);

   NewPeriod = Period * PeriodMultiplier;

   ps = NewPeriod * 60;

  for( cdbxx::iterator it1 = open_database.begin();it1 != open_database.end();it1++)

  {

      cdbxx::vector<int> key(it1->first);

      cdbxx::vector<float> content(it1->second);

      if(it1 == open_database.begin())

      {

        //0         ,0     ,1     ,2     ,3     ,4

        //TIME      ,OPEN  ,LOW   ,HIGH  ,CLOSE ,VOLUME

        //---- normalize open time

        i_time=key.get()[0]/ps;

        i_time*=ps;

        d_open=content.get()[0];

        d_low=content.get()[1];

        d_high=content.get()[2];

        d_close=content.get()[3];

        d_volume=content.get()[4];

       }

       LastBarTime = key.get()[0];

             //a new bar

      if (LastBarTime >=  i_time+ps)

      {

         //write the bar data

         std::vector<int> key1(1);

         std::vector<float> value1(5);

         //0         ,0     ,1     ,2     ,3     ,4

         //TIME      ,OPEN  ,LOW   ,HIGH  ,CLOSE ,VOLUME

            key1[0] = i_time;

            value1[0] = d_open;

            value1[1] = d_low;

            value1[2] = d_high;

            value1[3] = d_close;

            value1[4] = d_volume;

         new_database.insert(key1, value1);

         cnt++;

         //0         ,0     ,1     ,2     ,3     ,4

         //TIME      ,OPEN  ,LOW   ,HIGH  ,CLOSE ,VOLUME

         //---- normalize open time

         i_time=LastBarTime/ps;

         i_time*=ps;

         d_open=content.get()[0];

         d_low=content.get()[1];

         d_high=content.get()[2];

         d_close=content.get()[3];

         d_volume=content.get()[4];

      }

      else

      {

         //no new bar

         d_volume +=  content.get()[4];

         if (content.get()[1]<d_low) d_low = content.get()[1];

         if (content.get()[2]>d_high) d_high = content.get()[2];

         d_close = content.get()[3];

      }

  }

 std::cout << symbol << Period1 << "-" <<  cnt <<" record(s) written" << std::endl;

}

===========================================================================

//+------------------------------------------------------------------+

// Period_converter_auto

// automatically converts M1-data to M5/M15/M30/H1/H4/D1 in a single run

//

// to be used for importing M1-market data into Metatrader

//+------------------------------------------------------------------+

#property copyright "ps"

#property link      "http://www.tradingscape.com"

#property show_inputs

#include <WinUser32.mqh>


int        ExtHandle=-1;

//+------------------------------------------------------------------+

//| script program start function                                    |

//+------------------------------------------------------------------+

int start() {

  writeHistory(PERIOD_M5);

  writeHistory(PERIOD_M15);

  writeHistory(PERIOD_M30);

  writeHistory(PERIOD_H1);

  writeHistory(PERIOD_H4);

  writeHistory(PERIOD_D1);

  return(0);

}


int writeHistory(int histPeriod)

 {

  int    i, start_pos, i_time, time0, last_fpos, periodseconds;

  double d_open, d_low, d_high, d_close, d_volume, last_volume;

  int    hwnd=0,cnt=0;

//---- History header

 

  int    version=400;

  string c_copyright;

  string c_symbol=Symbol();

  int    i_period=Period()*histPeriod;

  int    i_digits=Digits;

  int    i_unused[13];

//---- 

 

  ExtHandle=FileOpenHistory(c_symbol+i_period+".hst", FILE_BIN|FILE_WRITE);

  if(ExtHandle < 0) return(-1);

//---- write history file header

  c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";

  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);

//---- write history file

  periodseconds=i_period*60;

  start_pos=Bars-1;

  d_open=Open[start_pos];

  d_low=Low[start_pos];

  d_high=High[start_pos];

  d_volume=Volume[start_pos];

  //---- normalize open time

  i_time=Time[start_pos]/periodseconds;

  i_time*=periodseconds;

  for(i=start_pos-1;i>=0; i--)

    {

     time0=Time[i];

     if(time0>=i_time+periodseconds || i==0)

       {

        if(i==0 && time0<i_time+periodseconds)

          {

           d_volume+=Volume[0];

           if (Low[0]<d_low)   d_low=Low[0];

           if (High[0]>d_high) d_high=High[0];

           d_close=Close[0];

          }

        last_fpos=FileTell(ExtHandle);

        last_volume=Volume[i];

        FileWriteInteger(ExtHandle, i_time, LONG_VALUE);

        FileWriteDouble(ExtHandle, d_open, DOUBLE_VALUE);

        FileWriteDouble(ExtHandle, d_low, DOUBLE_VALUE);

        FileWriteDouble(ExtHandle, d_high, DOUBLE_VALUE);

        FileWriteDouble(ExtHandle, d_close, DOUBLE_VALUE);

        FileWriteDouble(ExtHandle, d_volume, DOUBLE_VALUE);

        FileFlush(ExtHandle);

        cnt++;

        if(time0>=i_time+periodseconds)

          {

           i_time=time0/periodseconds;

           i_time*=periodseconds;

           d_open=Open[i];

           d_low=Low[i];

           d_high=High[i];

           d_close=Close[i];

           d_volume=last_volume;

          }

       }

      else

       {

        d_volume+=Volume[i];

        if (Low[i]<d_low)   d_low=Low[i];

        if (High[i]>d_high) d_high=High[i];

        d_close=Close[i];

       }

    }

  FileFlush(ExtHandle);

  Alert("Period:"+histPeriod+" | "+cnt," records written");

  if(ExtHandle>=0) { FileClose(ExtHandle); ExtHandle=-1; }


//----

  return(0);

 }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void deinit()

 {

  Alert("Finished.");

 }

//+------------------------------------------------------------------+