基于协整关系的配对量化交易策略-R语言

只愿长相守 提交于 2020-02-25 20:00:05

基于协整关系的配对交易比之前的基于距离和相关性要更先进一些。收益上确实也体现出来了。

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 &amp; model$residuals &lt; roll_me,1,0))

# 计算收益
spread_return &lt;- ret_xom - lag(hedge_ratio) * ret_cvx
cost &lt;- 0
trade_return &lt;- spread_return * lag(signal) - cost
names(trade_return) &lt;- "TradStrat_PT#3"

# 交易效果和绩效指标
summary(as.vector(trade_return))
cumm_ret &lt;- Return.cumulative(trade_return)
annual_ret &lt;- 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)

file

file

file

file

&gt; # 交易效果和绩效指标
&gt; 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 
&gt; cumm_ret &lt;- Return.cumulative(trade_return)
&gt; annual_ret &lt;- Return.annualized(trade_return)
&gt; par(mfrow = c(1, 1))
&gt; charts.PerformanceSummary(trade_return, main = "交易报告")
&gt; 
&gt; 
&gt; tail(cumm_ret)
                  TradStrat_PT#3
Cumulative Return      0.3088741
&gt; maxDrawdown(trade_return)
[1] 0.1295894
&gt; StdDev(trade_return)
              [,1]
StdDev 0.004902459
&gt; StdDev.annualized(trade_return)
                              TradStrat_PT#3
Annualized Standard Deviation     0.07782412
&gt; VaR(trade_return)
    TradStrat_PT#3
VaR   -0.006463428
&gt; SharpeRatio(trade_return, Rf = 0, FUN = "StdDev")
                              TradStrat_PT#3
StdDev Sharpe (Rf=0%, p=95%):     0.03917563
&gt; SharpeRatio.annualized(trade_return, Rf = 0)
                                TradStrat_PT#3
Annualized Sharpe Ratio (Rf=0%)      0.5964265

> 感谢阅读,欢迎关注和留言 > 量化投资与期货外汇散仙,基金保险水平也拿的出手</roll_lb,1,0))>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!