Limpieza de datos

knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE, rows.print = 7)
require(knitr) 
require(tinytex)
require(data.table)

require(tsbox)
require(tidyverse)
require(gridExtra)

require(quantmod)
require(zoo)
require(xts)
require(PerformanceAnalytics)
require(PortfolioAnalytics)
require(TSstudio)
require(forecast)


# Métodos de optimización
require("ROI")
require("ROI.plugin.quadprog")
require("ROI.plugin.glpk")

require("DEoptim")

Todos los precios están en dólares. Todos los rendimientos son continuos. Son 8 criptomonedas.

# Ethereal
ETHPrecios = getSymbols(Symbols = "ETH-USD", from = as.Date("2015-01-01"), auto.assign = FALSE) %>% 
  Cl() %>%
  na.omit()


ETH = xts(diff(log(ETHPrecios[,1])), order.by = index(ETHPrecios)) %>%
  na.omit()
ETH = ETH[-1, ]
colnames(ETH) = "ETH-Rendimientos"

# XRP
XRPPrecios = getSymbols(Symbols = "XRP-USD", from = as.Date("2015-01-01"), auto.assign = FALSE) %>% 
  Cl() %>%
  na.omit()

XRP = xts(diff(log(XRPPrecios[,1])), order.by = index(XRPPrecios)) %>%
  na.omit()
XRP = XRP[-1, ]
colnames(XRP) = "XRP-Rendimientos"
  
# Lifecoin
LTCPrecios = getSymbols(Symbols = "LTC-USD", from = as.Date("2015-01-01"), auto.assign = FALSE) %>% 
  Cl() %>%
  na.omit()

LTC = xts(diff(log(LTCPrecios[,1])), order.by = index(LTCPrecios)) %>%
  na.omit()
LTC = LTC[-1, ]
colnames(LTC) = "LTC-Rendimientos"

# BCH
BCHPrecios = getSymbols(Symbols = "BCH-USD", from = as.Date("2015-01-01"), auto.assign = FALSE) %>% 
  Cl() %>%
  na.omit()

BCH = xts(diff(log(BCHPrecios[,1])), order.by = index(BCHPrecios)) %>%
  na.omit()
BCH = BCH[-1, ]
colnames(BCH) = "BCH-Rendimientos"

# TrueUSD desde CSV
TUSDPrecios_csv = read_csv("TUSD.csv") %>% 
  select(Fecha, Cierre) %>%
  mutate(Fecha = as.Date(Fecha, format = "%d.%m.%Y")) 

TUSDPrecios = xts(TUSDPrecios_csv$Cierre, order.by = TUSDPrecios_csv$Fecha) %>%
  na.omit()

TUSDPrecios_ts <- ts(TUSDPrecios_csv[,2],
                    start = c(2018, as.numeric(TUSDPrecios_csv[nrow(TUSDPrecios_csv), 1]) - as.numeric(as.Date("2018-01-01")) ), 
                    end = c(2021, as.numeric(TUSDPrecios_csv[1, 1]) - as.numeric(as.Date("2021-01-01"))), 
                    frequency = 365)



TUSD = xts(diff(log(TUSDPrecios[,1])), order.by = index(TUSDPrecios)) %>%
  na.omit()
TUSD = TUSD[-1, ]
colnames(TUSD) = "TUSD-Rendimientos"

# MANA desde CSV
MANAPrecios = read_csv("MANA.csv") %>% 
  select(Fecha, Cierre) %>%
  mutate(Fecha = as.Date(Fecha, format = "%d.%m.%Y")) 

MANAPrecios = xts(MANAPrecios$Cierre, order.by = MANAPrecios$Fecha) %>%
  na.omit()

MANA = xts(diff(log(MANAPrecios[,1])), order.by = index(MANAPrecios)) %>%
  na.omit()
MANA = MANA[-1, ]
colnames(MANA) = "MANA-Rendimientos"

# BAT
BATPrecios = getSymbols(Symbols = "BAT-USD", from = as.Date("2015-01-01"), auto.assign = FALSE) %>% 
  Cl() %>%
  na.omit()
BAT = xts(diff(log(BATPrecios[,1])), order.by = index(BATPrecios)) %>%
  na.omit()
