MT4 Files

Check-in [f271f0b88e]
Login

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

Overview
Comment:More sophisticated trade sync feature on both opens and history
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:f271f0b88efd8f3d13a22380aa57b3ef9fa027c4
User & Date: onagano 2011-04-28 10:28:25
Context
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
08:23
Rebind sequence of UIntTimetPointer has been changed check-in: 9e6246a280 user: onagano tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to experts/MOB_executeOrder.mq4.

92
93
94
95
96
97
98
99
100
101
102
103
104
105

106
107
108
109
110
111
112
...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
...
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
...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
...
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
...
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
...
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
...
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989


990
991
992
993
994
995
996
....
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
....
1212
1213
1214
1215
1216
1217
1218


1219
1220
1221
1222
1223
1224
1225
1226
1227
datetime ordExpiration[1];
color    ordArrowColor[1];
int      ordErrorNumber[1];
string   ordErrorMessage = "C2345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      ordErrorMessageSize;

string tradeTable          = "TRADE";
int    truncateTradeStmt   = 0;
int    insertTradeStmt     = 0;
int    updateTradeStmt     = 0;
int    deleteTradeStmt     = 0;
string historyTable        = "HISTORY";
int    truncateHistoryStmt = 0;
int    insertHistoryStmt   = 0;


// 0: ticket, 1: type, 2: close_time
int  tradeList[MAX_ORDER_SIZE][3];
int  tradeListSize = 0;

int      trdTicket[1];
datetime trdOpenTime[1];
................................................................................
   rc = mob_open(ExtDataSourceName, ExtUsername, ExtPassword);
   if (rc < 0) return (rc);

   rc = mob_setAutoCommit(true);
   if (rc < 0) return (rc);
   
   // Preparing order table
   
   orderTable = ExtTablePrefix + orderTable;
   rc = createTableIfNotExists(orderTable, createOrderTableSQL(orderTable));
   if (rc < 0) return (rc);

   // Regstering select order statement
   
   rc = mob_registerStatement(selectOrderSQL(orderTable));
   if (rc < 0) return (rc);
   selectOrderStmt = rc;

   int i = 1;
   mob_bindIntColumn       (selectOrderStmt, i, ordId);              i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordOperation);       i++;
................................................................................
   mob_bindIntColumn       (selectOrderStmt, i, ordMagicNumber);     i++;
   mob_bindDatetimeColumn  (selectOrderStmt, i, ordExpiration);      i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordArrowColor);      i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordErrorNumber);     i++;
   mob_bindStringColumn    (selectOrderStmt, i, ordErrorMessage);    i++;

   // Registering update order statement
   
   rc = mob_registerStatement(updateOrderSQL(orderTable));
   if (rc < 0) return (rc);
   updateOrderStmt = rc;

   i = 1;
   mob_bindIntParameter       (updateOrderStmt, i, ordOperation);       i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordTicket);          i++;
................................................................................
   mob_bindIntParameter       (updateOrderStmt, i, ordMagicNumber);     i++;
   mob_bindDatetimeParameter  (updateOrderStmt, i, ordExpiration);      i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordArrowColor);      i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordErrorNumber);     i++;
   mob_bindStringParameter    (updateOrderStmt, i, ordErrorMessage);    i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordId);              i++;
   

   ordSymbolSize = StringLen(ordSymbol);
   ordCommentSize = StringLen(ordComment);
   ordErrorMessageSize = StringLen(ordErrorMessage);

   // Preparing trade table
   
   tradeTable = ExtTablePrefix + tradeTable;
   rc = createTableIfNotExists(tradeTable, createTradeTableSQL(tradeTable));
   if (rc < 0) return (rc);

   // Registering truncate trade statement
   
   rc = mob_registerStatement(truncateTradeSQL(tradeTable));
   if (rc < 0) return (rc);
   truncateTradeStmt = rc;

   // Registering insert trade statement
   
   rc = mob_registerStatement(insertTradeSQL(tradeTable));
   if (rc < 0) return (rc);
   insertTradeStmt = rc;

   i = 1;
   mob_bindIntParameter       (insertTradeStmt, i, trdTicket);       i++;
   mob_bindDatetimeParameter  (insertTradeStmt, i, trdOpenTime);     i++;
