Skip to content

Instantly share code, notes, and snippets.

@stockedge
stockedge / stock.forecast.r
Last active January 10, 2016 12:33
事後確率と(翌日の初値 - 翌日の終値)の関係を調べる
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
@stockedge
stockedge / improved.forecast.r
Last active September 17, 2015 15:50
日経平均株価の予測。推定された事後確率でさらに精度を上げる。
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))
@stockedge
stockedge / stock.forecast.r
Last active September 19, 2015 08:19
日経平均株価を予測
#日経平均株価のデータを読み込む
#使用したのは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の略
@stockedge
stockedge / OLMAR_sr.R
Last active August 29, 2015 14:20
OLMARのシャープレシオを計算してみる。
bh.ret <-
c(
1.329177,
1.089930,
0.859383,
0.931325,
1.030709,
0.979333,
0.841614,
0.678240,
@stockedge
stockedge / OLPS_cli.m
Last active August 29, 2015 14:20
日本株に対してもOLMARが有効なのか実験してみた。
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)
@stockedge
stockedge / gist:f7bef0692e19cbe9941b
Created May 8, 2015 02:02
OLPSの検証に使用した銘柄のコード一覧。東証一部に上場している453銘柄を使った。検証期間は1995-05-09から2015-05-07までの4916営業日。
1301
1332
1377
1518
1801
1802
1803
1812
1815
1820
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",]
@stockedge
stockedge / OLPS_cli.m
Last active August 29, 2015 14:20
AnticorをMSCI (global) peroid 04/01/2006 - 03/31/2010のデータセットに対して適用した。手数料は0.1%で計算。市場平均との比較もあり。
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;
@stockedge
stockedge / anticor.py
Last active August 29, 2015 14:20
Anticorアルゴリズムの解説
#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は銘柄の数。
@stockedge
stockedge / OVS.r
Created April 29, 2015 13:56
Optimal Volatility Strategy
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)