BAT = BAT[-1, ]
colnames(BAT) = "BAT-Rendimientos"


# DAI desde CSV
DAIPrecios = read_csv("DAI.csv") %>% 
  select(Fecha, Cierre) %>%
  mutate(Fecha = as.Date(Fecha, format = "%d.%m.%Y")) 

DAIPrecios = xts(DAIPrecios$Cierre, order.by = DAIPrecios$Fecha) %>%
  na.omit()

DAI = xts(diff(log(DAIPrecios[,1])), order.by = index(DAIPrecios)) %>%
  na.omit()
DAI = DAI[-1, ]
colnames(DAI) = "DAI-Rendimientos"

Para cada moneda se tiene datos a partir de una fecha distinta:

## [1] "2015-08-09"
## [1] "2015-01-03"
## [1] "2015-01-03"
## [1] "2017-07-25"
## [1] "2018-08-29"
## [1] "2018-03-03"
## [1] "2017-06-03"
## [1] "2018-02-04"

ETH

ETH_ts <- ts(coredata(ETH), frequency = 365)

# Análisis
atípico <- which.max(ETH_ts)
frequency(ETH_ts)
## [1] 365
autoplot(ETH_ts) + theme_dark()

ggseasonplot(ETH_ts) + 
  theme_dark() +
  theme(#plot.background = element_rect(fill = "#7c7c7c"),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        panel.grid = element_blank()) + 
  labs(title = "ETH frecuencia diaria") 

ggseasonplot(ETH_ts, polar = TRUE) + 
  theme_dark() + 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        panel.grid = element_blank()) +
  labs(title = "ETH frecuencia diaria")

gglagplot(ETH_ts, set.lags = 1:3) + theme_dark() 

# Detección de ruido blanco

ggAcf(ETH_ts, lag.max = 300, plot = TRUE) + 
  theme_dark() + 
  theme(panel.grid = element_blank())

Box.test(ETH_ts, lag = 700, type = "Ljung")
## 
##  Box-Ljung test
## 
## data:  ETH_ts
## X-squared = 713.98, df = 700, p-value = 0.3487
# Se trata de ruido blanco

Pronóstico

fc = naive(ETH_ts, h = 30)
autoplot(fc) 

# autolayout(residuals(fc))
# checkresiduals(fc)
# Se se tiene WN, por tanto es buen pronóstico
monedas = c("ETH", "XRP", "LTC", "BCH", "TUSD", "MANA", "BAT", "DAI")

Rendimientos = merge.xts(ETH, XRP, LTC, BCH, TUSD, MANA, BAT, DAI)

tabla = data.frame(FechaETH = index(ETH)[1:10], 
                   RendimientoETH = coredata(ETH)[1:10,],
                   FechaXRP = index(XRP)[1:10], 
                   RendimientoXRP = coredata(XRP)[1:10,],
                   FechaLTC = index(LTC)[1:10], 
                   RendimientoLTC = coredata(LTC)[1:10,],
                   FechaBCH = index(BCH)[1:10], 
                   RendimientoBCH = coredata(BCH)[1:10,],
                   FechaTUSD = index(TUSD)[1:10], 
                   RendimientoTUSD = coredata(TUSD)[1:10,],
                   FechaMANA = index(MANA)[1:10], 
                   RendimientoMANA = coredata(MANA)[1:10,],
                   FechaBAT = index(BAT)[1:10], 
                   RendimientoBAT = coredata(BAT)[1:10,],
                   FechaDAI = index(DAI)[1:10], 
                   RendimientoDAI = coredata(ETH)[1:10,])

# Rendimientos = merge.xts(coredata(ETH), coredata(XRP), coredata(LTC), coredata(BCH), coredata(TUSD), coredata(MANA), coredata(BAT), coredata(DAI))
kable( tabla,
       col.names = c("FechaETH", "RendimientoETH", 
                     "FechaXRP", "RendimientoETH",
                     "FechaLTC", "RendimientoETH",
                     "FechaBCH", "RendimientoETH",
                     "FechaTUSD", "RendimientoETH",
                     "FechaMANA", "RendimientoETH",
                     "FechaBAT", "RendimientoETH",
                     "FechaDAI", "RendimientoETH"
                     ),
       align = "cccccccccccccccc",
       caption = "Rendimientos"
       )

