我想在特定日期重新分配策略组合:
require(PerformanceAnalytics)
require(TTR)
require(quantmod)
获取资产价格并获取每日离散收益
tickers = c("ABI.BR","AI.PA","AIR.PA","ALV.DE","ASML.AS")
getSymbols(tickers, from="2012-01-01", to="2013-12-01")
close.prices = do.call(merge, lapply(tickers, function(x) Cl(get(x))))
colnames(close.prices) = c("Anheuser-Busch InBev",
"L'Air Liquide","AIRBUS GROUP","Allianz","ASML HLDG")
assets.ret = ROC(close.prices,type="discrete")[-1]
现在我通过将 RSI 函数应用于每个资产来获得 RSI 信号
rsi.fct = function(x) RSI(x, n=20, maType = SMA)
rsi = xts(apply(close.prices, 2, rsi.fct),
order.by=index(rsi.fct(close.prices[,1]) ) )
> tail(rsi)
Anheuser-Busch InBev L'Air Liquide AIRBUS GROUP Allianz ASML HLDG
2013-11-22 51.15171 49.36494 60.25836 61.07143 46.84159
2013-11-25 54.95495 50.82237 63.54717 61.07143 49.63168
2013-11-26 49.65470 52.55102 58.29563 58.18182 48.59023
2013-11-27 54.60575 61.81980 57.94677 62.05674 52.11640
2013-11-28 46.52778 60.76994 57.85061 63.35616 45.70000
2013-11-29 50.99905 61.90476 56.09756 65.49296 48.82479
策略如下:我在 RSI < 30 时买入资产,当 RSI >= 30 时不买入
ret.mat.rsi = lag(ifelse (rsi < 30, 1, 0))*assets.ret
现在这是我遇到问题的部分。ret.mat.rsi 的回报是每日回报。假设我想在每月的第一天查看 rsi 矩阵,例如
> rsi[110]
Anheuser-Busch InBev L'Air Liquide AIRBUS GROUP Allianz ASML HLDG
2012-06-01 39.66126 31.1599 30.39443 17.17647 43.85172
我想购买在我的投资组合中权重相等的前 4 种资产,因为它们的 RSI 低于 30,并在本月剩余时间内保持头寸不变(无论进一步的 RSI 信号如何),直到下个月的第一天:
> rsi[131]
Anheuser-Busch InBev L'Air Liquide AIRBUS GROUP Allianz ASML HLDG
2012-07-02 84.69529 73.87205 66.25561 74.52642 71.65021
我选择不购买任何资产。
现在的整个问题是如何优雅地编码在特定日期自动重新分配投资组合,即在每个月初(也可以是每周或每三周)。投资组合回报应仅包含在重新分配日期满足指标条件(此处 RSI < 30)的资产。