14/3/2019

Qué es el backtesting

El Backtesting evalúa la viabilidad de una estrategia de trading analizando cómo se comportaría con datos históricos.

www.investopedia.com

Backtesting en R 1/2

Los paquetes que vamos a utilizar en esta presentación son:

  • quantmod: A rapid prototyping environment, where quant traders can quickly and cleanly explore and build trading models.
  • TTR: Functions and data to construct technical trading rules with R.
  • quantStrat: Transaction-oriented infrastructure for constructing trading systems and simulation. Provides support for multi-asset class and multi-currency portfolios for backtesting and other financial research.
  • PerformanceAnalytics: Collection of econometric functions for performance and risk analysis.

Backtesting en R 2/2

Otro paquete interesante para hacer backtesting y análisis:

  • tidyquant: Provides a convenient wrapper to various ‘xts’, ‘zoo’, ‘quantmod’, ‘TTR’ and ‘PerformanceAnalytics’ package functions and returns the objects in the tidy ‘tibble’ format. The main advantage is being able to use quantitative functions with the ‘tidyverse’ functions including ‘purrr’, ‘dplyr’, ‘tidyr’, ‘ggplot2’, ‘lubridate’, etc.

Hay centenares de paquetes para trading y finanzas en CRAN que ha recopilado Dirk Eddelbuettel en esta página:

CRAN Task View: Empirical Finance

Importando datos 1/2

https://finance.yahoo.com/quote/%5EIBEX

library(quantmod) 
#IBEX <- getSymbols("^IBEX", src = "yahoo", auto.assign = FALSE)
IBEX <- readRDS("IBEX.rds")
tail(IBEX)
##            IBEX.Open IBEX.High IBEX.Low IBEX.Close IBEX.Volume
## 2019-02-27    9206.2    9226.2   9160.9     9211.7   174514400
## 2019-02-28    9166.3    9293.1   9141.0     9277.7   260630500
## 2019-03-01    9313.5    9361.4   9267.7     9267.7   167873400
## 2019-03-04    9309.3    9322.0   9248.0     9259.8   112921800
## 2019-03-05    9255.7    9298.7   9204.3     9258.2   140185000
## 2019-03-06    9243.1    9344.6   9235.4     9296.7   140841100
##            IBEX.Adjusted
## 2019-02-27        9211.7
## 2019-02-28        9277.7
## 2019-03-01        9267.7
## 2019-03-04        9259.8
## 2019-03-05        9258.2
## 2019-03-06        9296.7

Importando datos 2/2

class(IBEX)
## [1] "xts" "zoo"
range(index(IBEX))
## [1] "2007-01-02" "2019-03-06"
summary(IBEX)
##      Index              IBEX.Open       IBEX.High        IBEX.Low    
##  Min.   :2007-01-02   Min.   : 5950   Min.   : 6093   Min.   : 5905  
##  1st Qu.:2010-01-25   1st Qu.: 8782   1st Qu.: 8862   1st Qu.: 8700  
##  Median :2013-02-04   Median :10022   Median :10102   Median : 9916  
##  Mean   :2013-02-03   Mean   :10205   Mean   :10285   Mean   :10108  
##  3rd Qu.:2016-02-21   3rd Qu.:10890   3rd Qu.:10959   3rd Qu.:10802  
##  Max.   :2019-03-06   Max.   :15999   Max.   :16040   Max.   :15869  
##                       NA's   :2       NA's   :2       NA's   :2      
##    IBEX.Close     IBEX.Volume        IBEX.Adjusted  
##  Min.   : 5956   Min.   :        0   Min.   : 5956  
##  1st Qu.: 8787   1st Qu.:   303900   1st Qu.: 8787  
##  Median :10014   Median :179782450   Median :10014  
##  Mean   :10200   Mean   :166023994   Mean   :10200  
##  3rd Qu.:10884   3rd Qu.:256596575   3rd Qu.:10884  
##  Max.   :15946   Max.   :789490200   Max.   :15946  
##  NA's   :2       NA's   :2           NA's   :2

Visualizando datos 1/5

plot(Cl(IBEX))

Visualizando datos 2/5

chart_Series(IBEX["2018-06::"])

Visualizando datos 3/5

barChart(IBEX["2018-06::"])

Visualizando datos 4/5

candleChart(IBEX, TA = "addEMA(n = 200, col = 'red');
            addEMA(n = 50, col = 'blue');
            addVo()",
            subset='last 16 weeks');

Visualizando datos 5/5

https://www.quantmod.com/gallery/

candleChart(IBEX, TA = "addEMA(n = 200, col = 'red'); addEMA(n = 50, col = 'blue');
            addMACD(12, 26, 9); addBBands(n = 20, sd = 2); addVo()",
            subset='last 16 weeks')

Estrategia: Seguimiento de tendencias

Configuración de Quantsrat

  • Setup
  • Cáculo de indicadores
  • Cálculo de señales
  • Control de reglas

  • Ejecución del backtesting

Análisis de rendimiento

PerformanceAnalytics package.

  • Performance Analysis
  • Style Analysis
  • Risk Analysis
  • Value at Risk - VaR
  • Moments and Co-moments
  • Summary Tabular Data
  • Charts and Graphs
library(PerformanceAnalytics)

Optimización

  • Optimización de indicadores en paralelo (con R)
  • Análisis de Monte Carlo

Para saber más

Preguntas