This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nikkei225 <- read.csv(file="H:\\NIKKEI.csv", encoding="UTF-8", header=F, sep=",") | |
names(nikkei225) <- c("date", "open", "high", "low", "close") | |
nikkei225$next_close <- c(nikkei225[-1,]$close, NA) | |
#以下の二行を追加した | |
nikkei225$next_open <- c(nikkei225[-1,]$open, NA) | |
nikkei225$diff <- nikkei225$next_open - nikkei225$next_close |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pred <- predict(rf.model, test) | |
#RのrandomForestはtypeに"prob"を指定することで事後確率の推定値が出力できる | |
prob <- predict(rf.model, test, type="prob") | |
#事後確率が90%を超えた予測だけを見てみる | |
idx <- which(prob[,1] >= 0.9 | prob[,2] >= 0.9) | |
tbl <- table(pred[idx], test$r_next_close[idx]) | |
print(sum(diag(tbl))/sum(tbl)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#日経平均株価のデータを読み込む | |
#使用したのは1991年1月4日から2014年5月16日までのデータ(たまたまHD内にすぐ使える状態で置いてあったためこれを使う) | |
nikkei225 <- read.csv(file="H:\\NIKKEI.csv", encoding="UTF-8", header=F, sep=",") | |
names(nikkei225) <- c("date", "open", "high", "low", "close") | |
#翌日の終値をcolumnに追加する | |
nikkei225$next_close <- c(nikkei225[-1,]$close, NA) | |
nikkei225 <- na.omit(nikkei225) | |
#四本値および翌日の終値を初値で割っておく。rはrelativeの略 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bh.ret <- | |
c( | |
1.329177, | |
1.089930, | |
0.859383, | |
0.931325, | |
1.030709, | |
0.979333, | |
0.841614, | |
0.678240, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function OLPS_cli(dataset) | |
cd Strategy; | |
opts.quiet_mode = 0; opts.display_interval = 245; | |
opts.log_mode = 1; opts.mat_mode = 1; | |
opts.analyze_mode = 0; opts.progress = 0; | |
opts.his = 0; | |
if (nargin<1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1301 | |
1332 | |
1377 | |
1518 | |
1801 | |
1802 | |
1803 | |
1812 | |
1815 | |
1820 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(randomForest) | |
library(RSQLite) | |
random.rows <- function(df, n) { | |
df[sample(nrow(df),n),] | |
} | |
fix.cb <- function(df) { | |
c1 <- df[df$finishing_position == "TRUE",] | |
c2 <- df[df$finishing_position == "FALSE",] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function OLPS_cli(dataset) | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
% This file is part of OLPS: http://OLPS.stevenhoi.org/ | |
% Original authors: Bin LI, Doyen Sahoo, Steven C.H. Hoi | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
cd Strategy; | |
opts.quiet_mode = 0; opts.display_interval = 245; | |
opts.log_mode = 1; opts.mat_mode = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#https://github.com/Marigold/universal-portfolios/blob/master/universal/algos/anticor.py | |
#ここから一部引用 | |
#時刻0からn-1までの全期間のポートフォリオの状態が計算される | |
def weights(self, X): | |
#期待値やラグ付き相関の計算で使うウィンドウのサイズ | |
window = self.window | |
#全銘柄の株価時系列データ(ただし前日比の相対価格データ) | |
port = X | |
#nは時系列の長さ。mは銘柄の数。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sp500 <- tail(sp500ret,n=500)$SP500RET | |
sigma <- df.eg[, 'Sigma', drop = FALSE]$Sigma | |
#トレーニング期間の分散を定数cに設定する | |
const <- sd(head(sp500ret,n=5000)$SP500RET)^2 | |
leverage <- const / sigma^2 | |
#buy&holdのリターン | |
prod(1 + sp500) | |
#OVSのリターン | |
prod(1 + sp500*leverage) |