MT4 Files

Check-in [531bc34670]
Login

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

Overview
Comment:Added account info dumping function
Timelines: family | ancestors | descendants | both | exec_order
Files: files | file ages | folders
SHA1:531bc34670a37814983c0f937e4bb421c7140427
User & Date: onagano 2011-04-07 09:09:50
Context
2011-04-08
07:39
H2 Database confirmation check-in: f82e40cb08 user: onagano tags: exec_order
2011-04-07
09:09
Added account info dumping function check-in: 531bc34670 user: onagano tags: exec_order
07:47
Added trade dump function check-in: cc5604eee9 user: onagano tags: exec_order
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to experts/MOB_executeOrder.mq4.

110
111
112
113
114
115
116












117
118
119
120
121
122
123
...
216
217
218
219
220
221
222






























223
224


















225
226
227
228
229
230
231
...
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
...
345
346
347
348
349
350
351
352






353
354
355
356
357
358
359
...
592
593
594
595
596
597
598
599
600




601
602
603
604
605




606
607
608
609




















610
611












612
613
614






























double   trdCommission[1];
double   trdSwap[1];
double   trdProfit[1];
datetime trdExpiration[1];
int      trdMagicNumber[1];
string   trdComment = "12345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      trdCommentSize;













//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (!IsDllsAllowed()) {
................................................................................
   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++;































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


















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

int selectOrders() {
   int rc = 0;

   rc = checkMOBError(mob_selectToFetch(selectOrderStmt));
   while (checkMOBError(mob_fetch(selectOrderStmt)) != 0) {
      Print("Fetched: ID ", ordId[0]);
      
      switch (ordOperation[0]) {
      case OPE_SEND:
         orderSend();
         break;
      case OPE_CLOSE:
         orderClose();
         break;
................................................................................
      case OPE_DELETE:
         orderDelete();
         break;
      case OPE_MODIFY:
         orderModify();
         break;
      case OPE_DUMP_TRADES:
         dumpTrades(false);






         break;
      default:
         Print("Unknown operation: ", ordOperation[0]);
      }
      
      ordOperation[0] = - ordOperation[0];
      rc = checkMOBError(mob_executeStatement(updateOrderStmt));
................................................................................
   } else {
      truncateStmt = truncateTradeStmt;
      insertStmt = insertTradeStmt;
      n = OrdersTotal();
      pool = MODE_TRADES;
   }

   //checkMOBError(mob_setAutoCommit(false));
   checkMOBError(mob_executeStatement(truncateStmt));




   for (int i = 0; i < n; i++) {
      if (OrderSelect(i, SELECT_BY_POS, pool)) {
         bindTradeParameters();
         Print(TimeToStr(trdOpenTime[0]), ", ", TimeToStr(trdCloseTime[0]), ", ", TimeToStr(trdExpiration[0]));
         checkMOBError(mob_executeStatement(insertStmt));




      } else {
         getServerResponse();
      }
   }




















   //checkMOBError(mob_setAutoCommit(true));
   












   return (rc);
}






































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







 







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


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







 







<
|







 







|
>
>
>
>
>
>







 







<
|
>
>
>
>



<
|
>
>
>
>




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


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
...
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
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
291
...
386
387
388
389
390
391
392

393
394
395
396
397
398
399
400
...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
...
657
658
659
660
661
662
663

664
665
666
667
668
669
670
671

672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
double   trdCommission[1];
double   trdSwap[1];
double   trdProfit[1];
datetime trdExpiration[1];
int      trdMagicNumber[1];
string   trdComment = "12345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      trdCommentSize;

string accountTable     = "ACCOUNT";
int insertAccountStmt   = 0;

datetime accServerTime[1];
double   accBalance[1];
double   accEquity[1];
double   accMargin[1];
double   accFreeMargin[1];
double   accProfit[1];
int      accOpenTrades[1];
int      accClosedTrades[1];

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (!IsDllsAllowed()) {
................................................................................
   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++;

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

   rc = checkMOBError(mob_registerStatement(truncateTradeSQL(historyTable)));
   if (rc < 0) return (rc);
   truncateHistoryStmt = rc;

   rc = checkMOBError(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_bindIntParameter       (insertHistoryStmt, i, trdType);         i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdLots);         i++;
   mob_bindStringParameter    (insertHistoryStmt, i, trdSymbol);       i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdOpenPrice);    i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdStopLoss);     i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdTakeProfit);   i++;
   mob_bindDatetimeParameter  (insertHistoryStmt, i, trdCloseTime);    i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdClosePrice);   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);

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

   rc = checkMOBError(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++;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
................................................................................
}

