为什么MQL4会在一个循环中错过迭代?

时间:2016-06-02 13:15:56

标签: c++ algorithmic-trading mql4 metatrader4 metatrader5

我有从FIBO终端获取数据的代码,并将此数据推送到数据库中。问题是,当我经历数据数组的循环并将数据推送到DB时,我会错过很多循环迭代。

例如,前三或四次迭代正常工作,但其他迭代未命中。

Here is posted my log,其中包含我的时间范围,迭代次数以及我对数据库的查询示例。 enter image description here 怎么了?

P.S。:这不是我的代码,我只是尝试调试它。

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

          }
       }

1 个答案:

答案 0 :(得分:0)

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

除已删除的残差外,代码还指 symbolCache ,其中 ._symbol .symbol < / strong>属性会对 MCVE 代码设计提出更多问题。

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

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

  

原则一个相同的双for(){...}循环经过测试,帮助在简化的上{strong>隔离根本原因 {{1 }} - 代码,避免依赖尚未发布的 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 - er的清晰度/教育目的

MQL4