简体   繁体   中英

Why can MQL4 miss iterations in a cycle?

I have code that takes data from FIBO-terminal and pushes this data in database. The problem is that when I going through the cycle of data array and pushes data into DB, I miss a lot of cycle iterations.

For example first three or four iterations work correctly but other iterations misses.

Here is posted my log , where I have my timeframe, number of my iterations and a sample of my query to the database. 在此输入图像描述 Whats wrong?

PS: This is not my code, I only try to debug it.

for (int i = ArraySize(symbolCache.HD)-1; i >= 0; --i)
   {
      if (CopyRates(symbolCache._symbol, symbolCache.HD[i].TF, indexation, symbolCache.HD[i].number, symbolCache.HD[i].bars) != symbolCache.HD[i].number)
      {
         Print(symbolCache._symbol,", ",symbolCache.HD[i].TF, ", ", symbolCache.HD[i].number, ", ");
         return false;
      }

          q += " CREATE TABLE IF NOT EXISTS bridge_"+symbolCache.symbol+"_"+symbolCache.HD[i].strTF + "(open_date_time timestamp, date_time timestamp PRIMARY KEY, close_date_time timestamp, open_price DOUBLE PRECISION, high_price DOUBLE PRECISION, low_price DOUBLE PRECISION, close_price DOUBLE PRECISION, volume DOUBLE PRECISION); ";
          q2 += " CREATE TABLE IF NOT EXISTS bar_"+symbolCache.symbol+"_"+symbolCache.HD[i].strTF + "(open_date_time timestamp, date_time timestamp PRIMARY KEY, close_date_time timestamp, open_price DOUBLE PRECISION, high_price DOUBLE PRECISION, low_price DOUBLE PRECISION, close_price DOUBLE PRECISION, volume DOUBLE PRECISION); ";      

          Print("Created " +  IntegerToString(PSQL_Query(idDB,  q )) + ", " + q);
          Print("Created2 " + IntegerToString(PSQL_Query(idDB2, q2)) + ", " + q2);

          int barSize = ArraySize(symbolCache.HD[i].bars);
          Print("barSize = ", barSize);
          for (int j = 0; j < barSize; ++j)
          {
             //Print("j = ", j);
             q = ""; q2 = "";
             q += " INSERT INTO bridge_"+symbolCache.symbol+"_"+symbolCache.HD[i].strTF+
                         " VALUES ("+
                         TimeToStr(symbolCache.HD[i].bars[j].time,TIME_DATE|TIME_MINUTES|TIME_SECONDS) + "', '"+ 
                         TimeToStr(symbolCache.HD[i].bars[j].time,TIME_DATE|TIME_MINUTES|TIME_SECONDS) +"." + IntegerToString(GetTickCount() % 1000) + "', '" +
                         TimeToStr(symbolCache.HD[i].bars[j].time + symbolCache.HD[i].TF*60,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+ "', "+ 
                         DoubleToStr(symbolCache.HD[i].bars[j].open,symbolCache._digits)+", "+
                         DoubleToStr(symbolCache.HD[i].bars[j].high,symbolCache._digits)+", "+
                         DoubleToStr(symbolCache.HD[i].bars[j].low,symbolCache._digits)+", "+
                         DoubleToStr(symbolCache.HD[i].bars[j].close,symbolCache._digits)+", "+
                         IntegerToString(symbolCache.HD[i].bars[j].tick_volume)+");";
                         Print("First :" + IntegerToString(PSQL_Query(idDB, q)) + " iter : " + IntegerToString(j) + ", q : " + q);

             q2 += " INSERT INTO bar_"+symbolCache.symbol+"_"+symbolCache.HD[i].strTF+
                         " VALUES ('"+
                         TimeToStr(symbolCache.HD[i].bars[j].time+off,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+ "', '"+
                         TimeToStr(symbolCache.HD[i].bars[j].time+off,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+"."+ IntegerToString(GetTickCount() % 1000)+"', '"+
                         TimeToStr(symbolCache.HD[i].bars[j].time+off + symbolCache.HD[i].TF*60,TIME_DATE|TIME_MINUTES|TIME_SECONDS)+"."+IntegerToString(GetTickCount() % 1000)+"', "+
                         DoubleToStr(symbolCache.HD[i].bars[j].open,symbolCache._digits)+", "+
                         DoubleToStr(symbolCache.HD[i].bars[j].high,symbolCache._digits)+", "+
                         DoubleToStr(symbolCache.HD[i].bars[j].low,symbolCache._digits)+", "+
                         DoubleToStr(symbolCache.HD[i].bars[j].close,symbolCache._digits)+", "+
                         IntegerToString(symbolCache.HD[i].bars[j].tick_volume) + ");";
                         Print("Sec : " + IntegerToString(PSQL_Query(idDB2, q2)) + " iter : " + IntegerToString(j) + ", q : "+ q2);

          }
       }

A: Your code is simply NOT able to reproduce the claimed issue

besides the already deleted residual, the code refers to symbolCache , where both ._symbol and .symbol attributes raise additional questions on MCVE -code design.

Below posted code + results prove, that a nested for(){...} construct does perform as expected and is not responsible for the claimed issue.

The remaining, isolated, suspect is thus in the if ( symbolCache.HD[i].number != CopyRates( ...)

Principle
an identical dual- for(){...} loops was tested for helping to isolate the root cause on a simplified MCVE -code with avoided dependence on a still non-published struct symbolCache

//+------------------------------------------------------------------+
//|                             _____Demo_MCVE_StackOverflow_FOR.mq4 |
//+------------------------------------------------------------------+
#property strict

//                           1  2  3  4  5  6  7  8  9 10      ordinal ~ N-elements
//                           0  1  2  3  4  5  6  7  8  9      [idx#]  ~ Array Cell-index
int   aVarNumOfBARs[10] = {  5, 7, 3, 9, 2, 4, 3, 2, 1, 1 };// symbolCache[i].bars[j] proxy

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void  OnStart()
{   
    Comment( "START" );
    int aFH = FileOpen( __FILE__ + ".txt", FILE_WRITE | FILE_TXT );

    for ( int anInstrumentID  =  ArraySize( aVarNumOfBARs ) - 1;
              anInstrumentID >   0;
          //  anInstrumentID-- )
            --anInstrumentID   )
    {   string          aSTR1 = StringFormat( "symbolCache.HD[ i = anInstrumentID ] instrument ID#: %d",
                                               anInstrumentID
                                               );
        Comment(        aSTR1 );
        FileWrite( aFH, aSTR1 );

        for ( int aBarPTR     =  0;
                  aBarPTR    <   aVarNumOfBARs[anInstrumentID];
            //    aBarPTR++ )
                ++aBarPTR   )
        {   string           aSTR2 = aSTR1 + StringFormat( ": symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: %d from[%d] available in symbolCache.HD[i].bars[] array",
                                                            aBarPTR,
                                                            aVarNumOfBARs[anInstrumentID]
                                                    );
            Comment(         aSTR2 );
            FileWrite( aFH,  aSTR2 );
        }
    }
    FileClose( aFH );
}
//+------------------------------------------------------------------+

Produces a reasonably correct output:

 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 9
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 9: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[1] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 8
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 8: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[1] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 7
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 7: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[2] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 7: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 1 from[2] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 6
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 6: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[3] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 6: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 1 from[3] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 6: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 2 from[3] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 5
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 5: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[4] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 5: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 1 from[4] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 5: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 2 from[4] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 5: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 3 from[4] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 4
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 4: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[2] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 4: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 1 from[2] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 1 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 2 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 3 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 4 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 5 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 6 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 7 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 3: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 8 from[9] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 2
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 2: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[3] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 2: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 1 from[3] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 2: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 2 from[3] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 1
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 1: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 0 from[7] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 1: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 1 from[7] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 1: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 2 from[7] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 1: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 3 from[7] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 1: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 4 from[7] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 1: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 5 from[7] available in symbolCache.HD[i].bars[] array
 symbolCache.HD[ i = anInstrumentID ] instrument ID#: 1: symbolCache.HD[i].bars[j=aBarPTR].* reporting data from a Bar#: 6 from[7] available in symbolCache.HD[i].bars[] array

Original code a bit inefficient, de-noised for non MQL4 -er's clarity / education purpose

for (                 int i  = ArraySize( symbolCache.HD ) - 1;
                          i >= 0;
                        --i )
{     if ( symbolCache.HD[i].number != CopyRates( symbolCache._symbol,
                                                  symbolCache.HD[i].TF,
                                                  indexation,
                                                  symbolCache.HD[i].number,
                                                  symbolCache.HD[i].bars
                                                  )
           )
      {
         Print( symbolCache._symbol, ", ", symbolCache.HD[i].TF, ", ", symbolCache.HD[i].number, ", " );
         return( false );
      }

      q  += " CREATE TABLE IF NOT EXISTS bridge_" + symbolCache.symbol + "_" + symbolCache.HD[i].strTF + "(open_date_time timestamp, date_time timestamp PRIMARY KEY, close_date_time timestamp, open_price DOUBLE PRECISION, high_price DOUBLE PRECISION, low_price DOUBLE PRECISION, close_price DOUBLE PRECISION, volume DOUBLE PRECISION); ";
      q2 += " CREATE TABLE IF NOT EXISTS bar_"    + symbolCache.symbol + "_" + symbolCache.HD[i].strTF + "(open_date_time timestamp, date_time timestamp PRIMARY KEY, close_date_time timestamp, open_price DOUBLE PRECISION, high_price DOUBLE PRECISION, low_price DOUBLE PRECISION, close_price DOUBLE PRECISION, volume DOUBLE PRECISION); ";

      Print( "Created "  + IntegerToString( PSQL_Query( idDB,  q  ) ) + ", " + q  );
      Print( "Created2 " + IntegerToString( PSQL_Query( idDB2, q2 ) ) + ", " + q2 );

      int barSize = ArraySize( symbolCache.HD[i].bars );
      Print( "barSize = ", barSize );

      for ( int j = 0; j < barSize; ++j )
      {
         // Print( "j = ", j );
         q   = "";
         q2  = "";

         q  += " INSERT INTO bridge_"
               +                  symbolCache.symbol                                                                                      + "_"
               +                  symbolCache.HD[i].strTF                                                                                 + " VALUES ("
               + TimeToStr(       symbolCache.HD[i].bars[j].time,                                   TIME_DATE|TIME_MINUTES|TIME_SECONDS ) + "', '"
               + TimeToStr(       symbolCache.HD[i].bars[j].time,                                   TIME_DATE|TIME_MINUTES|TIME_SECONDS ) + "."    + IntegerToString( GetTickCount() % 1000 ) + "', '"
               + TimeToStr(       symbolCache.HD[i].bars[j].time       + symbolCache.HD[i].TF * 60, TIME_DATE|TIME_MINUTES|TIME_SECONDS ) + "', "
               + DoubleToStr(     symbolCache.HD[i].bars[j].open,        symbolCache._digits )                                            + ", "
               + DoubleToStr(     symbolCache.HD[i].bars[j].high,        symbolCache._digits )                                            + ", "
               + DoubleToStr(     symbolCache.HD[i].bars[j].low,         symbolCache._digits )                                            + ", "
               + DoubleToStr(     symbolCache.HD[i].bars[j].close,       symbolCache._digits )                                            + ", "
               + IntegerToString( symbolCache.HD[i].bars[j].tick_volume )
               + ");";

         Print( "First :" + IntegerToString( PSQL_Query( idDB, q ) ) + " iter : " + IntegerToString( j ) + ", q : " + q );

         q2 += " INSERT INTO bar_"
               +                  symbolCache.symbol                                                                                      + "_"
               +                  symbolCache.HD[i].strTF                                                                                 + " VALUES ('"
               + TimeToStr(       symbolCache.HD[i].bars[j].time + off,                             TIME_DATE|TIME_MINUTES|TIME_SECONDS ) + "', '"
               + TimeToStr(       symbolCache.HD[i].bars[j].time + off,                             TIME_DATE|TIME_MINUTES|TIME_SECONDS ) + "."    + IntegerToString( GetTickCount() % 1000 ) + "', '"
               + TimeToStr(       symbolCache.HD[i].bars[j].time + off + symbolCache.HD[i].TF * 60, TIME_DATE|TIME_MINUTES|TIME_SECONDS ) + "."    + IntegerToString( GetTickCount() % 1000 ) + "', "
               + DoubleToStr(     symbolCache.HD[i].bars[j].open,        symbolCache._digits )                                            + ", "
               + DoubleToStr(     symbolCache.HD[i].bars[j].high,        symbolCache._digits )                                            + ", "
               + DoubleToStr(     symbolCache.HD[i].bars[j].low,         symbolCache._digits )                                            + ", "
               + DoubleToStr(     symbolCache.HD[i].bars[j].close,       symbolCache._digits )                                            + ", "
               + IntegerToString( symbolCache.HD[i].bars[j].tick_volume )
               + ");";

         Print( "Sec : " + IntegerToString( PSQL_Query( idDB2, q2 ) ) */ + " iter : " + IntegerToString( j ) + ", q : "+ q2 );

      }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM