(非常)使用逻辑回归的简单量子交易模型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(非常)使用逻辑回归的简单量子交易模型相关的知识,希望对你有一定的参考价值。

我一直在玩R中的quantstrat回测试包,我想得到一些特别(差)策略的建议。

只要逻辑回归模型告诉我市场将会上涨(在prediction专栏中显示为1),这个想法就是买入。每天逻辑回归告诉我市场将会上涨,我买了.orderqtf = 10的谷歌股票。逻辑回归告诉我价格将下降的那一天(在prediction专栏中以0表示)然后我们将所有当前股票转储到谷歌并重新开始,直到它告诉我们购买。

问题:

我的代码是否正确我所描述的内容?

如果你注意到我已经滞后了两个输入变量。即momentum(lag(GOOG$close), n = 12)

那就是我想用t-1天预测一天t。这也正确吗?我不想使用任何可能为预测提供bia结果的指标

对我来说,quantstrat包似乎有一点学习曲线所以我只想确保我得到基本的正确。

该模型:

rm(list=ls())
require(quantstrat)
require(PerformanceAnalytics)

set.seed(1234)

#setting up some initial parameters for the quantstrat trading model
initDate="2007-01-01"
from <- "2017-01-01"
to <- "2018-12-01"
init_equity <- 1000
adjustment <- TRUE

.orderqty <- 10
.txnfees <- -10

currency('USD')
Sys.setenv(TZ="UTC")

#Collect the data
symbols <- c('GOOG')
getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE)  

colnames(GOOG) <- c("open", "high", "low", "close", "volume", "adjusted")

# create the dependent variable for a logistic regression
GOOG$direction <- with(GOOG, ifelse(close >= open, 1, 0))

#create two basic input variables - lagged
GOOG$rsi <- RSI(lag(GOOG$close), nFast=14, nSlow = 26, nSig = 9, maType = SMA)
GOOG$momentum <- momentum(lag(GOOG$close), n = 12)

GOOG <- GOOG[complete.cases(GOOG), ] 

# create a training and test set
train_date <- nrow(GOOG) *0.8
train <- GOOG[1:train_date,]
test <- GOOG[-c(1:train_date),]

#Run a simple logistic regression and obtain predicted probabilities
lm.fit <- glm(direction ~ rsi + momentum, data = train, family = binomial)
summary(lm.fit)
pr.lm <- predict(lm.fit, test, type = "response")


# Extract the OHLC from the GOOG stock and match it with the test dates
TEST <- subset(GOOG, index(GOOG) %in% index(test))

#Add out predictions to the TEST data if its greater than 0.6
TEST$prediction <- ifelse(pr.lm > 0.6, 1, 0)

paste0("Accuracy", mean(TEST$direction == TEST$prediction))

# Now that we have a strategy we want to buy everytime the logistic model states that
# the direction would be a "1"

# Setting up the strategy
GOOG <- TEST
stock("GOOG", currency="USD", multiplier=1)
strategy.st <- portfolio.st <- account.st <- "LogisticRegressionStrategy"
rm.strat(strategy.st)
rm.strat(portfolio.st)
rm.strat(account.st)

initPortf(name = portfolio.st,
          symbols = symbols, 
          initDate = initDate, 
          currency = 'USD')

initAcct(name = account.st, 
         portfolios = portfolio.st, 
         initDate = initDate, 
         currency = 'USD',
         initEq = init_equity)

initOrders(portfolio.st,
           symbols = symbols,
           initDate = initDate)

strategy(strategy.st, store = TRUE)


# Adding the rules, enter at the low price when "prediction" = 1, taking transaction fees into account
add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "prediction",
                          sigval = 1,
                          orderqty = .orderqty,
                          ordertype = "market",
                          #orderside = "long", 
                          prefer = "Low", 
                          TxnFees = .txnfees, 
                          replace = FALSE),
         type = "enter",
         label = "EnterLONG")

# As soon as the Logistic regression predicts a "0" we dump all our shares in GOOG

add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "prediction", 
                          sigval = 0, 
                          #orderside = "short", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = .txnfees, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2SHORT")


applyStrategy(strategy.st, portfolios = portfolio.st)

updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)

chart.Posn(portfolio.st, Symbol = "GOOG", 
           TA="add_SMA(n = 10, col = 2); add_SMA(n = 30, col = 4)")

以上是关于(非常)使用逻辑回归的简单量子交易模型的主要内容,如果未能解决你的问题,请参考以下文章

逻辑回归 vs 朴素贝叶斯

请你介绍下Logistic回归模型?

数据挖掘系列 啥是逻辑回归训练模型?

逻辑回归算法实现_基于R语言

机器学习逻辑回归介绍

机器学习基础---神经网络(属于逻辑回归)(构建假设函数)