Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中测试配对交易策略_R_Trading_Quantitative Finance_Performanceanalytics_Back Testing - Fatal编程技术网

如何在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:

我正在努力学习结对交易策略,并用它来编写我的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:
        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))