MT4 Files

Check-in [97d0fedc03]
Login

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

Overview
Comment:Added copyRates function
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:97d0fedc03d58dbd3957d05c2b6bb67e5a7fda2c
User & Date: onagano 2011-01-01 15:19:45
Context
2011-01-02
09:45
Added insertData check-in: f805005d6b user: onagano tags: trunk
2011-01-01
15:19
Added copyRates function check-in: 97d0fedc03 user: onagano tags: trunk
2010-12-31
17:46
Added period to symbol converter check-in: 8ed0326dc4 user: onagano tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to experts/libraries/mt4odbcbridge.mq4.

4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
..
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
...
128
129
130
131
132
133
134
135
136

137
138
139
140


141
142
143
144











































145
























































146


147
148
149
150
151
152
153
...
175
176
177
178
179
180
181
182

















































































//|                   http://www.google.com/profiles/onagano.g#about |
//+------------------------------------------------------------------+
#property copyright "Osamu Nagano"
#property link      "http://www.google.com/profiles/onagano.g#about"
#property library

//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
// #define MacrosHello   "Hello, world!"
// #define MacrosYear    2005


//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
#import "MT4ODBCBridge.dll"
int      MOB_create();
int      MOB_open(int conId, string dsn, string username, string password);
................................................................................
int      MOB_selectInt(int conId, string sql, int defaultVal);
datetime MOB_selectDatetime(int conId, string sql, datetime defaultVal);
#import

//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
// #import "stdlib.ex4"
//   string ErrorDescription(int error_code);
// #import


//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
int mob_conId;

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

void mob_create() {
   mob_conId = MOB_create();
}

int mob_open(string dsn, string username, string password) {
   return (MOB_open(mob_conId, dsn, username, password));
}
................................................................................
}

datetime mob_selectDatetime(string sql, datetime defaultVal) {
   return (MOB_selectDatetime(mob_conId, sql, defaultVal));
}

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

int logIfMOBError(int rc) {

   if (rc < 0) {
      int eno = mob_getLastErrorNo();
      string emesg = mob_getLastErrorMesg();
      Print("ERROR (", eno, "): ", emesg);


   }
   return (rc);
}












































string getTableName() {
























































   return (Symbol() + "_" + getPeriodSymbol(Period()));


}

string getPeriodSymbol(int period_XX) {
   switch (period_XX) {
   case PERIOD_M1:
      return ("M1");
      break;
................................................................................
   case PERIOD_MN1:
      return ("MN1");
      break;
   default:
      return ("");
   }
}

























































































|

|
|
>







 







|
|
|








|

<







 







|
|
>




>
>




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







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
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
...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
...
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//|                   http://www.google.com/profiles/onagano.g#about |
//+------------------------------------------------------------------+
#property copyright "Osamu Nagano"
#property link      "http://www.google.com/profiles/onagano.g#about"
#property library

//+------------------------------------------------------------------+
//| Defines                                                          |
//+------------------------------------------------------------------+
#define RDBMS_GENERIC      0
#define RDBMS_MYSQL        1
#define RDBMS_POSTGRESQL   2

//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
#import "MT4ODBCBridge.dll"
int      MOB_create();
int      MOB_open(int conId, string dsn, string username, string password);
................................................................................
int      MOB_selectInt(int conId, string sql, int defaultVal);
datetime MOB_selectDatetime(int conId, string sql, datetime defaultVal);
#import

//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
#import "stdlib.ex4"
   string ErrorDescription(int error_code);
#import


//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
int mob_conId;

//+------------------------------------------------------------------+
//| MOB wrapper functions                                            |
//+------------------------------------------------------------------+

void mob_create() {
   mob_conId = MOB_create();
}

int mob_open(string dsn, string username, string password) {
   return (MOB_open(mob_conId, dsn, username, password));
}
................................................................................
}

datetime mob_selectDatetime(string sql, datetime defaultVal) {
   return (MOB_selectDatetime(mob_conId, sql, defaultVal));
}

//+------------------------------------------------------------------+
//| Stateful helper functions                                        |
//+------------------------------------------------------------------+
int checkMOBError(int rc, bool rollback = true, bool disconnect = true) {
   if (rc < 0) {
      int eno = mob_getLastErrorNo();
      string emesg = mob_getLastErrorMesg();
      Print("ERROR (", eno, "): ", emesg);
      if (rollback) mob_rollback();
      if (disconnect) mob_close();
   }
   return (rc);
}

