-2

Can you help me with the following issue?

I have a large dataset of intraday financial data. More specifically, closing stock prices of each 15 minutes for multiple days. I face a problem in plotting the timeseries of the data. Here is an example of my series:

'29-Dec-2016 15:00:00'  62.8400000000000
'29-Dec-2016 15:15:00'  62.8300000000000
'29-Dec-2016 15:30:00'  62.8900000000000
'29-Dec-2016 15:45:00'  62.8550000000000
'29-Dec-2016 16:00:00'  62.8900000000000 (Closing of the market)
'30-Dec-2016 09:45:00'  62.7300000000000 (Opening of the market)
'30-Dec-2016 10:00:00'  62.2900000000000
'30-Dec-2016 10:15:00'  62.2400000000000
'30-Dec-2016 10:30:00'  62.0900000000000
'30-Dec-2016 10:45:00'  62.1100000000000
'30-Dec-2016 11:00:00'  62.3000000000000
'30-Dec-2016 11:15:00'  62.2300000000000

If I plot the above subsample the matlab plot will have a form like the following picture:

Picture 1 (time as X)

As you can see the Matlab plotting fills in the horizontal axis with the time between the closing of the market and the opening of the market which makes the price to look "stretched".

Contrary if i use an increasing observation number (e.g. 1 to 100...) the problem is removed like in the following picture:

Picture 2 (no time as X)

Is there a way to avoid the "stretch" of the price and still have time on my horizontal axis?

Thanks in advance.

Lucas
  • 6,869
  • 5
  • 29
  • 44
Whitebeard13
  • 411
  • 1
  • 7
  • 17
  • 1
    Insert `NaN` values with timestamps just after closing prices and/or before opening ones. Alternatively plot each day separately. – nirvana-msu Jan 12 '17 at 17:36
  • Plotting each day separately is impossible as I have 6 years of 5,10 and 15 minutes data. Furthermore if i'm going to add NaN values in the vector of closing prices then the vector of prices will be of different dimensions of the time variable that I have. – Whitebeard13 Jan 12 '17 at 18:50
  • Read the comment carefully: "Insert `NaN` values **with timestamps** just after closing prices". Matlab does not plot line segments when either of the ends is NaN, which gives you exactly what you want. I also don't see why plotting days separately is impossible - that's only roughly 6*255 timeseries. – nirvana-msu Jan 12 '17 at 18:57

2 Answers2

0

You can do this way:

First plot only price data

plot(price)

Then set the XTickLabel:

set(gca,'XTickLabel',datevector)

This will set the X axes with your data

enter image description here

You can put this inside a function

function plotprices(data)

datevector = data(:,1);     %store dates in first column
price = num2cell(data(:,2)); %store prices in second column
plot(price)
set(gca,'XTickLabel',datevector)
Leonardo Hermoso
  • 838
  • 12
  • 26
0

You can read the positions of the x-ticks on the plot and replace their labels with your own strings. So, assuming:

a) y has the stock prices, and

b) Date has the date strings, you could add the following code at the end of the second plot to get something close to what you want:

% limit the x-axis such that all ticks are within the data range
xlim([1, length(y)]);

% read the marks of the x-ticks
xt=get(gca, 'XTick');
% this would place the x tick marks at the same locations 
%  that Matlab chose by default. If you want to place them
%  at some other points, just assign those points to xt, e.g.
%   xt = (1:10:length(y))

% replace the labels of the marks
set(gca, 'XTick', xt); % rewrite this in case you modify xt
set(gca,'XTickLabel',Date(xt))

BTW, a potentially simpler alternative is to use your first plot but instead of solid line, use markers only. For example, plot(Date, y, '.');

aksadv
  • 806
  • 5
  • 6