问题描述
基于每秒的点击数(ticksLastSecond
),我现在要计算平均在最后一分钟每秒的滴答声.为此,我需要一个大小为60的 rolling 数组,我假设每秒要使用最新的ticksLastSecond
值(最好在 >数组的开始(0)),然后是最早的一个.如何做到这一点?
Based on ticks per second (ticksLastSecond
), I would now like to calculate the average ticks per second over the last minute. For this, I would need a rolling array with size = 60 I assume, each second pushing the array elements forward by the newest ticksLastSecond
value (preferrably at the beginning (0) of the array) and the oldest one out. How could this be achieved?
非常感谢!
推荐答案
您可以使用数组,请牢记以下几点:如果您需要平均60(x)个值,则数组的大小可能为60(非常大)不切实际:您将需要将元素1到59每秒复制到0到58个)或120个(每分钟复制一次).因此,我认为120是首选,不需要更多.
you can use arrays, keeping in mind the following: if you need average of 60 (x) values, your array could be of size 60(which is very impractical: you will need to copy elements 1 to 59 into 0 to 58 each second) or 120(copying once per minute) more. So, 120 is preferred I think, more is of no need.
input int size = 60;
int array[];
ArraySize(array,2*size);
int cursor=0; //- shows position of the last element
void add(const int element){
if(cursor>=2*size-1)resize();
array[cursor++]=element;
}
void resize(){
ArrayCopy(array,array,0,size);
cursor=size;
}
//for array average: iMAOnArray() or manually:
double getAvg(){
if(cursor+1<size)return 0;
double sum=0;
for(int i=0;i<size;i++){
sum+=array[cursor-1-i];
}
return(sum/cursor);
}
还可以保留平均值的计算值,然后再相加,然后相减,这将进一步提高速度,考虑到回测的情况.这一切最好放在一个结构中.
also possible to keep computed value of average and then add last, subtract first-this will speed up even more, think in case of backtesting.This all might be better to put in a structure.
使用CArrayInt
或CArrayObj
同样但容易-在这种情况下,您不必担心大小,请使用方法Add()
和DeleteRange(0,size-1)
进行循环:Total() and
At(i);
Same but easier is use of CArrayInt
or CArrayObj
- in such case you do not worry about size, use methods Add()
and DeleteRange(0,size-1)
, for looping :Total() and
At(i);
另一种方法是在mql5中使用链接列表,并且可以轻松访问第一个和最后一个元素.它已经实现,因此请尝试CLinkedList<T> : public ICollection
此处
Another approach is to use a linked list in mql5, with easy access to first and last element. It is already implemented so try CLinkedList<T> : public ICollection
here
这篇关于滚动(静态)数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!