How to calculate moving average excluding current data?

How to calculate moving average to not include current data for the current period. For example, I'm trying to create the Donchian Channel and the formula is the 20 high or 20 low of the preceding date. If so, this should not include the current date. All the moving and tsmovavg includes the current date.

回答 (1 件)

dpb
dpb 2016 年 11 月 27 日

0 投票

N=20; % number above/below midpoint
u=[ones(1,N) 0 ones(1,N)]/(2*N-1); % weighting
mAvg0=conv(data,u,'valid');

4 件のコメント

liu James
liu James 2016 年 11 月 27 日
I dont understand what is being done here. can you explain a little more
dpb
dpb 2016 年 11 月 27 日
Have you read
doc conv
and tried a small example? It's simply the convolution of the data with the u vector which is [1 1 ... 0 ... 1 1] weighted by the number of nonzero terms. IOW, you're multiplying each subsection of the data of length 2N+1 by 1/2N, the proportional weight for a mean of 2N points.
Image Analyst
Image Analyst 2016 年 11 月 27 日
It's just considering the N points to the right and left of the current point. The center point (i.e. the "current" point) has a weight of zero so it's not included in the sum.
dpb
dpb 2016 年 11 月 28 日
>> [x;[nan conv(x,[1 0 1]/2,'valid') nan]]
ans =
1 2 3 4 5 6 7 8 9 10
NaN 2 3 4 5 6 7 8 9 NaN
>>
should make it patently clear, one would think???

サインインしてコメントする。

カテゴリ

質問済み:

2016 年 11 月 27 日

コメント済み:

dpb
2016 年 11 月 28 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by