int createTableIfNotExists(string tableName, bool isBar = true,
                       bool drop = false, int rdbms = RDBMS_GENERIC) {
   int rc = 0;
 
   if (drop) rc = mob_execute("drop table " + tableName);
   
   rc = mob_selectInt("select count(*) from " + tableName, -1);
   bool exist = 0 <= rc;
   
   switch (rdbms) {
   case RDBMS_GENERIC:
   default:
      if (!exist) {
         if (isBar)
            rc = mob_execute(barTableSQL(tableName, rdbms));
         else
            rc = mob_execute(tickTableSQL(tableName, rdbms));
      }
   }
   
   return (rc);
}

int timeDifference(string dsn, string username, string password) {
   datetime before = TimeCurrent();
   datetime after;
   mob_create();
   mob_open(dsn, username, password);
   string tableName = "mobtest_timedifference";
   createTableIfNotExists(tableName, true, true);
   int stmt = mob_registerStatement(insertBarSQL(tableName, RDBMS_GENERIC));
   double vals[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
   mob_insertBar(stmt, before, vals);
   after = mob_selectDatetime("select time from " + tableName, 0);
   mob_execute("drop table " + tableName);
   mob_close();
   return (before - after);
}

//+------------------------------------------------------------------+
//| Stateless helper functions                                       |
//+------------------------------------------------------------------+
string barTableSQL(string tableName, int rdbms = RDBMS_GENERIC) {
   string sql;
   switch (rdbms) {
   case RDBMS_GENERIC:
   default:
      sql = "create table " + tableName + " ("
         + "  time timestamp"
         + ", open numeric(8, 5)"
         + ", low numeric(8, 5)"
         + ", high numeric(8, 5)"
         + ", close numeric(8, 5)"
         + ", volume double precision"
         + ", primary key (time)"
         + ")";
   }
   return (sql);
}

string insertBarSQL(string tableName, int rdbms = RDBMS_GENERIC) {
   string sql;
   switch (rdbms) {
   case RDBMS_GENERIC:
   default:
      sql = "insert into " + tableName
         + " (time, open, low, high, close, volume) values (?, ?, ?, ?, ?, ?)";
   }
   return (sql);
}

string tickTableSQL(string tableName, int rdbms = RDBMS_GENERIC) {
   string sql;
   switch (rdbms) {
   case RDBMS_GENERIC:
   default:
      sql = "create table " + tableName + " ("
         + "  time timestamp"
         + ", fraction integer"
         + ", ask numeric(8, 5)"
         + ", bid numeric(8, 5)"
         + ", primary key (time, fraction)"
         + ")";
   }
   return (sql);
}

string insertTickSQL(string tableName, int rdbms = RDBMS_GENERIC) {
   string sql;
   switch (rdbms) {
   case RDBMS_GENERIC:
   default:
      sql = "insert into " + tableName
         + " (time, fraction, ask, bid) values (?, ?, ?, ?)";
   }
   return (sql);
}

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

string getPeriodSymbol(int period_XX) {
   switch (period_XX) {
   case PERIOD_M1:
      return ("M1");
      break;
................................................................................
   case PERIOD_MN1:
      return ("MN1");
      break;
   default:
      return ("");
   }
}

//+------------------------------------------------------------------+
//| Typical usecase functions                                        |
//+------------------------------------------------------------------+
int copyRates(string dsn, string username, string password, string tableName) {
   mob_create();
   mob_open(dsn, username, password);
   int count      = mob_selectInt("select count(*) from " + tableName, -1);
   datetime dtMax = mob_selectDatetime("select max(time) from " + tableName, -1);
   datetime dtMin = mob_selectDatetime("select min(time) from " + tableName, -1);
   mob_close();
   if (count <= 0) {
      return (copyRatesLast(dsn, username, password, tableName, Bars));
   } else {
      datetime dts[];
      ArrayCopySeries(dts, MODE_TIME, NULL, 0);
      int iMax = ArrayBsearch(dts, dtMax, WHOLE_ARRAY, 0 , MODE_DESCEND);
      int iMin = ArrayBsearch(dts, dtMin, WHOLE_ARRAY, 0 , MODE_DESCEND);
      int rc = 0;
      rc += copyRatesBetween(dsn, username, password, tableName, 0, Bars - iMin - 1);
      rc += copyRatesBetween(dsn, username, password, tableName, Bars - iMax, Bars);
      return (rc);
   }
}

int copyRatesLast(string dsn, string username, string password, string tableName, int lastBars) {
   int start = Bars - lastBars;
   int end = Bars;
   return (copyRatesBetween(dsn, username, password, tableName, start, end));
}

int copyRatesBetween(string dsn, string username, string password, string tableName, int start, int end) {
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (-1);
   }

   if (start == end) return (0);
   if (start > end) {
      Print("Wrong indices. start ", start, " should be smaller than end ", end);
      return (-1);
   }
   
   double rates[][6];
   int n = ArrayCopyRates(rates);
   if (n < 0) {
      int ec = GetLastError();
      Print("ArrayCopyRates causess ", ec, ", ", ErrorDescription(ec));
      return (n);
   }
   if (n < end) {
      Print("End index ", end,  " is larger than available size ", n);
      return (-1);
   }

   mob_create();
   
   int rc = checkMOBError(mob_open(dsn, username, password));
   if (rc < 0) return (rc);
   
   rc = checkMOBError(mob_setAutoCommit(false));
   if (rc < 0) return (rc);

   rc = checkMOBError(createTableIfNotExists(tableName));
   if (rc < 0) return (rc);
   
   int insertTable = checkMOBError(mob_registerStatement(insertBarSQL(tableName)));
   if (rc < 0) return (rc);

   rc = checkMOBError(mob_copyRates(insertTable, rates, ArraySize(rates), start, end));
   if (rc < 0) return (rc);

   rc = checkMOBError(mob_commit());
   if (rc < 0) return (rc);

   rc = checkMOBError(mob_close());
   if (rc < 0) return (rc);

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

Changes to experts/scripts/copyRates.mq4.

2
3
4
5
6
7
8
9
10
11
12

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
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
//|                                                    copyRates.mq4 |
//|                                                     Osamu Nagano |
//|                   http://www.google.com/profiles/onagano.g#about |
//+------------------------------------------------------------------+
#property copyright "Osamu Nagano"
#property link      "http://www.google.com/profiles/onagano.g#about"

#import "stdlib.ex4"
string   ErrorDescription(int error_code);
#import "mt4odbcbridge.ex4"
void     mob_create();

int      mob_open(string dsn, string username, string password);
int      mob_close();
int      mob_isDead();
int		mob_commit();
int		mob_rollback();
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 millis, 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      logIfMOBError(int rc);
string   getTableName();
string   getPeriodSymbol(int period_XX);
#import

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (-1);
   }
