以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://weistock.com/bbs/index.asp)
--  公式模型编写问题提交  (http://weistock.com/bbs/list.asp?boardid=4)
----  如何筛选出“主流”平均值?  (http://weistock.com/bbs/dispbbs.asp?boardid=4&id=66948)

--  作者:yanxc
--  发布时间:2014/7/4 12:26:04
--  如何筛选出“主流”平均值?

例如,过去几天的增长率分别是:0.06、0.05、0.05、0.07、0.01、0.07、0.08、0.09、0.06、-0.02、0.07、0.08

 

希望把距离其他值最远的0.01、-0.02去掉,或者直接取得最“主流”的值0.07(它与其他数值的差距都在0.02之内,而0.01、-0.02距离大家都太远,视为异常)。

 

比较笨的算法,就是计算所有数值之间的差,“差<=0.02”的次数最多的那个值就是我们要找的数,在这里就是0.07

 

请问这个代码该怎么写呢?

或者有没有简单的函数可以直接表达这个意思?即计算抛弃掉与其他值的差比较大的少数值之后的平均值。


--  作者:jinzhe
--  发布时间:2014/7/4 13:16:07
--  

把这个赋值给一个数组

然后用sort排序

获取中间的数值


--  作者:yanxc
--  发布时间:2014/7/4 14:48:10
--  
以下是引用jinzhe在2014/7/4 13:16:07的发言:

把这个赋值给一个数组

然后用sort排序

获取中间的数值

能举个例子吗?如何获取“中间”的数值


--  作者:jinzhe
--  发布时间:2014/7/4 14:55:01
--  

比如你数组总共有10个元素,那么在排序之后就使用第五个元素

 


--  作者:yanxc
--  发布时间:2014/7/4 15:18:36
--  
以下是引用jinzhe在2014/7/4 14:55:01的发言:

比如你数组总共有10个元素,那么在排序之后就使用第五个元素

 


我要的并非这个结果啊。你这个是取中值。

 

我要的是排除掉离“主体”比较远的值,取剩下主流部分的均值。


--  作者:yanxc
--  发布时间:2014/7/4 15:22:15
--  

比如以一周的 H-ref(H,1) 值来排序,其中2个特别离谱,离得很远,就不要了。剩下的5个比较接近,取平均值。

如何做?


--  作者:jinzhe
--  发布时间:2014/7/4 15:44:04
--  

举个简单的例子,如果你的元素太多太复杂的话,那么这样的也没用了

 

variable:ss=0,kk=0;
hh:H-ref(H,1);
h1:ref(hh,1);
h2:ref(hh,2);
h3:ref(hh,3);
h4:ref(hh,4);
if (hh-h1)<=0.02 and (hh-h2)<=0.02 and (hh-h3)<=0.02 and (hh-h4)<=0.02 then begin
      ss:=ss+hh;
      kk:=kk+1;
end

if (h1-hh)<=0.02 and (h1-h2)<=0.02 and (h1-h3)<=0.02 and (h1-h4)<=0.02 then begin
      ss:=ss+h1;
      kk:=kk+1;
end
if (h2-h1)<=0.02 and (h2-hh)<=0.02 and (h2-h3)<=0.02 and (h2-h4)<=0.02 then begin
      ss:=ss+h2;
      kk:=kk+1;
end

if (h3-h1)<=0.02 and (h3-h2)<=0.02 and (h3-hh)<=0.02 and (h3-h4)<=0.02 then begin
      ss:=ss+h2;
      kk:=kk+1;
end
if (h4-h1)<=0.02 and (h4-h2)<=0.02 and (h4-h3)<=0.02 and (h4-hh)<=0.02 then begin
      ss:=ss+h4;
      kk:=kk+1;
end
avg:ss/kk;


--  作者:yanxc
--  发布时间:2014/7/5 13:10:53
--  
以下是引用jinzhe在2014/7/4 15:44:04的发言:

举个简单的例子,如果你的元素太多太复杂的话,那么这样的也没用了

 

variable:ss=0,kk=0;
hh:H-ref(H,1);
h1:ref(hh,1);
h2:ref(hh,2);
h3:ref(hh,3);
h4:ref(hh,4);
if (hh-h1)<=0.02 and (hh-h2)<=0.02 and (hh-h3)<=0.02 and (hh-h4)<=0.02 then begin
      ss:=ss+hh;
      kk:=kk+1;
end

if (h1-hh)<=0.02 and (h1-h2)<=0.02 and (h1-h3)<=0.02 and (h1-h4)<=0.02 then begin
      ss:=ss+h1;
      kk:=kk+1;
end
if (h2-h1)<=0.02 and (h2-hh)<=0.02 and (h2-h3)<=0.02 and (h2-h4)<=0.02 then begin
      ss:=ss+h2;
      kk:=kk+1;
end

if (h3-h1)<=0.02 and (h3-h2)<=0.02 and (h3-hh)<=0.02 and (h3-h4)<=0.02 then begin
      ss:=ss+h2;
      kk:=kk+1;
end
if (h4-h1)<=0.02 and (h4-h2)<=0.02 and (h4-h3)<=0.02 and (h4-hh)<=0.02 then begin
      ss:=ss+h4;
      kk:=kk+1;
end
avg:ss/kk;

 

这个方法有问题啊。

 

实际情况是,可能没有一个值 是与其他每个值的差都小于0.02的。

必须“抛弃”某一两个偏离主流数据比较远的值,才找得出我们想要的那个值。

 

比如顶楼的0.01就远离了其他值的区间,需要去掉它才行。


--  作者:jinzhe
--  发布时间:2014/7/7 9:29:09
--  
那这样就不行了,你要的东西太智能了,你去高级区发帖问问
--  作者:jinzhe
--  发布时间:2014/7/7 9:30:05
--  

或者是

if kk=0 then drawtext(islastbar,close,\'没有满足符合项\');

if kk<>0 then avg:ss/kk;