Plot financial data continuously in time

13 views (last 30 days)
Unfortunately couldn't find an existing topic on the following: I have different measures (e.g. returns) extracted from high frequency financial data. Stock markets run only during the day: I have a measure for each minute from 9:30 to 15:59 over different days. Data is stored in a timetable. I would like to plot the data against time in such a way that to 15:59 of day x follows 9:30 of day x+1. In this way the plot would look continuous w.r.t to the time range in which the data is available. By using plot(table.t,table.variable) i get the discontinuous series but the x axis is nicely labelled since table.t comes from a timetable object. Conversely, I would like to have a continuous plot like the one I would get with plot(table.variable), but here i am plotting the variable against its indexes, so no information about time is associated. I do NOT want to manually associate the x-axis indexes with time instances, work with xticks and xticklables to address the x axis correctly, this would not be fast to implement and intuitive plus I would end up loosing all the x axis dynamics coming from the timetable object. I think this couldn't be the right way to deal with this problem. Any suggestion on how to plot my data continuously in a simple and direct way? Thanks!
  1 Comment
dpb
dpb on 1 Mar 2018
Well, a time axis is what it is; the time values are what they are.
I don't see any way to fool it natively to behave as if certain time periods weren't extant. (Unless there's some specialized graphing tool in the Financial Toolbox designed for the purpose; I don't have it)
All I can see is to write the functionality to go between the index and associated time and use it to write the tick labels vis ordinal position to match. With call backs and all you should be able to make it work I'd think.

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 1 Mar 2018
dpb is correct that you can't do this with a datetime ruler. But you can get what you want, at least in a static version. It won't update the ticks when panning and zooming the way a native datetime plot would, but maybe that's not important to you.
Cook up something like the data I think you have:
>> t = datetime('today'):minutes(1):(datetime('today')+caldays(31));
>> t(ismember(day(t,'dayofweek'),[1 7])) = [];
>> t(timeofday(t)<hours(9.5)) = [];
>> t(hour(t)>=16) = [];
>> x = cumsum(randn(size(t)));
Plot vs. data index. In your case, the data are regular, at one minute intervals. If that wee not true, this step would have to take the irregularity into account.
>> plot(1:length(t),x)
Find some "nice" tick locations, e.g. each Monday. Again, for irregular data, this would be a little more complicated.
>> t0 = dateshift(dateshift(t(1),'dayofweek','monday','next'),'start','day');
>> t1 = dateshift(dateshift(t(end),'dayofweek','monday','previous'),'start','day');
>> tickDates = t0:calweeks(1):t1;
>> tickLocs = interp1(t([1 end]),[1 length(t)],tickDates,'linear','extrap');
Set those ticks and give them labels.
>> ax = gca;
>> ax.XTick = tickLocs;
>> ax.XTickLabel = cellstr(tickDates,'dd-MMM');
>> xlim(tickLocs([1 end])+[-.05 .05]*length(t))
>> axis tight
The next thing you might ask is to put some kind of dividing lines between the missing gaps. I think you'd just need to repeat what I did for the tick locations, except at 4:00pm each day, and maybe draw vertical lines or plot a red dot or whatever at each location.
  1 Comment
dpb
dpb on 2 Mar 2018
Unfortunately, Peter, I think updating the ticks when panning and zooming the way a native datetime plot would is the OP's chief plaint as the reason he doesn't like the ordinal axis results in his comment that "this would not be fast to implement and intuitive plus I would end up loosing [sic] all the x axis dynamics coming from the timetable object".
I've not had time to actually try the suggestion from earlier regarding trying to tie a routine to callbacks to see just how complex it would get to do so or if one runs into severe difficulties in trying.

Sign in to comment.

More Answers (1)

Pierre Lonfat
Pierre Lonfat on 12 Mar 2018
I have exactly the same issue but on a daily basis. I don't know why Matlab adds non existing days (i.e. gaps for the days where the stocks markets are closed in my case). It would be awesome if I can get some help! You can see the question on my page !
  1 Comment
dpb
dpb on 12 Mar 2018
Edited: dpb on 13 Mar 2018
The days are there; there just aren't data for those days. You wouldn't have us give up weekends just so you can have a continuous plot would you, I hope? < V,VBG >
As noted, add to the cacaphony of submitted enhancement requests.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!