2

我正在尝试使用 Quantmod、PerformanceAnalytics 和 Systematic Investors Toolbox 开发交易系统。

我想根据每日数据创建和测试一些简单的交易规则(价格 > SMA)、(rsi 2 < 0.5 = 多头)等(这部分工作正常)。

然后,我想根据它们在前 X 天的表现对这些策略进行排名。然后我想选择前3名的策略,50%投资前1名,30%投资第二名,20%投资第三名。这就是我的问题所在,因为我不知道该怎么做。

我研究了 Systematic Investor Toolbox 或 Rank 功能的一些功能,并查看了过去的问题,但未能成功地让这些功能发挥作用。

最终,我只想每月重新平衡一次策略权重,但让我们一次解决一个问题。

以下是我迄今为止测试策略和创建滚动性能的代码:

###############################################################################
# Load Systematic Investor Toolbox (SIT)
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)

#*****************************************************************
# Load historical data
#****************************************************************** 
load.packages('quantmod','PerformanceAnalytics')   
tickers = 'SPY'

models <- new.env()
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1950-01-01', env = data, auto.assign = T)
for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)        
bt.prep(data, align='remove.na', dates='1950::2013')

#*****************************************************************
# Code Strategies
#****************************************************************** 
prices = data$prices  
n = len(tickers)  
nperiods = nrow(prices)

#Define indicators
sma.long = bt.apply.matrix(prices, SMA, 200)
dv = bt.apply(data, function(x) { DV(HLC(x), 2, TRUE) } )   
rsi2 = bt.apply.matrix(prices, RSI, 2)

# Buy & Hold    
data$weight[] = 1
models$buy.hold = bt.run(data) 

# Simple TF
data$weight[] = NA
data$weight[] = iif(prices>sma.long,1,0)
data$weight[] = na.locf(data$weight[])
TFweight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$tf = bt.run.share(data, commission=0.005, trade.summary=T)

#Trend following + simple dv
data$weight[] = NA
data$weight[] = iif(prices>sma.long,iif(cross.dn(dv,0.5),1,iif(cross.up(dv,0.5),0,NA)),0)
data$weight[] = na.locf(data$weight[])
TFDVweight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)
models$tfsimpledv = bt.run.share(data, commission=0.005, trade.summary=T)

#Mean Reversion prices > prices - 6 days
data$weight[] = NA
data$weight[] = iif(prices < lag(prices,1),1,iif(prices>lag(prices,1),0,NA))
data$weight[] = na.locf(data$weight[])
MRD1weight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$MR1days = bt.run.share(data, commission=0.005, trade.summary=T)

#Mean Reversion rsi
data$weight[] = NA
data$weight[] = iif(rsi2<50,1,iif(rsi2>50,0,NA))
data$weight[] = na.locf(data$weight[])
MRrsiweight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$MRrsi = bt.run.share(data, commission=0.005, trade.summary=T)

#Mean Reversion rsi
data$weight[] = NA
data$weight[] = iif(rsi2<50 & prices < lag(prices,1),2,iif(rsi2>50,0,NA))
data$weight[] = na.locf(data$weight[])
MRrsi1dweight = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$MRrsi1d = bt.run.share(data, commission=0.005, trade.summary=T)


#Mean Reversion rsi scaling
data$weight[] = NA
data$weight[] = iif(rsi2<5 ,2,iif(rsi2<10,1.5,iif(rsi2<15,1,iif(rsi2<20,0.5,iif(rsi2>95,-2,iif(rsi2>90,-1.5,iif(rsi2>85,-1,iif(rsi2>80,-0.25,0))))))))
data$weight[] = na.locf(data$weight[])
MRrsiscaling = data$weight[]
popen = bt.apply(data, Op)
data$execution.price[] = NA
data$execution.price = Next(popen)

models$MRrsiscaling = bt.run.share(data, commission=0.005, trade.summary=T)


models$EQW = bt.run.share(data, commission=0.01, trade.summary=T)

#calculate daily returns
dailyRMRrsiscaling = diff(log(models$MRrsiscaling$equity))
dailyRMRrsi1d = diff(log(models$MRrsi1d$equity))
dailyRMRrsi = diff(log(models$MRrsi$equity))
dailyRTF = diff(log(models$tf$equity))
dailyRTFsimpledv = diff(log(models$tfsimpledv$equity))

#caculate rolling returns
rollingMRrsiscaling = apply.rolling(dailyRMRrsiscaling, FUN="mean", width=252)
rollingMRrsi1d = apply.rolling(dailyRMRrsi1d, FUN="mean", width=252)
rollingMRrsi = apply.rolling(dailyRMRrsi, FUN="mean", width=252)
rollingTF = apply.rolling(dailyRTF, FUN="mean", width=252)
rollingTFsimpledv = apply.rolling(dailyRTF, FUN="mean", width=252)


plotbt.custom.report(models$MRrsiscaling ,models$MRrsi1d, models$MRrsi, models$MR1days, models$tf,  models$tfsimpledv, models$buy.hold) 

这基本上就是我所拥有的。我可以创建和测试策略,但我不知道如何自动对策略进行排名并只选择前 3 个。

如果您需要更多信息,请告诉我!任何帮助将不胜感激!提前致谢

4

1 回答 1

0

您可以使用PerformanceAnalytics. 例如:

rest=cbind(dailyRMRrsi1d,dailyRMRrsi,dailyRTF,dailyRTFsimpledv)
charts.PerformanceSummary(rest)
于 2017-08-20T19:08:14.937 回答