MT4 Files

Check-in [ddc2997255]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added more functions
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:ddc2997255bbe4abdb3a0a77c5b7265316df8e1d
User & Date: onagano 2010-12-23 14:35:19
Context
2010-12-23
14:39
Renamed from insertCandle.mq4 to insertBar.mq4 check-in: fa9b1be39a user: onagano tags: trunk
14:35
Added more functions check-in: ddc2997255 user: onagano tags: trunk
2010-12-13
09:04
Name changed from candle to bar check-in: adf2e94f61 user: onagano tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to experts/include/mt4odbcbridge.mqh.

13
14
15
16
17
18
19
20
21





22
23
24
25

26
27
28
29
30
31
32
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61




















62
63
64
65
66
67
68
69
70
71
72
73
74
75




76
77
78
79
80
81
82
// #define MacrosYear    2005

//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
#import "MT4ODBCBridge.dll"
int      MOB_create();
void     MOB_open(int conId, string dsn, string username, string password);
void     MOB_close(int conId);





void     MOB_execute(int conId, string sql);
int      MOB_getLastErrorNo(int conId);
string   MOB_getLastErrorMesg(int conId);
int      MOB_prepareStatement(int conId, string sql);

int      MOB_insertTick(int conId, int stmtId, datetime dt, int millis, double vals[], int size);
int      MOB_insertBar(int conId, int stmtId, datetime dt, double vals[], int size);
double   MOB_selectDouble(int conId, string sql, double defaultVal);
int      MOB_selectInt(int conId, string sql, int defaultVal);
datetime MOB_selectDatetime(int conId, string sql, datetime defaultVal);
#import

................................................................................
//| Functions                                                        |
//+------------------------------------------------------------------+

void db_create() {
   conId = MOB_create();
}

void db_open(string dsn, string username, string password) {
   MOB_open(conId, dsn, username, password);
}

void db_close() {
   MOB_close(conId);
}





















void db_execute(string sql) {
   MOB_execute(conId, sql);
}

int db_getLastErrorNo() {
   return (MOB_getLastErrorNo(conId));
}

string db_getLastErrorMesg() {
   return (MOB_getLastErrorMesg(conId));
}

int db_prepareStatement(string sql) {
   return (MOB_prepareStatement(conId, sql));




}

int db_insertTick(int stmtId, datetime dt, int millis, double vals[]) {
   return (MOB_insertTick(conId, stmtId, dt, millis, vals, ArraySize(vals)));
}

