基于协整关系的配对交易比之前的基于距离和相关性要更先进一些。收益上确实也体现出来了。
library(tseries)
adf.test(xom)
adf.test(cvx)
model <- lm(xom ~ cvx + 0)
summary(model)
adf.test(as.ts(model$residuals))
bp <- get_symbol_cl("BP")
model <- lm(xom ~ bp + 0)
adf.test(as.ts(model$residuals))
plot(cbind(xom,bp * model$coefficients),
col = c("black", "red"),
main = "xom & model$coefficients")
plot(as.xts(model$residuals, index(xom)),
main = "model$residuals")
plot(signal, main = "交易信号")
roll_me <- rollapply(model$residuals,14,mean)
roll_std <- rollapply(model$residuals,14,sd)
n <- 1
roll_ub <- roll_me + n * roll_std
roll_lb <- roll_me - n * roll_std
signal <- NULL
signal <- ifelse(model$residuals>roll_ub,-1,
ifelse(model$residuals<roll_lb,1,0)) lagsignal <- lag(signal,1) signal ifelse(lagsignal="=" -1 & model$residuals> roll_me,-1,
ifelse(lagsignal==1 & model$residuals < roll_me,1,0))
# 计算收益
spread_return <- ret_xom - lag(hedge_ratio) * ret_cvx
cost <- 0
trade_return <- spread_return * lag(signal) - cost
names(trade_return) <- "TradStrat_PT#3"
# 交易效果和绩效指标
summary(as.vector(trade_return))
cumm_ret <- Return.cumulative(trade_return)
annual_ret <- Return.annualized(trade_return)
par(mfrow = c(1, 1))
charts.PerformanceSummary(trade_return, main = "交易报告")
tail(cumm_ret)
maxDrawdown(trade_return)
StdDev(trade_return)
StdDev.annualized(trade_return)
VaR(trade_return)
SharpeRatio(trade_return, Rf = 0, FUN = "StdDev")
SharpeRatio.annualized(trade_return, Rf = 0)
> # 交易效果和绩效指标
> summary(as.vector(trade_return))
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
-0.036732 0.000000 0.000000 0.000192 0.000520 0.031850 15
> cumm_ret <- Return.cumulative(trade_return)
> annual_ret <- Return.annualized(trade_return)
> par(mfrow = c(1, 1))
> charts.PerformanceSummary(trade_return, main = "交易报告")
>
>
> tail(cumm_ret)
TradStrat_PT#3
Cumulative Return 0.3088741
> maxDrawdown(trade_return)
[1] 0.1295894
> StdDev(trade_return)
[,1]
StdDev 0.004902459
> StdDev.annualized(trade_return)
TradStrat_PT#3
Annualized Standard Deviation 0.07782412
> VaR(trade_return)
TradStrat_PT#3
VaR -0.006463428
> SharpeRatio(trade_return, Rf = 0, FUN = "StdDev")
TradStrat_PT#3
StdDev Sharpe (Rf=0%, p=95%): 0.03917563
> SharpeRatio.annualized(trade_return, Rf = 0)
TradStrat_PT#3
Annualized Sharpe Ratio (Rf=0%) 0.5964265
> 感谢阅读,欢迎关注和留言 > 量化投资与期货外汇散仙,基金保险水平也拿的出手</roll_lb,1,0))>
来源:oschina
链接:https://my.oschina.net/changzw/blog/3164313