Perform calulations on moving window of data

6 views (last 30 days)
Rizwan Khan
Rizwan Khan on 27 Jun 2021
Edited: Matt J on 2 Jul 2021
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 Comments
Scott MacKenzie
Scott MacKenzie on 27 Jun 2021
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 on 27 Jun 2021
Edited: Rizwan Khan on 27 Jun 2021
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.

Sign in to comment.

Answers (1)

Matt J
Matt J on 28 Jun 2021
Edited: Matt J on 28 Jun 2021
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 Comments
Rizwan Khan
Rizwan Khan on 2 Jul 2021
Im still a little confused, how would i use the movingWindow function to perform this action?
Matt J
Matt J on 2 Jul 2021
Edited: Matt J on 2 Jul 2021
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.

Sign in to comment.

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!