Time series access modified 2
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <stdexcept>
#include <cstdio>
#include "cdbxx/db.hh"
#include "cdbxx/map.hh"
#define EMPTY_VALUE 0x7FFFFFFF //Default custom indicator empty value.
//Timeframe of the chart (chart period). It can be any of the following values:
#define PERIOD_M1 1 //1 minute.
#define PERIOD_M5 5 //5 minutes.
#define PERIOD_M15 15 //15 minutes.
#define PERIOD_M30 30 //30 minutes.
#define PERIOD_H1 60 //1 hour.
#define PERIOD_H4 240 //4 hour.
#define PERIOD_D1 1440 //Daily.
#define PERIOD_W1 10080 //Weekly.
#define PERIOD_MN1 43200 //Monthly.
//Market information identifiers, used with MarketInfo() function.
//It can be any of the following values:
#define MODE_LOW 1 //Low day price.
#define MODE_HIGH 2 //High day price.
#define MODE_TIME 5 //The last incoming tick time (last known server time).
#define MODE_BID 9 //Last incoming bid price. For the current symbol, it is stored in the predefined variable Bid
#define MODE_ASK 10 //Last incoming ask price. For the current symbol, it is stored in the predefined variable Ask
#define MODE_POINT 11 //Point size in the quote currency. For the current symbol, it is stored in the predefined variable Point
#define MODE_DIGITS 12 //Count of digits after decimal point in the symbol prices. For the current symbol, it is stored in the predefined variable Digits
#define MODE_SPREAD 13 //Spread value in points.
#define MODE_STOPLEVEL 14 //Stop level in points.
#define MODE_LOTSIZE 15 //Lot size in the base currency.
#define MODE_TICKVALUE 16 //Tick value in the deposit currency.
#define MODE_TICKSIZE 17 //Tick size in points.
#define MODE_SWAPLONG 18 //Swap of the long position.
#define MODE_SWAPSHORT 19 //Swap of the short position.
#define MODE_STARTING 20 //Market starting date (usually used for futures).
#define MODE_EXPIRATION 21 //Market expiration date (usually used for futures).
#define MODE_TRADEALLOWED 22 //Trade is allowed for the symbol.
#define MODE_MINLOT 23 //Minimum permitted amount of a lot.
#define MODE_LOTSTEP 24 //Step for changing lots.
#define MODE_MAXLOT 25 //Maximum permitted amount of a lot.
#define MODE_SWAPTYPE 26 //Swap calculation method. 0 - in points; 1 - in the symbol base currency; 2 - by interest; 3 - in the margin currency.
#define MODE_PROFITCALCMODE 27 //Profit calculation mode. 0 - Forex; 1 - CFD; 2 - Futures.
#define MODE_MARGINCALCMODE 28 //Margin calculation mode. 0 - Forex; 1 - CFD; 2 - Futures; 3 - CFD for indices.
#define MODE_MARGININIT 29 //Initial margin requirements for 1 lot.
#define MODE_MARGINMAINTENANCE 30 //Margin to maintain open positions calculated for 1 lot.
#define MODE_MARGINHEDGED 31 //Hedged margin calculated for 1 lot.
#define MODE_MARGINREQUIRED 32 //Free margin required to open 1 lot for buying.
#define MODE_FREEZELEVEL 33 //Order freeze level in points. If the execution price lies within the range defined by the freeze level, the order cannot be modified, cancelled or closed.
#define NR_OF_SYMBOLS 20
#define NR_OF_PERIODS 9
using namespace std;
string _Symbol_;
int _Period_;
string _history_;
string _marketinfo_;
string _Symbol[NR_OF_SYMBOLS];
int _Period[NR_OF_PERIODS];
void init_mt4();
string symbol_filename(string symbol, int timeframe);
int iTime( string symbol, int timeframe, int shift);
double iClose( string symbol, int timeframe, int shift);
double iHigh( string symbol, int timeframe, int shift);
double iLow( string symbol, int timeframe, int shift);
double iOpen( string symbol, int timeframe, int shift);
double iVolume( string symbol, int timeframe, int shift);
double iField_d( string symbol, int timeframe, int shift, int field);
vector<int> vTime( string symbol, int timeframe, int shift, int nr);
vector<double> vClose( string symbol, int timeframe, int shift, int nr);
vector<double> vHigh( string symbol, int timeframe, int shift, int nr);
vector <double> vLow( string symbol, int timeframe, int shift, int nr);
vector<double> vOpen( string symbol, int timeframe, int shift, int nr);
vector<double> vVolume( string symbol, int timeframe, int shift, int nr);
vector<double> vField_d( string symbol, int timeframe, int shift, int nr, int field);
int iBars( string symbol, int timeframe);
//int iHighest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0);
//int iLowest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0);
/*
int iBarShift( string symbol, int timeframe, datetime time, bool exact=false);
Search for bar by open time. The function returns bar shift with the open time specified. If the bar having the specified open time is missing, the function will return -1 or the nearest bar shift depending on the exact.
Parameters:
symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.
timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.
time - value to find (bar's open time).
exact - Return mode when bar not found. false - iBarShift returns nearest. true - iBarShift returns -1.
Sample:
datetime some_time=D'2004.03.21 12:00';
int shift=iBarShift("EUROUSD",PERIOD_M1,some_time);
Print("shift of bar with open time ",TimeToStr(some_time)," is ",shift);
*/
/*
double iMA( string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)
Calculates the Moving average indicator and returns its value.
Parameters:
symbol - Symbol the data of which should be used to calculate indicator. NULL means the current symbol.
timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means the current chart timeframe.
period - Averaging period for calculation.
ma_shift - MA shift. Indicators line offset relate to the chart by timeframe.
ma_method - MA method. It can be any of the Moving Average method enumeration value.
applied_price - Applied price. It can be any of Applied price enumeration values.
shift - Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago).
Sample:
AlligatorJawsBuffer[i]=iMA(NULL,0,13,8,MODE_SMMA,PRICE_MEDIAN,i);
MODE_SMA 0 Simple moving average,
MODE_EMA 1 Exponential moving average,
MODE_SMMA 2 Smoothed moving average,
MODE_LWMA 3 Linear weighted moving average.
PRICE_CLOSE 0 Close price.
PRICE_OPEN 1 Open price.
PRICE_HIGH 2 High price.
PRICE_LOW 3 Low price.
PRICE_MEDIAN 4 Median price, (high+low)/2.
PRICE_TYPICAL 5 Typical price, (high+low+close)/3.
PRICE_WEIGHTED 6 Weighted close price, (high+low+close+close)/4.
*/
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int main()
{
init_mt4();
int i;
string symbol = "EURUSD";
int period = PERIOD_M5;
vector<int> vtime1;
vector<double> vopen1;
vector<double> vclose1;
vector<double> vhigh1;
vector<double> vlow1;
vector<double> vvol1;
vector<int> vtime2;
vector<double> vopen2;
vector<double> vclose2;
vector<double> vhigh2;
vector<double> vlow2;
vector<double> vvol2;
int shift=1;
int nr=5;
vtime1 = vTime(symbol, period, shift,nr);
vopen1 = vClose(symbol, period, shift, nr);
vclose1 = vClose(symbol, period, shift, nr);
vhigh1 = vHigh(symbol, period, shift, nr);
vlow1 = vLow(symbol, period, shift, nr);
vvol1 = vVolume(symbol, period, shift, nr);
int nr1=-5;
vtime2 = vTime(symbol, period, shift,nr1);
vopen2 = vClose(symbol, period, shift, nr1);
vclose2 = vClose(symbol, period, shift, nr1);
vhigh2 = vHigh(symbol, period, shift, nr1);
vlow2 = vLow(symbol, period, shift, nr1);
vvol2 = vVolume(symbol, period, shift, nr1);
for (i=0; i<5; i++) cout << shift << "-" << i << " "
<< vtime2[i] << ","
<< vopen2[i] << ","
<< vclose2[i] << ","
<< vhigh2[i] << ","
<< vlow2[i] << ","
<< vvol2[i] << ","
<< endl;
cout << endl;
for (i=0; i<5; i++) cout << shift << "+" << i << " "
<< vtime1[i] << ","
<< vopen1[i] << ","
<< vclose1[i] << ","
<< vhigh1[i] << ","
<< vlow1[i] << ","
<< vvol1[i] << ","
<< endl;
cout << endl;
int shift2=0;//300000;
int nr2=10;
for(i=shift2;i<(shift2+nr2);i++)
{
int time1 = iTime(symbol, period, i);
double open1 = iClose(symbol, period, i);
double close1 = iClose(symbol, period, i);
double high1 = iHigh(symbol, period, i);
double low1 = iLow(symbol, period, i);
double vol1 = iVolume(symbol, period, i);
cout << i << " " << time1 << "," << open1 << "," << close1 << "," << high1 << "," << low1 << "," << vol1 << endl;
}
int bars1 = iBars(symbol, period);
cout << "Bars: " << bars1 << endl;
return 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string timeframe_to_str(int timeframe)
{
switch(timeframe)
{
case PERIOD_M1:
return "1";
break;
case PERIOD_M5:
return "5";
break;
case PERIOD_M15:
return "15";
break;
case PERIOD_M30:
return "30";
break;
case PERIOD_H1:
return "60";
break;
case PERIOD_H4:
return "240";
break;
case PERIOD_D1:
return "1440";
break;
case PERIOD_W1:
return "10080";
break;
case PERIOD_MN1:
return "43200";
break;
default:
return "1";
break;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string symbol_filename(string symbol, int timeframe)
{
return(_history_+"/"+symbol+timeframe_to_str(timeframe)+".cdb");
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void init_mt4()
{
_Symbol[0]="AUDCAD";
_Symbol[1]="AUDJPY";
_Symbol[2]="AUDNZD";
_Symbol[3]="AUDUSD";
_Symbol[4]="CADJPY";
_Symbol[5]="CHFJPY";
_Symbol[6]="EURAUD";
_Symbol[7]="EURCAD";
_Symbol[8]="EURCHF";
_Symbol[9]="EURGBP";
_Symbol[10]="EURJPY";
_Symbol[11]="EURUSD";
_Symbol[12]="GBPCHF";
_Symbol[13]="GBPJPY";
_Symbol[14]="GBPUSD";
_Symbol[15]="NZDUSD";
_Symbol[16]="USDCAD";
_Symbol[17]="USDCHF";
_Symbol[18]="USDJPY";
_Symbol[19]="USDMXN";
_Period[0]= PERIOD_M1;
_Period[1]= PERIOD_M5;
_Period[2]= PERIOD_M15;
_Period[3]= PERIOD_M30;
_Period[4]= PERIOD_H1;
_Period[5]= PERIOD_H4;
_Period[6]= PERIOD_D1;
_Period[7]= PERIOD_W1;
_Period[8]= PERIOD_MN1;
_Symbol_ = _Symbol[11];
_Period_ = _Period[0];
_history_ = "/home/barnix/mt4db/cdb";
_marketinfo_ = "/home/barnix/mt4db/Marketinfo.csv";
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int iBars( string symbol, int timeframe)
{
static int d_bars;
string db_name1=symbol_filename(symbol, timeframe);
cdbxx::in_db open_database(db_name1);
cdbxx::iterator it1 = open_database.begin();
cdbxx::iterator it2 = open_database.end();
d_bars = distance(it1,it2);
return(d_bars);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double iField_d( string symbol, int timeframe, int shift, int field)
{
static double d_field;
string db_name1=symbol_filename(symbol, timeframe);
cdbxx::in_db open_database(db_name1);
cdbxx::iterator it1 = open_database.begin();
it1+=shift;
cdbxx::vector<int> key(it1->first);
cdbxx::vector<float> content(it1->second);
d_field=content.get()[field];
return(d_field);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector<double> vField_d( string symbol, int timeframe, int shift, int nr, int field)
{
static double d_field;
vector<double> tmp;
string db_name1=symbol_filename(symbol, timeframe);
cdbxx::in_db open_database(db_name1);
int i=0;
tmp.clear();
cdbxx::iterator it1 = open_database.begin();
it1+=shift;
while( true )
{
cdbxx::vector<int> key(it1->first);
cdbxx::vector<float> content(it1->second);
if(nr >= 0)
{
if(it1 != open_database.end())
{
d_field=content.get()[field];
tmp.push_back(d_field);
it1++;
}
else
{
tmp.push_back(0);
//break;
}
i++;
}
else
{
if(it1 != open_database.begin())
{
d_field=content.get()[field];
tmp.push_back(d_field);
it1--;
}
else
{
tmp.push_back(0);
//break;
}
i--;
}
if(i == nr ) break;
}
return(tmp);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int iTime( string symbol, int timeframe, int shift)
{
static int i_time;
string db_name1=symbol_filename(symbol, timeframe);
cdbxx::in_db open_database(db_name1);
cdbxx::iterator it1 = open_database.begin();
it1+=shift;
cdbxx::vector<int> key(it1->first);
cdbxx::vector<float> content(it1->second);
i_time=key.get()[0];
return(i_time);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector<int> vTime( string symbol, int timeframe, int shift, int nr)
{
int field = 0;
static int i_time;
vector<int> tmp;
string db_name1=symbol_filename(symbol, timeframe);
cdbxx::in_db open_database(db_name1);
int i=0;
tmp.clear();
cdbxx::iterator it1 = open_database.begin();
it1+=shift;
while( true )
{
cdbxx::vector<int> key(it1->first);
cdbxx::vector<float> content(it1->second);
if(nr >= 0)
{
if(it1 != open_database.end())
{
i_time=key.get()[field];
tmp.push_back(i_time);
it1++;
}
else
{
tmp.push_back(0);
//break;
}
i++;
}
else
{
if(it1 != open_database.begin())
{
i_time=key.get()[field];
tmp.push_back(i_time);
it1--;
}
else
{
tmp.push_back(0);
//break;
}
i--;
}
if(i == nr ) break;
}
return(tmp);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double iOpen( string symbol, int timeframe, int shift)
{
int field = 0;
return(iField_d(symbol,timeframe,shift,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector<double> vOpen( string symbol, int timeframe, int shift, int nr)
{
int field = 0;
return(vField_d(symbol,timeframe,shift,nr,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double iLow( string symbol, int timeframe, int shift)
{
int field = 1;
return(iField_d(symbol,timeframe,shift,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector<double> vLow( string symbol, int timeframe, int shift, int nr)
{
int field = 1;
return(vField_d(symbol,timeframe,shift,nr,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double iHigh( string symbol, int timeframe, int shift)
{
int field = 2;
return(iField_d(symbol,timeframe,shift,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector<double> vHigh( string symbol, int timeframe, int shift, int nr)
{
int field = 2;
return(vField_d(symbol,timeframe,shift,nr,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double iClose( string symbol, int timeframe, int shift)
{
int field = 3;
return(iField_d(symbol,timeframe,shift,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector<double> vClose( string symbol, int timeframe, int shift, int nr)
{
int field = 3;
return(vField_d(symbol,timeframe,shift,nr,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double iVolume( string symbol, int timeframe, int shift)
{
int field = 4;
return(iField_d(symbol,timeframe,shift,field));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector<double> vVolume( string symbol, int timeframe, int shift, int nr)
{
int field = 4;
return(vField_d(symbol,timeframe,shift,nr,field));
}