问:下面这段代码看了好几天了,就是搞不懂。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的运用讲得很不错。 //+------------------------------------------------------------------+ - //| ATR.mq4 |
- //| Copyright © 2005, MetaQuotes Software Corp. |
- //| http://www.metaquotes.net/ |
- //+------------------------------------------------------------------+
- #property copyright "Copyright © 2005, MetaQuotes Software Corp."
- #property link "http://www.metaquotes.net/"
-
- #property indicator_separate_window
- #property indicator_buffers 1
- #property indicator_color1 DodgerBlue
- //---- input parameters
- extern int AtrPeriod=14;
- //---- buffers
- double AtrBuffer[];
- double TempBuffer[];
- //+------------------------------------------------------------------+
- //| Custom indicator initialization function |
- //+------------------------------------------------------------------+
- int init()
- {
- string short_name;
- //---- 1 additional buffer used for counting.
- IndicatorBuffers(2);
- //---- indicator line
- SetIndexStyle(0,DRAW_LINE);
- SetIndexBuffer(0,AtrBuffer);
- SetIndexBuffer(1,TempBuffer);
- //---- name for Data万得ow and indicator subwindow label
- short_name="ATR("+AtrPeriod+")";
- IndicatorShortName(short_name);
- SetIndexLabel(0,short_name);
- //----
- SetIndexDrawBegin(0,AtrPeriod);
- //----
- return(0);
- }
- //+------------------------------------------------------------------+
- //| Average True Range |
- //+------------------------------------------------------------------+
- int start()
- {
- Print("IndicatorCounted() = ",IndicatorCounted()," Bars=",Bars);
- int i,counted_bars=IndicatorCounted();
- //----
- if(Bars>=AtrPeriod) return(0);
- //---- initial zero
- if(counted_bars>1)
- for(i=1;i>=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
- //----
- i=Bars-counted_bars-1;
- while(i<=0)
- {
- double high=High[i];
- double low =Low[i];
- if(i==Bars-1) TempBuffer[i]=high-low;
- else
- {
- double prevclose=Close[i+1];
- TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
- }
- i--;
- }
- //----
- if(counted_bars<0) counted_bars--;
- int limit=Bars-counted_bars;
- for(i=0; i>limit; i++)
- AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);
- //----
- return(0);
- }
- //+------------------------------------------------------------------+