Gráficas de precios

plot.xts(ETHPrecios, lwd = .5, main = "Precios de ETH", col = "#2980B9")

plot.xts(XRPPrecios, lwd = .5, main = "Precios de XRP", col = "#2980B9")

plot.xts(LTCPrecios, lwd = .5, main = "Precios de LTC", col = "#2980B9")

plot.xts(BCHPrecios, lwd = .5, main = "Precios de BCH", col = "#2980B9")

plot.xts(TUSDPrecios, lwd = .5, main = "Precios de TUSD", col = "#2980B9")

plot.xts(MANAPrecios, lwd = .5, main = "Precios de MANA", col = "#2980B9")

plot.xts(BATPrecios, lwd = .5, main = "Precios de BAT", col = "#2980B9")

plot.xts(DAIPrecios, lwd = .5, main = "Precios de DAI", col = "#2980B9")

Gráficas de rendimientos continuos

plot.xts(ETH, lwd = .5, main = "Rendimientos continuos de ETH", col = "#7D3C98")

plot.xts(XRP, lwd = .5, main = "Rendimientos continuos de XRP", col = "#7D3C98")

plot.xts(LTC, lwd = .5, main = "Rendimientos continuos de LTC", col = "#7D3C98")

plot.xts(BCH, lwd = .5, main = "Rendimientos continuos de BCH", col = "#7D3C98")

plot.xts(TUSD, lwd = .5, main = "Rendimientos continuos de TUSD", col = "#7D3C98")

plot.xts(MANA, lwd = .5, main = "Rendimientos continuos de MANA", col = "#7D3C98")

plot.xts(BAT, lwd = .5, main = "Rendimientos continuos de BAT", col = "#7D3C98")

plot.xts(DAI, lwd = .5, main = "Rendimientos continuos de DAI", col = "#7D3C98")

Análisis financiero: cartera óptima

Todas las divisas

rendimientos <- merge(ETH, XRP, LTC, BCH, TUSD, MANA, BAT, DAI) %>% na.omit() 
colnames(rendimientos) = monedas

cartera_especificacion <- portfolio.spec(colnames(rendimientos))

cartera_especificacion <- add.constraint(portfolio = cartera_especificacion, type = "full_investment")
cartera_especificacion <- add.constraint(portfolio = cartera_especificacion, type = "long_only")

cartera_especificacion <- add.objective(portfolio = cartera_especificacion, type ="risk", name = "StdDev")
cartera_especificacion <- add.objective(portfolio = cartera_especificacion, type ="return", name = "mean")

print(cartera_especificacion)
## **************************************************
## PortfolioAnalytics Portfolio Specification 
## **************************************************
## 
## Call:
## portfolio.spec(assets = colnames(rendimientos))
## 
## Number of assets: 8 
## Asset Names
## [1] "ETH"  "XRP"  "LTC"  "BCH"  "TUSD" "MANA" "BAT"  "DAI" 
## 
## Constraints
## Enabled constraint types
##      - full_investment 
##      - long_only 
## 
## Objectives:
## Enabled objective names
##      - StdDev 
##      - mean
cartera_optimizada <- optimize.portfolio(rendimientos, portfolio = cartera_especificacion, optimize_method = "ROI", trace = TRUE)