int db_insertBar(int stmtId, datetime dt, double vals[]) {







|
|
>
>
>
>
>
|


|
>







 







|
|


|
|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|










|
|
>
>
>
>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
..
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// #define MacrosYear    2005

//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
#import "MT4ODBCBridge.dll"
int      MOB_create();
int      MOB_open(int conId, string dsn, string username, string password);
int      MOB_close(int conId);
int      MOB_isDead(int conId);
int		MOB_commit(int conId);
int		MOB_rollback(int conId);
int		MOB_getAutoCommit(int conId);
int		MOB_setAutoCommit(int conId, int autoCommit);
int      MOB_execute(int conId, string sql);
int      MOB_getLastErrorNo(int conId);
string   MOB_getLastErrorMesg(int conId);
int      MOB_registerStatement(int conId, string sql);
int      MOB_unregisterStatement(int conId, int stmtId);
int      MOB_insertTick(int conId, int stmtId, datetime dt, int millis, double vals[], int size);
int      MOB_insertBar(int conId, int stmtId, datetime dt, double vals[], int size);
double   MOB_selectDouble(int conId, string sql, double defaultVal);
int      MOB_selectInt(int conId, string sql, int defaultVal);
datetime MOB_selectDatetime(int conId, string sql, datetime defaultVal);
#import

................................................................................
//| Functions                                                        |
//+------------------------------------------------------------------+

void db_create() {
   conId = MOB_create();
}

int db_open(string dsn, string username, string password) {
   return (MOB_open(conId, dsn, username, password));
}

int db_close() {
   return (MOB_close(conId));
}

int db_isDead() {
   return (MOB_isDead(conId));
}

int db_commit() {
   return (MOB_commit(conId));
}

int db_rollback() {
   return (MOB_rollback(conId));
}

int db_getAutoCommit() {
   return (MOB_getAutoCommit(conId));
}

int db_setAutoCommit(int autoCommit) {
   return (MOB_setAutoCommit(conId, autoCommit));
}

int db_execute(string sql) {
   return (MOB_execute(conId, sql));
}

int db_getLastErrorNo() {
   return (MOB_getLastErrorNo(conId));
}

string db_getLastErrorMesg() {
   return (MOB_getLastErrorMesg(conId));
}

int db_registerStatement(string sql) {
   return (MOB_registerStatement(conId, sql));
}

int db_unregisterStatement(int stmtId) {
   return (MOB_unregisterStatement(conId, stmtId));
}

int db_insertTick(int stmtId, datetime dt, int millis, double vals[]) {
   return (MOB_insertTick(conId, stmtId, dt, millis, vals, ArraySize(vals)));
}

int db_insertBar(int stmtId, datetime dt, double vals[]) {

Changes to experts/insertCandle.mq4.

21
22
23
24
25
26
27
28
29
30
31

32
33


34
35
36
37
38
39
40
41
42

43
44
45


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
..
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

96
97
98
//----
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (0);
   }

   db_create();
   logIfError("db_create failed");
   
   db_open("testmysql", "onagano", "password");
   logIfError("db_open failed");

   
   string table = Symbol() + "_" + Period();


   db_execute("create table if not exists " + table + " ("
				  + "time datetime,"
				  + "open double,"
				  + "high double,"
				  + "low double,"
				  + "close double,"
				  + "vol double"
				  + ")"); 
	logIfError("Cannot create " + table);

	
	insertCandle = db_prepareStatement("insert into " + table + " values (?, ?, ?, ?, ?, ?)");
	logIfError("Cannot prepare insert " + table);


	
	lastTime = TimeCurrent();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   db_close();
   logIfError("db_close failed");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
................................................................................
      double candleData[5];
      candleData[0] = Open[i];
      candleData[1] = High[i];
      candleData[2] = Low[i];
      candleData[3] = Close[i];
      candleData[4] = Volume[i];
   
      db_insertBar(insertCandle, dt, candleData);
      logIfError("Insert bar failed");
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

void logIfError(string mesg) {
   int eno = GetLastError();
   if (eno != 0) {
      Print("ERROR (", eno, "): ", mesg);
   }
   int dbeno = db_getLastErrorNo();
   if (dbeno != 0) {
      Print("DB ERROR (", dbeno, "): ", db_getLastErrorMesg());

   }
}








<

|
<
>


>
>
|
|
|
|
|
|
|
<
|
>

|
<
>
>











|
|







 







|
|






|
|
<
<
<
|
<
|
>



21
22
23
24
25
26
27

28
29

30
31
32
33
34
35
36
37
38
39
40
41

42
43
44
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
..
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91



92

93
94
95
96
97
//----
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (0);
   }

   db_create();

   
   int rc = db_open("testpostgre", "onagano", "password");

   if (rc < 0) { logIfError(rc); return (rc); }
   
   string table = Symbol() + "_" + Period();
   rc = db_execute("drop table if exists " + table);
   if (rc < 0) { logIfError(rc); return (rc); }
   rc = db_execute("create table " + table + " ("
				  + "time timestamp,"
				  + "open numeric(8, 5),"
				  + "high numeric(8, 5),"
				  + "low numeric(8, 5),"
				  + "close numeric(8, 5),"
				  + "vol double precision,"

				  + "primary key (time))"); 
   if (rc < 0) { logIfError(rc); return (rc); }
	
	insertCandle = db_registerStatement("insert into " + table + " values (?, ?, ?, ?, ?, ?)");

   rc = insertCandle;
   if (rc < 0) { logIfError(rc); return (rc); }
	
	lastTime = TimeCurrent();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   int rc = db_close();
   if (rc < 0) { logIfError(rc); return (rc); }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
