MT4 Files

Check-in [9ff081ce76]
Login

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

Overview
Comment:Added market table feature
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:9ff081ce76c47aa6be49e4a54f0602a95d546f22
User & Date: onagano 2011-06-22 08:53:42
Context
2011-07-30
05:32
Updated MegaDroid check-in: a9f1d5f18e user: onagano tags: trunk
2011-06-22
08:53
Added market table feature check-in: 9ff081ce76 user: onagano tags: trunk
2011-04-28
10:28
More sophisticated trade sync feature on both opens and history check-in: f271f0b88e user: onagano tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to experts/MOB_insertData.mq4.

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
..
59
60
61
62
63
64
65

66
67
68
69

70
71
72
73
74





75
76
77
78
79
80
81
..
97
98
99
100
101
102
103
104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
...
241
242
243
244
245
246
247
248


































































int		mob_getAutoCommit();
int		mob_setAutoCommit(int autoCommit);
int      mob_execute(string sql);
int      mob_getLastErrorNo();
string   mob_getLastErrorMesg();
int      mob_registerStatement(string sql);
int      mob_unregisterStatement(int stmtId);





int      mob_insertTick(int stmtId, datetime dt, int fraction, double vals[]);
int      mob_insertBar(int stmtId, datetime dt, double vals[]);
int      mob_copyRates(int stmtId, double rates[][6], int size, int start, int end);
double   mob_selectDouble(string sql, double defaultVal);
int      mob_selectInt(string sql, int defaultVal);
datetime mob_selectDatetime(string sql, datetime defaultVal);

string   getPeriodSymbol(int period_XX);
datetime mob_time();

#import

//---- input parameters
extern string    ExtDataSourceName = "fxddx";
extern string    ExtUsername       = "";
extern string    ExtPassword       = "";
extern string    ExtTablePrefix    = "MOB_";


int      insertTick;
int      insertBar;
datetime lastTickTime;
int      lastTickFraction;
datetime lastBarTime;
datetime lastGMTime;












//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (!IsDllsAllowed()) {
................................................................................
   rc = mob_open(ExtDataSourceName, ExtUsername, ExtPassword);
   if (rc < 0) return (rc);

   rc = mob_setAutoCommit(true);
   if (rc < 0) return (rc);
   
   // Prepare tick table

   string tickTable = ExtTablePrefix + getDefaultTableName(false);
   rc = prepareTable(tickTable, false);
   if (rc < 0) return (rc);
	

   // Prepare bar table
   string barTable = ExtTablePrefix + getDefaultTableName(true);
   rc = prepareTable(barTable, true);
   if (rc < 0) return (rc);
	





	lastTickTime = TimeCurrent();
	lastTickFraction = GetTickCount() % 1000;
	lastBarTime = TimeCurrent();
//----
   return(0);
  }
//+------------------------------------------------------------------+
................................................................................
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   startTick();

   startBar();
   lastGMTime = mob_time();
//----
   return(0);
  }
//+------------------------------------------------------------------+

int prepareTable(string tableName, bool isBar) {
   int rc;
   
   rc = createTableIfNotExists(tableName, isBar);
   if (rc < 0) return (rc);
   
	rc = mob_registerStatement(insertSQL(tableName, isBar));
   if (rc < 0) return (rc);
   
   if (isBar)
      insertBar = rc;
................................................................................
   barData[3] = Close[i];
   barData[4] = Volume[i];

   int rc = mob_insertBar(insertBar, dt, barData);
   return (rc);
}

