MT4 Files

Check-in [8b84736cc6]
Login

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

Overview
Comment:Improved the logic of copyRates
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:8b84736cc60215acdc9d0df6c4b1558a9736eea9
User & Date: onagano 2011-04-20 12:05:11
Context
2011-04-20
12:14
Improved the closing condition of MOB_insertData.mq4 check-in: 3fa003f942 user: onagano tags: trunk
12:05
Improved the logic of copyRates check-in: 8b84736cc6 user: onagano tags: trunk
08:53
Consolidated check-in: c006007ae5 user: onagano tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to experts/MOB_executeOrder.mq4.

497
498
499
500
501
502
503

504
505
506
507
508
509
510
....
1014
1015
1016
1017
1018
1019
1020







1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038



1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
         Print(mesg);
         ordErrorNumber[0] = -1;
         mob_strcpy(ordErrorMessage, ordErrorMessageSize, mesg);
      }
      
      ordOperation[0] = - ordOperation[0];
      rc = mob_executeStatement(updateOrderStmt);

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

void getServerResponse() {
................................................................................

   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);
   int start;
   int end;
   int rv;








   if (count <= 0) {
      start = 0;
      end = iBars(symbol, period) - 1; // Exclude the latest bar.
      rv = copyRatesBetween(tableName, start, end, symbol, period);
   } else {
      datetime dts[];
      int nBars = ArrayCopySeries(dts, MODE_TIME, symbol, period);
      int iMax = ArrayBsearch(dts, dtMax, WHOLE_ARRAY, 0 , MODE_DESCEND);
      int iMin = ArrayBsearch(dts, dtMin, WHOLE_ARRAY, 0 , MODE_DESCEND);
      start = 0;
      end = nBars - (iMin + 1);
      rv += copyRatesBetween(tableName, start, end, symbol, period);
      start = nBars - iMax;
      end = nBars - 1; // Exclude the latest bar.
      rv += copyRatesBetween(tableName, start, end, symbol, period);
   }




   ordMagicNumber[0] = rv;
}

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

   int rc = 0;
   int rv = 0;
   
   int insertBarStmt = mob_registerStatement(insertBarSQL(tableName));

   rc = mob_setAutoCommit(false);

   // RateInfo structure's order is OLHCV, which will be adjusted to OHLCV in database.
   rc = mob_copyRates(insertBarStmt, rates, ArraySize(rates), start, end);
   if (rc < 0) {
      rc = mob_rollback();
   } else {
      rv = rc;
      rc = mob_commit();
   }

   rc = mob_setAutoCommit(true);

   rc = mob_unregisterStatement(insertBarStmt);

   return(rv);
}








>







 







>
>
>
>
>
>
>



|
|


|

<
<
<
<


|


>
>
>



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



<
|


|
|
|

<
|


|
<
|




497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
....
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037




1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049























1050
1051
1052

1053
1054
1055
1056
1057
1058
1059

1060
1061
1062
1063

1064
1065
1066
1067
1068
         Print(mesg);
         ordErrorNumber[0] = -1;
         mob_strcpy(ordErrorMessage, ordErrorMessageSize, mesg);
      }
      
      ordOperation[0] = - ordOperation[0];
      rc = mob_executeStatement(updateOrderStmt);
      Print("Operation ", - ordOperation[0], " done with #", ordErrorNumber[0], " (", ordErrorMessage, ")");
   }
   mob_fetchedAll(selectOrderStmt);
   
   return (rc);
}

void getServerResponse() {
................................................................................

   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);
   int start;
   int end;
   int rv;

   double rates[][6];
   int nBars = ArrayCopyRates(rates, symbol, period);
   if (nBars < 0) {
      getServerResponse();
      return;
   }

   if (count <= 0) {
      start = 0;
      end = nBars - 1; // Exclude the latest bar.
      rv = copyRatesBetween(tableName, start, end, symbol, period, rates);
   } else {
      datetime dts[];
      ArrayCopySeries(dts, MODE_TIME, symbol, period);
      int iMax = ArrayBsearch(dts, dtMax, WHOLE_ARRAY, 0 , MODE_DESCEND);




      start = nBars - iMax;
      end = nBars - 1; // Exclude the latest bar.
      rv = copyRatesBetween(tableName, start, end, symbol, period, rates);
   }

   if (rv < 0) {
      getDatabaseResponse();
   }
   ordMagicNumber[0] = rv;
}

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

   // RateInfo structure's order is OLHCV, which will be adjusted to OHLCV in database.
   rv = mob_copyRates(insertBarStmt, rates, ArraySize(rates), start, end);
   if (rv < 0) {
      mob_rollback();
   } else {

      mob_commit();
   }

   mob_setAutoCommit(true);

   mob_unregisterStatement(insertBarStmt);

   return(rv);
}