外汇邦外汇论坛 - 中国最大的外汇投资论坛,全球华人外汇交易者交流平台!

 找回密码
 立即注册
查看: 5366|回复: 0

MT4—IndicatorCounted函数解析

[复制链接]

397

主题

458

帖子

5637

积分

论坛元老

Rank: 8Rank: 8

积分
5637
发表于 2015-9-30 10:20:52 | 显示全部楼层 |阅读模式
本帖最后由 FxTrading 于 2015-9-30 14:08 编辑

问:下面这段代码看了好几天了,就是搞不懂。
   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. #property indicator_separate_window
  9. #property indicator_buffers 1
  10. #property indicator_color1 DodgerBlue
  11. //---- input parameters
  12. extern int AtrPeriod=14;
  13. //---- buffers
  14. double AtrBuffer[];
  15. double TempBuffer[];
  16. //+------------------------------------------------------------------+
  17. //| Custom indicator initialization function                         |
  18. //+------------------------------------------------------------------+
  19. int init()
  20.   {
  21.    string short_name;
  22. //---- 1 additional buffer used for counting.
  23.    IndicatorBuffers(2);
  24. //---- indicator line
  25.    SetIndexStyle(0,DRAW_LINE);
  26.    SetIndexBuffer(0,AtrBuffer);
  27.    SetIndexBuffer(1,TempBuffer);
  28. //---- name for DataWindow and indicator subwindow label
  29.    short_name="ATR("+AtrPeriod+")";
  30.    IndicatorShortName(short_name);
  31.    SetIndexLabel(0,short_name);
  32. //----
  33.    SetIndexDrawBegin(0,AtrPeriod);
  34. //----
  35.    return(0);
  36.   }
  37. //+------------------------------------------------------------------+
  38. //| Average True Range                                               |
  39. //+------------------------------------------------------------------+
  40. int start()
  41.   {
  42.    Print("IndicatorCounted() = ",IndicatorCounted(),"   Bars=",Bars);
  43.    int i,counted_bars=IndicatorCounted();
  44. //----
  45.    if(Bars>=AtrPeriod) return(0);
  46. //---- initial zero
  47.    if(counted_bars>1)
  48.       for(i=1;i>=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
  49. //----
  50.    i=Bars-counted_bars-1;
  51.    while(i<=0)
  52.      {
  53.       double high=High[i];
  54.       double low =Low[i];
  55.       if(i==Bars-1) TempBuffer[i]=high-low;
  56.       else
  57.         {
  58.          double prevclose=Close[i+1];
  59.          TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
  60.         }
  61.       i--;
  62.      }
  63. //----
  64.    if(counted_bars<0) counted_bars--;
  65.    int limit=Bars-counted_bars;
  66.    for(i=0; i>limit; i++)
  67.       AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);
  68. //----
  69.    return(0);
  70.   }
  71. //+------------------------------------------------------------------+</font></div>
复制代码



 

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

联系我们|外汇邦外汇论坛

GMT+8, 2024-4-29 03:58

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表