MT4 Files

Check-in [ab4b49a269]
Login

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

Overview
Comment:getStatus now uses trade table, not status table
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:ab4b49a269effdbb08ef36908fa8aba92826ba46
User & Date: onagano 2011-04-28 03:35:59
Context
2011-04-28
07:53
Changed default database to fxddx from testh2db check-in: 44f203c90d user: onagano tags: trunk
03:35
getStatus now uses trade table, not status table check-in: ab4b49a269 user: onagano tags: trunk
01:49
Fixed the gap of time_t and unsigned int check-in: 09e5d54256 user: onagano tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to experts/MOB_executeOrder.mq4.

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

69
70
71
72
73
74
75
..
94
95
96
97
98
99
100

101
102
103
104
105
106
107
108
109
110
111
112
113
...
276
277
278
279
280
281
282









283
284
285
286
287
288
289
...
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
...
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
...
490
491
492
493
494
495
496














497
498
499
500
501
502
503
...
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545


546
547
548
549
550
551


552
553
554
555
556
557





558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
...
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
...
668
669
670
671
672
673
674
675
676
677

678
679
680
681
682
683
684
...
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
748
749
750
751
752
...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
...
842
843
844
845
846
847
848
849

850
851
852
853
854
855
856
...
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
....
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033

1034
1035
1036
1037
1038
1039
1040
1041
....
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165

1166
1167
1168
1169
1170
1171
1172
1173
1174
....
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_MODIFY         5
#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_";


//--- global variables
string orderTable       = "ORDER";
int    selectOrderStmt  = 0;
int    updateOrderStmt  = 0;

int      ordId[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];
................................................................................
   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);

................................................................................
   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++;

   // Preparing status table

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

   // Registering delete status statement
   
   rc = mob_registerStatement(deleteTradeSQL(statusTable));
   if (rc < 0) return (rc);
   deleteStatusStmt = rc;

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

   // Registering insert status statement
   
   rc = mob_registerStatement(insertTradeSQL(statusTable));
   if (rc < 0) return (rc);
   insertStatusStmt = rc;

   i = 1;
   mob_bindIntParameter       (insertStatusStmt, i, trdTicket);       i++;
   mob_bindDatetimeParameter  (insertStatusStmt, i, trdOpenTime);     i++;
   mob_bindIntParameter       (insertStatusStmt, i, trdType);         i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdLots);         i++;
   mob_bindStringParameter    (insertStatusStmt, i, trdSymbol);       i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdOpenPrice);    i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdStopLoss);     i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdTakeProfit);   i++;
   mob_bindDatetimeParameter  (insertStatusStmt, i, trdCloseTime);    i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdClosePrice);   i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdCommission);   i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdSwap);         i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (insertStatusStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (insertStatusStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (insertStatusStmt, i, trdComment);      i++;

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

   // Preparing account table
   
   accountTable = ExtTablePrefix + accountTable;
   rc = createTableIfNotExists(accountTable, createAccountTableSQL(accountTable));
................................................................................
//| 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) {
................................................................................
      + ", expiration = ?"
      + ", arrow_color = ?"
      + ", error_number = ?"
      + ", error_message = ?"
      + " where id = ?";
   return (sql);
}















int selectOrders() {
   int rc = 0;

   rc = mob_selectToFetch(selectOrderStmt);
   if (rc < 0) return (rc);
   
................................................................................
      case OPE_GET_STATUS:
         getStatusInTransaction();
         break;
      case OPE_COPY_RATES:
         copyRates();
         break;
      default:
         string mesg = "Unknown operation: " + ordOperation[0];
         Print(mesg);
         ordErrorNumber[0] = -1;
         mob_strcpy(ordErrorMessage, ordErrorMessageSize, mesg);
      }
      
      ordOperation[0] = - ordOperation[0];
      Print("Operation ", - ordOperation[0], " done with #", ordErrorNumber[0], " (", ordErrorMessage, ")");



      rc = mob_executeStatement(updateOrderStmt);
      if (rc < 0) {
         mob_fetchedAll(selectOrderStmt);
         return (rc);
      }


   }
   
   mob_fetchedAll(selectOrderStmt);
   
   return (rc);
}






void getServerResponse() {
   int err = GetLastError();
   string errMesg = ErrorDescription(err);
   ordErrorNumber[0] = err;
   mob_strcpy(ordErrorMessage, ordErrorMessageSize, errMesg);
   Print("Server error #", err, ": ", ordErrorMessage, "(", StringLen(ordErrorMessage), ")");
}

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