................................................................................
   mob_bindDoubleParameter    (insertTradeStmt, i, trdSwap);         i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (insertTradeStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (insertTradeStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (insertTradeStmt, i, trdComment);      i++;

   // Registering update trade statement

   rc = mob_registerStatement(updateTradeSQL(tradeTable));
   if (rc < 0) return (rc);
   updateTradeStmt = rc;

   i = 1;
   mob_bindDatetimeParameter  (updateTradeStmt, i, trdOpenTime);     i++;
   mob_bindIntParameter       (updateTradeStmt, i, trdType);         i++;
................................................................................
   mob_bindDoubleParameter    (updateTradeStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (updateTradeStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (updateTradeStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (updateTradeStmt, i, trdComment);      i++;
   mob_bindIntParameter       (updateTradeStmt, i, trdTicket);       i++;

   // Registering delete trade statement
   
   rc = mob_registerStatement(deleteTradeSQL(tradeTable));
   if (rc < 0) return (rc);
   deleteTradeStmt = rc;

   i = 1;
   mob_bindIntParameter       (deleteTradeStmt, i, ordTicket);       i++;
   
   // Preparing history table


   

   historyTable = ExtTablePrefix + historyTable;
   rc = createTableIfNotExists(historyTable, createTradeTableSQL(historyTable));
   if (rc < 0) return (rc);

   // Registering truncate history statement
   
   rc = mob_registerStatement(truncateTradeSQL(historyTable));
   if (rc < 0) return (rc);
   truncateHistoryStmt = rc;

   // Registering insert history statement
   
   rc = mob_registerStatement(insertTradeSQL(historyTable));
   if (rc < 0) return (rc);
   insertHistoryStmt = rc;

   i = 1;
   mob_bindIntParameter       (insertHistoryStmt, i, trdTicket);       i++;
   mob_bindDatetimeParameter  (insertHistoryStmt, i, trdOpenTime);     i++;
................................................................................
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdCommission);   i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdSwap);         i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (insertHistoryStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (insertHistoryStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (insertHistoryStmt, i, trdComment);      i++;

   trdSymbolSize = StringLen(trdSymbol);
   trdCommentSize = StringLen(trdComment);



   // Preparing account table


   

   accountTable = ExtTablePrefix + accountTable;
   rc = createTableIfNotExists(accountTable, createAccountTableSQL(accountTable));
   if (rc < 0) return (rc);

   // Registering insert account statement
   
   rc = mob_registerStatement(insertAccountSQL(accountTable));
   if (rc < 0) return (rc);
   insertAccountStmt = rc;

   i = 1;
   mob_bindDatetimeParameter  (insertAccountStmt, i, accServerTime);    i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accBalance);       i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accEquity);        i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accMargin);        i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accFreeMargin);    i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accProfit);        i++;
   mob_bindIntParameter       (insertAccountStmt, i, accOpenTrades);    i++;
   mob_bindIntParameter       (insertAccountStmt, i, accClosedTrades);  i++;
   
   // Syncing trades first time











   
   dumpTradesInTransaction(false);

   fillTradeList();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
................................................................................
      + ", expiration timestamp default '1970-01-01 00:00:00'"
      + ", magic_number integer default 0"
      + ", comment varchar(140) default ''"
      + ")";
   return (sql);
}

string truncateTradeSQL(string tableName) {
   string sql = "truncate table " + tableName;
   return (sql);
}

string deleteTradeSQL(string tableName) {
   string sql = "delete from " + tableName + " where ticket = ?";
   return (sql);
}

string insertTradeSQL(string tableName) {
   string sql = "insert into " + tableName + " ("
................................................................................
      trdMagicNumber[0], ", ",
      trdComment
   );
}

