MT4 Files

Check-in [e7660d18ad]
Login

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

Overview
Comment:Added syncTrades function
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:e7660d18ad0ea5e6e721b16c7412a68c1afabf2d
User & Date: onagano 2011-04-26 05:47:17
Context
2011-04-28
01:49
Fixed the gap of time_t and unsigned int check-in: 09e5d54256 user: onagano tags: trunk
2011-04-26
05:47
Added syncTrades function check-in: e7660d18ad user: onagano tags: trunk
02:57
Added slightly fixed version of CounterADD2 EA check-in: 40ff258d16 user: onagano tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to experts/MOB_executeOrder.mq4.

55
56
57
58
59
60
61

62
63
64
65
66
67
68
..
92
93
94
95
96
97
98

99
100
101
102
103
104




105
106
107
108
109
110
111
...
247
248
249
250
251
252
253
























254
255
256
257
258
259
260
...
346
347
348
349
350
351
352





353
354
355
356
357
358
359
...
366
367
368
369
370
371
372





373
374
375
376
377
378
379
380
...
864
865
866
867
868
869
870





















871
872
873
874
875
876
877
....
1121
1122
1123
1124
1125
1126
1127
1128



















































#define OPE_DUMP_TRADES    6
#define OPE_DUMP_HISTORY   7
#define OPE_GET_ACCOUNT    8
#define OPE_GET_STATUS     9
#define OPE_COPY_RATES     10

#define ORD_DEFAULT_SLIPPAGE 3


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

................................................................................
int      ordErrorNumber[1];
string   ordErrorMessage = "C2345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      ordErrorMessageSize;

string tradeTable          = "TRADE";
int    truncateTradeStmt   = 0;
int    insertTradeStmt     = 0;

string historyTable        = "HISTORY";
int    truncateHistoryStmt = 0;
int    insertHistoryStmt   = 0;
string statusTable         = "STATUS";
int    deleteStatusStmt    = 0;
int    insertStatusStmt    = 0;





int      trdTicket[1];
datetime trdOpenTime[1];
int      trdType[1];
double   trdLots[1];
string   trdSymbol = "D234567890";
int      trdSymbolSize;
................................................................................
   mob_bindDoubleParameter    (insertTradeStmt, i, trdCommission);   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++;

























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

   // Registering truncate history statement
................................................................................
   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++;





//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
................................................................................
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----





   int rc = selectOrders();
   if (rc < 0) return (rc);
//----
   return(0);
  }
//+------------------------------------------------------------------+

string createOrderTableSQL(string tableName) {
................................................................................
      + ", profit"
      + ", expiration"
      + ", magic_number"
      + ", comment"
      + ") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
   return (sql);
}






















void bindTradeParameters() {
   trdTicket[0]      = OrderTicket();
   trdOpenTime[0]    = OrderOpenTime();
   trdType[0]        = OrderType();
   trdLots[0]        = OrderLots();
   mob_strcpy(trdSymbol, trdSymbolSize, OrderSymbol());
................................................................................
   }

   mob_setAutoCommit(true);
   mob_unregisterStatement(insertBarStmt);

   return(rv);
}



























































>







 







>






>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>







 







>
>
>
>
>
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
..
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
...
253
254
255
256
257
258
259
260
261
262
263
264
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
...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
...
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
...
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
....
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
#define OPE_DUMP_TRADES    6
#define OPE_DUMP_HISTORY   7
#define OPE_GET_ACCOUNT    8
#define OPE_GET_STATUS     9
#define OPE_COPY_RATES     10

#define ORD_DEFAULT_SLIPPAGE 3
#define MAX_ORDER_SIZE       100

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

................................................................................
int      ordErrorNumber[1];
string   ordErrorMessage = "C2345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      ordErrorMessageSize;

string tradeTable          = "TRADE";
int    truncateTradeStmt   = 0;
int    insertTradeStmt     = 0;
int    updateTradeStmt     = 0;
string historyTable        = "HISTORY";
int    truncateHistoryStmt = 0;
int    insertHistoryStmt   = 0;
string statusTable         = "STATUS";
int    deleteStatusStmt    = 0;
int    insertStatusStmt    = 0;

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

