Description
Encountering an error running RMD script: Error in lag.xts(x, n, na.pad = na.pad) : abs(k) must be less than nrow(x)
In the # Get weights function below
Expected behavior Script is calculating weights. Below is the current codeblock and the traceback.
Example code:
#subsetprices <- na.locf(prices['2010::20200410'])
subsetprices <- na.locf(prices['2010::'])
etf_returns <- na.omit(Return.calculate(subsetprices, method='discrete'))
**# Get weights**
unl_etf_weights <- get_unlevered_target_weights(etf_returns, rebal = 22, vol_lookback = 90, cor_lookback = 120, cor_shrinkage = 1, adjust_momo = TRUE, momo_multiplier = 0.1)
# Restrict IEMB exposures to 15%, due to concerns over potential credit risk negative skews which won't appear in the data
EMB_weight_cap <- 0.15
# Subset unl_etf_weights data to pick those which we need to modify
unl_etf_weights_EMB_more_then_cap <- unl_etf_weights[unl_etf_weights$IEMB.LSE > EMB_weight_cap]
# Modify EMB weights
EMB_modded_weights <- replace(unl_etf_weights$IEMB.LSE, unl_etf_weights$IEMB.LSE > EMB_weight_cap, EMB_weight_cap)
# Rescale weights back to 1 on subset
weights_ex_EMB <- subset(unl_etf_weights_EMB_more_then_cap, select = -IEMB.LSE)
weights_ex_EMB <- (1-EMB_weight_cap) * weights_ex_EMB / rowSums(weights_ex_EMB)
unl_etf_weights_EMB_more_then_cap <- cbind(weights_ex_EMB, EMB_modded_weights)
# Ensure we have a right order before we bind back our modified subset
unl_etf_weights_EMB_more_then_cap <- unl_etf_weights_EMB_more_then_cap[,symbols_RP]
unl_etf_weights <- unl_etf_weights[,symbols_RP]
# combine two subset into one
unl_etf_weights <- rbind(unl_etf_weights_EMB_more_then_cap, unl_etf_weights[unl_etf_weights$IEMB.LSE <= EMB_weight_cap])
unl_etf_weights <- unl_etf_weights[c(!duplicated(time(unl_etf_weights))[-1], TRUE)]
# Ensure returns matrix in xts object is in the same order as in the backtest
etf_backtest_returns <- etf_returns[,symbols_RP]
etf_backtest_weights <- unl_etf_weights[,symbols_RP]
# Subset so we're only reporting on the live trading record
etf_backtest_returns <- etf_backtest_returns['2019-9::']
etf_backtest_weights <- etf_backtest_weights['2019-9::']
# Generate performance charts
riskperformance(etf_backtest_returns, weights=etf_backtest_weights)
Error in lag.xts(x, n, na.pad = na.pad) : abs(k) must be less than nrow(x)
6.
lag.xts(x, n, na.pad = na.pad)
5.
TTR::ROC(synthetic_prices, n = formation_period, type = "discrete")
4.
xts::lag.xts(TTR::ROC(synthetic_prices, n = formation_period, type = "discrete"), 1)
3.
na.omit(xts::lag.xts(TTR::ROC(synthetic_prices, n = formation_period, type = "discrete"), 1))
2.
get_momo_adjustments(ret, formation_period = 12 * 22, vol_weights = volw)
1.
get_unlevered_target_weights(etf_returns, rebal = 22, vol_lookback = 90, cor_lookback = 120, cor_shrinkage = 1, adjust_momo = TRUE, momo_multiplier = 0.1)