void orderSend() {
   double margin;
   double thePip    = getPipSize(ordSymbol);
   int    theDigits = MarketInfo(ordSymbol, MODE_DIGITS);
   
................................................................................
      case OP_SELLLIMIT:
      case OP_SELLSTOP:
         ordTakeProfit[0] = NormalizeDouble(ordOpenPrice[0] - margin, theDigits);
         break;
      }
   }
   
   Print("Sending ",
      ordSymbol, ", ",
      ordType[0], ", ",
      ordLots[0], ", ",
      ordOpenPrice[0], ", ",
      ordSlippage[0], ", ",
      ordStopLoss[0], ", ",
      ordTakeProfit[0], ", ",
................................................................................
      ordMagicNumber[0],
      ordExpiration[0],
      ordArrowColor[0]
   );
   
   if (ticket == -1) {
      getServerResponse();
   } else {
      ordTicket[0] = ticket;
   }

}

void orderClose() {
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
      // close at market price
      if (ordClosePrice[0] == 0.0) {
         switch (OrderType()) {
................................................................................
         }
      }
      // close all position
      if (ordLots[0] == 0.0) {
         ordLots[0] = OrderLots();
      }
      
      Print("Closing ",
         ordTicket[0], ", ",
         ordLots[0], ", ",
         ordClosePrice[0], ", ",
         ordSlippage[0], ", ",
         ordArrowColor[0]
      );
      
      // closing
      if (OrderClose(
         ordTicket[0],
         ordLots[0],
         ordClosePrice[0],
         ordSlippage[0],
         ordArrowColor[0])) {

      } else {
         getServerResponse();
      }
   } else { // cannot select order
      getServerResponse();
   }
}

void orderCloseBy() {
   Print("Closing-by ",
      ordTicket[0], ", ",
      ordOppositeTicket[0], ", ",
      ordArrowColor[0]
   );
   
   if (OrderCloseBy(
      ordTicket[0],
      ordOppositeTicket[0],
      ordArrowColor[0])) {

   } else {
      getServerResponse();
   }
}

void orderDelete() {
   Print("Deleting ",
      ordTicket[0], ", ",
      ordArrowColor[0]
   );
   
   if (OrderDelete(
      ordTicket[0],
      ordArrowColor[0])) {

   } else {
      getServerResponse();
   }
}

void orderModify() {
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
................................................................................
         }
      }
      
      if (ordExpiration[0] == 0) {
         ordExpiration[0] = OrderExpiration();
      }
      
      Print("Modifying ",
         ordTicket[0], ", ",
         ordOpenPrice[0], ", ",
         ordStopLoss[0], ", ",
         ordTakeProfit[0], ", ",
         TimeToStr(ordExpiration[0]), ", ",
         ordArrowColor[0]
      );
................................................................................
      // modifying
      if (OrderModify(
         ordTicket[0],
         ordOpenPrice[0],
         ordStopLoss[0],
         ordTakeProfit[0],
         ordExpiration[0],
         ordArrowColor[0])) {

      } else {
         getServerResponse();
      }
   } else { // cannot select order
      getServerResponse();
   }
}
................................................................................
   if (rc < 0) {
      getDatabaseResponse();
      return (rc);
   }
   for (int i = 0; i < n; i++) {
      if (OrderSelect(i, SELECT_BY_POS, pool)) {
         bindTradeParameters();
         dumpTradeParameters("dumpTrades-" + history);
         rc = mob_executeStatement(insertStmt);
         if (rc < 0) {
            getDatabaseResponse();
            return (rc);
         }
      } else {
         getServerResponse();
................................................................................
      mob_commit();
   }
   mob_setAutoCommit(true);
}

int getStatus() {
   int rc = 0;
   rc = mob_executeStatement(deleteStatusStmt);
   if (rc < 0) {
      getDatabaseResponse();
      return (rc);
   }
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
      bindTradeParameters();

      rc = mob_executeStatement(insertStatusStmt);
      if (rc < 0) {
         getDatabaseResponse();
         return (rc);
      }
   } else {
      getServerResponse();
   }
................................................................................
      rv = copyRatesBetween(tableName, start, end, symbol, period, rates);
   }
   if (rv < 0) {
      getDatabaseResponse();
      return;
   }
   
   ordErrorNumber[0] = rv;
   string mesg = "Copied " + rv + " bars";
   if (rv > 0) {
      mesg = mesg + " from " + TimeToStr(rates[nBars - start - 1][0]) + " to " + TimeToStr(rates[nBars - end][0]);

   }
   mob_strcpy(ordErrorMessage, ordErrorMessageSize, mesg);
}

