ギャップ戦略①
通貨ペア:EURUSD
期間:2002年1月1日~2013年12月6日
ルール①:(当日始値/前日終値-1)*100が-閾値以下で買い、閾値以上で売り。
ルール②:ポジションは当日終値で決済。
備考①:四本値は欧州時間に基づく。
備考②:約定価格は当日始値。
備考③:損益の単位は%。
備考④:コストは考慮していない。
備考⑤:閾値は遺伝的アルゴリズムにより、0~5の範囲で利益を最大化するパラメータを求める。
期間:2002年1月1日~2013年12月6日
ルール①:(当日始値/前日終値-1)*100が-閾値以下で買い、閾値以上で売り。
ルール②:ポジションは当日終値で決済。
備考①:四本値は欧州時間に基づく。
備考②:約定価格は当日始値。
備考③:損益の単位は%。
備考④:コストは考慮していない。
備考⑤:閾値は遺伝的アルゴリズムにより、0~5の範囲で利益を最大化するパラメータを求める。
# パッケージの呼び出し
library(quantmod)
# 開始日
startDate <- as.Date("2002-01-01")
# 終了日
endDate <- as.Date("2013-12-06")
# 始値
op <- data_EUR$EURUSD_EUR.Open
# 終値
cl <- data_EUR$EURUSD_EUR.Close
# ギャップ
gap <- (op / lag(cl) - 1) * 100
# 終値/始値の変化率
delta <- (cl / op - 1) * 100
# トレード関数
f <- function(x) {
# 閾値
threshold <- x
# 順張り
#long <- gap >= threshold
#short <- gap <= -threshold
# 逆張り
long <- gap <= -threshold
short <- gap >= threshold
# ポジション
pos <- long - short
# リターン
ret <- delta * pos
# 累積リターン
totalRet <- cumsum(window(ret, start = startDate, end = endDate))
return(last(totalRet))
}
# バックテスト
GA <- ga(type = "real-valued", fitness = f, min = 0, max = 5)
# 要約
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
Min 0
Max 5
GA results:
Iterations = 100
Fitness function value = 74.04989
Solutions =
x1
[1,] 0.1009175
[2,] 0.1008734
# 総損益
threshold <- 0.1
#long <- gap >= threshold # 順張り
#short <- gap <= -threshold # 順張り
long <- gap <= -threshold # 逆張り
short <- gap >= threshold # 逆張り
pos <- long - short
ret <- delta*pos
totalRet <- cumsum(window(ret, start = startDate, end = endDate))
last(totalRet)
EURUSD_EUR.Close
2013-12-06 71.76682
# R2
r2 <- cor(totalRet, 1:length(totalRet))^2
r2
[,1]
EURUSD_EUR.Close 0.9678456
# グラフ
matplot(totalRet, type = "l")