Load library
library(dplyr)
library(ggplot2) library(quantmod)
library(PerformanceAnalytics)
library(caret)
library(caTools) library(kableExtra)
library(tidyr) library(scales) library(imputeTS)
p_white <- '#FFFFFF' p_green <- '#1FBF1D' p_blue <- '#5478DC' p_red <- '#FF4F62'
library(quantmod) library(PerformanceAnalytics) #GET TICKERS Tickers <- c("^MXX", "^GSPC", "^DJI", "AAPL", "WMT" ) getSymbols(Tickers, from = as.Date("2018-01-01"), to = as.Date("2023-11-20"))
## [1] "MXX" "GSPC" "DJI" "AAPL" "WMT"
#NAME THE VARIABLES P_MXX <- Ad(MXX) P_GSPC <- Ad(GSPC) P_DJI <- Ad(DJI) P_AAPL <- Ad(AAPL) P_WMT <- Ad(WMT) P_MXX
## MXX.Adjusted ## 2018-01-02 50033.95 ## 2018-01-03 49782.23 ## 2018-01-04 49744.62 ## 2018-01-05 49887.74 ## 2018-01-08 49995.57 ## 2018-01-09 49701.37 ## 2018-01-10 48785.25 ## 2018-01-11 48799.39 ## 2018-01-12 49135.91 ## 2018-01-15 49387.28 ## ... ## 2023-11-06 51633.63 ## 2023-11-07 51209.85 ## 2023-11-08 51016.30 ## 2023-11-09 51071.03 ## 2023-11-10 51258.24 ## 2023-11-13 51090.91 ## 2023-11-14 52511.37 ## 2023-11-15 52796.47 ## 2023-11-16 52469.36 ## 2023-11-17 52685.10
P_GSPC
## GSPC.Adjusted ## 2018-01-02 2695.81 ## 2018-01-03 2713.06 ## 2018-01-04 2723.99 ## 2018-01-05 2743.15 ## 2018-01-08 2747.71 ## 2018-01-09 2751.29 ## 2018-01-10 2748.23 ## 2018-01-11 2767.56 ## 2018-01-12 2786.24 ## 2018-01-16 2776.42 ## ... ## 2023-11-06 4365.98 ## 2023-11-07 4378.38 ## 2023-11-08 4382.78 ## 2023-11-09 4347.35 ## 2023-11-10 4415.24 ## 2023-11-13 4411.55 ## 2023-11-14 4495.70 ## 2023-11-15 4502.88 ## 2023-11-16 4508.24 ## 2023-11-17 4514.02
P_DJI
## DJI.Adjusted ## 2018-01-02 24824.01 ## 2018-01-03 24922.68 ## 2018-01-04 25075.13 ## 2018-01-05 25295.87 ## 2018-01-08 25283.00 ## 2018-01-09 25385.80 ## 2018-01-10 25369.13 ## 2018-01-11 25574.73 ## 2018-01-12 25803.19 ## 2018-01-16 25792.86 ## ... ## 2023-11-06 34095.86 ## 2023-11-07 34152.60 ## 2023-11-08 34112.27 ## 2023-11-09 33891.94 ## 2023-11-10 34283.10 ## 2023-11-13 34337.87 ## 2023-11-14 34827.70 ## 2023-11-15 34991.21 ## 2023-11-16 34945.47 ## 2023-11-17 34947.28
P_AAPL
## AAPL.Adjusted ## 2018-01-02 40.72288 ## 2018-01-03 40.71578 ## 2018-01-04 40.90489 ## 2018-01-05 41.37061 ## 2018-01-08 41.21697 ## 2018-01-09 41.21223 ## 2018-01-10 41.20277 ## 2018-01-11 41.43681 ## 2018-01-12 41.86471 ## 2018-01-16 41.65194 ## ... ## 2023-11-06 178.99419 ## 2023-11-07 181.58078 ## 2023-11-08 182.64937 ## 2023-11-09 182.17000 ## 2023-11-10 186.39999 ## 2023-11-13 184.80000 ## 2023-11-14 187.44000 ## 2023-11-15 188.00999 ## 2023-11-16 189.71001 ## 2023-11-17 189.69000
P_WMT
## WMT.Adjusted ## 2018-01-02 88.95239 ## 2018-01-03 89.72832 ## 2018-01-04 89.80954 ## 2018-01-05 90.34187 ## 2018-01-08 91.67718 ## 2018-01-09 90.57642 ## 2018-01-10 89.92683 ## 2018-01-11 90.24261 ## 2018-01-12 91.00952 ## 2018-01-16 90.84711 ## ... ## 2023-11-06 164.88000 ## 2023-11-07 165.64999 ## 2023-11-08 164.30000 ## 2023-11-09 163.92000 ## 2023-11-10 166.19000 ## 2023-11-13 167.67999 ## 2023-11-14 167.64999 ## 2023-11-15 169.78000 ## 2023-11-16 156.03999 ## 2023-11-17 155.35001
#GET THE PRICES Prices <- merge.xts(P_MXX, P_GSPC, P_DJI, P_AAPL, P_WMT, join = "inner")
## Warning in merge.xts(P_MXX, P_GSPC, P_DJI, P_AAPL, P_WMT, join = "inner"): ## 'join' only applicable to two object merges
Prices
## MXX.Adjusted GSPC.Adjusted DJI.Adjusted AAPL.Adjusted WMT.Adjusted ## 2018-01-02 50033.95 2695.81 24824.01 40.72288 88.95239 ## 2018-01-03 49782.23 2713.06 24922.68 40.71578 89.72832 ## 2018-01-04 49744.62 2723.99 25075.13 40.90489 89.80954 ## 2018-01-05 49887.74 2743.15 25295.87 41.37061 90.34187 ## 2018-01-08 49995.57 2747.71 25283.00 41.21697 91.67718 ## 2018-01-09 49701.37 2751.29 25385.80 41.21223 90.57642 ## 2018-01-10 48785.25 2748.23 25369.13 41.20277 89.92683 ## 2018-01-11 48799.39 2767.56 25574.73 41.43681 90.24261 ## 2018-01-12 49135.91 2786.24 25803.19 41.86471 91.00952 ## 2018-01-16 49491.72 2776.42 25792.86 41.65194 90.84711 ## ... ## 2023-11-06 51633.63 4365.98 34095.86 178.99419 164.88000 ## 2023-11-07 51209.85 4378.38 34152.60 181.58078 165.64999 ## 2023-11-08 51016.30 4382.78 34112.27 182.64937 164.30000 ## 2023-11-09 51071.03 4347.35 33891.94 182.17000 163.92000 ## 2023-11-10 51258.24 4415.24 34283.10 186.39999 166.19000 ## 2023-11-13 51090.91 4411.55 34337.87 184.80000 167.67999 ## 2023-11-14 52511.37 4495.70 34827.70 187.44000 167.64999 ## 2023-11-15 52796.47 4502.88 34991.21 188.00999 169.78000 ## 2023-11-16 52469.36 4508.24 34945.47 189.71001 156.03999 ## 2023-11-17 52685.10 4514.02 34947.28 189.69000 155.35001
#CALCULATE RETURNS Returns <- Return.calculate(Prices)[-1] Returns
## MXX.Adjusted GSPC.Adjusted DJI.Adjusted AAPL.Adjusted ## 2018-01-03 -0.0050309591 0.0063988188 3.974778e-03 -0.0001744220 ## 2018-01-04 -0.0007554779 0.0040286361 6.116966e-03 0.0046447256 ## 2018-01-05 0.0028770384 0.0070337674 8.803076e-03 0.0113854629 ## 2018-01-08 0.0021614937 0.0016623439 -5.087448e-04 -0.0037137653 ## 2018-01-09 -0.0058845057 0.0013029316 4.066004e-03 -0.0001149491 ## 2018-01-10 -0.0184325115 -0.0011122268 -6.566632e-04 -0.0002296469 ## 2018-01-11 0.0002898545 0.0070336465 8.104322e-03 0.0056802764 ## 2018-01-12 0.0068959781 0.0067496030 8.932997e-03 0.0103264358 ## 2018-01-16 0.0072413148 -0.0035244876 -4.003411e-04 -0.0050822892 ## 2018-01-17 0.0048610563 0.0094150516 1.251474e-02 0.0165164579 ## ... ## 2023-11-06 0.0071566576 0.0017529924 1.014026e-03 0.0146052026 ## 2023-11-07 -0.0082073903 0.0028401189 1.664196e-03 0.0144507131 ## 2023-11-08 -0.0037795615 0.0010049156 -1.180936e-03 0.0058849194 ## 2023-11-09 0.0010728036 -0.0080838393 -6.458911e-03 -0.0026245375 ## 2023-11-10 0.0036656207 0.0156164411 1.154139e-02 0.0232200459 ## 2023-11-13 -0.0032644143 -0.0008358389 1.597566e-03 -0.0085836422 ## 2023-11-14 0.0278026156 0.0190750177 1.426495e-02 0.0142857107 ## 2023-11-15 0.0054292556 0.0015970121 4.694876e-03 0.0030409307 ## 2023-11-16 -0.0061956677 0.0011904274 -1.307248e-03 0.0090421374 ## 2023-11-17 0.0041117748 0.0012820491 5.186652e-05 -0.0001054466 ## WMT.Adjusted ## 2018-01-03 0.0087230067 ## 2018-01-04 0.0009051200 ## 2018-01-05 0.0059272739 ## 2018-01-08 0.0147806544 ## 2018-01-09 -0.0120068464 ## 2018-01-10 -0.0071717287 ## 2018-01-11 0.0035114436 ## 2018-01-12 0.0084983623 ## 2018-01-16 -0.0017845886 ## 2018-01-17 0.0199622726 ## ... ## 2023-11-06 0.0013360939 ## 2023-11-07 0.0046699963 ## 2023-11-08 -0.0081496583 ## 2023-11-09 -0.0023128720 ## 2023-11-10 0.0138482449 ## 2023-11-13 0.0089655828 ## 2023-11-14 -0.0001789049 ## 2023-11-15 0.0127050699 ## 2023-11-16 -0.0809282930 ## 2023-11-17 -0.0044218611
#CALCULATE COVARIANCE OF THE RETURNS CVM <- cov(Returns) CVM
## MXX.Adjusted GSPC.Adjusted DJI.Adjusted AAPL.Adjusted ## MXX.Adjusted 1.179963e-04 7.710268e-05 7.915470e-05 7.878875e-05 ## GSPC.Adjusted 7.710268e-05 1.677969e-04 1.588003e-04 2.087289e-04 ## DJI.Adjusted 7.915470e-05 1.588003e-04 1.638034e-04 1.848431e-04 ## AAPL.Adjusted 7.878875e-05 2.087289e-04 1.848431e-04 4.155721e-04 ## WMT.Adjusted 2.912069e-05 7.681608e-05 7.646662e-05 9.329783e-05 ## WMT.Adjusted ## MXX.Adjusted 2.912069e-05 ## GSPC.Adjusted 7.681608e-05 ## DJI.Adjusted 7.646662e-05 ## AAPL.Adjusted 9.329783e-05 ## WMT.Adjusted 1.950326e-04
#CREATE A ATRIX w <- matrix(rep(0.2, 5), nrow = 1) w
## [,1] [,2] [,3] [,4] [,5] ## [1,] 0.2 0.2 0.2 0.2 0.2
w %*% CVM %*% t(w)
## [,1] ## [1,] 0.0001274576
Lets create our minimal variance portfolio Markowitz
#CALCULATE THE FORMULA OF MINIMUN VARIANCE Ones <- matrix(data = rep(1, 5), nrow = 1) Inverse <- solve(CVM) CVM %*% Inverse
## MXX.Adjusted GSPC.Adjusted DJI.Adjusted AAPL.Adjusted ## MXX.Adjusted 1.000000e+00 3.104505e-17 -3.674156e-17 1.479456e-16 ## GSPC.Adjusted 1.275787e-16 1.000000e+00 1.361528e-15 2.997912e-16 ## DJI.Adjusted 1.156214e-16 -9.107946e-16 1.000000e+00 7.992053e-17 ## AAPL.Adjusted 1.092481e-16 8.009742e-17 6.374839e-16 1.000000e+00 ## WMT.Adjusted 8.307870e-17 1.035661e-16 2.717355e-16 6.449752e-17 ## WMT.Adjusted ## MXX.Adjusted 1.315327e-17 ## GSPC.Adjusted -3.579954e-17 ## DJI.Adjusted -8.679280e-18 ## AAPL.Adjusted -6.963901e-17 ## WMT.Adjusted 1.000000e+00
Numerator <- Ones %*% Inverse Numerator
## MXX.Adjusted GSPC.Adjusted DJI.Adjusted AAPL.Adjusted WMT.Adjusted ## [1,] 6713.06 3130.514 -924.2192 -849.5496 3660.774
Denominator <- Ones %*% Inverse %*% t(Ones) Denominator
## [,1] ## [1,] 11730.58
Denominator <- as.numeric(Denominator) w <- Numerator / Denominator w
## MXX.Adjusted GSPC.Adjusted DJI.Adjusted AAPL.Adjusted WMT.Adjusted ## [1,] 0.5722702 0.2668678 -0.07878718 -0.0724218 0.312071
w %*% CVM %*% t(w)
## [,1] ## [1,] 8.524729e-05
# Calculate the portfolio daily returns portfolio_returns <- Returns %*% t(w) # Calculate the cumulative returns cumulative_returns <- prod(1 + portfolio_returns) # Calculate the portfolio value at the end of the period portfolio_value <- 1000000 * cumulative_returns print(portfolio_value)
## [1] 1265271
#PORTFOLIO RETURNS finalreturn <- portfolio_value/1000000 -1 print(finalreturn)
## [1] 0.265271
RV <- (cumulative_returns*.6) #GET TICKERS Tickers <- c("CETETRCISHRS.MX", "^FVX" ) getSymbols(Tickers, from = as.Date("2018-01-01"), to = as.Date("2023-11-20"))
## Warning: CETETRCISHRS.MX contains missing values. Some functions will not work ## if objects contain missing values in the middle of the series. Consider using ## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## Warning: ^FVX contains missing values. Some functions will not work if objects ## contain missing values in the middle of the series. Consider using na.omit(), ## na.approx(), na.fill(), etc to remove or replace them.
## [1] "CETETRCISHRS.MX" "FVX"
#NAME THE VARIABLES P_CETES <- Ad(CETETRCISHRS.MX) P_USBOND <- Ad(FVX) library(imputeTS) P_CETES <- na.interpolation(P_CETES)
## Warning: na.interpolation will be replaced by na_interpolation. ## Functionality stays the same. ## The new function name better fits modern R code style guidelines. ## Please adjust your code accordingly.
P_USBOND <- na.interpolation(P_USBOND)
## Warning: na.interpolation will be replaced by na_interpolation. ## Functionality stays the same. ## The new function name better fits modern R code style guidelines. ## Please adjust your code accordingly.
P_CETES
## CETETRCISHRS.MX.Adjusted ## 2018-01-02 94.48975 ## 2018-01-03 94.40782 ## 2018-01-04 94.55347 ## 2018-01-05 94.57167 ## 2018-01-08 94.55347 ## 2018-01-09 94.55347 ## 2018-01-10 94.58076 ## 2018-01-11 94.67179 ## 2018-01-12 94.68999 ## 2018-01-15 94.68999 ## ... ## 2023-11-06 107.16009 ## 2023-11-07 107.30007 ## 2023-11-08 107.33007 ## 2023-11-09 107.45006 ## 2023-11-10 107.47006 ## 2023-11-13 107.49006 ## 2023-11-14 107.60004 ## 2023-11-15 107.64005 ## 2023-11-16 107.76003 ## 2023-11-17 107.83002
P_USBOND
## FVX.Adjusted ## 2018-01-02 2.2500 ## 2018-01-03 2.2470 ## 2018-01-04 2.2680 ## 2018-01-05 2.2850 ## 2018-01-07 2.2850 ## 2018-01-08 2.2850 ## 2018-01-09 2.3240 ## 2018-01-10 2.3280 ## 2018-01-11 2.3140 ## 2018-01-12 2.3470 ## ... ## 2023-11-08 4.5220 ## 2023-11-09 4.6400 ## 2023-11-10 4.6660 ## 2023-11-12 4.6645 ## 2023-11-13 4.6630 ## 2023-11-14 4.4210 ## 2023-11-15 4.5260 ## 2023-11-16 4.4280 ## 2023-11-17 4.4550 ## 2023-11-19 4.4550
#GET THE PRICES Prices2 <- merge.xts(P_CETES, P_USBOND, join = "inner") Prices2
## CETETRCISHRS.MX.Adjusted FVX.Adjusted ## 2018-01-02 94.48975 2.250 ## 2018-01-03 94.40782 2.247 ## 2018-01-04 94.55347 2.268 ## 2018-01-05 94.57167 2.285 ## 2018-01-08 94.55347 2.285 ## 2018-01-09 94.55347 2.324 ## 2018-01-10 94.58076 2.328 ## 2018-01-11 94.67179 2.314 ## 2018-01-12 94.68999 2.347 ## 2018-01-16 94.65359 2.355 ## ... ## 2023-11-06 107.16009 4.609 ## 2023-11-07 107.30007 4.538 ## 2023-11-08 107.33007 4.522 ## 2023-11-09 107.45006 4.640 ## 2023-11-10 107.47006 4.666 ## 2023-11-13 107.49006 4.663 ## 2023-11-14 107.60004 4.421 ## 2023-11-15 107.64005 4.526 ## 2023-11-16 107.76003 4.428 ## 2023-11-17 107.83002 4.455
#CALCULATE RETURNS Returns2 <- Return.calculate(Prices2)[-1] Returns2
## CETETRCISHRS.MX.Adjusted FVX.Adjusted ## 2018-01-03 -0.0008670194 -0.0013333427 ## 2018-01-04 0.0015428041 0.0093457537 ## 2018-01-05 0.0001924425 0.0074956787 ## 2018-01-08 -0.0001924055 0.0000000000 ## 2018-01-09 0.0000000000 0.0170677437 ## 2018-01-10 0.0002885427 0.0017212509 ## 2018-01-11 0.0009624995 -0.0060138217 ## 2018-01-12 0.0001922826 0.0142610173 ## 2018-01-16 -0.0003844912 0.0034086646 ## 2018-01-17 0.0010579993 0.0165605240 ## ... ## 2023-11-06 0.0000000000 0.0269608494 ## 2023-11-07 0.0013063085 -0.0154046639 ## 2023-11-08 0.0002795784 -0.0035258419 ## 2023-11-09 0.0011179298 0.0260946561 ## 2023-11-10 0.0001861727 0.0056034534 ## 2023-11-13 0.0001860671 -0.0006429024 ## 2023-11-14 0.0010232142 -0.0518979407 ## 2023-11-15 0.0003717556 0.0237502870 ## 2023-11-16 0.0011146396 -0.0216526843 ## 2023-11-17 0.0006495179 0.0060975498
# Calculate the portfolio daily returns w2 <- c(.5,.5) portfolio_returns2 <- Returns2 %*% (w2) # Calculate the cumulative returns cumulative_returns2 <- prod(1 + portfolio_returns2) RF <- (cumulative_returns2*.4) RF
## [1] 0.8507569
RV
## [1] 0.7591626
Portfolio <- RF+RV Portfolio
## [1] 1.60992
Asset 1 BMV IPC
Initial_date <- as.Date ("2018-01-01") Final_date <- as.Date ("2023-11-20") Asset <- getSymbols ("^MXX", from = Initial_date, to = Final_date, auto.assign = F, warnings =F) Price <- Ad(Asset) Dates <- index(Asset) plot(Price)
Moving Average
WL <- 5 SMA <- rep(NA, length(Price)) Price[1:5]
## MXX.Adjusted ## 2018-01-02 50033.95 ## 2018-01-03 49782.23 ## 2018-01-04 49744.62 ## 2018-01-05 49887.74 ## 2018-01-08 49995.57
for (i in WL: length(Price)) { SMA[i] <- mean(Price[i-4:i]) } plot(SMA)
Fast and slow SMA
SMA_fast <- SMA(Price, n=5) SMA_slow <- SMA(Price, n=20) Data <- data.frame(Date=Dates, Price= as.numeric(Price), SMA1 = as.numeric(SMA_fast), SMA2 = as.numeric(SMA_slow) ) Data %>% tail(250)%>% ggplot(aes(x=Date, y=Price))+ geom_line(col = p_blue, size = 1)+ geom_line(aes(y = SMA1), col= p_red, size=1)+ geom_line(aes(y = SMA2), col= p_green, size=1)+ labs(title = "SMA Indicators", subtitle = "IPC")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0. ## ℹ Please use `linewidth` instead. ## This warning is displayed once every 8 hours. ## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was ## generated.
Bear or bull:
Data <- Data %>% mutate(Trend=case_when(SMA1>=SMA2~"Bull", SMA1< SMA2 ~ "Bear")) Return <- Return.calculate(Price) Return
## MXX.Adjusted ## 2018-01-02 NA ## 2018-01-03 -0.0050309591 ## 2018-01-04 -0.0007554779 ## 2018-01-05 0.0028770384 ## 2018-01-08 0.0021614937 ## 2018-01-09 -0.0058845057 ## 2018-01-10 -0.0184325115 ## 2018-01-11 0.0002898545 ## 2018-01-12 0.0068959781 ## 2018-01-15 0.0051158327 ## ... ## 2023-11-06 0.0071566576 ## 2023-11-07 -0.0082073903 ## 2023-11-08 -0.0037795615 ## 2023-11-09 0.0010728036 ## 2023-11-10 0.0036656207 ## 2023-11-13 -0.0032644143 ## 2023-11-14 0.0278026156 ## 2023-11-15 0.0054292556 ## 2023-11-16 -0.0061956677 ## 2023-11-17 0.0041117748
Plots
R_k <- as.numeric(last(Price))/as.numeric(first(Price))-1 R_k
## [1] 0.05298707
G_Return <- 1+Return prod(G_Return, na.rm = T)
## [1] 1.052987
#Net Return prod(G_Return, na.rm = T) -1
## [1] 0.05298707
Long only strategy
Data <- Data %>% mutate(Returns =as.numeric(Return)) Data %>% glimpse()
## Rows: 1,483 ## Columns: 6 ## $ Date <date> 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-08, 2… ## $ Price <dbl> 50033.95, 49782.23, 49744.62, 49887.74, 49995.57, 49701.37, 48… ## $ SMA1 <dbl> NA, NA, NA, NA, 49888.82, 49822.31, 49622.91, 49433.86, 49283.… ## $ SMA2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Trend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Returns <dbl> NA, -0.0050309591, -0.0007554779, 0.0028770384, 0.0021614937, …
Data <- Data %>% mutate(Signal = case_when(Trend == "Bull" ~1, Trend == "Bear" ~0)) #And we are going to plot the Signal series Data %>% tail(150) %>% ggplot(aes(x=Date, y=Signal))+ geom_line(col=p_blue, linewidht=1)+ labs(title="Signal Trends", subtitle = "IPC")
## Warning in geom_line(col = p_blue, linewidht = 1): Ignoring unknown parameters: ## `linewidht`
Data <- Data %>% mutate(Profit = Signal * Returns) Data <- Data %>% mutate(G_Return = 1+Profit) Data %>% summarize(R_k = prod(G_Return, na.rm = T) -1)
## R_k ## 1 0.8851487
Asset 2 S&P500
Initial_date <- as.Date ("2018-01-01") Final_date <- as.Date ("2023-11-20") Asset <- getSymbols ("^GSPC", from = Initial_date, to = Final_date, auto.assign = F, warnings =F) Price <- Ad(Asset) Dates <- index(Asset) plot(Price)
Moving Average
WL <- 5 SMA <- rep(NA, length(Price)) Price[1:5]
## GSPC.Adjusted ## 2018-01-02 2695.81 ## 2018-01-03 2713.06 ## 2018-01-04 2723.99 ## 2018-01-05 2743.15 ## 2018-01-08 2747.71
for (i in WL: length(Price)) { SMA[i] <- mean(Price[i-4:i]) } plot(SMA)
Fast and slow SMA
SMA_fast <- SMA(Price, n=5) SMA_slow <- SMA(Price, n=20) Data <- data.frame(Date=Dates, Price= as.numeric(Price), SMA1 = as.numeric(SMA_fast), SMA2 = as.numeric(SMA_slow) ) Data %>% tail(250)%>% ggplot(aes(x=Date, y=Price))+ geom_line(col = p_blue, size = 1)+ geom_line(aes(y = SMA1), col= p_red, size=1)+ geom_line(aes(y = SMA2), col= p_green, size=1)+ labs(title = "SMA Indicators", subtitle = "S&P500")
Bear or bull:
Data <- Data %>% mutate(Trend=case_when(SMA1>=SMA2~"Bull", SMA1< SMA2 ~ "Bear")) Return <- Return.calculate(Price) Return
## GSPC.Adjusted ## 2018-01-02 NA ## 2018-01-03 0.0063988188 ## 2018-01-04 0.0040286361 ## 2018-01-05 0.0070337674 ## 2018-01-08 0.0016623439 ## 2018-01-09 0.0013029316 ## 2018-01-10 -0.0011122268 ## 2018-01-11 0.0070336465 ## 2018-01-12 0.0067496030 ## 2018-01-16 -0.0035244876 ## ... ## 2023-11-06 0.0017529924 ## 2023-11-07 0.0028401189 ## 2023-11-08 0.0010049156 ## 2023-11-09 -0.0080838393 ## 2023-11-10 0.0156164411 ## 2023-11-13 -0.0008358389 ## 2023-11-14 0.0190750177 ## 2023-11-15 0.0015970121 ## 2023-11-16 0.0011904274 ## 2023-11-17 0.0012820491
Plots
R_k <- as.numeric(last(Price))/as.numeric(first(Price))-1 R_k
## [1] 0.6744577
G_Return <- 1+Return prod(G_Return, na.rm = T)
## [1] 1.674458
#Net Return prod(G_Return, na.rm = T) -1
## [1] 0.6744577
Long only strategy
Data <- Data %>% mutate(Returns =as.numeric(Return)) Data %>% glimpse()
## Rows: 1,481 ## Columns: 6 ## $ Date <date> 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-08, 2… ## $ Price <dbl> 2695.81, 2713.06, 2723.99, 2743.15, 2747.71, 2751.29, 2748.23,… ## $ SMA1 <dbl> NA, NA, NA, NA, 2724.744, 2735.840, 2742.874, 2751.588, 2760.2… ## $ SMA2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Trend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Returns <dbl> NA, 0.0063988188, 0.0040286361, 0.0070337674, 0.0016623439, 0.…
Data <- Data %>% mutate(Signal = case_when(Trend == "Bull" ~1, Trend == "Bear" ~0)) #And we are going to plot the Signal series Data %>% tail(150) %>% ggplot(aes(x=Date, y=Signal))+ geom_line(col=p_blue, linewidht=1)+ labs(title="Signal Trends", subtitle = "S&P500")
## Warning in geom_line(col = p_blue, linewidht = 1): Ignoring unknown parameters: ## `linewidht`
Data <- Data %>% mutate(Profit = Signal * Returns) Data <- Data %>% mutate(G_Return = 1+Profit) Data %>% summarize(R_k = prod(G_Return, na.rm = T) -1)
## R_k ## 1 1.476312
Asset 3 Dow Jones
Initial_date <- as.Date ("2018-01-01") Final_date <- as.Date ("2023-11-20") Asset <- getSymbols ("^DJI", from = Initial_date, to = Final_date, auto.assign = F, warnings =F) Price <- Ad(Asset) Dates <- index(Asset) plot(Price)
Moving Average
WL <- 5 SMA <- rep(NA, length(Price)) Price[1:5]
## DJI.Adjusted ## 2018-01-02 24824.01 ## 2018-01-03 24922.68 ## 2018-01-04 25075.13 ## 2018-01-05 25295.87 ## 2018-01-08 25283.00
for (i in WL: length(Price)) { SMA[i] <- mean(Price[i-4:i]) } plot(SMA)
Fast and slow SMA
SMA_fast <- SMA(Price, n=5) SMA_slow <- SMA(Price, n=20) Data <- data.frame(Date=Dates, Price= as.numeric(Price), SMA1 = as.numeric(SMA_fast), SMA2 = as.numeric(SMA_slow) ) Data %>% tail(250)%>% ggplot(aes(x=Date, y=Price))+ geom_line(col = p_blue, size = 1)+ geom_line(aes(y = SMA1), col= p_red, size=1)+ geom_line(aes(y = SMA2), col= p_green, size=1)+ labs(title = "SMA Indicators", subtitle = "Dow Jones")
Bear or bull:
Data <- Data %>% mutate(Trend=case_when(SMA1>=SMA2~"Bull", SMA1< SMA2 ~ "Bear")) Return <- Return.calculate(Price) Return
## DJI.Adjusted ## 2018-01-02 NA ## 2018-01-03 3.974778e-03 ## 2018-01-04 6.116966e-03 ## 2018-01-05 8.803076e-03 ## 2018-01-08 -5.087448e-04 ## 2018-01-09 4.066004e-03 ## 2018-01-10 -6.566632e-04 ## 2018-01-11 8.104322e-03 ## 2018-01-12 8.932997e-03 ## 2018-01-16 -4.003411e-04 ## ... ## 2023-11-06 1.014026e-03 ## 2023-11-07 1.664196e-03 ## 2023-11-08 -1.180936e-03 ## 2023-11-09 -6.458911e-03 ## 2023-11-10 1.154139e-02 ## 2023-11-13 1.597566e-03 ## 2023-11-14 1.426495e-02 ## 2023-11-15 4.694876e-03 ## 2023-11-16 -1.307248e-03 ## 2023-11-17 5.186652e-05
Plots
R_k <- as.numeric(last(Price))/as.numeric(first(Price))-1 R_k
## [1] 0.4078016
G_Return <- 1+Return prod(G_Return, na.rm = T)
## [1] 1.407802
#Net Return prod(G_Return, na.rm = T) -1
## [1] 0.4078016
Long only strategy
Data <- Data %>% mutate(Returns =as.numeric(Return)) Data %>% glimpse()
## Rows: 1,481 ## Columns: 6 ## $ Date <date> 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-08, 2… ## $ Price <dbl> 24824.01, 24922.68, 25075.13, 25295.87, 25283.00, 25385.80, 25… ## $ SMA1 <dbl> NA, NA, NA, NA, 25080.14, 25192.50, 25281.79, 25381.71, 25483.… ## $ SMA2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Trend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Returns <dbl> NA, 0.0039747778, 0.0061169655, 0.0088030759, -0.0005087448, 0…
Data <- Data %>% mutate(Signal = case_when(Trend == "Bull" ~1, Trend == "Bear" ~0)) #And we are going to plot the Signal series Data %>% tail(150) %>% ggplot(aes(x=Date, y=Signal))+ geom_line(col=p_blue, linewidht=1)+ labs(title="Signal Trends", subtitle = "Dow Jones")
## Warning in geom_line(col = p_blue, linewidht = 1): Ignoring unknown parameters: ## `linewidht`
Data <- Data %>% mutate(Profit = Signal * Returns) Data <- Data %>% mutate(G_Return = 1+Profit) Data %>% summarize(R_k = prod(G_Return, na.rm = T) -1)
## R_k ## 1 1.0715
Asset 4 Apple
Initial_date <- as.Date ("2018-01-01") Final_date <- as.Date ("2023-11-20") Asset <- getSymbols ("AAPL", from = Initial_date, to = Final_date, auto.assign = F, warnings =F) Price <- Ad(Asset) Dates <- index(Asset) plot(Price)
Moving Average
WL <- 5 SMA <- rep(NA, length(Price)) Price[1:5]
## AAPL.Adjusted ## 2018-01-02 40.72287 ## 2018-01-03 40.71579 ## 2018-01-04 40.90490 ## 2018-01-05 41.37062 ## 2018-01-08 41.21696
for (i in WL: length(Price)) { SMA[i] <- mean(Price[i-4:i]) } plot(SMA)
Fast and slow SMA
SMA_fast <- SMA(Price, n=5) SMA_slow <- SMA(Price, n=20) Data <- data.frame(Date=Dates, Price= as.numeric(Price), SMA1 = as.numeric(SMA_fast), SMA2 = as.numeric(SMA_slow) ) Data %>% tail(250)%>% ggplot(aes(x=Date, y=Price))+ geom_line(col = p_blue, size = 1)+ geom_line(aes(y = SMA1), col= p_red, size=1)+ geom_line(aes(y = SMA2), col= p_green, size=1)+ labs(title = "SMA Indicators", subtitle = "Apple (AAPL)")
Bear or bull:
Data <- Data %>% mutate(Trend=case_when(SMA1>=SMA2~"Bull", SMA1< SMA2 ~ "Bear")) Return <- Return.calculate(Price) Return
## AAPL.Adjusted ## 2018-01-02 NA ## 2018-01-03 -0.0001739537 ## 2018-01-04 0.0046447247 ## 2018-01-05 0.0113855540 ## 2018-01-08 -0.0037144097 ## 2018-01-09 -0.0001149491 ## 2018-01-10 -0.0002292768 ## 2018-01-11 0.0056803689 ## 2018-01-12 0.0103262507 ## 2018-01-16 -0.0050820163 ## ... ## 2023-11-06 0.0146052026 ## 2023-11-07 0.0144507131 ## 2023-11-08 0.0058849194 ## 2023-11-09 -0.0026245375 ## 2023-11-10 0.0232200459 ## 2023-11-13 -0.0085836422 ## 2023-11-14 0.0142857107 ## 2023-11-15 0.0030409307 ## 2023-11-16 0.0090421374 ## 2023-11-17 -0.0001054466
Plots
R_k <- as.numeric(last(Price))/as.numeric(first(Price))-1 R_k
## [1] 3.658071
G_Return <- 1+Return prod(G_Return, na.rm = T)
## [1] 4.658071
#Net Return prod(G_Return, na.rm = T) -1
## [1] 3.658071
Long only strategy
Data <- Data %>% mutate(Returns =as.numeric(Return)) Data %>% glimpse()
## Rows: 1,481 ## Columns: 6 ## $ Date <date> 2018-01-02, 2018-01-03, 2018-01-04, 2018-01-05, 2018-01-08, 2… ## $ Price <dbl> 40.72287, 40.71579, 40.90490, 41.37062, 41.21696, 41.21222, 41… ## $ SMA1 <dbl> NA, NA, NA, NA, 40.98623, 41.08410, 41.18149, 41.28788, 41.386… ## $ SMA2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Trend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Returns <dbl> NA, -0.0001739537, 0.0046447247, 0.0113855540, -0.0037144097, …
Data <- Data %>% mutate(Signal = case_when(Trend == "Bull" ~1, Trend == "Bear" ~0)) #And we are going to plot the Signal series Data %>% tail(150) %>% ggplot(aes(x=Date, y=Signal))+ geom_line(col=p_blue, linewidht=1)+ labs(title="Signal Trends", subtitle = "Apple (AAPL)")
## Warning in geom_line(col = p_blue, linewidht = 1): Ignoring unknown parameters: ## `linewidht`
Data <- Data %>% mutate(Profit = Signal * Returns) Data <- Data %>% mutate(G_Return = 1+Profit) Data %>% summarize(R_k = prod(G_Return, na.rm = T) -1)
## R_k ## 1 8.740937