如何监控指标中 变量的值?

 

请老师们指教:

我的这个指标似乎有很大的问题,指标中的变量 pos 的值在平台的汇率自动更新时好象不对劲,出来的指标线结果也是错的,问题出自哪里呢?是不是平台对变量 pos 的赋值有自己的规定?

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Yellow
//---- input parameters
int Cbars,pos;
double top[],aa[];
double m,Grd;
//+------------------------------------------------------------------+20
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(2);
   SetIndexBuffer(0,top);
   SetIndexBuffer(1,aa);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i,j;
   Cbars=IndicatorCounted();
   if (Cbars<0) return(-1);
   if (Cbars>0) Cbars--;
//----------------------------------------
   for (i=Bars-Cbars-1;i>0;i--)
     {
      aa[i]=10*MathAbs(Close[i]-Close[i+1]);
      m=0;
      for (j=i+99;j>=i;j--) m+=aa[j];
      Grd=m/100;
      if (MathAbs(Close[i]-Close[pos])>=Grd) pos=i;
      top[i]=Close[pos];
     }
   return(0);
  }
 

int start ()
{
int i, j ;
Cbars = IndicatorCounted () ;
if ( Cbars < 0 ) return ( - 1 ) ;
if ( Cbars > 0 ) Cbars --;
//----------------------------------------
for ( i = Bars - Cbars - 1 ; i > 0 ; i -- ) // 这里有点问题,应该是 for ( i = Bars - Cbars - 1 ; i >= 0 ; i -- ) 这样才能让第一个数据放进去第一个buffer中
{
aa [ i ] = 10 * MathAbs ( Close [ i ] - Close [ i + 1 ]) ;
m = 0 ;
for ( j = i + 99 ; j >= i ; j -- ) m += aa [ j ] ;
Grd = m / 100 ;
if ( MathAbs ( Close [ i ] - Close [ pos ]) >= Grd ) pos = i ; //--- Close [ pos ] 不知道你想要取哪个值

top [ i ] = Close [ pos ] ;
}
return ( 0 ) ;
}


我也才学几天而已,如果没能为你解决问题不要怪我哈

我想这次应该会对了

希望大家都能相互交流,大家都会进步的更快

 

哈哈,谢谢你和我交流,我们都是初学者,只要大家能坦诚交流,没什么怪不怪的问题,也许我提的问题太低级,高手们觉得没什么意思。

看来只有我们这帮雏互相勉励了。

如果你把我的代码在平台上运行一下,就明白我的意图了!

 

如果 if ( MathAbs ( Close [ i ] - Close [ pos ]) >= Grd ) 条件不满足, pos =?

Print(pos);可以在日志窗口看到记录

 

谢谢 DxdCn !

如果条件不满足,pos 将保持之前的取值。

如果这个指标不用 indicatorcounted() ,出来的指标线就是我所期望的,用了 indicatorcounted() 以后,当汇率更新时,pos 的值就不是我所期望的了。

我想,是不是在用 if() 条件语句时,如果不用 else { }, 平台将会给 pos 赋别的值?

 

再次感谢 CxdCn !

你的回应提醒了我,我好象明白我的指标的问题在哪里了。

我改好后再提交你审视。

 

平台将会给 pos 赋别的值?

平台没有修改你的pos,但是平台修改了Close[]数组的下标和元素值的对应关系,因为MT设计当前时间和价格永远对应下标0,所以你原来保存的pos跟新的下标不同了。

如果当前:

0123456789......

abcdefghij....

新时间周期:

012345678......

xabcdefg......

象排队,编号虽然不变,排队的内容(人)随时间移动.

 

谢谢 DxdCn !!

问题已经解决,具体做法是:

.........

int init ()

...

a=Bars;

...

return(0);

int srtart ()

.............

b=a;

a=Bars;

if (a==b) count=0; else count=1;

...

return(0);

请审视。