I am trying to get a grasp on how to use machine learning to predict financial timeseries 1 or more steps into the future.
I have a financial timeseries with some descriptive data and I would like to form a model and then use the model to predict n-steps ahead.
What I have been doing so far is:
getSymbols("GOOG")
GOOG$sma <- SMA(Cl(GOOG))
GOOG$range <- GOOG$GOOG.High-GOOG$GOOG.Low
tail(GOOG)
GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted sma range
2013-05-07 863.01 863.87 850.67 857.23 1959000 857.23 828.214 13.20
2013-05-08 857.00 873.88 852.91 873.63 2468300 873.63 834.232 20.97
2013-05-09 870.84 879.66 868.23 871.48 2200600 871.48 840.470 11.43
2013-05-10 875.31 880.54 872.16 880.23 1897700 880.23 848.351 8.38
2013-05-13 878.89 882.47 873.38 877.53 1448500 877.53 854.198 9.09
2013-05-14 877.50 888.69 877.14 887.10 1579300 887.10 860.451 11.55
Then I have fitted a randomForest model to this data.
fit <- randomForest(GOOG$GOOG.Close ~ GOOG$sma + GOOG$range, GOOG)
Which seems to fit surprisingly well:
> fit
Call:
randomForest(formula = GOOG$GOOG.Close ~ GOOG$sma + GOOG$range, data = GOOG)
Type of random forest: regression
Number of trees: 500
No. of variables tried at each split: 1
Mean of squared residuals: 353.9844
% Var explained: 97.28
And tried to use it to predict:
predict(fit, GOOG, n.ahead=2)
But this prediction ofc did not work.
I try to predict the Close, should I lag the other variables by as many steps as I want the prediction, before fitting the model?
Probably a lot of other stuff I should take into account as well but these are really my first steps trying out machine-learning.