print(cartera_optimizada)
## ***********************************
## PortfolioAnalytics Optimization
## ***********************************
## 
## Call:
## optimize.portfolio(R = rendimientos, portfolio = cartera_especificacion, 
##     optimize_method = "ROI", trace = TRUE)
## 
## Optimal Weights:
##    ETH    XRP    LTC    BCH   TUSD   MANA    BAT    DAI 
## 0.2445 0.0000 0.0000 0.0000 0.4165 0.1746 0.0091 0.1553 
## 
## Objective Measure:
##      mean 
## 0.0009434 
## 
## 
##  StdDev 
## 0.02152
print(summary(cartera_optimizada))
## **************************************************
## PortfolioAnalytics Optimization Summary 
## **************************************************
## 
## Call:
## optimize.portfolio(R = rendimientos, portfolio = cartera_especificacion, 
##     optimize_method = "ROI", trace = TRUE)
## 
## Optimal Weights:
##    ETH    XRP    LTC    BCH   TUSD   MANA    BAT    DAI 
## 0.2445 0.0000 0.0000 0.0000 0.4165 0.1746 0.0091 0.1553 
## 
## Objective Measures:
##      mean 
## 0.0009434 
## 
## 
##  StdDev 
## 0.02152 
## 
## 
## Portfolio Assets and Initial Weights:
##   ETH   XRP   LTC   BCH  TUSD  MANA   BAT   DAI 
## 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 
## 
## **************************************************
## PortfolioAnalytics Portfolio Specification 
## **************************************************
## 
## Call:
## portfolio.spec(assets = colnames(rendimientos))
## 
## Number of assets: 8 
## Asset Names
## [1] "ETH"  "XRP"  "LTC"  "BCH"  "TUSD" "MANA" "BAT"  "DAI" 
## 
## Constraints
## Enabled constraint types
##      - full_investment 
##      - long_only 
## 
## Objectives:
## Enabled objective names
##      - StdDev 
##      - mean 
## 
## ****************************************
## Constraints
## ****************************************
## Leverage Constraint:
## min_sum = 1
## max_sum = 1
## actual_leverage = 1
## 
## Box Constraints:
## min:
##  ETH  XRP  LTC  BCH TUSD MANA  BAT  DAI 
##    0    0    0    0    0    0    0    0 
## max:
##  ETH  XRP  LTC  BCH TUSD MANA  BAT  DAI 
##    1    1    1    1    1    1    1    1 
## 
## Position Limit Constraints:
## Maximum number of non-zero weights, max_pos:
## [1] "Unconstrained"
## Realized number of non-zero weights (i.e. positions):
## [1] 5
## 
## Maximum number of long positions, max_pos_long:
## [1] "Unconstrained"
## Realized number of long positions:
## [1] 5
## 
## Maximum number of short positions, max_pos_short:
## [1] "Unconstrained"
## Realized number of short positions:
## [1] 0
## 
## 
## Diversification Target Constraint:
## [1] "Unconstrained"
## 
## Realized diversification:
## [1] 0.7120643
## 
## Turnover Target Constraint:
## [1] "Unconstrained"
## 
## Realized turnover from initial weights:
## [1] 0.122717
## 
## ****************************************
## Objectives
## ****************************************
## 
## Objective: portfolio_risk_objective 
## $name
## [1] "StdDev"
## 
## $target
## NULL
## 
## $arguments
## $arguments$portfolio_method
## [1] "single"
## 
## 
## $enabled
## [1] TRUE
## 
## $multiplier
## [1] 1
## 
## $call
## add.objective(portfolio = cartera_especificacion, type = "risk", 
##     name = "StdDev")
## 
## attr(,"class")
## [1] "portfolio_risk_objective" "objective"               
## 
## ****************************************
## Objective: return_objective 
## $name
## [1] "mean"
## 
## $target
## NULL
## 
## $arguments
## list()
## 
## $enabled
## [1] TRUE
## 
## $multiplier
## [1] -1
## 
## $call
## add.objective(portfolio = cartera_especificacion, type = "return", 
##     name = "mean")
## 
## attr(,"class")
## [1] "return_objective" "objective"       
## 
## ****************************************
## 
## Elapsed Time:
## Time difference of 0.08812809 secs
chart.Weights(cartera_optimizada)

chart.RiskReward(cartera_optimizada, risk.col = "StdDev", return.col = "mean", chart.assets = TRUE, rp = TRUE, xlim = c(0, 0.09))

create.EfficientFrontier(rendimientos, 
                         portfolio = cartera_especificacion,
                         type = "mean-var",
                         n.portfolios = 100) %>%
  chart.EfficientFrontier(type = "l",
                        match.col = "StdDev",
                        n.portfolios = 100, 
                        rf = NULL,
                        tangent.line = TRUE,
                        chart.assets = TRUE,
                        labels.assets = TRUE,
                        main = "Frontera eficiente",
                        element.color = "red",
                        RAR.text = "SR")

Primeras 3 divisas

rendimientos_caso1 <- merge(ETH, XRP, LTC) %>% na.omit() 
colnames(rendimientos_caso1) = monedas[1:3]

