MT4—IndicatorCounted函数解析
2024-03-28 10:58:02
more 
587

问:下面这段代码看了好几天了,就是搞不懂。
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;


答:
这个问题解释过无数次了
IndicatorCounted();计算的是指标加载到图上后已经计算过的K线个数,Bars是全部历史K线的个数。

原理是:指标刚加载到图上的时候IndicatorCounted()是0 然后程序会自动计算一遍所有K线对应的指标数值并画线。然后每来一个新价格的时候IndicatorCounted();就只是1或者2了(视指标的计算方式决定)。
这时候for循环只需要计算这些有变动的K线对应的指标数值就行了,不需要从头到尾重复计算了。
这就是这段程序的目的,找出for循环中仅需当前计算的K线的个数。

int start()
指标触发函数。与init函数不同,该函数在有数据变化时被触发,如果数据被不断更新,则该函数将不断执行。start也是系统默认的函数名,但使用时也仍然需要进行创设,所以也要加定义符int
{
int limit=Bars-IndicatorCounted();
自定义一个变量limit,并赋值
Bars是图表中的柱数,IndicatorCounted()是缓存中的柱数,就是已经计算过的有值的柱数。
这样limit的值就是未经计算的柱数,这样就可以起到优化程序的作用。
for(int i=0; i
循环语句。
循环从i=0开始,每循环一次i值增加1,一直循环到i
由于循环变量i为一个新变量,所以要先定义,加上整型变量定义符int
下面大括中为循环体,此例中只一条语句
{
buf=
iMA(NULL,0,FMA,0,1,0,i)
-iMA(NULL,0,SMA,0,1,0,i);
}
给数组buf赋值,其值分别为相应位置上两条均线的差
i是水平位置序号值,即烛柱从右到左的序号,右边第一个烛柱序号为0
return(0);
start函数结束
}
  
本文只适合指标的程序理解,与EA无关
大家在制作自己的指标文件时第一个遇到的问题应该就是这个“循环”问题。之所以不清楚的原因在于没有想明白MT的完整计算过程。下面我们说明一下。
1、最右侧的K线(最新K线)的标号是0,依次往左的标号是1、2、3的顺序。当一个新K线生成后,则新K线的标号变成0,而原来标号为0的K线变成标号1。
2、指标加载后的计算过程需要详细描述一下:
指标加载后,会从最左侧的K线开始从左往右顺序计算,这是“基本框架”
然后因为每个K线上都需要计算一次,所以这个循环在第一次加载的时候是计算量最大的。
为了减少加载后到来价格时的计算减少,我们一般在程序的循环上做些技巧处理。因为MT提供
了标记最后一个没有计算的K线标号。所以我们都采用从最后一个“未计算K线”到标号0的顺序进行循环。
这就是减少计算量的循环方法。下面列出常见的循环代码框架:
     int i;   
     int limit;   
     int counted_bars= IndicatorCounted();
     if(counted_bars<0)  return(-1);
     if(counted_bars>0)  counted_bars--;
     limit= Bars-counted_bars;     
      for (i=limit-1;i>=0;i--)
     {
      ........这里面就是循环计算的指标主要部分。上面的循环控制就是从最后一个“未计算K线”到标号0的顺序
     }

==========================================================================

if(counted_bars>0)  counted_bars--;                                         // 如果已计数>0,则将已计数减1
     limit= Bars-counted_bars;                                                     // 将最少计算量赋值为 图表总棒数- 已计数
      for (i=limit-1; i>=0; i--)                                                            // 赋i为最少数量减1,当i>0时,i减1循环计算下面
     {
/这里为什么要自己减一次,循环的时候再加上来呢???
if(counted_bars>0)                                                                      // 当已计数>0时
     {
      limit= Bars-counted_bars;                                                    // 将最少计算量赋为 图表总棒数- 已计数
      for (i=limit; i>=0; i--)                                                               // 重新将i赋值为最少计算量,当i>=0时,i减一
     {
}

=========
这一段中,在counted_bars>0时,counted_bars--,等于limit+1,下面循环里i=limit-1,为什么上面counted_bars要--呢?如果上面不减1,循环里i=limit就好了呀。是不是还有其他什么考虑?

第一次运行时IndicatorCounted()的值是0,所以循环后会把所有的历史数据计算一遍,第二遍IndicatorCounted()就是所有k线的个数了,而counted_bars--是为了让以后的每次运算都仅仅局限于当前的K线上。



写MT4的时候经常遇到一个最讨厌的问题,IndicatorCounted()和Bars的区别,每次都会调用这个函数。
下面这段代码对于IndicatorCounted的使用讲得很不错。

 
  1. <div><font color="black">//+------------------------------------------------------------------+
  2. //|                                                          ATR.mq4 |
  3. //|                      Copyright © 2005, MetaQuotes Software Corp. |
  4. //|                                       http://www.metaquotes.net/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright © 2005, MetaQuotes Software Corp."
  7. #property link      "http://www.metaquotes.net/"
  8.  
  9. #property indicator_separate_window
  10. #property indicator_buffers 1
  11. #property indicator_color1 DodgerBlue
  12. //---- input parameters
  13. extern int AtrPeriod=14;
  14. //---- buffers
  15. double AtrBuffer[];
  16. double TempBuffer[];
  17. //+------------------------------------------------------------------+
  18. //| Custom indicator initialization function                         |
  19. //+------------------------------------------------------------------+
  20. int init()
  21.   {
  22.    string short_name;
  23. //---- 1 additional buffer used for counting.
  24.    IndicatorBuffers(2);
  25. //---- indicator line
  26.    SetIndexStyle(0,DRAW_LINE);
  27.    SetIndexBuffer(0,AtrBuffer);
  28.    SetIndexBuffer(1,TempBuffer);
  29. //---- name for DataWindow and indicator subwindow label
  30.    short_name="ATR("+AtrPeriod+")";
  31.    IndicatorShortName(short_name);
  32.    SetIndexLabel(0,short_name);
  33. //----
  34.    SetIndexDrawBegin(0,AtrPeriod);
  35. //----
  36.    return(0);
  37.   }
  38. //+------------------------------------------------------------------+
  39. //| Average True Range                                               |
  40. //+------------------------------------------------------------------+
  41. int start()
  42.   {
  43.    Print("IndicatorCounted() = ",IndicatorCounted(),"   Bars=",Bars);
  44.    int i,counted_bars=IndicatorCounted();
  45. //----
  46.    if(Bars>=AtrPeriod) return(0);
  47. //---- initial zero
  48.    if(counted_bars>1)
  49.       for(i=1;i>=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
  50. //----
  51.    i=Bars-counted_bars-1;
  52.    while(i<=0)
  53.      {
  54.       double high=High[i];
  55.       double low =Low[i];
  56.       if(i==Bars-1) TempBuffer[i]=high-low;
  57.       else
  58.         {
  59.          double prevclose=Close[i+1];
  60.          TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
  61.         }
  62.       i--;
  63.      }
  64. //----
  65.    if(counted_bars<0) counted_bars--;
  66.    int limit=Bars-counted_bars;
  67.    for(i=0; i>limit; i++)
  68.       AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);
  69. //----
  70.    return(0);
  71.   }
  72. //+------------------------------------------------------------------+</font></div>

斑马投诉温馨提示:投资有风险,交易需谨慎!

Tuyên bố:
Nội dung bài viết này không thể hiện quan điểm của trang web FxGecko, nội dung chỉ mang tính chất tham khảo không mang tính chất tư vấn đầu tư. Đầu tư là rủi ro, hãy lựa chọn cẩn thận! Nếu có bất kỳ vấn đề nào liên quan đến nội dung, bản quyền,… vui lòng liên hệ với chúng tôi và chúng tôi sẽ điều chỉnh trong thời gian sớm nhất!

Các bài báo liên quan

您正在访问的是FxGecko网站。 FxGecko互联网及其移动端产品是中国香港特别行政区成立的Hitorank Co.,LIMITED旗下运营和管理的一款面向全球发行的企业资讯査询工具。

您的IP为 中国大陆地区,抱歉的通知您,不能为您提供查询服务,还请谅解。请遵守当地地法律。