//----
   mob_create();

   
   int rc = mob_open("testpostgresql", "testuser", "password");
   if (rc < 0) { logIfMOBError(rc); return (rc); }
   
   string table = getTableName();
   rc = mob_execute("drop table if exists " + table);
   if (rc < 0) { logIfMOBError(rc); return (rc); }
   rc = mob_execute("create table " + table + " ("
				  + "time timestamp,"
				  + "open numeric(8, 5),"
				  + "low numeric(8, 5),"
				  + "high numeric(8, 5),"
				  + "close numeric(8, 5),"
				  + "vol double precision,"
				  + "primary key (time))"); 
   if (rc < 0) { logIfMOBError(rc); return (rc); }
	
	int insertBar = mob_registerStatement("insert into " + table + " values (?, ?, ?, ?, ?, ?)");
   rc = insertBar;
   if (rc < 0) { logIfMOBError(rc); return (rc); }

   double rates[][6];
   rc = ArrayCopyRates(rates);
   if (rc < 0) {
      int ec = GetLastError();
      Print("ArrayCopyRates causess ", ec, ", ", ErrorDescription(ec));
      return (rc);
   }
   rc = mob_copyRates(insertBar, rates, ArraySize(rates), Bars - 10, Bars);
   if (rc < 0) { logIfMOBError(rc); return (rc); }
   
   rc = mob_close();
   if (rc < 0) { logIfMOBError(rc); return (rc); }
//----
   return(0);
  }
//+------------------------------------------------------------------+








<
<

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












|
>

|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

|



2
3
4
5
6
7
8


9

10
11




















12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27































28
29
30
31
32
//|                                                    copyRates.mq4 |
//|                                                     Osamu Nagano |
//|                   http://www.google.com/profiles/onagano.g#about |
//+------------------------------------------------------------------+
#property copyright "Osamu Nagano"
#property link      "http://www.google.com/profiles/onagano.g#about"



#import "mt4odbcbridge.ex4"