................................................................................
      double candleData[5];
      candleData[0] = Open[i];
      candleData[1] = High[i];
      candleData[2] = Low[i];
      candleData[3] = Close[i];
      candleData[4] = Volume[i];
   
      int rc = db_insertBar(insertCandle, dt, candleData);
      if (rc < 0) { logIfError(rc); return (rc); }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

void logIfError(int rc) {
   if (rc < 0) {



      int eno = db_getLastErrorNo();

      string emesg = db_getLastErrorMesg();
      Print("ERROR (", eno, "): ", emesg);
   }
}

Changes to experts/insertTick.mq4.

20
21
22
23
24
25
26
27
28
29
30

31
32


33
34
35
36
37
38
39

40
41
42


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
..
62
63
64
65
66
67
68

69
70

71
72
73
74
75
76
77
78
79
80
81
82
83

84
85
86
//----
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (0);
   }

   db_create();
   logIfError("db_create failed");
   
   db_open("testmysql", "onagano", "password");
   logIfError("db_open failed");

   
   string tickTable = Symbol() + "_TICK";


   db_execute("create table if not exists " + tickTable + " ("
				  + "time datetime,"
				  + "msec int,"
				  + "ask double,"
				  + "bid double"
				  + ")"); 
	logIfError("Cannot create " + tickTable);

	
	insertTick = db_prepareStatement("insert into " + tickTable + " values (?, ?, ?, ?)");
	logIfError("Cannot prepare insert " + tickTable);


//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   db_close();
   logIfError("db_close failed");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
................................................................................
//----
   datetime dt = TimeCurrent();
   int millis  = GetTickCount() % 1000;
   double tickData[2];
   tickData[0] = Ask;
   tickData[1] = Bid;
   

   db_insertTick(insertTick, dt, millis, tickData);
   logIfError("Insert tick failed");

//----
   return(0);
  }
//+------------------------------------------------------------------+

void logIfError(string mesg) {
   int eno = GetLastError();
   if (eno != 0) {
      Print("ERROR (", eno, "): ", mesg);
   }
   int dbeno = db_getLastErrorNo();
   if (dbeno != 0) {
      Print("DB ERROR (", dbeno, "): ", db_getLastErrorMesg());

   }
}








<

|
<
>


>
>
|
|
|
|
|
<
|
>

|
<
>
>









|
|







 







>
|
<
>





|
|
<
<
<
|
<
|
>



20
21
22
23
24
25
26

27
28

29
30
31
32
33
34
35
36
37
38

39
40
41
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
..
64
65
66
67
68
69
70
71
72

73
74
75
76
77
78
79
80



81

82
83
84
85
86
//----
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (0);
   }

   db_create();

   
   int rc = db_open("testpostgre", "onagano", "password");

   if (rc < 0) { logIfError(rc); return (rc); }
   
   string tickTable = Symbol() + "_TICK";
   rc = db_execute("drop table if exists " + tickTable);
   if (rc < 0) { logIfError(rc); return (rc); }
   rc = db_execute("create table " + tickTable + " ("
				  + "time timestamp,"
				  + "msec smallint,"
				  + "ask numeric(8, 5),"
				  + "bid numeric(8, 5),"

				  + "primary key (time, msec))"); 
   if (rc < 0) { logIfError(rc); return (rc); }
	
	insertTick = db_registerStatement("insert into " + tickTable + " values (?, ?, ?, ?)");

	rc = insertTick;
   if (rc < 0) { logIfError(rc); return (rc); }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   int rc = db_close();
   if (rc < 0) { logIfError(rc); return (rc); }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
................................................................................
//----
   datetime dt = TimeCurrent();
   int millis  = GetTickCount() % 1000;
   double tickData[2];
   tickData[0] = Ask;
   tickData[1] = Bid;
   
   int rc = 0;
   rc = db_insertTick(insertTick, dt, millis, tickData);

   if (rc < 0) { logIfError(rc); return (rc); }