int      trdTicket[1];
datetime trdOpenTime[1];
int      trdType[1];
double   trdLots[1];
string   trdSymbol = "D234567890";
int      trdSymbolSize;
................................................................................
   mob_bindDoubleParameter    (insertTradeStmt, i, trdCommission);   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, trdLots);         i++;
   mob_bindStringParameter    (updateTradeStmt, i, trdSymbol);       i++;
   mob_bindDoubleParameter    (updateTradeStmt, i, trdOpenPrice);    i++;
   mob_bindDoubleParameter    (updateTradeStmt, i, trdStopLoss);     i++;
   mob_bindDoubleParameter    (updateTradeStmt, i, trdTakeProfit);   i++;
   mob_bindDatetimeParameter  (updateTradeStmt, i, trdCloseTime);    i++;
   mob_bindDoubleParameter    (updateTradeStmt, i, trdClosePrice);   i++;
   mob_bindDoubleParameter    (updateTradeStmt, i, trdCommission);   i++;
   mob_bindDoubleParameter    (updateTradeStmt, i, trdSwap);         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++;
   
   // Preparing history table
   
   historyTable = ExtTablePrefix + historyTable;
   rc = createTableIfNotExists(historyTable, createTradeTableSQL(historyTable));
   if (rc < 0) return (rc);

   // Registering truncate history statement
................................................................................
   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()
................................................................................
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int rc = 0;
   
   rc = syncTrades();
   if (rc < 0) return (rc);
   
   rc = selectOrders();
   if (rc < 0) return (rc);
//----
   return(0);
  }
//+------------------------------------------------------------------+

string createOrderTableSQL(string tableName) {
................................................................................
      + ", profit"
      + ", expiration"
      + ", magic_number"
      + ", comment"
      + ") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
   return (sql);
}

string updateTradeSQL(string tableName) {
   string sql = "update " + tableName + " set"
      + "  open_time = ?"
      + ", type = ?"
      + ", lots = ?"
      + ", symbol = ?"
      + ", open_price = ?"
      + ", stop_loss = ?"
      + ", take_profit = ?"
      + ", close_time = ?"
      + ", close_price = ?"
      + ", commission = ?"
      + ", swap = ?"
      + ", profit = ?"
      + ", expiration = ?"
      + ", magic_number = ?"
      + ", comment = ?"
      + " where ticket = ?";
   return (sql);
}

void bindTradeParameters() {
   trdTicket[0]      = OrderTicket();
   trdOpenTime[0]    = OrderOpenTime();
   trdType[0]        = OrderType();
   trdLots[0]        = OrderLots();
   mob_strcpy(trdSymbol, trdSymbolSize, OrderSymbol());
................................................................................
   }

   mob_setAutoCommit(true);
   mob_unregisterStatement(insertBarStmt);

   return(rv);
}

void fillTradeList() {
   tradeListSize = OrdersTotal();
   for (int i = 0; i < tradeListSize; i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      tradeList[i][0] = OrderTicket();
      tradeList[i][1] = OrderType();
      tradeList[i][2] = OrderCloseTime();
   }
}

bool isNewOrder(int ticket) {
   for (int i = 0; i < tradeListSize; i++) {
      if (ticket == tradeList[i][0]) return (false);
   }
   return (true);
}

int syncTrades() {
   int rc = 0;
   int i = 0;
   
   for (i = 0; i < tradeListSize; i++) {
      int ticket = tradeList[i][0];
      int odtype = tradeList[i][1];
      int closet = tradeList[i][2];
      OrderSelect(ticket, SELECT_BY_TICKET);
      // Check if closed or a pending order has been hit
      if (closet != OrderCloseTime() || odtype != OrderType()) {
         bindTradeParameters();
         dumpTradeParameters("syncTrades-close-or-hit");
         rc = mob_executeStatement(updateTradeStmt);
         if (rc < 0) 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();
         dumpTradeParameters("syncTrades-new");
         rc = mob_executeStatement(insertTradeStmt);
         if (rc < 0) return (rc);
      }
   }

   fillTradeList();
   return (rc);
}