int selectOrders() {
   int rc = 0;

   rc = checkMOBError(mob_selectToFetch(selectOrderStmt));
   while (checkMOBError(mob_fetch(selectOrderStmt)) != 0) {

   
      switch (ordOperation[0]) {
      case OPE_SEND:
         orderSend();
         break;
      case OPE_CLOSE:
         orderClose();
         break;
................................................................................
      case OPE_DELETE:
         orderDelete();
         break;
      case OPE_MODIFY:
         orderModify();
         break;
      case OPE_DUMP_TRADES:
         dumpTradesInTransaction(false);
         break;
      case OPE_DUMP_HISTORY:
         dumpTradesInTransaction(true);
         break;
      case OPE_DUMP_ACCOUNT:
         insertAccount();
         break;
      default:
         Print("Unknown operation: ", ordOperation[0]);
      }
      
      ordOperation[0] = - ordOperation[0];
      rc = checkMOBError(mob_executeStatement(updateOrderStmt));
................................................................................
   } else {
      truncateStmt = truncateTradeStmt;
      insertStmt = insertTradeStmt;
      n = OrdersTotal();
      pool = MODE_TRADES;
   }


   rc = checkMOBError(mob_executeStatement(truncateStmt));
   if (rc < 0) {
      getDatabaseResponse();
      return (rc);
   }
   for (int i = 0; i < n; i++) {
      if (OrderSelect(i, SELECT_BY_POS, pool)) {
         bindTradeParameters();

         rc = checkMOBError(mob_executeStatement(insertStmt));
         if (rc < 0) {
            getDatabaseResponse();
            return (rc);
         }
      } else {
         getServerResponse();
      }
   }
   
   return (rc);
}

void getDatabaseResponse() {
   int err = mob_getLastErrorNo();
   string errMesg = mob_getLastErrorMesg();
   ordErrorNumber[0] = err;
   mob_strcpy(ordErrorMessage, ordErrorMessageSize, errMesg);
   //Print("#", err, ":", ordErrorMessage, "(", StringLen(ordErrorMessage), ")");
}

void dumpTradesInTransaction(bool history) {
   mob_setAutoCommit(false);
   int rc = dumpTrades(history);
   if (rc < 0) {
      mob_rollback();
   } else {
      mob_commit();
   }
   mob_setAutoCommit(true);
}

string createAccountTableSQL(string tableName) {
   string sql = "create table " + tableName + " ("
      + "  server_time timestamp with time zone default '1970-01-01 00:00:00-00' primary key"
      + ", balance double precision"
      + ", equity double precision"
      + ", margin double precision"
      + ", free_margin double precision"
      + ", profit double precision"
      + ", open_trades integer"
      + ", closed_trades integer"
      + ")";
   return (sql);
}

string insertAccountSQL(string tableName) {
   string sql = "insert into " + tableName + " ("
      + "  server_time"
      + ", balance"
      + ", equity"
      + ", margin"
      + ", free_margin"
      + ", profit"
      + ", open_trades"
      + ", closed_trades"
      + ") values (?, ?, ?, ?, ?, ?, ?, ?)";
   return (sql);
}

void insertAccount() {
   accServerTime[0]     = TimeCurrent();
   accBalance[0]        = AccountBalance();
   accEquity[0]         = AccountEquity();
   accMargin[0]         = AccountMargin();
   accFreeMargin[0]     = AccountFreeMargin();
   accProfit[0]         = AccountProfit();
   accOpenTrades[0]     = OrdersTotal();
   accClosedTrades[0]   = OrdersHistoryTotal();

   int rc = checkMOBError(mob_executeStatement(insertAccountStmt));
   if (rc < 0) {
      getDatabaseResponse();
   }
}