//----
   return(0);
  }
//+------------------------------------------------------------------+

void logIfError(int rc) {
   if (rc < 0) {



      int eno = db_getLastErrorNo();

      string emesg = db_getLastErrorMesg();
      Print("ERROR (", eno, "): ", emesg);
   }
}

Changes to experts/scripts/testMT4ODBCBridge.mq4.

14
15
16
17
18
19
20
21


22
23
24
25
26

27


































28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

47
48

49
50
51
52
int start()
  {
//----
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (0);
   }
   


   db_create();
   logIfError("create failed");
   
   db_open("testmysql", "onagano", "password");
   logIfError("open failed");

   


































   int ti = db_selectInt("select count(*) from eurjpy_1", -1);
   Print("Count: ", ti);
   
   double td = db_selectDouble("select open from eurjpy_1 order by time", -1);
   Print("Double: ", td);
   
   datetime tt = db_selectDatetime("select time from eurjpy_1 order by time desc", TimeCurrent());
   Print("Datetime: ", TimeToStr(tt));
   Print("Datetime: ", TimeToStr(tt + 9 * 3600));
   Print("Current : ", TimeToStr(TimeCurrent()));

   db_close();
   logIfError("close failed");
//----
   return(0);
  }
//+------------------------------------------------------------------+

void logIfError(string mesg) {

   int eno = GetLastError();
   if (eno > 0) {

      Print("ERROR (", eno, "): ", mesg);
   }
}









>
>

<

|
<
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|
|

|
|

<
<

|
|

|



|
>
|
<
>
|



14
15
16
17
18
19
20
21
22
23
24

25
26

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70


71
72
73
74
75
76
77
78
79
80
81

82
83
84
85
86
int start()
  {
//----
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (0);
   }
   
   int rc = 0;
   
   db_create();

   
   rc = db_open("testpostgre", "onagano", "password");

   if (rc < 0) { logIfError(rc); return (rc); }
   
   rc = db_execute("drop table if exists ticks");
   if (rc < 0) { logIfError(rc); return (rc); }

   rc = db_execute("create table ticks (time timestamp, msec smallint, ask decimal(8, 5), bid decimal(8, 5), primary key (time, msec))");
   if (rc < 0) { logIfError(rc); return (rc); }

   int insertTick;
   rc = db_registerStatement("insert into ticks values (?, ?, ?, ?)");
   insertTick = rc;
   if (rc < 0) { logIfError(rc); return (rc); }

   datetime dt;
   int millis;
   double vals[2];
   
   dt = D'2010.12.20 13:34:45';
   millis  = 333;
   vals[0] = 1.4567;
   vals[1] = 1.4507;
   
   rc = db_insertTick(insertTick, dt, millis, vals);
   if (rc < 0) { logIfError(rc); return (rc); }

   dt = D'2010.12.20 13:34:55';
   millis  = 335;
   vals[0] = 1.4560;
   vals[1] = 1.4555;
   
   rc = db_insertTick(insertTick, dt, millis, vals);
   if (rc < 0) { logIfError(rc); return (rc); }
   
   rc = db_unregisterStatement(insertTick);
   if (rc < 0) { logIfError(rc); return (rc); }

   int ti = db_selectInt("select count(*) from ticks", -1);
   Print("Count: ", ti);
 
   double td = db_selectDouble("select ask from ticks order by time, msec", -1.0);
   Print("Double: ", td);

   datetime tt = db_selectDatetime("select time from ticks order by time desc, msec desc", TimeCurrent());
   Print("Datetime: ", TimeToStr(tt));



   rc = db_close();
   logIfError(rc);
//----
   return(rc);
  }
//+------------------------------------------------------------------+

void logIfError(int rc) {
   if (rc < 0) {
      int eno = db_getLastErrorNo();

      string emesg = db_getLastErrorMesg();
      Print("ERROR (", eno, "): ", emesg);
   }
}