如何避免数组超出范围

How to avoid array out of range

有人知道如何在指标缓冲区中显示长柱数(比方说 7000)时避免错误 array out of range 吗?

虽然数组(问题)看起来很相似,但有很大的不同

MQL4 指标使用 "other" 机制来处理数组,而不是 "ordinary" 数组。

...  Testing pass stopped due to a critical error in the EA
...  array out of range in '!2015-09-08___!EA 2xAMA 01 2015-09-08_msMOD_0.00.mq4' (519,39)

是,MT4 在尝试处理错误 ptr[=14 时抛出致命错误=]array[aStaticSIZE] 并且必须适当注意 避免 这种情况,或者 trim ptr (类似于低延迟循环缓冲区场景)不要直接越过数组边界(下溢/溢出)或 extend 通过 ArrayResize() array[] 以便为了跟上声明为动态数组 double Array[]; 上的 ptr 增长(直到内存允许)),但是 MQL4 技术指标 完全其他情况。


限制?是的。 。 . . . . (并且应该审查每个新的 MQL4.56789 隐形更新)

"New"-MQL4.56789-Build-840 开始,您的“普通”数组不能超过 2.147.483.647 个元素,如果 O/S 内存池管理器允许,那么你应该有足够的 space,即使使用更高维度的映射 { 2D | 3D | 4D }.

几年前,我们使用许多并行 2D/3D 阵列来实现快速和 private(安全封装)堆/堆栈处理程序,以维护 100k+ 行/2D 平面和所有规模的高度动态实体在旧 MQL4.

中运作良好

所以一些 7k+ 元素应该不会让你担心。


指标通过自动缓冲区处理程序间接使用数组

从这个意义上说,您的代码不需要关心这些问题。

/*
#property "pragmas" help MQL4-compiler decide about setup of internal handlers
          so this part of code "speaks" to MetaLang.exe at compile-time*/

#property indicator_buffers      3             // .DEF N-Buffs

#property indicator_color1       White         // .SET Buf[0].color
#property indicator_color2       SeaGreen      // .SET Buf[1].color
#property indicator_color3       FireBrick     // .SET Buf[2].color

#property indicator_width1       1             // .SET Buf[0].width
#property indicator_width2       2             // .SET Buf[1].width
#property indicator_width3       2             // .SET Buf[2].width

         double   buffer_line_up[],            // .DEF Arrays as dynamic ...[]
                  buffer_line_dn[],            //      with human-readable-names
                  buffer_line_ax[];            //      and MT4 will take care

int   init()   {

      SetIndexBuffer(   0, buffer_line_ax );   // .ASSOC IndexBuffer.0<-array[]
      SetIndexLabel(    0, "SuperTrend"     );

      SetIndexBuffer(   1, buffer_line_up   ); // .ASSOC IndexBuffer.0<-array[]
      SetIndexLabel(    1, "Up Trend"       );
      SetIndexStyle(    1, DRAW_LINE,
                           STYLE_SOLID,
                           1 + int( ATR_Multiplier / 5 ),
                           SeaGreen
                           );

      SetIndexBuffer(   2, buffer_line_dn );   // .ASSOC IndexBuffer.0<-array[]
      SetIndexLabel(    2, "Down Trend"     );
      SetIndexStyle(    2, DRAW_LINE,
                           STYLE_SOLID,
                           1 + int( ATR_Multiplier / 5 ),
                           FireBrick
                           );

      SetIndexDrawBegin(0, ATR_Period );       // .DEF initial depth of Buffer before 1st GUI output

      IndicatorShortName( "xxxx[" + ATR_Period + "," + ATR_Multiplier + "]" );

      IndicatorDigits( MarketInfo( Symbol(), MODE_DIGITS ) );

我有一个类似的问题,我的一个缓冲区总是出现“Array out of range”错误。我检查了 ArraySize(),它返回了 0

最后我只是忘了在onInit() {...}[=中为这个缓冲区数组调用SetIndexBuffer(...) 30=] 我的指标。

因为我使用的是没有画线的内部缓冲区,所以我使用 IndicatorBuffers() 函数先增加缓冲区的数量,然后使用 SetIndexBuffer(...).

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   IndicatorBuffers(5);

//buffers with #properties settings
   SetIndexBuffer(0,Buffer1);
   SetIndexBuffer(1,Buffer2);
   SetIndexBuffer(2,Buffer3);
   SetIndexBuffer(3,Buffer4);

//additional buffer without #properties 
   SetIndexBuffer(4,AdditionalBuffer);