I have a xts file with monthly returns for 17 industry portfolios. The data looks as follows:
Cars Chems Clths Cnstr Cnsum Durbl FabPr Finan Food Machn Mines Oil Other Rtail Steel Trans
1926-07-31 4.77 1.20 -0.09 4.20 2.05 1.33 0.61 0.44 0.46 2.06 2.65 -2.57 1.99 1.46 3.05 -0.69
1926-08-31 -1.11 3.55 3.57 0.85 4.10 0.75 -0.49 8.84 4.72 5.57 1.16 3.85 4.81 0.63 -0.58 4.96
1926-09-30 -3.39 1.85 -4.89 -1.06 2.50 1.27 -3.10 -2.55 1.66 0.52 1.44 -4.93 -2.09 -1.20 2.28 0.06
1926-10-31 -10.66 -9.15 0.49 -6.49 -1.41 -5.02 -3.92 -4.40 -4.79 -4.52 5.73 0.23 -3.50 -2.44 -4.98 -2.79
1926-11-30 -0.73 4.98 2.66 2.91 8.35 0.12 1.36 -0.27 7.04 -0.75 1.13 2.92 -0.47 1.72 1.81 1.38
1926-12-31 5.14 2.59 2.30 3.37 1.96 4.23 2.22 2.40 -1.39 2.93 -1.38 6.39 2.59 3.06 2.17 2.18
Utils
1926-07-31 4.85
1926-08-31 -2.00
1926-09-30 2.06
1926-10-31 -2.98
1926-11-30 5.71
1926-12-31 1.72
My aim is to perform a backtest with a naive portfolio selection rule. Instead of holding an equal-weighted portfolio, i want to assing the weights according to the following naive rule:
- Assign weights 2/N to each asset that has above-median historical returns
- assign weight 0 if below-median historical returns
Instead of the equal-weighted vector:
w <- c(rep(1/17,17))
This weighting vector works well for getting the portfolio returns. To do so I used this function:
portfolio_returns_tq_rebl <-
returns %>%
tq_portfolio(assets_col = symbol,
returns_col = return,
weights = w, # here i want to have a weighting function?!
col_rename = "returns",
rebalance_on = "months")
I stuck in incorporate a weighting function to a standard backtest script (tidyquant, PerformanceAnalytics, quantmod). In most of these, only optimization-problems can be solved but not simple naive rules.
Does somebody have an idea in how to pursue such a backtest with a simple portfolio selection rule?
Thanks for your help!