int dumpTrades(bool history) {
   int rc = 0;
   int truncateStmt;
   int insertStmt;
   int n;
   int pool;

   if (history) {
      truncateStmt = truncateHistoryStmt;
      insertStmt = insertHistoryStmt;
      n = OrdersHistoryTotal();
      pool = MODE_HISTORY;
   } else {
      truncateStmt = truncateTradeStmt;
      insertStmt = insertTradeStmt;
      n = OrdersTotal();
      pool = MODE_TRADES;
   }

   rc = mob_executeStatement(truncateStmt);
   if (rc < 0) {
      getDatabaseResponse();
      return (rc);
   }
   for (int i = 0; i < n; i++) {
      if (OrderSelect(i, SELECT_BY_POS, pool)) {
         bindTradeParameters();
         if (ExtDebugPrint) dumpTradeParameters("dumpTrades-" + history);


         rc = mob_executeStatement(insertStmt);
         if (rc < 0) {
            getDatabaseResponse();
            return (rc);
         }
      } else {
         getServerResponse();
................................................................................
      mob_commit();
   }
   mob_setAutoCommit(true);
}

int getTrade() {
   int rc = 0;
   rc = mob_executeStatement(deleteTradeStmt);
   // The row doesn't need to exist already.
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
      bindTradeParameters();
      if (ExtDebugPrint) dumpTradeParameters("getTrade");
      rc = mob_executeStatement(insertTradeStmt);
      if (rc < 0) {
         getDatabaseResponse();
         return (rc);
................................................................................
   // Check if a new order
   int n = OrdersTotal();
   for (i = 0; i < n; i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (isNewOrder(OrderTicket())) {
         bindTradeParameters();
         if (ExtDebugPrint) dumpTradeParameters("syncTrades-new");


         rc = mob_executeStatement(insertTradeStmt);
         if (rc < 0) return (rc);
      }
   }

   fillTradeList();
   return (rc);
}








<




<

>







 







<




|
<







 







<







 







>





<




<
<
<
<
<
<

<







 







<







 







<






|
|
>
>
|
>




<
<
<
<
<
<

<







 







|
|
>
>

<
>
>
|
>





<













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







 







<
<
<
<
<







 







|





|




|





<
<
<
<
<




>
>







 







|
<







 







>
>









92
93
94
95
96
97
98

99
100
101
102

103
104
105
106
107
108
109
110
111
...
154
155
156
157
158
159
160

161
162
163
164
165

166
167
168
169
170
171
172
...
184
185
186
187
188
189
190

191
192
193
194
195
196
197
...
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
...
242
243
244
245
246
247
248

249
250
251
252
253
254
255
...
265
266
267
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
...
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
...
859
860
861
862
863
864
865





866
867
868
869
870
871
872
...
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972





973
974
975
976
977
978
979
980
981
982
983
984
985
...
998
999
1000
1001
1002
1003
1004
1005

1006
1007
1008
1009
1010
1011
1012
....
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
datetime ordExpiration[1];
color    ordArrowColor[1];
int      ordErrorNumber[1];
string   ordErrorMessage = "C2345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      ordErrorMessageSize;

string tradeTable          = "TRADE";

int    insertTradeStmt     = 0;
int    updateTradeStmt     = 0;
int    deleteTradeStmt     = 0;
string historyTable        = "HISTORY";

int    insertHistoryStmt   = 0;
int    deleteHistoryStmt   = 0;

// 0: ticket, 1: type, 2: close_time
int  tradeList[MAX_ORDER_SIZE][3];
int  tradeListSize = 0;

int      trdTicket[1];
datetime trdOpenTime[1];
................................................................................
   rc = mob_open(ExtDataSourceName, ExtUsername, ExtPassword);
   if (rc < 0) return (rc);

   rc = mob_setAutoCommit(true);
   if (rc < 0) return (rc);
   
   // Preparing order table

   orderTable = ExtTablePrefix + orderTable;
   rc = createTableIfNotExists(orderTable, createOrderTableSQL(orderTable));
   if (rc < 0) return (rc);

   // Regstering select order statement   

   rc = mob_registerStatement(selectOrderSQL(orderTable));
   if (rc < 0) return (rc);
   selectOrderStmt = rc;

   int i = 1;
   mob_bindIntColumn       (selectOrderStmt, i, ordId);              i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordOperation);       i++;
................................................................................
   mob_bindIntColumn       (selectOrderStmt, i, ordMagicNumber);     i++;
   mob_bindDatetimeColumn  (selectOrderStmt, i, ordExpiration);      i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordArrowColor);      i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordErrorNumber);     i++;
   mob_bindStringColumn    (selectOrderStmt, i, ordErrorMessage);    i++;

   // Registering update order statement

   rc = mob_registerStatement(updateOrderSQL(orderTable));
   if (rc < 0) return (rc);
   updateOrderStmt = rc;

   i = 1;
   mob_bindIntParameter       (updateOrderStmt, i, ordOperation);       i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordTicket);          i++;
