필터 지우기
필터 지우기

Fints - extract x years historical prices

조회 수: 7 (최근 30일)
lao
lao 2021년 9월 13일
답변: Jaynik 2024년 6월 27일 6:00
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

답변 (1개)

Jaynik
Jaynik 2024년 6월 27일 6:00
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!

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by