单个顾问中的多个缓冲区。复制缓冲区 return 错误 4806

Multiple buffers in single advisor. CopyBuffer return error 4806

在 Advisor 中使用了 3 条移动平均线并且每个 MA 都有自己的缓冲区,但是 CopyBuffer 从来没有 return 请求第二个 MA 的柱形计数器。只有第一个 MA ExtFastBuffer 获得了缓冲区值,其他所有内容都为空,而他们的 CopyBuffer return 错误 4806。我的错误是什么?

#property indicator_buffers 3

input ENUM_TIMEFRAMES ma_fast_timeframe = PERIOD_M1;
input int ma_fast_period = 2;
input ENUM_APPLIED_PRICE ma_fast_price_type = PRICE_MEDIAN;

input ENUM_TIMEFRAMES ma_middle_timeframe = PERIOD_M1;
input int ma_middle_period = 15;
input ENUM_APPLIED_PRICE ma_middle_price_type = PRICE_MEDIAN;

input ENUM_TIMEFRAMES ma_slow_timeframe = PERIOD_M1;
input int ma_slow_period = 30;
input ENUM_APPLIED_PRICE ma_slow_price_type = PRICE_MEDIAN;

int ExtFastHandle;
int ExtMiddleHandle;
int ExtSlowHandle;

double ExtFastBuffer[];
double ExtMiddleBuffer[];
double ExtSlowBuffer[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, ExtFastBuffer, INDICATOR_CALCULATIONS);
   SetIndexBuffer(1, ExtMiddleBuffer, INDICATOR_CALCULATIONS);
   SetIndexBuffer(2, ExtSlowBuffer, INDICATOR_CALCULATIONS);

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);

   ExtFastHandle = iMA(NULL, ma_fast_timeframe, ma_fast_period, 0, MODE_SMA, ma_fast_price_type);
   ExtMiddleHandle = iMA(NULL, ma_middle_timeframe, ma_middle_period, 0, MODE_SMA, ma_middle_price_type);
   ExtSlowHandle = iMA(NULL, ma_slow_timeframe, ma_slow_period, 0, MODE_SMA, ma_slow_price_type);

   if(ExtFastHandle == INVALID_HANDLE || ExtMiddleHandle == INVALID_HANDLE || ExtSlowHandle == INVALID_HANDLE)
     {
      Print("Error creating MA indicator-----------------------------------------------------------------------");
      return(INIT_FAILED);
     }

   return(INIT_SUCCEEDED);
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   int startFromIndex = 0;
   int bufferSize = 2;

//---------------- Fast MA
   if(CopyBuffer(ExtFastHandle, 0, startFromIndex, bufferSize, ExtFastBuffer) < 2)
     {
      Print("Loading ExtFastHandle failed ", GetLastError());
     }
   ENUM_ORDER_TYPE fastDir = ExtFastBuffer[0] == ExtFastBuffer[1] ? NULL : (ExtFastBuffer[0] < ExtFastBuffer[1] ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);

//---------------- Middle MA
   if(CopyBuffer(ExtMiddleHandle, 1, startFromIndex, bufferSize, ExtMiddleBuffer) < 2)
     {
      Print("Loading ExtMiddleHandle failed ", GetLastError());
     }
   ENUM_ORDER_TYPE middleDir = ExtMiddleBuffer[0] == ExtMiddleBuffer[1] ? NULL : (ExtMiddleBuffer[0] < ExtMiddleBuffer[1] ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);

//---------------- Slow MA
   if(CopyBuffer(ExtSlowHandle, 2, startFromIndex, bufferSize, ExtSlowBuffer) < 2)
     {
      Print("Loading ExtSlowHandle failed ", GetLastError());
     }
   ENUM_ORDER_TYPE slowDir = ExtSlowBuffer[0] == ExtSlowBuffer[1] ? NULL : (ExtSlowBuffer[0] < ExtSlowBuffer[1] ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);

   string fast = fastDir == NULL ? "EMPTY" : fastDir == ORDER_TYPE_BUY ? "BUY" : "SELL";
   string middle = middleDir == NULL ? "EMPTY" : middleDir == ORDER_TYPE_BUY ? "BUY" : "SELL";
   string slow = slowDir == NULL ? "EMPTY" : slowDir == ORDER_TYPE_BUY ? "BUY" : "SELL";
   Comment("Fast: ", fast, " Middle: ", middle, " Slow: ", slow);
  }
//+------------------------------------------------------------------+

查看示例 EA,例如 EMA 或 MACD。

  1. EA 中的 SetIndexBuffer 调用不需要,它们在指标中使用。
  2. CopyBuffer中的第二个参数是指标缓冲区的编号。因此,如果 MA 只有一个缓冲区,则始终使用 0。如果指标有两个缓冲区,如 MACD,则主线的索引为 0,信号线的索引为 1。

    //---------------- Middle MA
    if(CopyBuffer(ExtMiddleHandle, 0, startFromIndex, bufferSize, ExtMiddleBuffer) < 2)  
      {  
       Print("Loading ExtMiddleHandle failed ", GetLastError());
      }  
    ENUM_ORDER_TYPE middleDir = ExtMiddleBuffer[0] == ExtMiddleBuffer[1] ? NULL : (ExtMiddleBuffer[0] < ExtMiddleBuffer[1] ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);
    
    //---------------- Slow MA  
    if(CopyBuffer(ExtSlowHandle, 0, startFromIndex, bufferSize, ExtSlowBuffer) < 2)  
      {  
       Print("Loading ExtSlowHandle failed ", GetLastError());
      }  
    ENUM_ORDER_TYPE slowDir = ExtSlowBuffer[0] == ExtSlowBuffer[1] ? NULL : (ExtSlowBuffer[0] < ExtSlowBuffer[1] ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);