# Cargar librerías library(quantmod)
## Warning: package 'quantmod' was built under R version 4.3.2
## Warning: package 'zoo' was built under R version 4.3.2
library(PerformanceAnalytics)
## Warning: package 'PerformanceAnalytics' was built under R version 4.3.2
library(zoo) #Importar librerías library(dplyr)
library(ggplot2) library(tidyr) library(scales) library(quantmod) library(PerformanceAnalytics) library(caret)
## Warning: package 'caret' was built under R version 4.3.2
library(caTools)
## Warning: package 'caTools' was built under R version 4.3.2
#Definir colores p_blue <- '#5478DC' p_red <- '#FF4E62' p_green <- '#1FBF1D' p_yellow <- '#F0C24F' p_purple <- '#764DE8' p_orange <- '#FF7342'
Portafolio de inversión creado con la metodología de Markowitz.
# 10 Acciones del IPC y S&P500 Tickers <- c("GCARSOA1.MX", "FEMSAUBD.MX", "META", "COST", "MAR", "IBM", "WMT", "JNPR", "TSLA", "FTNT") # Datos de los tickers de los últimos 6 meses getSymbols(Tickers, from = as.Date("2023-08-01"), to = as.Date("2024-01-31"))
## [1] "GCARSOA1.MX" "FEMSAUBD.MX" "META" "COST" "MAR" ## [6] "IBM" "WMT" "JNPR" "TSLA" "FTNT"
# Precios ajustados de los tickers Precios_ajustados <- lapply(Tickers, function(ticker) Ad(get(ticker))) # Contar NA's NA_ticker <- sapply(Precios_ajustados, function(data) sum(is.na(data))) NA_ticker
## [1] 0 0 0 0 0 0 0 0 0 0
# Combinar los precios ajustados Precios <- do.call(merge, Precios_ajustados) Precios
## GCARSOA1.MX.Adjusted FEMSAUBD.MX.Adjusted META.Adjusted ## 2023-08-01 129.0008 189.65 322.71 ## 2023-08-02 125.4825 187.28 314.31 ## 2023-08-03 128.5921 189.49 313.19 ## 2023-08-04 132.0506 189.47 310.73 ## 2023-08-07 128.0938 192.29 316.56 ## 2023-08-08 127.4659 193.06 312.64 ## 2023-08-09 128.6021 192.49 305.21 ## 2023-08-10 129.2300 191.49 305.74 ## 2023-08-11 127.5058 190.08 301.64 ## 2023-08-14 128.0141 192.11 306.19 ## ... ## 2024-01-17 162.2700 221.78 368.37 ## 2024-01-18 153.2900 227.70 376.13 ## 2024-01-19 158.7400 228.25 383.45 ## 2024-01-22 153.3700 231.42 381.78 ## 2024-01-23 158.9300 235.54 385.20 ## 2024-01-24 163.0300 228.50 390.70 ## 2024-01-25 161.4700 233.11 393.18 ## 2024-01-26 165.5500 233.85 394.14 ## 2024-01-29 168.0700 234.66 401.02 ## 2024-01-30 165.9900 235.82 400.06 ## COST.Adjusted MAR.Adjusted IBM.Adjusted WMT.Adjusted JNPR.Adjusted ## 2023-08-01 540.3077 203.7101 140.1104 157.9597 27.57579 ## 2023-08-02 537.2728 202.4862 140.9315 158.0689 27.85145 ## 2023-08-03 542.4671 199.8396 141.2052 158.1086 27.84160 ## 2023-08-04 536.9713 201.9589 140.9999 157.1953 27.80222 ## 2023-08-07 543.1675 203.6703 142.8963 159.3297 27.65455 ## 2023-08-08 540.9399 205.4214 142.6324 159.4886 27.29029 ## 2023-08-09 544.4222 205.0831 140.8921 159.7467 27.33951 ## 2023-08-10 546.1634 207.3417 141.6436 159.6670 27.41827 ## 2023-08-11 547.5933 207.4611 141.5151 160.6035 27.25090 ## 2023-08-14 546.1439 205.7995 140.3186 159.4079 27.33951 ## ... ## 2024-01-17 683.1857 227.7000 166.0800 161.0700 37.22000 ## 2024-01-18 686.3011 232.6800 166.8400 163.2400 37.32000 ## 2024-01-19 693.9498 233.6700 171.4800 162.3800 37.41000 ## 2024-01-22 691.4935 237.3800 172.8300 162.3900 37.47000 ## 2024-01-23 686.5807 237.1000 173.9400 162.8000 37.29000 ## 2024-01-24 685.5023 237.8700 173.9300 160.5000 37.19000 ## 2024-01-25 678.9020 242.5200 190.4300 162.8400 37.23000 ## 2024-01-26 685.8718 241.6000 187.4200 164.2700 37.20000 ## 2024-01-29 692.9913 242.1300 187.1400 165.0400 37.23000 ## 2024-01-30 699.7114 243.1900 187.8700 165.5900 37.18000 ## TSLA.Adjusted FTNT.Adjusted ## 2023-08-01 261.07 78.03 ## 2023-08-02 254.11 74.32 ## 2023-08-03 259.32 75.76 ## 2023-08-04 253.86 56.77 ## 2023-08-07 251.45 57.70 ## 2023-08-08 249.70 58.57 ## 2023-08-09 242.19 59.78 ## 2023-08-10 245.34 60.56 ## 2023-08-11 242.65 59.68 ## 2023-08-14 239.76 59.66 ## ... ## 2024-01-17 215.55 60.62 ## 2024-01-18 211.88 61.41 ## 2024-01-19 212.19 60.90 ## 2024-01-22 208.80 62.66 ## 2024-01-23 209.14 64.41 ## 2024-01-24 207.83 66.11 ## 2024-01-25 182.63 65.55 ## 2024-01-26 183.25 66.12 ## 2024-01-29 190.93 66.45 ## 2024-01-30 191.59 66.71
# Contar y rellenar NA's NA_total <- sum(is.na(Precios)) NA_total
## [1] 30
Precios <- na.locf(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
# Rendimientos diarios Rends <- Return.calculate(Precios)[-1] Rends
## GCARSOA1.MX.Adjusted FEMSAUBD.MX.Adjusted META.Adjusted ## 2023-08-02 -0.027273383 -0.012496679 -0.026029544 ## 2023-08-03 0.024781603 0.011800549 -0.003563346 ## 2023-08-04 0.026895080 -0.000105569 -0.007854630 ## 2023-08-07 -0.029964626 0.014883581 0.018762226 ## 2023-08-08 -0.004901930 0.004004391 -0.012383065 ## 2023-08-09 0.008913936 -0.002952409 -0.023765426 ## 2023-08-10 0.004882614 -0.005195075 0.001736505 ## 2023-08-11 -0.013342671 -0.007363328 -0.013410008 ## 2023-08-14 0.003986613 0.010679707 0.015084165 ## 2023-08-15 0.028417921 0.003123242 -0.013847579 ## ... ## 2024-01-17 -0.033301539 -0.007962063 0.002476470 ## 2024-01-18 -0.055339932 0.026693111 0.021065803 ## 2024-01-19 0.035553607 0.002415472 0.019461376 ## 2024-01-22 -0.033828967 0.013888272 -0.004355231 ## 2024-01-23 0.036252186 0.017803108 0.008958074 ## 2024-01-24 0.025797561 -0.029888739 0.014278297 ## 2024-01-25 -0.009568776 0.020175057 0.006347531 ## 2024-01-26 0.025267863 0.003174491 0.002441686 ## 2024-01-29 0.015222013 0.003463748 0.017455661 ## 2024-01-30 -0.012375806 0.004943338 -0.002393874 ## COST.Adjusted MAR.Adjusted IBM.Adjusted WMT.Adjusted ## 2023-08-02 -0.005616894 -0.0060077108 5.860532e-03 6.913608e-04 ## 2023-08-03 0.009667853 -0.0130705498 1.942165e-03 2.511776e-04 ## 2023-08-04 -0.010131213 0.0106048933 -1.453746e-03 -5.776696e-03 ## 2023-08-07 0.011539217 0.0084737509 1.344971e-02 1.357840e-02 ## 2023-08-08 -0.004101017 0.0085979388 -1.847012e-03 9.969514e-04 ## 2023-08-09 0.006437498 -0.0016467970 -1.220094e-02 1.618409e-03 ## 2023-08-10 0.003198161 0.0110129761 5.333618e-03 -4.989895e-04 ## 2023-08-11 0.002618027 0.0005758612 -9.074892e-04 5.865480e-03 ## 2023-08-14 -0.002646858 -0.0080091673 -8.454410e-03 -7.444151e-03 ## 2023-08-15 -0.005271780 -0.0040611611 -2.819728e-04 -5.125032e-03 ## ... ## 2024-01-17 0.003961919 0.0060086623 -5.270753e-03 -4.696252e-03 ## 2024-01-18 0.004560133 0.0218708643 4.576075e-03 1.347239e-02 ## 2024-01-19 0.011144879 0.0042547942 2.781107e-02 -5.268320e-03 ## 2024-01-22 -0.003539679 0.0158771205 7.872674e-03 6.155011e-05 ## 2024-01-23 -0.007104506 -0.0011795382 6.422500e-03 2.524809e-03 ## 2024-01-24 -0.001570726 0.0032475284 -5.754723e-05 -1.412778e-02 ## 2024-01-25 -0.009628386 0.0195485318 9.486575e-02 1.457942e-02 ## 2024-01-26 0.010266174 -0.0037934939 -1.580630e-02 8.781675e-03 ## 2024-01-29 0.010380230 0.0021937035 -1.493964e-03 4.687338e-03 ## 2024-01-30 0.009697226 0.0043778034 3.900800e-03 3.332544e-03 ## JNPR.Adjusted TSLA.Adjusted FTNT.Adjusted ## 2023-08-02 0.0099964337 -0.026659542 -0.0475458047 ## 2023-08-03 -0.0003535088 0.020502958 0.0193757057 ## 2023-08-04 -0.0014144669 -0.021055092 -0.2506599940 ## 2023-08-07 -0.0053116098 -0.009493436 0.0163818971 ## 2023-08-08 -0.0131719212 -0.006959634 0.0150779709 ## 2023-08-09 0.0018037501 -0.030076070 0.0206590249 ## 2023-08-10 0.0028808178 0.013006292 0.0130478857 ## 2023-08-11 -0.0061041790 -0.010964386 -0.0145310609 ## 2023-08-14 0.0032514841 -0.011910156 -0.0003351283 ## 2023-08-15 -0.0158444630 -0.028361645 -0.0082132363 ## ... ## 2024-01-17 -0.0034805156 -0.019826295 -0.0222580817 ## 2024-01-18 0.0026866865 -0.017026203 0.0130320180 ## 2024-01-19 0.0024115797 0.001463081 -0.0083048090 ## 2024-01-22 0.0016038860 -0.015976245 0.0288998075 ## 2024-01-23 -0.0048038511 0.001628335 0.0279285640 ## 2024-01-24 -0.0026817454 -0.006263735 0.0263933683 ## 2024-01-25 0.0010755826 -0.121252931 -0.0084706936 ## 2024-01-26 -0.0008057690 0.003394815 0.0086956471 ## 2024-01-29 0.0008064188 0.041909919 0.0049908377 ## 2024-01-30 -0.0013429825 0.003456784 0.0039127487
# Matriz de varianzas y covarianzas MVC <- cov(Rends) MVC * 100
## GCARSOA1.MX.Adjusted FEMSAUBD.MX.Adjusted META.Adjusted ## GCARSOA1.MX.Adjusted 0.0641462537 0.0003533643 -0.0008831271 ## FEMSAUBD.MX.Adjusted 0.0003533643 0.0188470744 0.0053723197 ## META.Adjusted -0.0008831271 0.0053723197 0.0271592998 ## COST.Adjusted 0.0038171185 0.0039989069 0.0068886927 ## MAR.Adjusted 0.0039592210 0.0041245549 0.0072772176 ## IBM.Adjusted 0.0031099361 0.0051523014 0.0030492316 ## WMT.Adjusted -0.0031353709 0.0034602444 0.0028732668 ## JNPR.Adjusted -0.0072061946 0.0020733007 0.0014664182 ## TSLA.Adjusted 0.0140235417 0.0013337057 0.0194338507 ## FTNT.Adjusted -0.0087060247 0.0015564852 0.0124156785 ## COST.Adjusted MAR.Adjusted IBM.Adjusted WMT.Adjusted ## GCARSOA1.MX.Adjusted 0.003817119 0.003959221 0.0031099361 -0.003135371 ## FEMSAUBD.MX.Adjusted 0.003998907 0.004124555 0.0051523014 0.003460244 ## META.Adjusted 0.006888693 0.007277218 0.0030492316 0.002873267 ## COST.Adjusted 0.011957713 0.004755007 0.0016271815 0.006285577 ## MAR.Adjusted 0.004755007 0.017387427 0.0043481243 0.001569623 ## IBM.Adjusted 0.001627181 0.004348124 0.0156508209 0.001683607 ## WMT.Adjusted 0.006285577 0.001569623 0.0016836072 0.011564379 ## JNPR.Adjusted 0.002320421 0.001141518 0.0004738935 0.002749496 ## TSLA.Adjusted 0.010202287 0.004063402 -0.0043675069 0.006643276 ## FTNT.Adjusted 0.005295179 0.002764378 0.0035279752 0.003968557 ## JNPR.Adjusted TSLA.Adjusted FTNT.Adjusted ## GCARSOA1.MX.Adjusted -0.0072061946 0.0140235417 -0.008706025 ## FEMSAUBD.MX.Adjusted 0.0020733007 0.0013337057 0.001556485 ## META.Adjusted 0.0014664182 0.0194338507 0.012415678 ## COST.Adjusted 0.0023204210 0.0102022874 0.005295179 ## MAR.Adjusted 0.0011415184 0.0040634025 0.002764378 ## IBM.Adjusted 0.0004738935 -0.0043675069 0.003527975 ## WMT.Adjusted 0.0027494957 0.0066432756 0.003968557 ## JNPR.Adjusted 0.0514186913 0.0006939598 -0.001682964 ## TSLA.Adjusted 0.0006939598 0.0890739401 0.020858509 ## FTNT.Adjusted -0.0016829640 0.0208585090 0.088224452
# Matriz de varianzas y covarianzas invertida MVC_Inv = solve(MVC) MVC_Inv
## GCARSOA1.MX.Adjusted FEMSAUBD.MX.Adjusted META.Adjusted ## GCARSOA1.MX.Adjusted 1914.75973 40.22975 559.88653 ## FEMSAUBD.MX.Adjusted 40.22975 6459.39075 -788.92414 ## META.Adjusted 559.88653 -788.92414 5593.09104 ## COST.Adjusted -1190.70336 -936.56838 -2059.77516 ## MAR.Adjusted -258.61562 -512.98723 -1352.71007 ## IBM.Adjusted -642.33886 -1613.01611 -686.62762 ## WMT.Adjusted 1255.46852 -1030.90073 1104.68090 ## JNPR.Adjusted 264.28853 -106.21013 24.28155 ## TSLA.Adjusted -467.32861 164.83915 -1038.37714 ## FTNT.Adjusted 273.75834 143.19765 -328.19841 ## COST.Adjusted MAR.Adjusted IBM.Adjusted WMT.Adjusted ## GCARSOA1.MX.Adjusted -1190.7034 -258.61562 -642.33886 1255.468519 ## FEMSAUBD.MX.Adjusted -936.5684 -512.98723 -1613.01611 -1030.900729 ## META.Adjusted -2059.7752 -1352.71007 -686.62762 1104.680899 ## COST.Adjusted 15359.8899 -2216.38367 725.23377 -7300.851498 ## MAR.Adjusted -2216.3837 7348.10399 -1387.03496 750.740670 ## IBM.Adjusted 725.2338 -1387.03496 7944.63468 -1213.497223 ## WMT.Adjusted -7300.8515 750.74067 -1213.49722 13507.132782 ## JNPR.Adjusted -335.1438 -66.88114 -39.88816 -202.967044 ## TSLA.Adjusted -357.6456 122.75542 828.90287 -686.735994 ## FTNT.Adjusted -285.9788 68.07617 -398.21203 0.718737 ## JNPR.Adjusted TSLA.Adjusted FTNT.Adjusted ## GCARSOA1.MX.Adjusted 264.28853 -467.32861 273.758341 ## FEMSAUBD.MX.Adjusted -106.21013 164.83915 143.197647 ## META.Adjusted 24.28155 -1038.37714 -328.198406 ## COST.Adjusted -335.14376 -357.64562 -285.978771 ## MAR.Adjusted -66.88114 122.75542 68.076165 ## IBM.Adjusted -39.88816 828.90287 -398.212029 ## WMT.Adjusted -202.96704 -686.73599 0.718737 ## JNPR.Adjusted 2017.06719 -30.57289 103.178233 ## TSLA.Adjusted -30.57289 1610.75986 -268.939411 ## FTNT.Adjusted 103.17823 -268.93941 1300.622847
MVC %*% MVC_Inv
## GCARSOA1.MX.Adjusted FEMSAUBD.MX.Adjusted META.Adjusted ## GCARSOA1.MX.Adjusted 1.000000e+00 5.204170e-18 3.816392e-17 ## FEMSAUBD.MX.Adjusted -1.387779e-17 1.000000e+00 -1.734723e-18 ## META.Adjusted 4.163336e-17 -5.898060e-17 1.000000e+00 ## COST.Adjusted -1.561251e-17 4.857226e-17 2.775558e-17 ## MAR.Adjusted 1.647987e-17 -3.382711e-17 9.540979e-17 ## IBM.Adjusted -1.214306e-17 1.639314e-16 1.387779e-17 ## WMT.Adjusted -8.673617e-18 2.255141e-17 -1.214306e-17 ## JNPR.Adjusted 3.382711e-17 1.821460e-17 1.301043e-17 ## TSLA.Adjusted -7.632783e-17 -4.163336e-17 -1.387779e-17 ## FTNT.Adjusted 5.551115e-17 -5.551115e-17 1.665335e-16 ## COST.Adjusted MAR.Adjusted IBM.Adjusted WMT.Adjusted ## GCARSOA1.MX.Adjusted -9.714451e-17 6.938894e-18 -6.938894e-18 3.831299e-17 ## FEMSAUBD.MX.Adjusted 2.688821e-17 -3.144186e-17 3.122502e-17 4.840116e-17 ## META.Adjusted -2.289835e-16 -3.642919e-17 2.775558e-17 9.985502e-17 ## COST.Adjusted 1.000000e+00 -6.852158e-17 -2.081668e-17 1.301043e-16 ## MAR.Adjusted 4.510281e-17 1.000000e+00 2.949030e-17 1.394555e-16 ## IBM.Adjusted -5.377643e-17 1.170938e-17 1.000000e+00 4.000706e-17 ## WMT.Adjusted 8.500145e-17 -4.163336e-17 3.469447e-18 1.000000e+00 ## JNPR.Adjusted -3.209238e-17 8.456777e-18 -7.806256e-18 9.260950e-17 ## TSLA.Adjusted -7.632783e-17 -3.989864e-17 1.526557e-16 6.562134e-17 ## FTNT.Adjusted 1.665335e-16 -6.938894e-17 1.110223e-16 3.653761e-17 ## JNPR.Adjusted TSLA.Adjusted FTNT.Adjusted ## GCARSOA1.MX.Adjusted 0.000000e+00 3.816392e-17 -2.775558e-17 ## FEMSAUBD.MX.Adjusted -9.757820e-18 9.540979e-18 3.469447e-18 ## META.Adjusted 5.204170e-18 -3.469447e-17 -2.775558e-17 ## COST.Adjusted 0.000000e+00 -3.989864e-17 0.000000e+00 ## MAR.Adjusted -2.602085e-18 -2.949030e-17 -6.938894e-18 ## IBM.Adjusted -4.336809e-18 -5.204170e-18 0.000000e+00 ## WMT.Adjusted -3.469447e-18 -6.938894e-18 0.000000e+00 ## JNPR.Adjusted 1.000000e+00 4.336809e-18 1.040834e-17 ## TSLA.Adjusted -3.469447e-18 1.000000e+00 0.000000e+00 ## FTNT.Adjusted -2.775558e-17 8.326673e-17 1.000000e+00
# Matriz de unos Unos <- matrix(data = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), nrow = 1) Unos
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] ## [1,] 1 1 1 1 1 1 1 1 1 1
# Formula de Markowitz: w* = (1n * MVC-1) / (1n * MVC-1 * t(1n)) Numerador <- Unos %*% MVC_Inv Numerador
## GCARSOA1.MX.Adjusted FEMSAUBD.MX.Adjusted META.Adjusted COST.Adjusted ## [1,] 1749.405 1819.051 1027.327 1402.073 ## MAR.Adjusted IBM.Adjusted WMT.Adjusted JNPR.Adjusted TSLA.Adjusted ## [1,] 2495.064 3518.156 6183.789 1627.152 -122.3424 ## FTNT.Adjusted ## [1,] 608.2234
Denominador <- Unos %*% MVC_Inv %*% t(Unos) Denominador
## [,1] ## [1,] 20307.9
w <- Numerador / as.numeric(Denominador) w
## GCARSOA1.MX.Adjusted FEMSAUBD.MX.Adjusted META.Adjusted COST.Adjusted ## [1,] 0.08614406 0.08957355 0.05058758 0.06904079 ## MAR.Adjusted IBM.Adjusted WMT.Adjusted JNPR.Adjusted TSLA.Adjusted ## [1,] 0.1228617 0.1732408 0.3045017 0.08012411 -0.006024374 ## FTNT.Adjusted ## [1,] 0.02995009
sum(w)
## [1] 1
# Rendimiento punta a punta RendsGCARSOA1.MX <- as.numeric(last(Precios$GCARSOA1.MX))/as.numeric(first(Precios$GCARSOA1.MX)) -1 RendsGCARSOA1.MX
## [1] 0.2867365
RendsFEMSAUBD.MX <- as.numeric(last(Precios$FEMSAUBD.MX))/as.numeric(first(Precios$FEMSAUBD.MX)) -1 RendsFEMSAUBD.MX
## [1] 0.2434485
RendsMETA <- as.numeric(last(Precios$META))/as.numeric(first(Precios$META)) -1 RendsMETA
## [1] 0.2396889
RendsCOST <- as.numeric(last(Precios$COST))/as.numeric(first(Precios$COST)) -1 RendsCOST
## [1] 0.2950239
RendsMAR <- as.numeric(last(Precios$MAR))/as.numeric(first(Precios$MAR)) -1 RendsMAR
## [1] 0.1938045
RendsIBM <- as.numeric(last(Precios$IBM))/as.numeric(first(Precios$IBM)) -1 RendsIBM
## [1] 0.3408716
RendsWMT <- as.numeric(last(Precios$WMT))/as.numeric(first(Precios$WMT)) -1 RendsWMT
## [1] 0.04830533
RendsJNPR <- as.numeric(last(Precios$JNPR))/as.numeric(first(Precios$JNPR)) -1 RendsJNPR
## [1] 0.3482841
RendsTSLA <- as.numeric(last(Precios$TSLA))/as.numeric(first(Precios$TSLA)) -1 RendsTSLA
## [1] -0.2661356
RendsFTNT <- as.numeric(last(Precios$FTNT))/as.numeric(first(Precios$FTNT)) -1 RendsFTNT
## [1] -0.1450724
Rendimiento_1 <- (0.08614403) * RendsGCARSOA1.MX + (0.08957364) * RendsFEMSAUBD.MX + (0.05058752) * RendsMETA + (0.06904116) * RendsCOST + (0.1228616) * RendsMAR + (0.1732409) * RendsIBM + (0.3045014) * RendsWMT + (0.0801241) * RendsJNPR + (-0.006024369) * RendsTSLA + (0.02995006) * RendsFTNT Rendimiento_1 <- Rendimiento_1 * 100 Rendimiento_1
## [1] 20.17387
# Varianza del portafolio óptimo: Var(p) = w * MVC * t(w) VarP <- w %*% MVC %*% t(w) VarP * 100
## [,1] ## [1,] 0.004924192
# Rendimiento esperado de la cartera E_rp <- colMeans(Rends, na.rm = TRUE) E_rp
## GCARSOA1.MX.Adjusted FEMSAUBD.MX.Adjusted META.Adjusted ## 0.0022877081 0.0017966395 0.0018147929 ## COST.Adjusted MAR.Adjusted IBM.Adjusted ## 0.0020809454 0.0014709518 0.0023695538 ## WMT.Adjusted JNPR.Adjusted TSLA.Adjusted ## 0.0004274654 0.0025691018 -0.0019707002 ## FTNT.Adjusted ## -0.0007309768
Estrategia personalizada de trading automático.
GCARSOA1.MX
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("GCARSOA1.MX", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 129.0008, 125.4825, 128.5921, 132.0506, 128.0938, 127.4659, 12… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 128.4038, 128.6689, 129.53… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "GCARSOA1.MX", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "GCARSOA1.MX)", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 129.0008, 125.4825, 128.5921, 132.0506, 128.0938, 127.4659, 12… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 128.4038, 128.6689, 129.53… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] 28.67365
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] 28.67365
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 129.0008, 125.4825, 128.5921, 132.0506, 128.0938, 127.4659, 12… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 128.4038, 128.6689, 129.53… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, -0.0272733832, 0.0247816032, 0.0268950799, -0.0299646259, …
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "GCARSOA1.MX")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "GCARSOA1.MX")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 11.59781
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_GCARSOA1.MX <- Resultado$RendsTotalC R_L_GCARSOA1.MX
## [1] 11.59781
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "GCARSOA1.MX")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "GCARSOA1.MX")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 -0.5599776
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_GCARSOA1.MX <- Resultado$RendsCyV R_CyV_GCARSOA1.MX
## [1] -0.5599776
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "GCARSOA1.MX")
FEMSAUBD.MX
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("FEMSAUBD.MX", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 189.65, 187.28, 189.49, 189.47, 192.29, 193.06, 192.49, 191.49… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 190.741, 191.047, 192.286,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "FEMSAUBD.MX", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "FEMSAUBD.MX", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 189.65, 187.28, 189.49, 189.47, 192.29, 193.06, 192.49, 191.49… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 190.741, 191.047, 192.286,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] 24.34485
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] 24.34485
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 189.65, 187.28, 189.49, 189.47, 192.29, 193.06, 192.49, 191.49… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 190.741, 191.047, 192.286,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, -0.0124966791, 0.0118005485, -0.0001055690, 0.0148835808, …
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "FEMSAUBD.MX")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "FEMSAUBD.MX")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 9.344261
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_FEMSAUBD.MX <- Resultado$RendsTotalC R_L_FEMSAUBD.MX
## [1] 9.344261
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "FEMSAUBD.MX")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "FEMSAUBD.MX")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 -5.597308
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_FEMSAUBD.MX <- Resultado$RendsCyV R_CyV_FEMSAUBD.MX
## [1] -5.597308
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "FEMSAUBD.MX")
META
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("META", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 322.71, 314.31, 313.19, 310.73, 316.56, 312.64, 305.21, 305.74… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 310.892, 308.816, 306.814,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "META", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "META", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 322.71, 314.31, 313.19, 310.73, 316.56, 312.64, 305.21, 305.74… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 310.892, 308.816, 306.814,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] 23.96889
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] 23.96889
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 322.71, 314.31, 313.19, 310.73, 316.56, 312.64, 305.21, 305.74… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 310.892, 308.816, 306.814,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, -0.026029544, -0.003563346, -0.007854630, 0.018762226, -0.…
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "META")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "META")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 25.8658
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_META <- Resultado$RendsTotalC R_L_META
## [1] 25.8658
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "META")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "META")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 18.94593
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_META <- Resultado$RendsCyV R_CyV_META
## [1] 18.94593
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "META")
COST
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("COST", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 540.3077, 537.2728, 542.4671, 536.9713, 543.1674, 540.9399, 54… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 542.5449, 542.8406, 543.49… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "COST", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "COST", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 540.3077, 537.2728, 542.4671, 536.9713, 543.1674, 540.9399, 54… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 542.5449, 542.8406, 543.49… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] 29.50239
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] 29.50239
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 540.3077, 537.2728, 542.4671, 536.9713, 543.1674, 540.9399, 54… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 542.5449, 542.8406, 543.49… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, -0.0056168942, 0.0096678534, -0.0101312127, 0.0115391037, …
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "COST")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "COST")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 24.72474
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_COST <- Resultado$RendsTotalC R_L_COST
## [1] 24.72474
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "COST")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "COST")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 20.90402
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_COST <- Resultado$RendsCyV R_CyV_COST
## [1] 20.90402
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "COST")
MAR
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("MAR", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 203.7101, 202.4862, 199.8396, 201.9589, 203.6703, 205.4214, 20… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 204.2772, 204.4026, 204.75… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "MAR", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "MAR", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 203.7101, 202.4862, 199.8396, 201.9589, 203.6703, 205.4214, 20… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 204.2772, 204.4026, 204.75… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] 19.38045
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] 19.38045
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 203.7101, 202.4862, 199.8396, 201.9589, 203.6703, 205.4214, 20… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 204.2772, 204.4026, 204.75… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, -0.0060077108, -0.0130705498, 0.0106048933, 0.0084737509, …
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "MAR")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "MAR")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 23.31322
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_MAR <- Resultado$RendsTotalC R_L_MAR
## [1] 23.31322
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "MAR")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "MAR")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 19.46224
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_MAR <- Resultado$RendsCyV R_CyV_MAR
## [1] 19.46224
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "MAR")
IBM
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("IBM", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 140.1104, 140.9315, 141.2052, 140.9999, 142.8963, 142.6324, 14… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 141.3145, 141.3314, 141.14… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "IBM", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "IBM", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 140.1104, 140.9315, 141.2052, 140.9999, 142.8963, 142.6324, 14… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 141.3145, 141.3314, 141.14… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] 34.08716
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] 34.08716
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 140.1104, 140.9315, 141.2052, 140.9999, 142.8963, 142.6324, 14… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 141.3145, 141.3314, 141.14… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, 0.0058605321, 0.0019421649, -0.0014537461, 0.0134497114, -…
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "IBM")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "IBM")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 23.04566
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_IBM <- Resultado$RendsTotalC R_L_IBM
## [1] 23.04566
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "IBM")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "IBM")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 16.25114
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_IBM <- Resultado$RendsCyV R_CyV_IBM
## [1] 16.25114
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "IBM")
WMT
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("WMT", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 157.9597, 158.0689, 158.1086, 157.1953, 159.3297, 159.4886, 15… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 158.9576, 159.0207, 159.08… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "WMT", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "WMT", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 157.9597, 158.0689, 158.1086, 157.1953, 159.3297, 159.4886, 15… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 158.9576, 159.0207, 159.08… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] 4.830533
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] 4.830533
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 157.9597, 158.0689, 158.1086, 157.1953, 159.3297, 159.4886, 15… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 158.9576, 159.0207, 159.08… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, 0.0006913608, 0.0002511776, -0.0057766957, 0.0135784986, 0…
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "WMT")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "WMT")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 -1.935859
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_WMT <- Resultado$RendsTotalC R_L_WMT
## [1] -1.935859
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "WMT")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "WMT")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 -5.972746
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_WMT <- Resultado$RendsCyV R_CyV_WMT
## [1] -5.972746
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "WMT")
JNPR
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("JNPR", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 27.57579, 27.85145, 27.84160, 27.80222, 27.65455, 27.29029, 27… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 27.53641, 27.46946, 27.354… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "JNPR", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "JNPR", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 27.57579, 27.85145, 27.84160, 27.80222, 27.65455, 27.29029, 27… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 27.53641, 27.46946, 27.354… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] 34.82842
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] 34.82842
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 27.57579, 27.85145, 27.84160, 27.80222, 27.65455, 27.29029, 27… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 27.53641, 27.46946, 27.354… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, 0.0099965036, -0.0003535088, -0.0014144669, -0.0053115412,…
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "JNPR")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "JNPR")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 37.76975
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_JNPR <- Resultado$RendsTotalC R_L_JNPR
## [1] 37.76975
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "JNPR")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "JNPR")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 40.10435
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_JNPR <- Resultado$RendsCyV R_CyV_JNPR
## [1] 40.10435
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "JNPR")
TSLA
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("TSLA", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 261.07, 254.11, 259.32, 253.86, 251.45, 249.70, 242.19, 245.34… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 249.945, 247.134, 244.283,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "TSLA", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "TSLA", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 261.07, 254.11, 259.32, 253.86, 251.45, 249.70, 242.19, 245.34… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 249.945, 247.134, 244.283,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] -26.61356
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] -26.61356
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 261.07, 254.11, 259.32, 253.86, 251.45, 249.70, 242.19, 245.34… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 249.945, 247.134, 244.283,… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, -0.0266595416, 0.0205029582, -0.0210550924, -0.0094934360,…
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "TSLA")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "TSLA")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 -1.858286
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_TSLA <- Resultado$RendsTotalC R_L_TSLA
## [1] -1.858286
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "TSLA")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "TSLA")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 17.945
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_TSLA <- Resultado$RendsCyV R_CyV_TSLA
## [1] 17.945
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "TSLA")
FTNT
# Definir el periodo y la acción Fecini <- as.Date("2023-08-01") Fecfin <- as.Date("2024-01-31") Accion <- getSymbols("FTNT", from = Fecini, to = Fecfin, auto.assign = F, warnings = F) Precios <- Ad(Accion) Fechas <- index(Precios) NA_total <- sum(is.na(Precios)) NA_total
## [1] 0
NA_total <- sum(is.na(Fechas)) NA_total
## [1] 0
# Definir momento rápido y momento lento MMrapida <- SMA(Precios, n = 10) MMlenta <- SMA(Precios, n = 40) # Crear un data frame Datos <- data.frame(Fechas = Fechas, Precios = as.numeric(Precios), MM1 = as.numeric(MMrapida), MM2 = as.numeric(MMlenta)) Datos %>% glimpse()
## Rows: 126 ## Columns: 4 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 78.03, 74.32, 75.76, 56.77, 57.70, 58.57, 59.78, 60.56, 59.68,… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 64.083, 62.197, 60.583, 58… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
#Gráfica 1 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Fechas vs Precios", subtitle = "FTNT", x = "Fecha")
#Gráfica 2 Datos %>% ggplot(aes(x = Fechas, y = Precios)) + geom_line(col = p_blue, linewidth = 1) + geom_line(aes(y = MM1), col = p_red) + geom_line(aes(y = MM2), col = p_green) + labs(title = "Precios vs Medias Móviles", subtitle = "FTNT", x = "Fecha")
## Warning: Removed 9 rows containing missing values (`geom_line()`).
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Consideraciones: # Si la media móvil rápida es mayor que la media móvil lenta --> Tendencia Bullish (Toro) # Si la media móvil lenta es mayor que la media móvil rápida --> Tendencia Bearish (Oso) Datos <- Datos %>% mutate(Tend = case_when(MM1 >= MM2 ~ "Bull", MM2 >= MM1 ~ "Bear")) Datos %>% glimpse()
## Rows: 126 ## Columns: 5 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 78.03, 74.32, 75.76, 56.77, 57.70, 58.57, 59.78, 60.56, 59.68,… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 64.083, 62.197, 60.583, 58… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
# Rendimiento diario Rend <- Return.calculate(Precios) # Rendimiento punta a punta R_simple <- as.numeric(last(Precios))/as.numeric(first(Precios)) -1 R_simple * 100
## [1] -14.50724
# Rendimiento agregado G_rend <- 1 + Rend (prod(G_rend, na.rm = T) - 1) * 100
## [1] -14.50724
# Añadir columna de rendimiento Datos <- Datos %>% mutate(Rends = as.numeric(Rend)) Datos%>% glimpse()
## Rows: 126 ## Columns: 6 ## $ Fechas <date> 2023-08-01, 2023-08-02, 2023-08-03, 2023-08-04, 2023-08-07, 2… ## $ Precios <dbl> 78.03, 74.32, 75.76, 56.77, 57.70, 58.57, 59.78, 60.56, 59.68,… ## $ MM1 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 64.083, 62.197, 60.583, 58… ## $ MM2 <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Tend <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ Rends <dbl> NA, -0.0475458047, 0.0193757057, -0.2506599940, 0.0163818971, …
# PORTAFOLIOS CON POSICIÓN LARGA # Señal de compra Datos <- Datos %>% mutate(Compra = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ 0)) Datos%>% tail(200) %>% ggplot(aes(x = Fechas, y = Compra)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señales de compra en tendencia Bullish", subtitle = "FTNT")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra Datos <- Datos %>% mutate(Ganancia = Compra * Rends) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = Ganancia)) + geom_line(col = p_purple, linewidth = 1) + geom_line(aes(y = Rends), col = p_yellow, linewidth = 1) + labs(title = "Ganancia por señales de compra en tendencia Bullish", subtitle = "FTNT")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
## Warning: Removed 1 row containing missing values (`geom_line()`).
Datos <- Datos %>% mutate(GanNeta = 1 + Ganancia) Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100
## RendsTotalC ## 1 16.93252
Resultado <- Datos %>% summarize(RendsTotalC = prod(GanNeta, na.rm = T) - 1) * 100 R_L_FTNT <- Resultado$RendsTotalC R_L_FTNT
## [1] 16.93252
#Generar una gráfica con el rendimiento acumulado Datos <- Datos %>% mutate(G_Rend_L = replace_na(GanNeta, 1), RendAcum = cumprod(G_Rend_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcum)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de ganancias netas en portafolio largo (Bullish)", subtitle = "FTNT")
# PORTAFOLIOS CON VENTA EN CORTO # Señal de compra y venta Datos <- Datos %>% mutate(CyV = case_when(Tend == "Bull" ~ 1, Tend == "Bear" ~ -1)) Datos %>% tail(200) %>% ggplot(aes(x = Fechas, y = CyV)) + geom_line(col = p_orange, linewidth = 1) + labs(title = "Señal de compra y venta (venta en corto)", subtitle = "FTNT")
## Warning: Removed 39 rows containing missing values (`geom_line()`).
# Calcular y graficar los rendimientos con la señal de compra y venta Datos <- Datos %>% mutate(GanCyV = CyV * Rends, GanCyVNeta = GanCyV + 1) Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100
## RendsCyV ## 1 15.8759
Resultado <- Datos %>% summarize(RendsCyV = prod(GanCyVNeta, na.rm = T) - 1) * 100 R_CyV_FTNT <- Resultado$RendsCyV R_CyV_FTNT
## [1] 15.8759
Datos <- Datos %>% mutate(G_RendCyV_L = replace_na(GanCyVNeta, 1), RendAcumCyV = cumprod(G_RendCyV_L) - 1) Datos %>% ggplot(aes(x = Fechas, y = RendAcumCyV)) + geom_line(col = p_blue, linewidth = 1) + labs(title = "Rendimiento acumulado de compra y venta (venta en corto)", subtitle = "FTNT")
# Calcular el rendimiento del portafolio largo Rendimiento_Largo <- (0.08614403) * R_L_GCARSOA1.MX + (0.08957364) * R_L_FEMSAUBD.MX + (0.05058752) * R_L_META + (0.06904116) * R_L_COST + (0.1228616) * R_L_MAR + (0.1732409) * R_L_IBM + (0.3045014) * R_L_WMT + (0.0801241) * R_L_JNPR + (-0.006024369) * R_L_TSLA + (0.02995006) * R_L_FTNT # Calcular el rendimiento del portafolio corto Rendimiento_Corto <- (0.08614403) * R_CyV_GCARSOA1.MX + (0.08957364) * R_CyV_FEMSAUBD.MX + (0.05058752) * R_CyV_META + (0.06904116) * R_CyV_COST + (0.1228616) * R_CyV_MAR + (0.1732409) * R_CyV_IBM + (0.3045014) * R_CyV_WMT + (0.0801241) * R_CyV_JNPR + (-0.006024369) * R_CyV_TSLA + (0.02995006) * R_CyV_FTNT # Calcular el rendimiento total del portafolio combinado Rendimiento_2 <- Rendimiento_Largo + Rendimiento_Corto Rendimiento_2
## [1] 23.48404
# Comparar el rendimiento de los dos ejercicios Rendimiento_1
## [1] 20.17387
Rendimiento_2
## [1] 23.48404
Diferencia <- Rendimiento_2 - Rendimiento_1 Diferencia
## [1] 3.31017
Regresion lineal del tipo de cambio en dólares y pesos para tomar la decision de que moneda utilizar para solicitar el préstamo
# Establecer el símbolo, fechas de inicio y fin symbol <- "USDMXN=X" start_date <- as.Date("2020-01-01") end_date <- as.Date("2024-01-31") # Obtener los datos del tipo de cambio getSymbols(symbol, src = "yahoo", from = start_date, to = end_date)
## [1] "USDMXN=X"
# Preparar los datos datos <- fortify.zoo(Cl(get(symbol))) datos$fechas <- index(Cl(get(symbol))) datos$precios <- coredata(Cl(get(symbol))) datos$fechas_num <- as.numeric(as.Date(datos$fechas)) - as.numeric(as.Date(min(datos$fechas))) # Realizar regresión lineal usando fechas_num como variable independiente modelo_regresion <- lm(precios ~ fechas_num, data = datos) # Obtener coeficientes del modelo coeficientes <- coef(modelo_regresion) # Graficar la regresión lineal y los puntos de datos ggplot(datos, aes(x = fechas, y = precios)) + geom_point() + # Puntos de datos geom_line(aes(x = fechas, y = predict(modelo_regresion, newdata = datos)), color = "blue") + # Línea de regresión labs(title = "Regresión Lineal del Tipo de Cambio USD/MXN", x = "Fecha", y = "Tipo de Cambio") + annotate("text", x = max(datos$fechas), y = max(datos$precios), label = paste("Tendencia:", ifelse(coeficientes["fechas_num"] > 0, "Subirá", "Bajará")), hjust = 1, vjust = 1, color = "red") + theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Mejora la legibilidad de las fechas