Time series plot from structure array

7 ビュー (過去 30 日間)
Anthony Mukanya
Anthony Mukanya 2019 年 7 月 19 日
コメント済み: Anthony Mukanya 2019 年 7 月 20 日
Hello everyone,
I have a stock prices data from Yahoo Finance that I collected and placed in a structure array in the following manner:
clc
load('SP500stocks.mat'); %load data from a previously saved workspace
fields ={'Open', 'High', 'Low', 'Close', 'Volume'}; %create a variable fields
stocks_adj_close = rmfield(stocks, fields); % removes the fields mentionned here above
struct2csv(stocks_adj_close, 'SP500 Adjusted Stocks Prices (2000-20019).csv');
The result is:
stocks_adj_close =
1×503 struct array with fields:
Date
AdjClose
Ticker
The field "ticker" stores the ticker names of each SP500 companies and the period is from Jan-2000 to Jul-2019.
I would like to plot the stock prices of several comapnies in the same graph for a chosen period of my choice, say for instance AAPL, AIG, etc
How can I do that?
  2 件のコメント
Guillaume
Guillaume 2019 年 7 月 19 日
A mat file instead of a screenshot would allow us to experiment with the data...
What format is the date stored as in the cell array? datetime? datestr? datenum? something else?
Most of the Tickers have 4913 dates. Are they identical for all of them? For the tickers with less dates, is it a subset of the 4913 dates? If yes, to these it would be a lot simpler to convert the structure into a table or timetable with columns as tickers. Plotting is just one line of code.
Even if the answer is no, as long as there's significant overlap, I'd still consider using that table.
Anthony Mukanya
Anthony Mukanya 2019 年 7 月 19 日
To answer your questions, the date format is datenum. I'm publishing a small subset of the data for just 5 companies (see the mat-file) but for the original data set tickers will have different number of dates so it will not be possible to put them in a table (I think?)

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

採用された回答

Guillaume
Guillaume 2019 年 7 月 19 日
but for the original data set tickers will have different number of dates so it will not be possible to put them in a table (I think?)
Not a problem, as long as there's good overlap between the date. If they're completely distinct then it may be a waste of time.
Using your demo test file:
%assuming that you have a structure stocks in the workspace
numdateperticker = arrayfun(@(s) numel(s.Date), stocks);
tickers = table(datetime(vertcat(stocks.Date)), vertcat(stocks.AdjClose), repelem({stocks.Ticker}', numdateperticker), ...
'VariableNames', {'Date', 'AdjClose', 'Ticker'});
tickers = table2timetable(unstack(tickers, 'AdjClose', 'Ticker'))
Tickers for which there's no value for a date will get a NaN for that particular date.
Now to plot this for a given time range:
range = timerange('01-Jan-2016', '10-Apr-2018'); %whatever you want it to be
selectedtickers = {'AAPL', 'FB'};
tickersrange = tickers(range, :);
plot(tickersrange.Properties.RowTimes, tickersrange{:, selectedtickers});
legend(selectedtickers);
Or using stacked plots, even simpler:
range = timerange('01-Jan-2016', '10-Apr-2018');
selectedtickers = {'AAPL', 'FB'};
stackedplot(tickers(range, selectedtickers));
  3 件のコメント
Anthony Mukanya
Anthony Mukanya 2019 年 7 月 20 日
Hi |Guillaume,
I'm now trying to apply price2ret to calculate the daily returns for each stock during that predefined period. I tried the following commands:
stocks_ret = arrayfun(@(s) price2ret(s.AdjClose)*100, stocks_adj_close, 'UniformOutput',false)
Which returns a 1509*1 cell array and then when I do this (a variation of your code):
numdateperticker = arrayfun(@(s) numel(s.Date), stocks_adj_close)
tickers = table(datetime(vertcat(stocks_adj_close.Date)), vertcat(stocks_ret), repelem({stocks_adj_close.Ticker}', numdateperticker), ...
'VariableNames', {'Date', 'AdjClose', 'Ticker'});
Then I get the following error mistake (see picture):
Capture_matlab.JPG
Anthony Mukanya
Anthony Mukanya 2019 年 7 月 20 日
Sorry, wrong error message:
The message that I get says:
Capture_matlab_1.JPG

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFinancial Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by