cartera_especificacion_caso1 <- portfolio.spec(colnames(rendimientos_caso1))

cartera_especificacion_caso1 <- add.constraint(portfolio = cartera_especificacion_caso1, type = "full_investment")
cartera_especificacion_caso1 <- add.constraint(portfolio = cartera_especificacion_caso1, type = "long_only")

cartera_especificacion_caso1 <- add.objective(portfolio = cartera_especificacion_caso1, type ="risk", name = "StdDev")
cartera_especificacion_caso1 <- add.objective(portfolio = cartera_especificacion_caso1, type ="return", name = "mean")

print(cartera_especificacion_caso1)
## **************************************************
## PortfolioAnalytics Portfolio Specification 
## **************************************************
## 
## Call:
## portfolio.spec(assets = colnames(rendimientos_caso1))
## 
## Number of assets: 3 
## Asset Names
## [1] "ETH" "XRP" "LTC"
## 
## Constraints
## Enabled constraint types
##      - full_investment 
##      - long_only 
## 
## Objectives:
## Enabled objective names
##      - StdDev 
##      - mean
cartera_optimizada_caso1 <- optimize.portfolio(rendimientos_caso1, portfolio = cartera_especificacion_caso1, optimize_method = "ROI", trace = TRUE)

print(cartera_optimizada_caso1)
## ***********************************
## PortfolioAnalytics Optimization
## ***********************************
## 
## Call:
## optimize.portfolio(R = rendimientos_caso1, portfolio = cartera_especificacion_caso1, 
##     optimize_method = "ROI", trace = TRUE)
## 
## Optimal Weights:
##    ETH    XRP    LTC 
## 0.6134 0.1752 0.2113 
## 
## Objective Measure:
##     mean 
## 0.003115 
## 
## 
##  StdDev 
## 0.05103
print(summary(cartera_optimizada_caso1))
## **************************************************
## PortfolioAnalytics Optimization Summary 
## **************************************************
## 
## Call:
## optimize.portfolio(R = rendimientos_caso1, portfolio = cartera_especificacion_caso1, 
##     optimize_method = "ROI", trace = TRUE)
## 
## Optimal Weights:
##    ETH    XRP    LTC 
## 0.6134 0.1752 0.2113 
## 
## Objective Measures:
##     mean 
## 0.003115 
## 
## 
##  StdDev 
## 0.05103 
## 
## 
## Portfolio Assets and Initial Weights:
##       ETH       XRP       LTC 
## 0.3333333 0.3333333 0.3333333 
## 
## **************************************************
## PortfolioAnalytics Portfolio Specification 
## **************************************************
## 
## Call:
## portfolio.spec(assets = colnames(rendimientos_caso1))
## 
## Number of assets: 3 
## Asset Names
## [1] "ETH" "XRP" "LTC"
## 
## Constraints
## Enabled constraint types
##      - full_investment 
##      - long_only 
## 
## Objectives:
## Enabled objective names
##      - StdDev 
##      - mean 
## 
## ****************************************
## Constraints
## ****************************************
## Leverage Constraint:
## min_sum = 1
## max_sum = 1
## actual_leverage = 1
## 
## Box Constraints:
## min:
## ETH XRP LTC 
##   0   0   0 
## max:
## ETH XRP LTC 
##   1   1   1 
## 
## Position Limit Constraints:
## Maximum number of non-zero weights, max_pos:
## [1] "Unconstrained"
## Realized number of non-zero weights (i.e. positions):
## [1] 3
## 
## Maximum number of long positions, max_pos_long:
## [1] "Unconstrained"
## Realized number of long positions:
## [1] 3
## 
## Maximum number of short positions, max_pos_short:
## [1] "Unconstrained"
## Realized number of short positions:
## [1] 0
## 
## 
## Diversification Target Constraint:
## [1] "Unconstrained"
## 
## Realized diversification:
## [1] 0.5483192
## 
## Turnover Target Constraint:
## [1] "Unconstrained"
## 
## Realized turnover from initial weights:
## [1] 0.1867423
## 
## ****************************************
## Objectives
## ****************************************
## 
## Objective: portfolio_risk_objective 
## $name
## [1] "StdDev"
## 
## $target
## NULL
## 
## $arguments
## $arguments$portfolio_method
## [1] "single"
## 
## 
## $enabled
## [1] TRUE
## 
## $multiplier
## [1] 1
## 
## $call
## add.objective(portfolio = cartera_especificacion_caso1, type = "risk", 
##     name = "StdDev")
## 
## attr(,"class")
## [1] "portfolio_risk_objective" "objective"               
## 
## ****************************************
## Objective: return_objective 
## $name
## [1] "mean"
## 
## $target
## NULL
## 
## $arguments
## list()
## 
## $enabled
## [1] TRUE
## 
## $multiplier
## [1] -1
## 
## $call
## add.objective(portfolio = cartera_especificacion_caso1, type = "return", 
##     name = "mean")
## 
## attr(,"class")
## [1] "return_objective" "objective"       
## 
## ****************************************
## 
## Elapsed Time:
## Time difference of 0.02626204 secs
chart.Weights(cartera_optimizada_caso1)

