Skip to content

Instantly share code, notes, and snippets.

@stockedge
stockedge / JoinOdds.scala
Created April 23, 2015 03:44
oddsportal scraper with casperjs
def toDouble(s: String) = {
if (s.contains("/")) {
val Array(a, b) = s.split("/");
a.toDouble / b.toDouble
} else
s.toDouble
}
val lines = List(
io.Source.fromFile("odds2014.txt").getLines.toList,
@stockedge
stockedge / UfocatchScraper.scala
Created April 23, 2015 04:02
ufocatch scraper
import org.apache.commons.io.FileUtils
import java.io.File
import java.net.URL
import java.lang.Thread
val xbrlFolder = new File("xbrl")
for (code <- 1301 to 9997) {
println(code)
@stockedge
stockedge / FiscoScraper.scala
Last active August 29, 2015 14:19
フィスコマーケットマスターズの記事をSeleniumでスクレイピング
import java.lang.Thread
import java.net.URL
import java.io.File
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.By
import org.apache.commons.io.FileUtils
val mail = "enter your email address"
val password = "enter your password"
val maxArticleId = 1477 // Please manually set maxArticleId
@stockedge
stockedge / garch_aic_example.r
Last active August 29, 2015 14:20
garchとegarchでsp500のデータ使ってaicを比較してみる
require(rugarch)
#SP500の日足データを使う。
data(sp500ret)
#まずは通常のGARCH(1,1)。
spec.sg <- ugarchspec(
variance.model = list(model = "sGARCH", garchOrder=c(1,1)),
#平均値のモデルにはARIMA(1,1,1)を使う。
mean.model = list(armaOrder = c(1,1,1), arfima = T),
@stockedge
stockedge / vola_pred.r
Last active August 29, 2015 14:20
S&P500のボラティリティを予測する
roll.eg <- ugarchroll(
spec.eg, sp500ret,
#100日毎にパラメータを推定しなおす。
refit.every = 100,
#一日先の値を予測する。
n.ahead = 1,
#予測開始地点を指定するにはforecast.lengthかn.startのどちらかを使う。
n.start = nrow(sp500ret)-500, #5023日目から予測を始める。sp500retには5523日分のデータが入っている。
refit.window = "moving", solver = "hybrid", keep.coef = TRUE)
@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)
@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 / 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;
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 / 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