跳至内容

IndicatorBuffers

IndicatorBuffers

分配用于自定义指标计算的缓冲区内存。

bool  IndicatorBuffers(
   int     count         // buffers
   );

参数

count

[in] 要分配的缓冲区数量。应在 indicator_buffers 和 512 个缓冲区之间。

返回值

如果缓冲区数量已成功更改,则返回 true;否则返回 false。

注意

缓冲区数量不得超过 512,且应小于 #property indicator_buffers中设置的值。如果自定义指标需要额外的缓冲区进行计数,应使用 IndicatorBuffers() 函数来指定总缓冲区数量。

示例:

//+------------------------------------------------------------------+
//|                                                        Bulls.mq4 |
//|                   Copyright 2005-2013, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2013, MetaQuotes Software Corp."
#property link        "https://www.mql4.com"
#property description "Bulls Power"
#property strict
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver
//--- input parameter
input int InpBullsPeriod=13;
//--- buffers
double ExtBullsBuffer[];
double ExtTempBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit(void)
  {
   string short_name;
//--- 1 additional buffer used for counting.
   IndicatorBuffers(2);
   IndicatorDigits(Digits);
//--- drawing style
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtBullsBuffer);
   SetIndexBuffer(1,ExtTempBuffer);
//--- name for DataWindow and indicator subwindow label
   short_name="Bulls("+IntegerToString(InpBullsPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
  }
//+------------------------------------------------------------------+
//| Bulls Power                                                      |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int limit=rates_total-prev_calculated;
//---
   if(rates_total<=InpBullsPeriod)
      return(0);
//---
   if(prev_calculated>0)
      limit++;
   for(int i=0; i<limit; i++)
     {
      ExtTempBuffer[i]=iMA(NULL,0,InpBullsPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      ExtBullsBuffer[i]=high[i]-ExtTempBuffer[i];
     }
//---
   return(rates_total);
  }

另请参阅

自定义指标属性对时间序列和指标的访问

最后更新于