如何在R中测试配对交易策略
我正在努力学习结对交易策略,并用它来编写我的R程序如何在R中测试配对交易策略,r,trading,quantitative-finance,performanceanalytics,back-testing,R,Trading,Quantitative Finance,Performanceanalytics,Back Testing,我正在努力学习结对交易策略,并用它来编写我的R程序 if X and Y are cointegrated: calculate Beta between X and Y calculate spread as X - Beta * Y calculate z-score of spread # entering trade (spread is away from mean by two sigmas): if z-score > 2:
if X and Y are cointegrated:
calculate Beta between X and Y
calculate spread as X - Beta * Y
calculate z-score of spread
# entering trade (spread is away from mean by two sigmas):
if z-score > 2:
sell spread (sell 1000 of X, buy 1000 * Beta of Y)
if z-score < -2:
buy spread (buy 1000 of X, sell 1000 * Beta of Y)
# exiting trade (spread converged close to mean):
if we're short spread and z-score < 1:
close the trades
if we're long spread and z-score > -1:
close the trades
# repeat above on each new bar, recalculating rolling Beta and spread etc.
当前的问题是如何在SIT中模拟成对的买卖。如果SIT不能进行配对交易策略回测,那么我应该如何执行配对交易策略,尤其是进入和退出。我应该用什么逻辑
编辑
搜索了一段时间后,我知道我们可以使用PerformanceAnalytics
从头开始制作backtester;但在回测之前,我们必须创建信号和返回值。下面是一个示例代码
library(quantmod)
library(PerformanceAnalytics)
s <- get(getSymbols('SPY'))["2016::"]
s$sma20 <- SMA(Cl(s) , 20)
s$signal <- ifelse(Cl(s) > s$sma20 , 1 , -1)
myReturn <- lag(s$signal) * dailyReturn(s)
charts.PerformanceSummary(cbind(dailyReturn(s),myReturn))
库(quantmod)
库(性能分析)
s
library(quantmod)
library(PerformanceAnalytics)
s <- get(getSymbols('SPY'))["2016::"]
s$sma20 <- SMA(Cl(s) , 20)
s$signal <- ifelse(Cl(s) > s$sma20 , 1 , -1)
myReturn <- lag(s$signal) * dailyReturn(s)
charts.PerformanceSummary(cbind(dailyReturn(s),myReturn))