Timeseries access

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <sstream>

#include <algorithm>

#include <stdexcept>

#include <cstdio>

#include "cdbxx/db.hh"

#include "cdbxx/map.hh"

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

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

string symbol = "EURUSD";

int period = PERIOD_M5;

int shift=1005;

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

{

int time1 = iTime(symbol, period, shift);

double open1 = iClose(symbol, period, shift);

double close1 = iClose(symbol, period, shift);

double high1 = iHigh(symbol, period, shift);

double low1 = iLow(symbol, period, shift);

double vol1 = iVolume(symbol, period, shift);

cout << i << " " << time1 << "," << open1 << "," << close1 << "," << high1 << "," << low1 << "," << vol1 << endl;

shift++;

}

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 iClose( string symbol, int timeframe, int shift)

{

static double d_close;

string db_name1=symbol_filename(symbol, timeframe);

cdbxx::in_db open_database(db_name1);

int i=0;

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

{

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

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

d_close=content.get()[3];

i++;

if(i == shift) break;

}

return(d_close);

}

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

//| |

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

double iOpen( string symbol, int timeframe, int shift)

{

static double d_open;

string db_name1=symbol_filename(symbol, timeframe);

cdbxx::in_db open_database(db_name1);

int i=0;

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

{

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

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

d_open=content.get()[0];

i++;

if(i == shift) break;

}

return(d_open);

}

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

//| |

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

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

int i=0;

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

{

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

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

i_time=key.get()[0];

i++;

if(i == shift) break;

}

return(i_time);

}

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

//| |

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

double iHigh( string symbol, int timeframe, int shift)

{

static double d_high;

string db_name1=symbol_filename(symbol, timeframe);

cdbxx::in_db open_database(db_name1);

int i=0;

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

{

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

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

d_high=content.get()[2];

i++;

if(i == shift) break;

}

return(d_high);

}

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

//| |

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

double iLow( string symbol, int timeframe, int shift)

{

static double d_low;

string db_name1=symbol_filename(symbol, timeframe);

cdbxx::in_db open_database(db_name1);

int i=0;

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

{

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

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

d_low=content.get()[1];

i++;

if(i == shift) break;

}

return(d_low);

}

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

//| |

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

double iVolume( string symbol, int timeframe, int shift)

{

static double d_volume;

string db_name1=symbol_filename(symbol, timeframe);

cdbxx::in_db open_database(db_name1);

int i=0;

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

{

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

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

d_volume=content.get()[4];

i++;

if(i == shift) break;

}

return(d_volume);

}