MQL4自编指标学习4

0
回复
8697
查看
[复制链接]

360

主题

15

回帖

2473

积分

超级版主

积分
2473
来源: 2019-11-23 18:56:34 显示全部楼层 |阅读模式
he OnCalculate() function is called only in custom indicators when it's necessary to calculate the indicator values by the Calculate event. This usually happens when a new tick is received for the symbol, for which the indicator is calculated.

Parameters of open[], high[], low[] and close[] contain arrays with open prices, high and low prices and close prices of the current time frame. The time[] parameter contains an array with open time values, the spread[] parameter has an array containing the history of spreads (if any spread is provided for the traded security). The parameters of volume[] and tick_volume[] contain the history of trade and tick volume, respectively.

To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.

The first rates_total parameter contains the number of bars, available to the indicator for calculation, and corresponds to the number of bars available in the chart.

We should note the connection between the return value of OnCalculate() and the second input parameter prev_calculated. During the function call, the prev_calculated parameter contains a value returned by OnCalculate() during previous call. This allows for economical algorithms for calculating the custom indicator in order to avoid repeated calculations for those bars that haven't changed since the previous run of this function.

For this, it is usually enough to return the value of the rates_total parameter, which contains the number of bars in the current function call. If since the last call of OnCalculate() price data has changed (a deeper history downloaded or history blanks filled), the value of the input parameter prev_calculated will be set to zero by the terminal.

不知道Bars 和rates_total  以及IndicatorCounted ()和prev_calculated的区别,有明白的朋友不妨指点一下。

例子使用官方给的例子
  1. #property indicator_chart_window
  2. #property indicator_buffers 1
  3. //---- plot Line
  4. #property indicator_label1  "Line"
  5. #property indicator_type1   DRAW_LINE
  6. #property indicator_color1  clrDarkBlue
  7. #property indicator_style1  STYLE_SOLID
  8. #property indicator_width1  1
  9. //--- indicator buffers
  10. double         LineBuffer[];
  11. //+------------------------------------------------------------------+
  12. //| Custom indicator initialization function                         |
  13. //+------------------------------------------------------------------+
  14. int OnInit()
  15.   {
  16. //--- indicator buffers mapping
  17.    SetIndexBuffer(0,LineBuffer,INDICATOR_DATA);
  18. //---
  19.    return(INIT_SUCCEEDED);
  20.   }
  21. //+------------------------------------------------------------------+
  22. //| Custom indicator iteration function                              |
  23. //+------------------------------------------------------------------+
  24. int OnCalculate(const int rates_total,
  25.                 const int prev_calculated,
  26.                 const datetime& time[],
  27.                 const double& open[],
  28.                 const double& high[],
  29.                 const double& low[],
  30.                 const double& close[],
  31.                 const long& tick_volume[],
  32.                 const long& volume[],
  33.                 const int& spread[])
  34.   {
  35. //--- Get the number of bars available for the current symbol and chart period
  36.    int bars=Bars(Symbol(),0);
  37.    Print("Bars = ",bars,", rates_total = ",rates_total,",  prev_calculated = ",prev_calculated);
  38.    Print("time[0] = ",time[0]," time[rates_total-1] = ",time[rates_total-1]);
  39. //--- return value of prev_calculated for next call
  40.    return(rates_total);
  41.   }
复制代码

02.jpg


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信