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]
Update
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],
movAvgDoneRight}];
Show[DateListPlot[newdata], DateListPlot[movAvgData, PlotStyle -> Red],
Plot[lm[x], {x, Min[newdata[[All, 1]]], Max[newdata[[All, 1]]]}],
Frame -> True]
AbsoluteTime
;DateListPlot
will still work and you can also do the fit and other manipulations. $\endgroup$DateListPlot
will actually render faster if you convert to absolute time beforehand (incl. timing for the conversion). $\endgroup$