Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have data which has the following appearance: data = {{DateList, Real}, ..., {DateList, Real}} It's easy to visualize the data by using DateListPlot. In addition to DateListPlot, I would also like to show a function which fits the data and the moving average of the data.

I hope I described the problem clear enough.

share|improve this question
You can convert the dates to numbers using AbsoluteTime; DateListPlot will still work and you can also do the fit and other manipulations. – b.gatessucks Oct 8 '12 at 9:29
DateListPlot will actually render faster if you convert to absolute time beforehand (incl. timing for the conversion). – Mike Honeychurch Oct 8 '12 at 10:38

3 Answers 3

up vote 11 down vote accepted

To fit a function and to calculate the moving average you need to convert your dates in absolute time using AbsoluteTime[].

data = FinancialData["IBM", "Jan. 1, 2004"];
newdata = 
    Table[{AbsoluteTime[data[[i, 1]]], data[[i, 2]]}, {i, Length[data]}];
lm = LinearModelFit[newdata, x, x];
movAvg = MovingAverage[newdata, 200];
Show[DateListPlot[newdata],DateListPlot[movAvg, PlotStyle -> Red],
    Plot[lm[x], {x, Min[newdata[[All, 1]]], Max[newdata[[All, 1]]]}], 
    Frame -> True]

DateListPlot with fit and moving average


This update implements the comment by Mike Honeychurch. Note that the moving average can be computed by averaging runs of only odd r elements.

movAvgDoneRight = MovingAverage[newdata[[All, 2]], 201];
elementsToDrop = (Length[newdata] - Length[movAvgDoneRight]);
movAvgData = Transpose[{Drop[
    Drop[newdata[[All, 1]], elementsToDrop/2], -elementsToDrop/2], 
Show[DateListPlot[newdata], DateListPlot[movAvgData, PlotStyle -> Red], 
    Plot[lm[x], {x, Min[newdata[[All, 1]]], Max[newdata[[All, 1]]]}], 
    Frame -> True]

DateListPlot with fit and moving average

share|improve this answer
Just a minor comment: You don't really need a table to get newdata. I find modifiying data in place using data[[All, 1]] = AbsoluteTime /@ data[[All, 1]]; more elegant :) Otherwise +1 – Ajasja Oct 8 '12 at 10:08
@Ajasja: ...unless you have to do something after plotting/fitting that needs the date lists as opposed to the absolute times. – J. M. Oct 8 '12 at 10:10
I'd interpret moving average in this context as meaning just of the data -- not of the dates (times) as well. – Mike Honeychurch Oct 8 '12 at 10:36
Thx, exactly what I've been looking for. – RMMA Oct 8 '12 at 10:53
@VLC trim the dates (times) and transpose with the averaged data. – Mike Honeychurch Oct 8 '12 at 11:45

Another possibility is to use TradingChart or InteractiveTradingChart, which have a long list of statistical indicators :

InteractiveTradingChart[{"IBM", {"Jan. 1, 2004"}}]


share|improve this answer

For data in the form { ..., {date_i, value_i}, ... } you should consider using TimeSeries.

For example:

data = FinancialData["IBM", "Jan. 1, 2015"];


ts = TimeSeries[data]

enter image description here

And then you can operate on the time series directly, with functions like MovingAverage, and DateListPlot:

newts = MovingAverage[ts, 5]

enter image description here

DateListPlot[ newts]

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.