我正在尝试编写一个对资产组合应用头寸规模分析的函数,但我坚持使用 R 中的一些基本编码。确定投资组合分配百分比的一项资产的基本设置很简单比率:
资产/资产波动性的固定风险百分比
多个资产很棘手,因为总分配百分比不能超过 100%。到目前为止,这是我所拥有的,但我不知道如何优化代码以使其在整个投资组合上下文中有意义。有什么建议么?提前致谢!
# start by downloading 2 assets
library(tseries)
library(TTR)
library(quantmod)
library(PerformanceAnalytics)
symbols = c("SPY","AGG")
getSymbols(symbols, src='yahoo', from='2003-12-31')
spy <-Ad(SPY)
agg <-Ad(AGG)
# calculate daily % return and rolling 100-day volatility (std dev)
spy.ret <-na.omit(ROC(spy,1,"discrete"))
agg.ret <-na.omit(ROC(agg,1,"discrete"))
# calculate annualized volatility
spy.sd <-na.omit(rollapplyr(spy.ret,100,sd))*sqrt(252)
agg.sd <-na.omit(rollapplyr(agg.ret,100,sd))*sqrt(252)
# calculate position size (as %) for each asset with fixed 2% risk factor
# to determine fraction of the account size
spy.pos.size <-.02/spy.sd
agg.pos.size <-.02/agg.sd
# combine position size data and
# sum for aggregate portfolio allocation
spy.agg.pos.size <-cbind(spy.pos.size,agg.pos.size)
port.pos.size <-apply(spy.agg.pos.size,1,sum)
检查 max allocation total:显示 % 超过 100%(即 1.0)
> max(port.pos.size)
[1] 1.131178
关于如何将总分配保持在/低于 100%,同时在每个资产的单独头寸规模计算中保留分配建议的任何建议?再次感谢!!!