R语言神经网络量化交易模型

本小妞迷上赌 提交于 2020-02-26 00:10:39

上篇是逻辑回归模型,这次我们用神经网络模型。再算一遍,试试看

# 载入示例股票

library(quantmod)
getSymbols("^DJI", src = "yahoo")
dji <- DJI[, "DJI.Close"]

# 生成技术指标

avg10 <- rollapply(dji, 10, mean)
avg20 <- rollapply(dji, 20, mean)
std10 <- rollapply(dji, 10, sd)
std20 <- rollapply(dji, 20, sd)
rsi5 <- RSI(dji, 5, "SMA")
rsi14 <- RSI(dji, 14, "SMA")
macd12269 <- MACD(dji, 12, 26, 9, "SMA")
macd7205 <- MACD(dji, 7, 20, 5, "SMA")
bbands <- BBands(dji, 20, "SMA", 2)

# 生成市场方向,收盘价与之后20天价格比较,上涨、下跌、横盘

direction <- data.frame(matrix(NA, dim(dji)[1], 1))
lagret <- (dji - Lag(dji, 20)) / Lag(dji, 20)
direction[lagret > 0.02] <- "Up"
direction[lagret < -0.02] <- "Down"
direction[lagret < 0.02 & lagret > -0.02] <- "NoWhere"

# 合并结果

dji <-
  cbind(dji,
        avg10,
        avg20,
        std10,
        std20,
        rsi5,
        rsi14,
        macd12269,
        macd7205,
        bbands)

# 准备训练集、验证集和测试集数据

train_sdate <- "2010-01-01"
train_edate <- "2013-12-31"

vali_sdate <- "2014-01-01"
vali_edate <- "2014-12-31"

test_sdate <- "2015-01-01"
test_edate <- "2015-12-31"

trainrow <-
  which(index(dji) >= train_sdate & index(dji) <= train_edate)
valirow <- which(index(dji) >= vali_sdate & index(dji) <= vali_edate)
testrow <- which(index(dji) >= test_sdate & index(dji) <= test_edate)

traindji <- dji[trainrow, ]
validji <- dji[valirow, ]
testdji <- dji[testrow, ]

trainme <- apply(traindji, 2, mean)
trainstd <- apply(traindji, 2, sd)

trainidn <- (matrix(1, dim(traindji)[1], dim(traindji)[2]))
valiidn <- (matrix(1, dim(validji)[1], dim(validji)[2]))
testidn <- (matrix(1, dim(testdji)[1], dim(testdji)[2]))

norm_traindji <-
  (traindji - t(trainme * t(trainidn))) / t(trainstd * t(trainidn))
norm_validji <-
  (validji - t(trainme * t(valiidn))) / t(trainstd * t(valiidn))
norm_testdji <-
  (testdji - t(trainme * t(testidn))) / t(trainstd * t(testidn))

traindir <- direction[trainrow, 1]
validir <- direction[valirow, 1]
testdir <- direction[testrow, 1]

#神经网络建模

library(nnet)

#set.seed(1)

model <- nnet(norm_traindji,
              class.ind(traindir),
              size = 4,
              trace = FALSE)

model
dim(norm_traindji)

#数据验证

vali_pred <- predict(model, norm_validji)
head(vali_pred)

vali_pred_calss <- data.frame(matrix(NA, dim(vali_pred)[1], 1))
vali_pred_calss[vali_pred[, "Down"] > 0.5, 1] <- "Down"
vali_pred_calss[vali_pred[, "NoWhere"] > 0.5, 1] <- "NoWhere"
vali_pred_calss[vali_pred[, "Up"] > 0.5, 1] <- "Up"

library(caret)
matrix <-
  confusionMatrix(as.factor(vali_pred_calss[, 1]), as.factor(validir))
matrix

#数据泛化能力测试

test_pred <- predict(model, norm_testdji)

test_pred_class <- data.frame(matrix(NA, dim(test_pred)[1], 1))
test_pred_class[test_pred[, "Down"] > 0.5, 1] <- "Down"
test_pred_class[test_pred[, "NoWhere"] > 0.5, 1] <- "NoWhere"
test_pred_class[test_pred[, "Up"] > 0.5, 1] <- "Up"

test_matrix <-
  confusionMatrix(as.factor(test_pred_class[, 1]), as.factor(testdir))
test_matrix

#模拟交易

signal <- ifelse(test_pred_class == "Up",
                 1,
                 ifelse(test_pred_class == "Down", -1, 0))

ret <- Delt(DJI[, "DJI.Close"])
ret <- ret[testrow]

cost <- 0
trade_ret <- ret * Lag(signal) - cost

library(PerformanceAnalytics)
cumm_ret <- Return.cumulative(trade_ret)
annual_ret <- Return.annualized(trade_ret)
charts.PerformanceSummary(trade_ret)

> model
a 15-4-3 network with 79 weights
options were -

> dim(norm_traindji)
[1] 1006   15

> head(vali_pred)
           Down    NoWhere        Up
2014-01-02    0 0.09929159 0.9494381
2014-01-03    0 0.09929054 0.9494395
2014-01-06    0 0.09928971 0.9494405
2014-01-07    0 0.09928971 0.9494405
2014-01-08    0 0.09928971 0.9494405
2014-01-09    0 0.09928971 0.9494405

> matrix
Confusion Matrix and Statistics

          Reference
Prediction Down NoWhere  Up
   Down      21       0   0
   NoWhere   18     128  11
   Up         0      13  57

Overall Statistics
                                          
               Accuracy : 0.8306          
                 95% CI : (0.7781, 0.8751)
    No Information Rate : 0.5685          
    P-Value [Acc > NIR] : < 2.2e-16       
                                          
                  Kappa : 0.6917          
                                          
 Mcnemar's Test P-Value : NA              

Statistics by Class:

                     Class: Down Class: NoWhere
Sensitivity              0.53846         0.9078
Specificity              1.00000         0.7290
Pos Pred Value           1.00000         0.8153
Neg Pred Value           0.92070         0.8571
Prevalence               0.15726         0.5685
Detection Rate           0.08468         0.5161
Detection Prevalence     0.08468         0.6331
Balanced Accuracy        0.76923         0.8184
                     Class: Up
Sensitivity             0.8382
Specificity             0.9278
Pos Pred Value          0.8143
Neg Pred Value          0.9382
Prevalence              0.2742
Detection Rate          0.2298
Detection Prevalence    0.2823
Balanced Accuracy       0.8830
> 
> test_matrix
Confusion Matrix and Statistics

          Reference
Prediction Down NoWhere  Up
   Down      22       2   0
   NoWhere   36     143   9
   Up         0       3  37

Overall Statistics
                                         
               Accuracy : 0.8016         
                 95% CI : (0.7469, 0.849)
    No Information Rate : 0.5873         
    P-Value [Acc > NIR] : 3.964e-13      
                                         
                  Kappa : 0.6117         
                                         
 Mcnemar's Test P-Value : NA             

Statistics by Class:

                     Class: Down Class: NoWhere
Sensitivity              0.37931         0.9662
Specificity              0.98969         0.5673
Pos Pred Value           0.91667         0.7606
Neg Pred Value           0.84211         0.9219
Prevalence               0.23016         0.5873
Detection Rate           0.08730         0.5675
Detection Prevalence     0.09524         0.7460
Balanced Accuracy        0.68450         0.7668
                     Class: Up
Sensitivity             0.8043
Specificity             0.9854
Pos Pred Value          0.9250
Neg Pred Value          0.9575
Prevalence              0.1825
Detection Rate          0.1468
Detection Prevalence    0.1587
Balanced Accuracy       0.8949
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!