Timeseries functions (iBarshift)
#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];
/*
|
|
+---+---+
| |
|| |
+++ |
| | |
+++-+---+
| |
timeframe1 |
timeframe2
*/
int main()
{
init_mt4();
//Timeframe M1
string symbol1="EURUSD";
int timeframe1=PERIOD_M1;
int b1=180;
//Timeframe M5
string symbol2="EURUSD";
int timeframe2=PERIOD_M5;
int b5= iBarShift3(symbol2, timeframe2, symbol1, timeframe1, b1, true);
cout << "b1=" << b1 << " b5=" << b5 << endl;
return 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int iBarShift3(string symbol2, int timeframe2, string symbol1, int timeframe1, int b1, bool exact)
{
int b2=-1;
string idx1=symbol_idxfile(symbol1, timeframe1);
cdbxx::in_db open_idx(idx1);
cdbxx::iterator it1 = open_idx.begin();
it1+=b1;
cdbxx::vector<int> key(it1->first);
cdbxx::vector<int> content(it1->second);
int i_time1=key.get()[0];
int i=0;
switch(timeframe2)
{
case PERIOD_M5:
i=0;
break;
case PERIOD_H1:
i=1;
break;
case PERIOD_H4:
i=2;
break;
default:
i=0;
break;
}
int i_time2=content.get()[i];
string db_name2=symbol_filename(symbol2, timeframe2);
cdbxx::in_db open_db(db_name2);
cdbxx::iterator it2a = open_db.begin();
std::vector<int> key2(1);
key2[0] = i_time2;
cdbxx::iterator it2b = open_db.find(key2);
if (it2b != open_db.end())
{
b2=distance(it2a,it2b);;
}
return (b2);
}