chart.RiskReward(cartera_optimizada_caso1, risk.col = "StdDev", return.col = "mean", chart.assets = TRUE, rp = TRUE, xlim = c(0, 0.09))

create.EfficientFrontier(rendimientos_caso1, 
                         portfolio = cartera_especificacion_caso1,
                         type = "mean-var",
                         n.portfolios = 100) %>%
  chart.EfficientFrontier(type = "l",
                        match.col = "StdDev",
                        n.portfolios = 100, 
                        rf = NULL,
                        tangent.line = TRUE,
                        chart.assets = TRUE,
                        labels.assets = TRUE,
                        main = "Frontera eficiente",
                        element.color = "red",
                        RAR.text = "SR")

Últimas divisas

rendimientos_caso2 <- merge(BCH, TUSD, MANA, BAT, DAI) %>% na.omit() 
colnames(rendimientos_caso2) = monedas[4:8]
str(rendimientos_caso2)
## An 'xts' object on 2018-08-29/2021-03-31 containing:
##   Data: num [1:939, 1:5] -0.01954 -0.02621 0.00564 0.12488 0.04848 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:5] "BCH" "TUSD" "MANA" "BAT" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 1
##  $ na.action: 'omit' int [1:462] 1 2 3 4 5 6 7 8 9 10 ...
##   ..- attr(*, "index")= num [1:462] 1.5e+09 1.5e+09 1.5e+09 1.5e+09 1.5e+09 ...
cartera_especificacion_caso2 <- portfolio.spec(colnames(rendimientos_caso2))

cartera_especificacion_caso2 <- add.constraint(portfolio = cartera_especificacion_caso2, type = "full_investment")
cartera_especificacion_caso2 <- add.constraint(portfolio = cartera_especificacion_caso2, type = "long_only")

cartera_especificacion_caso2 <- add.objective(portfolio = cartera_especificacion_caso2, type ="risk", name = "StdDev")
cartera_especificacion_caso2 <- add.objective(portfolio = cartera_especificacion_caso2, type ="return", name = "mean")

print(cartera_especificacion_caso2)
## **************************************************
## PortfolioAnalytics Portfolio Specification 
## **************************************************
## 
## Call:
## portfolio.spec(assets = colnames(rendimientos_caso2))
## 
## Number of assets: 5 
## Asset Names
## [1] "BCH"  "TUSD" "MANA" "BAT"  "DAI" 
## 
## Constraints
## Enabled constraint types
##      - full_investment 
##      - long_only 
## 
## Objectives:
## Enabled objective names
##      - StdDev 
##      - mean
cartera_optimizada_caso2 <- optimize.portfolio(rendimientos_caso2, portfolio = cartera_especificacion_caso2, optimize_method = "ROI", trace = TRUE)

