Technical analysis: Moving Averages, compute index for multiple stocks

Hello guys,
i would kindly ask you the following question:
as SMA,EMA,WMA functions admit only univariate x(sma<-SMA(data$prices$AAPL, n=20) and i deal with a wide portfolio, how can i set up a for circle in order to compute the aforementiomed indexes for each stock? My main goal is to build up a momentum proxy, in order to assess my portfolio, by using models like AAA(Adaptive Asset Allocation). Looking forward to having an answer,
Many Thanks,
Cristiano

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

           This is just the tail of my portfolio:

LMNX MGRC MO MPC NBL NEE NFLX NOW NRZ NVS NXGN OGE OIS OOMA ORA ORCL OTEX PBR PKX PM PNM
2018-12-20 22.76 50.47 50.44 57.42 18.83 174.91 260.58 169.92 14.31 75.47491 15.22 40.03 14.10 13.97 51.53 46.24 32.64 12.59 54.83 69.08 43.26
2018-12-21 21.63 50.24 49.09 56.74 18.17 174.11 246.39 162.00 14.41 74.68638 14.75 39.92 13.88 13.55 51.03 44.00 31.44 12.38 53.12 66.21 42.48
2018-12-24 21.72 49.30 47.56 54.32 17.36 168.08 233.88 158.92 14.00 73.91577 14.58 38.22 13.63 13.38 49.82 42.69 31.44 12.10 53.25 65.97 39.96
2018-12-26 22.69 51.38 48.43 58.35 19.08 170.34 253.67 169.51 14.91 75.95878 15.20 38.82 14.46 13.92 51.76 44.59 32.61 12.72 55.41 66.86 40.64
2018-12-27 23.03 50.85 48.70 59.10 18.90 172.16 255.57 178.20 14.98 75.33154 15.32 39.21 14.47 13.90 52.46 44.97 32.66 12.80 55.33 66.68 40.79
2018-12-28 23.16 50.95 48.89 58.24 18.43 172.72 256.08 180.33 14.49 76.39785 15.40 39.05 14.65 13.77 52.24 44.82 32.44 13.03 54.70 67.27 40.83
POR PTLA RDY RIO RWT RYAAY RYN SAN-PB SBLK SHI SHW SPG SPH SPKE SPSC SQM SRDX SRE STOK STT-PD SWX
2018-12-20 47.27 16.33 37.29 47.74 14.69 70.97 27.70 18.48 8.52 43.81 379.39 168.07 20.26 7.57 40.870 38.47 49.71 113.06 1200 24.94 79.89
2018-12-21 46.67 15.46 36.84 47.76 14.67 69.35 27.33 19.02 8.25 43.22 376.55 164.88 19.65 7.47 38.660 38.34 49.36 111.82 1200 24.89 79.98
2018-12-24 44.19 15.80 37.07 47.37 14.52 67.95 26.65 18.17 8.30 43.30 365.24 160.66 19.13 7.38 38.460 36.94 44.93 107.40 1200 24.74 74.09
2018-12-26 44.94 16.83 37.85 49.10 15.07 70.48 27.45 19.05 8.95 43.83 386.72 166.66 19.24 7.40 40.305 39.04 47.77 107.99 1200 24.35 74.97
2018-12-27 45.34 16.23 37.58 48.31 15.18 70.09 27.38 18.54 8.83 43.28 391.17 167.33 19.03 7.42 40.665 38.92 46.96 107.61 1200 24.10 75.45
2018-12-28 45.50 17.09 37.69 48.21 15.26 71.39 27.61 18.59 8.86 43.04 389.58 168.10 19.35 7.32 40.695 38.16 47.62 107.27 1200 24.32 75.47
SYY TAP TEO TGE TPB TSLA UTL VB VFC VG VOD VTI VWO VZ WEC WHR WLL WM WMT YORW ZTO
2018-12-20 61.82 57.24 14.80 23.13 28.01 315.38 50.60 130.48 65.96045 8.66 20.00 126.17 38.18 55.07 71.74 109.16 21.98 87.95 87.28 33.63 15.47
2018-12-21 60.89 55.50 14.39 22.65 28.21 319.77 51.79 127.52 65.79096 8.19 19.35 123.49 37.87 54.92 71.44 104.93 20.29 88.01 87.13 30.74 15.93
2018-12-24 59.95 55.15 14.20 22.10 27.32 295.39 49.89 123.92 63.38983 8.10 19.17 119.70 37.32 53.05 67.62 101.72 18.89 83.70 85.82 29.88 15.99
2018-12-26 61.23 56.16 14.46 23.19 28.48 326.09 50.45 129.89 65.67796 8.58 19.56 125.46 37.98 54.44 68.45 106.01 21.83 86.94 90.41 31.32 16.43
2018-12-27 62.05 56.54 14.80 23.43 27.99 316.13 50.55 130.61 66.30885 8.44 19.30 126.59 37.90 55.15 69.18 106.70 22.60 88.10 91.59 31.46 16.00
2018-12-28 62.28 55.94 14.97 23.83 27.94 333.87 50.80 130.75 66.45010 8.56 19.51 126.43 38.17 55.27 69.01 107.27 22.21 87.95 92.13 32.32 16.00

Let us suppose that i want to employ the TTR package computing a simple moving average(SMA), of the SYY's ticker :
sma<-SMA(data$prices$SYY, n=2000),

the formula above helps compute SMAs stock by stock, since SMA uses only univariate x(no matrices!!!!). Then, how can i set up an iterative process(a circle for) that lets me repeat the operation in order to fill a matrix of SMAs values??????

The AAA model is the following:
n.top = trunc(n*(top.assets/100)) # numero di titoli da detenere in portafoglio
n.mom = mom.months22 # lunghezza del momentum lookback ==> mesi desiderati x giorni lavorativi (medi)
n.vol = vol.lookback
22 # lunghezza del volatility lookback ==> mesi desiderati x giorni lavorativi (medi)

if ((!exists("top.assets"))|!exists("mom.months")|!exists("vol.lookback"))
{"Please set all parameters: 'top.assets','mom.months','vol.lookback'"}

#*****************************************************************

Finding portfolio updating dates

#*****************************************************************

period.ends = endpoints(prices, 'months')
period.ends = period.ends[period.ends > 0]

#*****************************************************************

Adaptive Asset Allocation (AAA)

#*****************************************************************

Volatility based portfolio balancing

ret.log = bt.apply.matrix(prices,ROC, type="continuous")

hist.vol = bt.apply.matrix(ret.log, runSD, n = n.vol)
adj.vol = 1/hist.vol[period.ends,]

momentum = prices / mlag(prices, n.mom)

Best momentum assets

weight = NA * prices
weight[period.ends,] = ntop(momentum[period.ends,], n.top)

Basically i've to replace "momentum" with a sma matrix, according to which i'll rank my assets. Pratically sma matrix might be a momentum proxy!!

Volatility minimization

for( i in period.ends[period.ends >= n.mom] ) {
hist = ret.log[ (i - n.vol + 1):i, ]
include.index = count(hist)== n.vol
index = ( weight[i,] > 0 ) & include.index
n = sum(index)
if(n > 0) {
hist = hist[ , index]
ia = create.historical.ia(hist, 252)
s0 = apply(coredata(hist),2,sd)
ia$cov = cor(coredata(hist), use='complete.obs',method='pearson') * (s0 %*% t(s0))
constraints = new.constraints(n, lb = 0, ub = 1)
constraints = add.constraints(rep(1, n), 1, type = '=', constraints)

# Minimum variance weights   
weight[i,] = 0        
weight[i,index] = min.risk.portfolio(ia, constraints)

}
}

AAA Portfolio

data$weight = NA
data$weight[period.ends,] = weight[period.ends,]

models$AAA = bt.run.share(data, clean.signal=F,trade.summary = TRUE)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.