1
$\begingroup$

I am trying to forecast future price using supervised machine learning. My logic is to take open and close price from t, t-1, t-2 and t-3 period to predict future close price in the period t+1,t+3 and t+5

library(quantmod)
symbol= getSymbols("AAPL",from="2010-03-01", auto.assign=F)
close<-Cl(symbol)
open<-Op(symbol)
lc1<-lag(close)
lc2<-lag(close,2)
lc3<-lag(close,3)
lo1<-lag(open)
lo2<-lag(open,2)
lo3<-lag(open,3)
X<-cbind(close,open,lc1,lc2,lc3,lo1,lo2,lo3)
Y<-need to assign close price for t+1,t+3 and t+5

I have created feature vectors using lag function for period 0,1,2 and 3 but my question is how to I assign target variable for t+1, t+3 and t+5 periods.

$\endgroup$
1
$\begingroup$

You need to assign each of the target variables to their own column and then train a model for each of your forecast horizons

library(quantmod)
symbol= getSymbols("AAPL",from="2010-03-01", auto.assign=F)
close<-Cl(symbol)
open<-Op(symbol)
lc1<-lag(close)
lc2<-lag(close,2)
lc3<-lag(close,3)
lo1<-lag(open)
lo2<-lag(open,2)
lo3<-lag(open,3)
X<-cbind(close,open,lc1,lc2,lc3,lo1,lo2,lo3)

Add outcome variables (Y)

X$t1<-lag(close,-1)
X$t3<-lag(close,-3)
X$t5<-lag(close,-5)

From this you can nou train 3 models to predict the closing of $t+n, n=1,..,n$

$\endgroup$
0
$\begingroup$

What you appear to be doing is actually an auto-regressive (AR) model, so perhaps you can benefit from using existing models like ARIMA R already has great implementations for.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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