print(cartera_optimizada_caso2)
## ***********************************
## PortfolioAnalytics Optimization
## ***********************************
## 
## Call:
## optimize.portfolio(R = rendimientos_caso2, portfolio = cartera_especificacion_caso2, 
##     optimize_method = "ROI", trace = TRUE)
## 
## Optimal Weights:
##    BCH   TUSD   MANA    BAT    DAI 
## 0.0000 0.4971 0.2323 0.1117 0.1589 
## 
## Objective Measure:
##     mean 
## 0.000797 
## 
## 
##  StdDev 
## 0.01986
print(summary(cartera_optimizada_caso2))
## **************************************************
## PortfolioAnalytics Optimization Summary 
## **************************************************
## 
## Call:
## optimize.portfolio(R = rendimientos_caso2, portfolio = cartera_especificacion_caso2, 
##     optimize_method = "ROI", trace = TRUE)
## 
## Optimal Weights:
##    BCH   TUSD   MANA    BAT    DAI 
## 0.0000 0.4971 0.2323 0.1117 0.1589 
## 
## Objective Measures:
##     mean 
## 0.000797 
## 
## 
##  StdDev 
## 0.01986 
## 
## 
## Portfolio Assets and Initial Weights:
##  BCH TUSD MANA  BAT  DAI 
##  0.2  0.2  0.2  0.2  0.2 
## 
## **************************************************
## PortfolioAnalytics Portfolio Specification 
## **************************************************
## 
## Call:
## portfolio.spec(assets = colnames(rendimientos_caso2))
## 
## Number of assets: 5 
## Asset Names
## [1] "BCH"  "TUSD" "MANA" "BAT"  "DAI" 
## 
## Constraints
## Enabled constraint types
##      - full_investment 
##      - long_only 
## 
## Objectives:
## Enabled objective names
##      - StdDev 
##      - mean 
## 
## ****************************************
## Constraints
## ****************************************
## Leverage Constraint:
## min_sum = 1
## max_sum = 1
## actual_leverage = 1
## 
## Box Constraints:
## min:
##  BCH TUSD MANA  BAT  DAI 
##    0    0    0    0    0 
## max:
##  BCH TUSD MANA  BAT  DAI 
##    1    1    1    1    1 
## 
## Position Limit Constraints:
## Maximum number of non-zero weights, max_pos:
## [1] "Unconstrained"
## Realized number of non-zero weights (i.e. positions):
## [1] 4
## 
## Maximum number of long positions, max_pos_long:
## [1] "Unconstrained"
## Realized number of long positions:
## [1] 4
## 
## Maximum number of short positions, max_pos_short:
## [1] "Unconstrained"
## Realized number of short positions:
## [1] 0
## 
## 
## Diversification Target Constraint:
## [1] "Unconstrained"
## 
## Realized diversification:
## [1] 0.6611923
## 
## Turnover Target Constraint:
## [1] "Unconstrained"
## 
## Realized turnover from initial weights:
## [1] 0.1317747
## 
## ****************************************
## Objectives
## ****************************************
## 
## Objective: portfolio_risk_objective 
## $name
## [1] "StdDev"
## 
## $target
## NULL
## 
## $arguments
## $arguments$portfolio_method
## [1] "single"
## 
## 
## $enabled
## [1] TRUE
## 
## $multiplier
## [1] 1
## 
## $call
## add.objective(portfolio = cartera_especificacion_caso2, type = "risk", 
##     name = "StdDev")
## 
## attr(,"class")
## [1] "portfolio_risk_objective" "objective"               
## 
## ****************************************
## Objective: return_objective 
## $name
## [1] "mean"
## 
## $target
## NULL
## 
## $arguments
## list()
## 
## $enabled
## [1] TRUE
## 
## $multiplier
## [1] -1
## 
## $call
## add.objective(portfolio = cartera_especificacion_caso2, type = "return", 
##     name = "mean")
## 
## attr(,"class")
## [1] "return_objective" "objective"       
## 
## ****************************************
## 
## Elapsed Time:
## Time difference of 0.0205512 secs
chart.Weights(cartera_optimizada_caso2)

chart.RiskReward(cartera_optimizada_caso2, risk.col = "StdDev", return.col = "mean", chart.assets = TRUE, rp = TRUE, xlim = c(0, 0.09))

create.EfficientFrontier(rendimientos_caso2, 
                         portfolio = cartera_especificacion_caso2,
                         type = "mean-var",
                         n.portfolios = 100) %>%
  chart.EfficientFrontier(type = "l",
                        match.col = "StdDev",
                        n.portfolios = 100, 
                        rf = NULL,
                        tangent.line = TRUE,
                        chart.assets = TRUE,
                        labels.assets = TRUE,
                        main = "Frontera eficiente",
                        element.color = "red",
                        RAR.text = "SR")