string   getDefaultTableName();
int      copyRates(string dsn, string username, string password, string tableName);




















#import

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (-1);
   }
//----
   int rc = 0;
   string tableName = getDefaultTableName();
   
   rc = copyRates("testpostgresql", "testuser", "password", tableName);































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

Changes to experts/scripts/testMT4ODBCBridge.mq4.

2
3
4
5
6
7
8


9
10
11
12
13
14
15
..
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//|                                            testMT4ODBCBridge.mq4 |
//|                                                     Osamu Nagano |
//|                   http://www.google.com/profiles/onagano.g#about |
//+------------------------------------------------------------------+
#property copyright "Osamu Nagano"
#property link      "http://www.google.com/profiles/onagano.g#about"



#import "mt4odbcbridge.ex4"
void     mob_create();
int      mob_open(string dsn, string username, string password);
int      mob_close();
int      mob_isDead();
int		mob_commit();
int		mob_rollback();
................................................................................
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 millis, double vals[]);
int      mob_insertBar(int stmtId, datetime dt, double vals[]);

double   mob_selectDouble(string sql, double defaultVal);
int      mob_selectInt(string sql, int defaultVal);
datetime mob_selectDatetime(string sql, datetime defaultVal);








#import

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----

   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");

      return (0);
   }
   
   int rc = 0;

   
   mob_create();
   
   rc = mob_open("testpostgresql", "testuser", "password");
   if (rc < 0) { logIfError(rc); return (rc); }
   

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


   rc = mob_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 = mob_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 = mob_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 = mob_insertTick(insertTick, dt, millis, vals);
   if (rc < 0) { logIfError(rc); return (rc); }
   
   rc = mob_unregisterStatement(insertTick);
   if (rc < 0) { logIfError(rc); return (rc); }

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

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

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

void logIfError(int rc) {
   if (rc < 0) {
      int eno = mob_getLastErrorNo();
      string emesg = mob_getLastErrorMesg();
      Print("ERROR (", eno, "): ", emesg);
   }
}








>
>







 







>



>
>
>
>
>
>
>
>







<
>
|
<
>
|
|

|
>


<
|
|
<
>
|
<

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

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




<
<
<
<
<
<
<
<
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
..
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








//|                                            testMT4ODBCBridge.mq4 |
//|                                                     Osamu Nagano |
//|                   http://www.google.com/profiles/onagano.g#about |
//+------------------------------------------------------------------+
#property copyright "Osamu Nagano"
#property link      "http://www.google.com/profiles/onagano.g#about"

#import "stdlib.ex4"
string   ErrorDescription(int error_code);
#import "mt4odbcbridge.ex4"
void     mob_create();
int      mob_open(string dsn, string username, string password);
int      mob_close();
int      mob_isDead();
int		mob_commit();
int		mob_rollback();
................................................................................
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 millis, 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   insertBarSQL(string tableName);
string   insertTickSQL(string tableName);
int      checkMOBError(int rc);
int      createTableIfNotExists(string tableName);
int      timeDifference(string dsn, string username, string password);
string   getDefaultTableName();
string   getPeriodSymbol(int period_XX);
int      copyRates(string dsn, string username, string password, string tableName);
#import

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {

   int rc = 0;
   string tableName = getDefaultTableName();

   
   rc = copyRates("testpostgresql", "testuser", "password", tableName);
   
   
   int timeDiff = timeDifference("testpostgresql", "testuser", "password");
   Print("Time diff is ", timeDiff);
   
   mob_create();

   mob_open("testpostgresql", "testuser", "password");
   datetime dtMax = mob_selectDatetime("select max(time) from " + tableName, -1);

   datetime dtMin = mob_selectDatetime("select min(time) from " + tableName, -1);
   mob_close();


   Print("Max date selected ", TimeToStr(dtMax));
   Print("Min date selected ", TimeToStr(dtMin));

   





   datetime dts[];


   ArrayCopySeries(dts, MODE_TIME, NULL, 0);
   int iMax = ArrayBsearch(dts, dtMax, WHOLE_ARRAY, 0 , MODE_DESCEND);
   int iMin = ArrayBsearch(dts, dtMin, WHOLE_ARRAY, 0 , MODE_DESCEND);





   Print("Selected indice max: ", iMax, " and min: ", iMin);
   Print("Max date ", TimeToStr(dts[iMax]));
   Print("Min date ", TimeToStr(dts[iMin]));
   Print("Bars ", Bars);
   


























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