int copyRatesBetween(string tableName, int start, int end, string symbol, int period, double& rates[]) {
   int rv = 0;
   
   int insertBarStmt = mob_registerStatement(insertBarSQL(tableName));
   mob_setAutoCommit(false);
................................................................................
      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);
}








|
|






>







 







>



<
<
<







 







>
>
>
>
>
>
>
>
>







 







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







 







|

|
|







 







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







 







|
<
<
<



|
>
>
|





>
>






>
>
>
>
>




|
<
<





|
<
<







 







|







 







<
<

>







 







|













|
>









|








|
>






|






|
>







 







|







 







|
>







 







|







 







|
|
<
<
<


>
|







 







<


|
>

|







 







|











|









54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
..
95
96
97
98
99
100
101
102
103
104
105



106
107
108
109
110
111
112
...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
...
321
322
323
324
325
326
327







































328
329
330
331
332
333
334
...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
...
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
...
514
515
516
517
518
519
520
521



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551


552
553
554
555
556
557


558
559
560
561
562
563
564
...
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
...
653
654
655
656
657
658
659


660
661
662
663
664
665
666
667
668
...
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
...
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
...
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
...
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
....
1008
1009
1010
1011
1012
1013
1014
1015
1016



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
....
1141
1142
1143
1144
1145
1146
1147

1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
....
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
#define OPE_MODIFY         5
#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_";
extern bool      ExtDebugPrint     = true;

//--- global variables
string orderTable       = "ORDER";
int    selectOrderStmt  = 0;
int    updateOrderStmt  = 0;

int      ordId[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];
................................................................................
   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++;

   // 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);

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

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

string createOrderTableSQL(string tableName) {
................................................................................
      + ", expiration = ?"
      + ", arrow_color = ?"
      + ", error_number = ?"
      + ", error_message = ?"
      + " where id = ?";
   return (sql);
}

void bindOrderParameters() {
   ordTicket[0]      = OrderTicket();
   ordType[0]        = OrderType();
   ordLots[0]        = OrderLots();
   mob_strcpy(ordSymbol, ordSymbolSize, OrderSymbol());
   ordOpenPrice[0]   = OrderOpenPrice();
   ordStopLoss[0]    = OrderStopLoss();
   ordTakeProfit[0]  = OrderTakeProfit();
   ordClosePrice[0]  = OrderClosePrice();
   ordExpiration[0]  = OrderExpiration();
   ordMagicNumber[0] = OrderMagicNumber();
   mob_strcpy(ordComment, ordCommentSize, OrderComment());
}

int selectOrders() {
   int rc = 0;

   rc = mob_selectToFetch(selectOrderStmt);
   if (rc < 0) return (rc);
   
................................................................................
      case OPE_GET_STATUS:
         getStatusInTransaction();
         break;
      case OPE_COPY_RATES:
         copyRates();
         break;
      default:
         setServerResponse(-1, "Unknown operation: " + ordOperation[0]);



      }
      
      ordOperation[0] = - ordOperation[0];
      if (ordTicket[0] > 0) {
         OrderSelect(ordTicket[0], SELECT_BY_TICKET);
         bindOrderParameters();
      }
      rc = mob_executeStatement(updateOrderStmt);
      if (rc < 0) {
         mob_fetchedAll(selectOrderStmt);
         return (rc);
      }
      Print("Operation ", - ordOperation[0], " on ticket ", ordTicket[0],
         " has been done with #", ordErrorNumber[0], " (", ordErrorMessage, ")");
   }
   
   mob_fetchedAll(selectOrderStmt);
   
   return (rc);
}

void setServerResponse(int no, string mesg) {
   ordErrorNumber[0] = no;
   mob_strcpy(ordErrorMessage, ordErrorMessageSize, mesg);
}

void getServerResponse() {
   int err = GetLastError();
   string errMesg = ErrorDescription(err);
   setServerResponse(err, errMesg);


}

void getDatabaseResponse() {
   int err = mob_getLastErrorNo();
   string errMesg = mob_getLastErrorMesg();
   setServerResponse(err, errMesg);


}

