Plot financial data continuously in time
16 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!
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.
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.
More Answers (1)
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 !