跳至内容

ArrayGetAsSeries

ArrayGetAsSeries

此函数用于检查数组索引的方向。

bool  ArrayGetAsSeries(
   const void&  array[]    // array for checking
   );

参数

array

[in] 要检查的数组。

返回值

如果指定数组设置了 AS_SERIES 标志,即按时间序列的方式从后向前访问数组,则返回 true时间序列与普通数组的不同之处在于,时间序列元素的索引是从末尾到开头进行的(从最新数据到旧数据)。

注意

要检查数组是否属于时间序列,请使用 ArrayIsSeries() 函数。作为输入参数传递给 OnCalculate() 函数的价格数据数组不一定具有与时间序列相同的索引方向。可以使用 ArraySetAsSeries() 函数设置所需的索引方向。

示例:

#property description "Indicator calculates absolute values of the difference between"
#property description "Open and Close or High and Low prices displaying them in a separate subwindow"
#property description "as a histrogram."
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 1
//--- input parameters
input bool InpAsSeries=true; // Indexing direction in the indicator buffer
input bool InpPrices=true;   // Calculation prices (true - Open,Close; false - High,Low)
//--- indicator buffer
double ExtBuffer[];
//+------------------------------------------------------------------+
//| Calculating indicator values                                     |
//+------------------------------------------------------------------+
void CandleSizeOnBuffer(const int rates_total,const int prev_calculated,
                        const double &first[],const double &second[],double &buffer[])
  {
//--- start variable for calculation of bars
   int start_index=prev_calculated;
//--- work at the last bar if the indicator values have already been calculated at the previous tick
   if(prev_calculated>0)
      start_index--;
//--- define indexing direction in arrays
   bool as_series_first=ArrayGetAsSeries(first);
   bool as_series_second=ArrayGetAsSeries(second);
   bool as_series_buffer=ArrayGetAsSeries(buffer);
//--- replace indexing direction with direct one if necessary
   if(as_series_first)
      ArraySetAsSeries(first,false);
   if(as_series_second)
      ArraySetAsSeries(second,false);
   if(as_series_buffer)
      ArraySetAsSeries(buffer,false);
//--- calculate indicator values
   for(int i=start_index;i<rates_total;i++)
      buffer[i]=MathAbs(first[i]-second[i]);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- bind indicator buffers
   SetIndexBuffer(0,ExtBuffer);
//--- set indexing element in the indicator buffer
   ArraySetAsSeries(ExtBuffer,InpAsSeries);
//--- check for what prices the indicator is calculated
   if(InpPrices)
     {
      //--- Open and Close prices
      IndicatorShortName("BodySize");
      //--- set the indicator color
      SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3,clrOrange);
     }
   else
     {
      //--- High and Low prices
      IndicatorShortName("ShadowSize");
      //--- set the indicator color
      SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3,clrDodgerBlue);
     }
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//--- calculate the indicator according to the flag value
   if(InpPrices)
      CandleSizeOnBuffer(rates_total,prev_calculated,open,close,ExtBuffer);
   else
      CandleSizeOnBuffer(rates_total,prev_calculated,high,low,ExtBuffer);
//--- return value of prev_calculated for next call
   return(rates_total);
  }

另请参阅

时间序列访问, ArraySetAsSeries,

最后更新于