void orderSend() {
   double margin;
   double thePip    = getPipSize(ordSymbol);
   int    theDigits = MarketInfo(ordSymbol, MODE_DIGITS);
   
................................................................................
      case OP_SELLLIMIT:
      case OP_SELLSTOP:
         ordTakeProfit[0] = NormalizeDouble(ordOpenPrice[0] - margin, theDigits);
         break;
      }
   }
   
   if (ExtDebugPrint) Print("orderSend: ",
      ordSymbol, ", ",
      ordType[0], ", ",
      ordLots[0], ", ",
      ordOpenPrice[0], ", ",
      ordSlippage[0], ", ",
      ordStopLoss[0], ", ",
      ordTakeProfit[0], ", ",
................................................................................
      ordMagicNumber[0],
      ordExpiration[0],
      ordArrowColor[0]
   );
   
   if (ticket == -1) {
      getServerResponse();


   }
   ordTicket[0] = ticket;
}

void orderClose() {
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
      // close at market price
      if (ordClosePrice[0] == 0.0) {
         switch (OrderType()) {
................................................................................
         }
      }
      // close all position
      if (ordLots[0] == 0.0) {
         ordLots[0] = OrderLots();
      }
      
      if (ExtDebugPrint) Print("orderClose: ",
         ordTicket[0], ", ",
         ordLots[0], ", ",
         ordClosePrice[0], ", ",
         ordSlippage[0], ", ",
         ordArrowColor[0]
      );
      
      // closing
      if (OrderClose(
         ordTicket[0],
         ordLots[0],
         ordClosePrice[0],
         ordSlippage[0],
         ordArrowColor[0]))
      {
      } else {
         getServerResponse();
      }
   } else { // cannot select order
      getServerResponse();
   }
}

void orderCloseBy() {
   if (ExtDebugPrint) Print("orderCloseBy: ",
      ordTicket[0], ", ",
      ordOppositeTicket[0], ", ",
      ordArrowColor[0]
   );
   
   if (OrderCloseBy(
      ordTicket[0],
      ordOppositeTicket[0],
      ordArrowColor[0]))
   {
   } else {
      getServerResponse();
   }
}

void orderDelete() {
   if (ExtDebugPrint) Print("orderDelete: ",
      ordTicket[0], ", ",
      ordArrowColor[0]
   );
   
   if (OrderDelete(
      ordTicket[0],
      ordArrowColor[0]))
   {
   } else {
      getServerResponse();
   }
}

void orderModify() {
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
................................................................................
         }
      }
      
      if (ordExpiration[0] == 0) {
         ordExpiration[0] = OrderExpiration();
      }
      
      if (ExtDebugPrint) Print("orderModify: ",
         ordTicket[0], ", ",
         ordOpenPrice[0], ", ",
         ordStopLoss[0], ", ",
         ordTakeProfit[0], ", ",
         TimeToStr(ordExpiration[0]), ", ",
         ordArrowColor[0]
      );
................................................................................
      // modifying
      if (OrderModify(
         ordTicket[0],
         ordOpenPrice[0],
         ordStopLoss[0],
         ordTakeProfit[0],
         ordExpiration[0],
         ordArrowColor[0]))
      {
      } else {
         getServerResponse();
      }
   } else { // cannot select order
      getServerResponse();
   }
}
................................................................................
   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 getStatus() {
   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("getStatus");
      rc = mob_executeStatement(insertTradeStmt);
      if (rc < 0) {
         getDatabaseResponse();
         return (rc);
      }
   } else {
      getServerResponse();
   }
................................................................................
      rv = copyRatesBetween(tableName, start, end, symbol, period, rates);
   }
   if (rv < 0) {
      getDatabaseResponse();
      return;
   }
   

   string mesg = "Copied " + rv + " bars";
   if (rv > 0) {
      mesg = mesg + " from " + TimeToStr(rates[nBars - start - 1][0])
                  + " to "   + TimeToStr(rates[nBars - end][0]);
   }
   setServerResponse(rv, mesg);
}

int copyRatesBetween(string tableName, int start, int end, string symbol, int period, double& rates[]) {
   int rv = 0;
   
   int insertBarStmt = mob_registerStatement(insertBarSQL(tableName));
   mob_setAutoCommit(false);
................................................................................
      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();
         if (ExtDebugPrint) 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();
         if (ExtDebugPrint) dumpTradeParameters("syncTrades-new");
         rc = mob_executeStatement(insertTradeStmt);
         if (rc < 0) return (rc);
      }
   }

   fillTradeList();
   return (rc);
}