Asked by Martin Magris
on 1 Mar 2018

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!

Answer by Peter Perkins
on 1 Mar 2018

Accepted Answer

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.

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.

Answer by Pierre Lonfat
on 12 Mar 2018

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## dpb (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/385639-plot-financial-data-continuously-in-time#comment_540921

Sign in to comment.