ギャップ戦略②

通貨ペア:AUDUSD
期間:2002年1月1日~2013年12月6日
ルール①:当日始値が前日高値*(1+閾値*前日標準偏差)より上で買い、前日安値*(1-閾値*前日標準偏差)より下で売り。
ルール②:ポジションは当日終値で決済。
備考①:四本値は欧州時間に基づく。
備考②:約定価格は当日始値。
備考③:損益の単位は%。
備考④:コストは考慮していない。
備考⑤:遺伝的アルゴリズムにより、標準偏差の計算期間は5~250、閾値は0.0~0.1の範囲で利益を最大化するパラメータを求める。

# パッケージの呼び出し
library(quantmod)
# 開始日
startDate <- as.Date("2002-01-01")
# 終了日
endDate <- as.Date("2013-12-06")
# 始値
op <- data_EUR$AUDUSD_EUR.Open
# 高値
hi <- data_EUR$AUDUSD_EUR.High
# 安値
lo <- data_EUR$AUDUSD_EUR.Low
# 終値
cl <- data_EUR$AUDUSD_EUR.Close
# 変化率
deltaCC <- diff(cl) / lag(cl) * 100
# 終値/始値の変化率
deltaCO <- (cl / op - 1) * 100
# トレード関数
f <- function(x1, x2) {
# 計算期間
lookback <- x1
# 閾値
threshold <- x2
# 標準偏差
std <- rollapply(deltaCC, lookback, sd)
# 順張り
long <- op > lag(hi) * (1 + threshold * lag(std))
short <- op < lag(lo) * (1 - threshold * lag(std))
# 逆張り
#long <- op < lag(lo) * (1 - threshold * lag(std))
#short <- op > lag(hi) * (1 + threshold * lag(std))
# ポジション
pos <- long - short
# リターン
ret <- deltaCO * pos
# 累積リターン
totalRet <- cumsum(window(ret, start = startDate, end = endDate))
return(last(totalRet))
}
# バックテスト
GA <- ga(type = "real-valued", fitness = function(x) + f(x[1], x[2]),
min = c(5, 0.0), max = c(250, 0.1))
# 要約
summary(GA)

+-----------------------------------+
| Genetic Algorithm |
+-----------------------------------+

GA settings:
Type = real-valued
Population size = 50
Number of generations = 100
Elitism =
Crossover probability = 0.8
Mutation probability = 0.1
Search domain
x1 x2
Min 5 0.0
Max 250 0.1

GA results:
Iterations = 100
Fitness function value = 46.15164
Solution =
x1 x2
[1,] 6.978066 0.005555136
# 総損益
lookback <- 6
threshold <- 0.0055
std <- rollapply(deltaCC, lookback, sd)
long <- op > lag(hi) * (1 + threshold * lag(std)) # 順張り
short <- op < lag(lo) * (1 - threshold * lag(std)) # 順張り
#long <- op < lag(lo) * (1 - threshold * lag(std)) # 逆張り
#short <- op > lag(hi) * (1 + threshold * lag(std)) # 逆張り
pos <- long - short
ret <- deltaCO * pos
totalRet <- cumsum(window(ret, start = startDate, end = endDate))
last(totalRet)

AUDUSD_EUR.Close
2013-12-06 43.12089
# R2
r2 <- cor(totalRet, 1:length(totalRet))^2
r2

[,1]
AUDUSD_EUR.Close 0.8568064
# グラフ
matplot(totalRet, type = "l")

コメントの投稿

非公開コメント

プロフィール

Author:fxst24
自分用のノートとして作っていますが、同学の方々の参考になれば幸いです。
記事はことわりなく修正、削除、非公開にすることがあります。

最新記事
最新コメント
最新トラックバック
月別アーカイブ
カテゴリ
検索フォーム
RSSリンクの表示
リンク
QRコード
QR