1

我有从 FIBO 终端获取数据并将这些数据推送到数据库中的代码。问题是当我通过数据数组的循环并将数据推送到数据库时,我错过了很多循环迭代。

例如,前三个或四个迭代可以正常工作,但其他迭代会失败。

这里发布了我的日志,其中有我的时间范围、我的迭代次数和我对数据库的查询示例。 在此处输入图像描述 怎么了?

PS:这不是我的代码,我只是尝试调试它。

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

          }
       }
4

1 回答 1

0

答:您的代码根本无法重现声称的问题

除了已经删除的残差之外,代码指的是symbolCache,其中._symbol和属性都对代码设计.symbol提出了额外的问题。MCVE

下面发布的代码 + 结果证明,嵌套for(){...}构造确实按预期执行,并且不对声称的问题负责。

剩下的,孤立的,嫌疑人因此在if ( symbolCache.HD[i].number != CopyRates( ...)

原则
上测试了相同的双for(){...}循环,以帮助将根本原因隔离在简化的MCVE代码上,避免依赖于尚未发布的代码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 );
}
//+------------------------------------------------------------------+

产生合理正确的输出:

 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

原始代码效率低下,为非用户的清晰度/教育目的去噪MQL4

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

      }
 }
于 2016-06-02T18:13:45.363 回答