................................................................................
   mob_bindIntParameter       (updateOrderStmt, i, ordMagicNumber);     i++;
   mob_bindDatetimeParameter  (updateOrderStmt, i, ordExpiration);      i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordArrowColor);      i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordErrorNumber);     i++;
   mob_bindStringParameter    (updateOrderStmt, i, ordErrorMessage);    i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordId);              i++;
   
   // Storing string buffer size for order table
   ordSymbolSize = StringLen(ordSymbol);
   ordCommentSize = StringLen(ordComment);
   ordErrorMessageSize = StringLen(ordErrorMessage);

   // Preparing trade table

   tradeTable = ExtTablePrefix + tradeTable;
   rc = createTableIfNotExists(tradeTable, createTradeTableSQL(tradeTable));
   if (rc < 0) return (rc);







   // Registering insert trade statement

   rc = mob_registerStatement(insertTradeSQL(tradeTable));
   if (rc < 0) return (rc);
   insertTradeStmt = rc;

   i = 1;
   mob_bindIntParameter       (insertTradeStmt, i, trdTicket);       i++;
   mob_bindDatetimeParameter  (insertTradeStmt, i, trdOpenTime);     i++;
................................................................................
   mob_bindDoubleParameter    (insertTradeStmt, i, trdSwap);         i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (insertTradeStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (insertTradeStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (insertTradeStmt, i, trdComment);      i++;

   // Registering update trade statement

   rc = mob_registerStatement(updateTradeSQL(tradeTable));
   if (rc < 0) return (rc);
   updateTradeStmt = rc;

   i = 1;
   mob_bindDatetimeParameter  (updateTradeStmt, i, trdOpenTime);     i++;
   mob_bindIntParameter       (updateTradeStmt, i, trdType);         i++;
................................................................................
   mob_bindDoubleParameter    (updateTradeStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (updateTradeStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (updateTradeStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (updateTradeStmt, i, trdComment);      i++;
   mob_bindIntParameter       (updateTradeStmt, i, trdTicket);       i++;

   // Registering delete trade statement

   rc = mob_registerStatement(deleteTradeSQL(tradeTable));
   if (rc < 0) return (rc);
   deleteTradeStmt = rc;

   i = 1;
   mob_bindIntParameter       (deleteTradeStmt, i, ordTicket);       i++;

   // Storing string buffer size for trade table
   trdSymbolSize = StringLen(trdSymbol);
   trdCommentSize = StringLen(trdComment);

   // Preparing history table
   historyTable = ExtTablePrefix + historyTable;
   rc = createTableIfNotExists(historyTable, createTradeTableSQL(historyTable));
   if (rc < 0) return (rc);







   // Registering insert history statement

   rc = mob_registerStatement(insertTradeSQL(historyTable));
   if (rc < 0) return (rc);
   insertHistoryStmt = rc;

   i = 1;
   mob_bindIntParameter       (insertHistoryStmt, i, trdTicket);       i++;
   mob_bindDatetimeParameter  (insertHistoryStmt, i, trdOpenTime);     i++;
................................................................................
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdCommission);   i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdSwap);         i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (insertHistoryStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (insertHistoryStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (insertHistoryStmt, i, trdComment);      i++;

   // Registering delete history statement
   rc = mob_registerStatement(deleteTradeSQL(historyTable));
   if (rc < 0) return (rc);
   deleteHistoryStmt = rc;


   i = 1;
   mob_bindIntParameter       (deleteHistoryStmt, i, ordTicket);       i++;

   // Preparing account table
   accountTable = ExtTablePrefix + accountTable;
   rc = createTableIfNotExists(accountTable, createAccountTableSQL(accountTable));
   if (rc < 0) return (rc);

   // Registering insert account statement

   rc = mob_registerStatement(insertAccountSQL(accountTable));
   if (rc < 0) return (rc);
   insertAccountStmt = rc;

   i = 1;
   mob_bindDatetimeParameter  (insertAccountStmt, i, accServerTime);    i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accBalance);       i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accEquity);        i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accMargin);        i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accFreeMargin);    i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accProfit);        i++;
   mob_bindIntParameter       (insertAccountStmt, i, accOpenTrades);    i++;
   mob_bindIntParameter       (insertAccountStmt, i, accClosedTrades);  i++;

   // Move closed trades from trade table to history table
   string whereClause = " where close_time > '1970-01-01 00:00:00'";
   mob_setAutoCommit(false);
   rc = mob_execute("delete from " + historyTable
      + " where ticket in (select ticket from " + tradeTable + whereClause + ")");
   if (rc < 0) { mob_rollback(); mob_setAutoCommit(true); return (rc); }
   rc = mob_execute("insert into " + historyTable + " select * from " + tradeTable + whereClause);
   if (rc < 0) { mob_rollback(); mob_setAutoCommit(true); return (rc); }
   rc = mob_execute("delete from " + tradeTable + whereClause);
   if (rc < 0) { mob_rollback(); mob_setAutoCommit(true); return (rc); }
   mob_commit();
   mob_setAutoCommit(true);


   // To sync trades first time
   tradeListSize = 0;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
