Perform calulations on moving window of data

6 次查看(过去 30 天)
I have stock data (Open, High, Low, Close), i would like to perform a calculation on a moving window of data
for eg, the average price on 10 bars of data - Yes i know matlab has some function, i want to do my own function not available by matlab.
I've investigated the moving windows function in matlab below, but the problem is that it centres the moving average windows i don't want that.
I want it to take the last 10 data points.
so for eg. if i have a 20 bars of data, then bar 20 uses its last 10 data points, and bar 19 uses its last 10 data points and so on.
What is the function i want to apply?
I want to apply a linear regression model, but i need it to only use the last X (150) bars of data.
The current problem is when a new data point comes in, the complete linear regression model can change, because it uses all the data points, so this makes past historical data useles, because yesterday's model was based on a different data set, so thats why i want it to use the last X data bars, so the output is consistent, what we say yesterday will always be that value yesterday regardless of what new data comes out today.
https://au.mathworks.com/help/matlab/ref/matlab.tall.movingwindow.html
  2 个评论
Scott MacKenzie
Scott MacKenzie 2021-6-27
I might help if you post an example data file. It seems you want a moving mean with a window size of 10 and you want to consider only the last 150 samples. Thus, you'd have 141 means (1-10, 2-11, 3-12, ... 141-150). Is this correct?
You've also tossed in something about a regression model. What are the (two) variables in the model?
Rizwan Khan
Rizwan Khan 2021-6-27
编辑:Rizwan Khan 2021-6-27
Hi Scott,
What you describe is correct, however, i want it to use the last 10 data points, not the next 10 data points.
So it would use data points 10-1, 11-2, 12-3, (for values 10, 11, and 12), and the mean value for the first 10 datapoints would be empty, as there is not 10 data points yet to use.
This ensure that data point 150 makes use of its last 10 data points (140-150) to calculate its mean.
Your thoughts on how i can achieve this would be appreciate it.

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2021-6-28
编辑:Matt J 2021-6-28
but the problem is that it centres the moving average windows i don't want that.
The function you reference is not limited to centered windows. The same is true, for example of movmean, movmax, and other sliding window functions in that family, e.g.,
M = movmean(A,[-9 0])
  4 个评论
Rizwan Khan
Rizwan Khan 2021-7-2
Im still a little confused, how would i use the movingWindow function to perform this action?
Matt J
Matt J 2021-7-2
编辑:Matt J 2021-7-2
In your post, you said that the function would help you if only IT allowed you to use non-centered windows. But it does, for example,
tA = matlab.tall.movingWindow(fcn,[150,0],tX)
will form the window from the previous 150 data points.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Financial Toolbox 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by