int createTableIfNotExists(string tableName, bool isBar = true, bool drop = false) {
   int rc = 0;
 
   if (drop) rc = mob_execute("drop table " + tableName);
   
   rc = mob_selectInt("select count(*) from " + tableName, -1);
   bool exist = 0 <= rc;
   
................................................................................

string getDefaultTableName(bool isBar = true) {
   if (isBar)
      return (Symbol() + "_" + getPeriodSymbol(Period()));
   else
      return (Symbol() + "_TICK");
}










































































>
>
>
>
>






>


>







>







>
>
>
>
>
>
>
>
>
>
>







 







>
|
|
|
|
>





>
>
>
>
>







 







|
>










|







 







|







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
..
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
...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
int		mob_getAutoCommit();
int		mob_setAutoCommit(int autoCommit);
int      mob_execute(string sql);
int      mob_getLastErrorNo();
string   mob_getLastErrorMesg();
int      mob_registerStatement(string sql);
int      mob_unregisterStatement(int stmtId);
int		mob_bindIntParameter(int stmtId, int slot, int intp[]);
int		mob_bindDoubleParameter(int stmtId, int slot, double dblp[]);
int		mob_bindStringParameter(int stmtId, int slot, string strp);
int		mob_bindDatetimeParameter(int stmtId, int slot, datetime dtp[]);
int      mob_executeStatement(int stmtId);
int      mob_insertTick(int stmtId, datetime dt, int fraction, double vals[]);
int      mob_insertBar(int stmtId, datetime dt, double vals[]);
int      mob_copyRates(int stmtId, double rates[][6], int size, int start, int end);
double   mob_selectDouble(string sql, double defaultVal);
int      mob_selectInt(string sql, int defaultVal);
datetime mob_selectDatetime(string sql, datetime defaultVal);
int      createTableIfNotExists(string tableName, string sql);
string   getPeriodSymbol(int period_XX);
datetime mob_time();
int      mob_strcpy(string dest, int size, string src);
#import

//---- input parameters
extern string    ExtDataSourceName = "fxddx";
extern string    ExtUsername       = "";
extern string    ExtPassword       = "";
extern string    ExtTablePrefix    = "MOB_";
extern bool      ExtAccumTickData  = false;

int      insertTick;
int      insertBar;
datetime lastTickTime;
int      lastTickFraction;
datetime lastBarTime;
datetime lastGMTime;

string marketTable = "MARKET";
int updateMarketStmt = 0;

string   mktSymbol = "A123456789";
int      mktSymbolSize;
datetime mktTime[1];
int      mktFraction[1];
double   mktBid[1];
double   mktAsk[1];
double   mktSpread[1];

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (!IsDllsAllowed()) {
................................................................................
   rc = mob_open(ExtDataSourceName, ExtUsername, ExtPassword);
   if (rc < 0) return (rc);

   rc = mob_setAutoCommit(true);
   if (rc < 0) return (rc);
   
   // Prepare tick table
   if (ExtAccumTickData) {
      string tickTable = ExtTablePrefix + getDefaultTableName(false);
      rc = prepareTable(tickTable, false);
      if (rc < 0) return (rc);
   }
   
   // Prepare bar table
   string barTable = ExtTablePrefix + getDefaultTableName(true);
   rc = prepareTable(barTable, true);
   if (rc < 0) return (rc);
	
	// Prepare Market table
	marketTable = ExtTablePrefix + marketTable;
	rc = prepareMarketTable(marketTable, Symbol());
   if (rc < 0) return (rc);
	
	lastTickTime = TimeCurrent();
	lastTickFraction = GetTickCount() % 1000;
	lastBarTime = TimeCurrent();
//----
   return(0);
  }
//+------------------------------------------------------------------+
................................................................................
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if (ExtAccumTickData) startTick();
   updateMarket();
   startBar();
   lastGMTime = mob_time();
//----
   return(0);
  }
//+------------------------------------------------------------------+

int prepareTable(string tableName, bool isBar) {
   int rc;
   
   rc = createTableIfNotExists2(tableName, isBar);
   if (rc < 0) return (rc);
   
	rc = mob_registerStatement(insertSQL(tableName, isBar));
   if (rc < 0) return (rc);
   
   if (isBar)
      insertBar = rc;
................................................................................
   barData[3] = Close[i];
   barData[4] = Volume[i];

   int rc = mob_insertBar(insertBar, dt, barData);
   return (rc);
}

int createTableIfNotExists2(string tableName, bool isBar = true, bool drop = false) {
   int rc = 0;
 
   if (drop) rc = mob_execute("drop table " + tableName);
   
   rc = mob_selectInt("select count(*) from " + tableName, -1);
   bool exist = 0 <= rc;
   
................................................................................

string getDefaultTableName(bool isBar = true) {
   if (isBar)
      return (Symbol() + "_" + getPeriodSymbol(Period()));
   else
      return (Symbol() + "_TICK");
}

int prepareMarketTable(string tableName, string symbol) {
   int rc = 0;
   
   rc = createTableIfNotExists(tableName, marketTableSQL(tableName));
   if (rc < 0) return (rc);

   rc = mob_selectInt("select count(*) from " + tableName
                     + " where symbol = '" + symbol + "'", -1);
   if (rc != 1) {
      rc = mob_execute("insert into " + tableName
                     + " (symbol) values ('" + symbol + "')");
   }

   // Regstering update market statement   
   rc = mob_registerStatement(updateMarketSQL(tableName));
   if (rc < 0) return (rc);
   updateMarketStmt = rc;

   int i = 1;
   mob_bindDatetimeParameter  (updateMarketStmt, i, mktTime);      i++;
   mob_bindIntParameter       (updateMarketStmt, i, mktFraction);  i++;
   mob_bindDoubleParameter    (updateMarketStmt, i, mktBid);       i++;
   mob_bindDoubleParameter    (updateMarketStmt, i, mktAsk);       i++;
   mob_bindDoubleParameter    (updateMarketStmt, i, mktSpread);    i++;
   mob_bindStringParameter    (updateMarketStmt, i, mktSymbol);    i++;
   mktSymbolSize = StringLen(mktSymbol);
   
   mob_strcpy(mktSymbol, mktSymbolSize, symbol);
   
   return (0);
}

string marketTableSQL(string tableName) {
   string sql = "create table " + tableName + " ("
      + "  symbol varchar(10) primary key"
      + ", time timestamp"
      + ", fraction integer"
      + ", bid double precision"
      + ", ask double precision"
      + ", spread double precision"
      + ")";
   return (sql);
}

string updateMarketSQL(string tableName) {
   string sql = "update " + tableName
      + " set time = ?, fraction = ?, bid = ?, ask = ?, spread = ?"
      + " where symbol = ?";
   return (sql);
}

int updateMarket() {
   int rc = 0;

   mktTime[0] = TimeCurrent();
   mktFraction[0]  = GetTickCount() % 1000;
   mktBid[0] = Bid;
   mktAsk[0] = Ask;
   mktSpread[0] = MarketInfo(mktSymbol, MODE_SPREAD);
   
   rc = mob_executeStatement(updateMarketStmt);
   if (rc < 0) return (rc);

   return (rc);
}