................................................................................
      + ", expiration timestamp default '1970-01-01 00:00:00'"
      + ", magic_number integer default 0"
      + ", comment varchar(140) default ''"
      + ")";
   return (sql);
}






string deleteTradeSQL(string tableName) {
   string sql = "delete from " + tableName + " where ticket = ?";
   return (sql);
}

string insertTradeSQL(string tableName) {
   string sql = "insert into " + tableName + " ("
................................................................................
      trdMagicNumber[0], ", ",
      trdComment
   );
}

int dumpTrades(bool history) {
   int rc = 0;
   int deleteStmt;
   int insertStmt;
   int n;
   int pool;

   if (history) {
      deleteStmt = deleteHistoryStmt;
      insertStmt = insertHistoryStmt;
      n = OrdersHistoryTotal();
      pool = MODE_HISTORY;
   } else {
      deleteStmt = deleteTradeStmt;
      insertStmt = insertTradeStmt;
      n = OrdersTotal();
      pool = MODE_TRADES;
   }






   for (int i = 0; i < n; i++) {
      if (OrderSelect(i, SELECT_BY_POS, pool)) {
         bindTradeParameters();
         if (ExtDebugPrint) dumpTradeParameters("dumpTrades-" + history);
         ordTicket[0] = trdTicket[0];
         mob_executeStatement(deleteStmt); // ignore result
         rc = mob_executeStatement(insertStmt);
         if (rc < 0) {
            getDatabaseResponse();
            return (rc);
         }
      } else {
         getServerResponse();
................................................................................
      mob_commit();
   }
   mob_setAutoCommit(true);
}

int getTrade() {
   int rc = 0;
   mob_executeStatement(deleteTradeStmt); // ignore result

   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
      bindTradeParameters();
      if (ExtDebugPrint) dumpTradeParameters("getTrade");
      rc = mob_executeStatement(insertTradeStmt);
      if (rc < 0) {
         getDatabaseResponse();
         return (rc);
................................................................................
   // Check if a new order
   int n = OrdersTotal();
   for (i = 0; i < n; i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (isNewOrder(OrderTicket())) {
         bindTradeParameters();
         if (ExtDebugPrint) dumpTradeParameters("syncTrades-new");
         ordTicket[0] = trdTicket[0];
         mob_executeStatement(deleteTradeStmt); // ignore result
         rc = mob_executeStatement(insertTradeStmt);
         if (rc < 0) return (rc);
      }
   }

   fillTradeList();
   return (rc);
}