- Converting from fints to timetables: https://www.mathworks.com/help/finance/convert-from-fints-to-timetables.html
- years: https://www.mathworks.com/help/matlab/ref/duration.years.html
- datetime: https://www.mathworks.com/help/matlab/ref/datetime.html
Fints - extract x years historical prices
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a set of historical prices for MSFT for the last 20 years, from 2001 to 2021. To create the timeseries, I'm using "fints"
How should I retrieve the most recent 5 years of data from a time series, knowing that the reference start date is "31/08/2021"?
I tried using the "fetch" function, but this requires knowing the time series' end date.
Do you have any ideas/suggestions about what function or approach to take in this situation?
Many thanks,
L
0 个评论
回答(1 个)
Jaynik
2024-6-27
Hi,
Since R2018a, 'fints' has been replaced with 'timetable' which can be used directly as table.
Here is a sample code to get data of last 5 years using the reference date with the help of a 'timetable' object:
% Assuming that the timetable TT has properties Time, Price
% Convert your reference date to a datetime
refDate = datetime('31/08/2021', 'InputFormat', 'dd/MM/yyyy');
% Calculate the date 5 years prior to the reference date
startDate = refDate - years(5);
% Create a logical vector spanning the desired date range to index into the data
timeVector = (startDate <= TT.Time) & (TT.Time <= refDate);
recentData = TT(timeVector,:);
If you are using the Financial Time Series (fints) object in MATLAB, you can use the 'fints' object’s tsobj property to access the time series data. The tsobj property is a MATLAB timeseries object, which has a Time property that you can use to index into the time series data. Overall code remains the same, just changing the object type of input data:
% Assuming 'ft' is your fints object
ts = ft.tsobj; % Get the timeseries object
refDate = datetime('31/08/2021', 'InputFormat', 'dd/MM/yyyy');
startDate = refDate - years(5);
timeVector = (startDate <= ts.Time) & (ts.Time <= refDate);
recentData = ts.Data(timeVector);
In the code, recentData will contain the data for the most recent 5 years of the time series. Note that this code assumes that the Time property of the tsobj timeseries object is in datetime format. If its not, you may need to convert it using the 'datetime' function.
Refer to the following documentation for better understanding:
Hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!