-
Notifications
You must be signed in to change notification settings - Fork 32
/
ML - SL - Logistic Regression - Research.R
66 lines (49 loc) · 1.57 KB
/
ML - SL - Logistic Regression - Research.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#Logistic Regression - Research
setwd("~/Quant Finance")
library(quantmod)
library(TTR)
library(PerformanceAnalytics)
library(tseries)
library(zoo)
library(xts)
library(dplyr)
library(knitr)
#Get the data
getSymbols('GOOG', src = 'yahoo', from = "2016-01-01", to = '2020-01-01')
GOOG <- GOOG[,'GOOG.Close']
#Get indicators
bbands <- BBands(Cl(GOOG))
rsi14 <- RSI(Cl(GOOG))
rsi5 <- RSI(Cl(GOOG))
mean10 <- SMA(GOOG, n = 10)
mean20 <- SMA(GOOG, n = 20)
macd7205 <- MACD(GOOG, 7, 20, 5, 'SMA')
macd12269 <- MACD(GOOG, 12, 26, 9, 'SMA')
#create direction of price (up or down) depending on whether current price
#is greater or lower than the previous 20 days price.
direction <- NA
direction[GOOG > Lag(GOOG, 20)] <- 1
direction[GOOG < Lag(GOOG, 20)] <- 0
#bind everything into one dataset
GOOG <- cbind(GOOG$GOOG.Close, mean10, mean20, bbands,
rsi14, rsi5, macd7205, macd12269, direction)
GOOG <- na.omit(GOOG)
#normalize data
GOOG <- scale(GOOG, 1:13)
#split into training and testing sets
train <- GOOG[1:700]
test <- GOOG[701:973]
logistic <- glm(train$direction ~ train$SMA + train$rsi + train$macd,
family = binomial)
summary(logistic)
logistic.probs <- predict(logistic,
newdata = test,
type = "response")
summary(logistic.probs)
#logistic.pred is going to be a vector of trues and falses.
#So if logistic.probs is bigger than 0.5, logistic.pred calls "Up" (1)
#Otherwise, it calls "Down"(0).
logistic.pred <- ifelse(logistic.probs > 0.5, 1, 0)
table(logistic.pred)
table(logistic.pred, train$direction)
#86% accuracy