函数未正确从数组中检索值

Function not retrieving values from an array correctly

我有一个名为 Crossed1 的函数,我正在尝试实现它以将值收集到数组中。 然后我想从该数组中找到最后 5 个柱的最高值。

int Crossed1()
  {

   static int CurrentDirection1=0;
   static int LastDirection1=0;
   static bool FirstTime1=true;
   
//----
   if((BB20DO<BB30DO)&& (CandleCLOSE<BB20DO) && (CandleCLOSEp<BB20DOp)  && (RSIi< 30))
      CurrentDirection1=1;  // line1 above line2
   if(BB20DO>BB30DO)
      CurrentDirection1=2;  // line1 below line2
//----
   if(FirstTime1==true) // Need to check if this is the first time the function is run
     {
      FirstTime1=false; // Change variable to false
      LastDirection1=CurrentDirection1; // Set new direction
      return (0);
     }

   if(CurrentDirection1!=LastDirection1 && FirstTime1==false) // If not the first time and there is a direction change
     {
      LastDirection1=CurrentDirection1; // Set new direction
      return(CurrentDirection1); // 1 for up, 2 for down
     }
   else
     {
      return(0);  // No direction change
           
     }
         
  }

Arraymax 检索最大值

int ArrayCalcMax()
   {   
      int bars = 5;
      int malookback=bars;
     
                  
      double madaily[5];
      double dllv;
      
      for(int i = 0; i < malookback; i++)
      madaily[i] = Crossed1();
      Print(" array[",i,"] = ",madaily[i]);

      int maxPos = ArrayMaximum(madaily,malookback,0); // Check the "maxPos" to make sure it is in range as it could be "-1" if it fails
      Print(" maxPos = ", maxPos );

      if( maxPos >= 0 )
         {
         dllv = madaily[maxPos];  // Please note that "dllv" is a local variable that will be discarded as soon as you return
         Print(" dllv = ", dllv );
         }
   else
      Print("Something is wrong!");

      
   return(dllv);
}

我认为按照目前的代码,我只收集数组中的当前值,对吗?

如果我尝试包含 i,它 returns 错误 Wrong parameters count

我该如何解决这个问题?

这些行:

for(int i = 0; i < malookback; i++)
      madaily[i] = Crossed1();
      Print(" array[",i,"] = ",madaily[i]);

不要做你认为他们做的事。

他们翻译成

for(int i = 0; i< malookback; i++)
{
    madaily[i] = Crossed1();
}
Print(" array[",i,"] = ",madaily[i]); // Error because i has been destructed

在 C++ 中编写循环语句时,如果它包含多行代码,则必须在其内容周围包含花括号 {}

将这些行更改为

for(int i = 0; i < malookback; i++)
{
    madaily[i] = Crossed1();
    Print(" array[",i,"] = ",madaily[i]);
}

它将按预期工作。

谢谢 JensB。

正如你所说,它工作正常。 尽管如此,该值不会存储在下一次计算中:

    17:10:44.622    2019.08.23 12:20:00  BBReverse V1 EURUSD,H4:  array[0] = 1
0   17:10:44.622    2019.08.23 12:20:00  BBReverse V1 EURUSD,H4:  array[1] = 0
0   17:10:44.622    2019.08.23 12:20:00  BBReverse V1 EURUSD,H4:  array[2] = 0
0   17:10:44.622    2019.08.23 12:20:00  BBReverse V1 EURUSD,H4:  array[3] = 0
0   17:10:44.622    2019.08.23 12:20:00  BBReverse V1 EURUSD,H4:  array[4] = 0
0   17:10:44.622    2019.08.23 12:20:00  BBReverse V1 EURUSD,H4:  maxPos = 0
0   17:10:44.622    2019.08.23 12:20:00  BBReverse V1 EURUSD,H4:  dllv = 1

数组 [1] 的结果应该是 1,它显示的值为零。


17:10:44.622    2019.08.23 12:40:00  BBReverse V1 EURUSD,H4:  array[0] = 0
0   17:10:44.622    2019.08.23 12:40:00  BBReverse V1 EURUSD,H4:  array[1] = 0
0   17:10:44.622    2019.08.23 12:40:00  BBReverse V1 EURUSD,H4:  array[2] = 0
0   17:10:44.622    2019.08.23 12:40:00  BBReverse V1 EURUSD,H4:  array[3] = 0
0   17:10:44.622    2019.08.23 12:40:00  BBReverse V1 EURUSD,H4:  array[4] = 0
0   17:10:44.622    2019.08.23 12:40:00  BBReverse V1 EURUSD,H4:  maxPos = 0
0   17:10:44.622    2019.08.23 12:40:00  BBReverse V1 EURUSD,H4:  dllv = 0

谢谢 安德烈