1.
rm(list = ls())
ifelse(!require(quantmod),install.packages('quantmod'),library(quantmod))
## Loading required package: quantmod
## Loading required package: xts
## Warning: package 'xts' was built under R version 3.5.3
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.5.3
## Version 0.4-0 included new data defaults. See ?getSymbols.
## [1] "quantmod"
tw20_adj_close<-read.csv("2018Q4_20.csv")
2.
ifelse(!require(readr),install.packages('readr'),library(readr))
## Loading required package: readr
## Warning: package 'readr' was built under R version 3.5.3
## [1] "readr"
tw20.txt<-read.table("tw20.txt",header = T)
3.
ifelse(!require(reshape2),install.packages('reshape2'),library(reshape2))
## Loading required package: reshape2
## Warning: package 'reshape2' was built under R version 3.5.3
## [1] "reshape2"
colnames(tw20.txt)<-c("id","", "date", "price")
tw20.xts=dcast(tw20.txt,date~id)
## Using price as value column: use value.var to override.
tw20.xts$date<-as.Date(as.character(tw20.xts$date), "%Y%m%d")
tw20.xts<-xts(tw20.xts[,-1], order.by = tw20.xts$date)
head(tw20.xts)
## 1101 1216 1301 1402 2002 2105 2303 2308 2317 2327
## 2014-01-02 32.76 40.41 65.53 27.04 22.13 62.60 10.10 139.78 63.71 30.20
## 2014-01-03 32.15 40.04 65.28 26.68 21.97 61.71 10.06 138.12 62.91 30.88
## 2014-01-06 31.46 40.04 64.87 26.36 21.88 61.71 10.18 136.87 62.60 31.69
## 2014-01-07 31.86 39.96 64.38 26.36 21.72 63.00 10.35 136.87 62.60 31.82
## 2014-01-08 31.68 39.81 64.05 26.76 21.72 63.40 10.64 137.28 62.99 32.49
## 2014-01-09 30.96 39.14 64.71 26.52 21.67 62.60 10.55 137.28 63.79 33.71
## 2330 2357 2408 2412 2474 2912 3008 3481 6505 9904
## 2014-01-02 89.00 199.29 33.91 73.37 171.06 170.17 1119.08 9.36 66.81 35.77
## 2014-01-03 87.29 199.29 35.99 73.37 169.32 167.29 1128.36 9.40 65.90 35.77
## 2014-01-06 87.29 198.18 38.47 72.97 165.41 168.52 1091.22 9.14 66.73 34.54
## 2014-01-07 86.87 199.29 41.11 72.81 168.89 165.64 1095.86 9.27 66.81 35.36
## 2014-01-08 88.57 206.67 43.91 72.81 168.89 165.64 1054.07 9.23 67.47 34.95
## 2014-01-09 86.02 205.93 46.94 72.81 173.23 165.23 1049.42 9.73 68.05 33.92
tail(tw20.xts)
## 1101 1216 1301 1402 2002 2105 2303 2308 2317 2327 2330
## 2018-12-22 35.45 69.5 98.3 27.50 24.05 41.00 11.25 128.5 70.9 322.5 221.5
## 2018-12-24 35.80 69.0 98.3 28.25 24.15 41.15 11.20 128.0 71.4 331.0 220.0
## 2018-12-25 35.85 68.0 96.7 27.95 23.80 40.30 11.15 127.0 70.7 328.5 217.5
## 2018-12-26 35.40 68.6 96.3 27.65 23.80 40.15 11.20 126.0 70.2 314.0 216.5
## 2018-12-27 35.40 69.2 97.8 27.50 23.95 40.55 11.25 127.5 70.8 321.0 223.0
## 2018-12-28 35.60 69.8 101.0 27.90 24.25 40.85 11.25 129.5 70.8 319.0 225.5
## 2357 2408 2412 2474 2912 3008 3481 6505 9904
## 2018-12-22 202.0 53.5 108.0 223.5 311.0 3240 9.97 107.5 32.05
## 2018-12-24 202.5 56.0 107.0 227.0 311.0 3270 9.81 104.5 32.40
## 2018-12-25 201.0 57.0 106.5 221.0 308.5 3165 9.62 103.0 31.90
## 2018-12-26 198.5 54.1 106.0 221.5 307.0 3125 9.48 103.0 31.70
## 2018-12-27 200.0 55.8 106.5 225.5 312.0 3135 9.71 109.5 32.15
## 2018-12-28 201.5 55.0 113.0 225.0 311.0 3215 9.72 109.0 32.60
str(tw20.xts)
## An 'xts' object on 2014-01-02/2018-12-28 containing:
## Data: num [1:1229, 1:20] 32.8 32.1 31.5 31.9 31.7 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:20] "1101" "1216" "1301" "1402" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
4.
library(xts)
library(quantmod)
tw20.mon.ret <- to.monthly(tw20.xts, indexAt = "lastof", OHLC=FALSE)
head( tw20.mon.ret)
## 1101 1216 1301 1402 2002 2105 2303 2308 2317 2327
## 2014-01-31 31.97 37.42 64.05 25.01 21.72 61.39 10.14 138.53 67.27 33.71
## 2014-02-28 34.86 37.87 63.23 25.37 21.42 63.32 10.10 140.20 66.56 38.97
## 2014-03-31 33.92 39.66 62.57 25.77 21.38 69.74 10.55 156.42 68.38 37.62
## 2014-04-30 34.57 38.24 63.89 24.61 21.13 69.50 10.72 153.92 68.62 38.70
## 2014-05-31 32.19 39.29 62.98 24.69 20.51 65.00 11.54 162.24 73.21 47.60
## 2014-06-30 32.62 40.11 65.53 25.61 20.92 61.31 12.28 180.96 79.24 57.58
## 2330 2357 2408 2412 2474 2912 3008 3481 6505
## 2014-01-31 89.42 208.15 39.99 72.66 175.40 167.70 1081.93 8.85 62.77
## 2014-02-28 91.98 215.16 38.07 72.73 191.03 154.93 1249.09 9.10 64.09
## 2014-03-31 100.92 222.54 32.39 74.08 191.46 177.18 1337.32 8.81 63.84
## 2014-04-30 100.92 230.29 39.59 74.47 220.98 185.01 1750.59 8.77 62.53
## 2014-05-31 101.77 244.31 66.30 75.90 232.71 204.37 1820.24 9.99 60.38
## 2014-06-30 107.73 245.79 61.58 76.05 241.82 196.95 2210.29 11.80 64.09
## 9904
## 2014-01-31 34.54
## 2014-02-28 33.26
## 2014-03-31 35.32
## 2014-04-30 31.58
## 2014-05-31 27.22
## 2014-06-30 29.56
library(PerformanceAnalytics)
## Warning: package 'PerformanceAnalytics' was built under R version 3.5.3
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(magrittr)
## Warning: package 'magrittr' was built under R version 3.5.3
tw20.day.ret<-Return.calculate(tw20.xts, method = "log")
head(tw20.day.ret)
## 1101 1216 1301 1402 2002
## 2014-01-02 NA NA NA NA NA
## 2014-01-03 -0.018795808 -0.009198325 -0.003822342 -0.01340303 -0.007256268
## 2014-01-06 -0.021695553 0.000000000 -0.006300443 -0.01206651 -0.004104909
## 2014-01-07 0.012634407 -0.002000001 -0.007582241 0.00000000 -0.007339482
## 2014-01-08 -0.005665738 -0.003760817 -0.005138998 0.01506053 0.000000000
## 2014-01-09 -0.022989518 -0.016973175 0.010251721 -0.00900907 -0.002304680
## 2105 2303 2308 2317 2327
## 2014-01-02 NA NA NA NA NA
## 2014-01-03 -0.014319286 -0.003968259 -0.011946886 -0.012636403 0.022266801
## 2014-01-06 0.000000000 0.011857846 -0.009091302 -0.004939855 0.025892449
## 2014-01-07 0.020688734 0.016561509 0.000000000 0.000000000 0.004093849
## 2014-01-08 0.006329135 0.027633964 0.002991066 0.006210706 0.020837327
## 2014-01-09 -0.012698583 -0.008494624 0.000000000 0.012620455 0.036862180
## 2330 2357 2408 2412 2474
## 2014-01-02 NA NA NA NA NA
## 2014-01-03 -0.01940046 0.000000000 0.05953117 0.000000000 -0.01022396
## 2014-01-06 0.00000000 -0.005585342 0.06663759 -0.005466735 -0.02336318
## 2014-01-07 -0.00482316 0.005585342 0.06637268 -0.002195089 0.02082038
## 2014-01-08 0.01938045 0.036362268 0.06589068 0.000000000 0.00000000
## 2014-01-09 -0.02921337 -0.003587013 0.06672811 0.000000000 0.02537258
## 2912 3008 3481 6505 9904
## 2014-01-02 NA NA NA NA NA
## 2014-01-03 -0.017069104 0.008258332 0.004264399 -0.013714328 0.00000000
## 2014-01-06 0.007325604 -0.033468915 -0.028049304 0.012516185 -0.03499148
## 2014-01-07 -0.017237678 0.004243107 0.014122994 0.001198143 0.02346317
## 2014-01-08 0.000000000 -0.038880582 -0.004324331 0.009830285 -0.01166277
## 2014-01-09 -0.002478316 -0.004421231 0.052754848 0.008559674 -0.02991366
6.
1101
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
load.packages('quantmod')
bt.simple <- function(data, signal)
{
# lag serial
signal <- lag(signal,1)
# back fill
signal <- na.locf(signal, na.rm = FALSE)
signal[is.na(signal)] = 0
# calculate close-to-close returns
# ROC() : Calculate the (rate of) change of a series over n periods.
ret <- ROC(Cl(data), type="discrete")
ret[1] = 0
# compute stats
bt <- list()
bt$ret <- ret * signal
bt$equity <- cumprod(1 + bt$ret)
return(bt)}
ifelse(!require(reshape2),install.packages('reshape2'),library(reshape2))
## [1] "magrittr"
colnames(tw20.txt)<-c("id","", "date", "price")
tw20.xts=dcast(tw20.txt,date~id)
## Using price as value column: use value.var to override.
tw20.xts$date<-as.Date(as.character(tw20.xts$date), "%Y%m%d")
tw20.xts<-xts(tw20.xts[,-1], order.by = tw20.xts$date)
head(tw20.xts)
## 1101 1216 1301 1402 2002 2105 2303 2308 2317 2327
## 2014-01-02 32.76 40.41 65.53 27.04 22.13 62.60 10.10 139.78 63.71 30.20
## 2014-01-03 32.15 40.04 65.28 26.68 21.97 61.71 10.06 138.12 62.91 30.88
## 2014-01-06 31.46 40.04 64.87 26.36 21.88 61.71 10.18 136.87 62.60 31.69
## 2014-01-07 31.86 39.96 64.38 26.36 21.72 63.00 10.35 136.87 62.60 31.82
## 2014-01-08 31.68 39.81 64.05 26.76 21.72 63.40 10.64 137.28 62.99 32.49
## 2014-01-09 30.96 39.14 64.71 26.52 21.67 62.60 10.55 137.28 63.79 33.71
## 2330 2357 2408 2412 2474 2912 3008 3481 6505 9904
## 2014-01-02 89.00 199.29 33.91 73.37 171.06 170.17 1119.08 9.36 66.81 35.77
## 2014-01-03 87.29 199.29 35.99 73.37 169.32 167.29 1128.36 9.40 65.90 35.77
## 2014-01-06 87.29 198.18 38.47 72.97 165.41 168.52 1091.22 9.14 66.73 34.54
## 2014-01-07 86.87 199.29 41.11 72.81 168.89 165.64 1095.86 9.27 66.81 35.36
## 2014-01-08 88.57 206.67 43.91 72.81 168.89 165.64 1054.07 9.23 67.47 34.95
## 2014-01-09 86.02 205.93 46.94 72.81 173.23 165.23 1049.42 9.73 68.05 33.92
tail(tw20.xts)
## 1101 1216 1301 1402 2002 2105 2303 2308 2317 2327 2330
## 2018-12-22 35.45 69.5 98.3 27.50 24.05 41.00 11.25 128.5 70.9 322.5 221.5
## 2018-12-24 35.80 69.0 98.3 28.25 24.15 41.15 11.20 128.0 71.4 331.0 220.0
## 2018-12-25 35.85 68.0 96.7 27.95 23.80 40.30 11.15 127.0 70.7 328.5 217.5
## 2018-12-26 35.40 68.6 96.3 27.65 23.80 40.15 11.20 126.0 70.2 314.0 216.5
## 2018-12-27 35.40 69.2 97.8 27.50 23.95 40.55 11.25 127.5 70.8 321.0 223.0
## 2018-12-28 35.60 69.8 101.0 27.90 24.25 40.85 11.25 129.5 70.8 319.0 225.5
## 2357 2408 2412 2474 2912 3008 3481 6505 9904
## 2018-12-22 202.0 53.5 108.0 223.5 311.0 3240 9.97 107.5 32.05
## 2018-12-24 202.5 56.0 107.0 227.0 311.0 3270 9.81 104.5 32.40
## 2018-12-25 201.0 57.0 106.5 221.0 308.5 3165 9.62 103.0 31.90
## 2018-12-26 198.5 54.1 106.0 221.5 307.0 3125 9.48 103.0 31.70
## 2018-12-27 200.0 55.8 106.5 225.5 312.0 3135 9.71 109.5 32.15
## 2018-12-28 201.5 55.0 113.0 225.0 311.0 3215 9.72 109.0 32.60
str(tw20.xts)
## An 'xts' object on 2014-01-02/2018-12-28 containing:
## Data: num [1:1229, 1:20] 32.8 32.1 31.5 31.9 31.7 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:20] "1101" "1216" "1301" "1402" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
tw20.all<-readRDS("c:/test0415/TEST0415/tw20_xts_all")
head(tw20.all)
## 1101 1216 1301 1402 2002 2105 2303 2308 2317 2327
## 2014-01-02 32.76 40.41 65.53 27.04 22.13 62.60 10.10 139.78 63.71 30.20
## 2014-01-03 32.15 40.04 65.28 26.68 21.97 61.71 10.06 138.12 62.91 30.88
## 2014-01-06 31.46 40.04 64.87 26.36 21.88 61.71 10.18 136.87 62.60 31.69
## 2014-01-07 31.86 39.96 64.38 26.36 21.72 63.00 10.35 136.87 62.60 31.82
## 2014-01-08 31.68 39.81 64.05 26.76 21.72 63.40 10.64 137.28 62.99 32.49
## 2014-01-09 30.96 39.14 64.71 26.52 21.67 62.60 10.55 137.28 63.79 33.71
## 2330 2357 2408 2412 2474 2912 3008 3481 6505 9904
## 2014-01-02 89.00 199.29 33.91 73.37 171.06 170.17 1119.08 9.36 66.81 35.77
## 2014-01-03 87.29 199.29 35.99 73.37 169.32 167.29 1128.36 9.40 65.90 35.77
## 2014-01-06 87.29 198.18 38.47 72.97 165.41 168.52 1091.22 9.14 66.73 34.54
## 2014-01-07 86.87 199.29 41.11 72.81 168.89 165.64 1095.86 9.27 66.81 35.36
## 2014-01-08 88.57 206.67 43.91 72.81 168.89 165.64 1054.07 9.23 67.47 34.95
## 2014-01-09 86.02 205.93 46.94 72.81 173.23 165.23 1049.42 9.73 68.05 33.92
str(tw20.all)
## An 'xts' object on 2014-01-02/2018-12-28 containing:
## Data: num [1:1229, 1:20] 32.8 32.1 31.5 31.9 31.7 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:20] "1101" "1216" "1301" "1402" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
tw20.all.1<-tw20.all[complete.cases(tw20.all),]
head(tw20.all.1)
## 1101 1216 1301 1402 2002 2105 2303 2308 2317 2327
## 2014-01-02 32.76 40.41 65.53 27.04 22.13 62.60 10.10 139.78 63.71 30.20
## 2014-01-03 32.15 40.04 65.28 26.68 21.97 61.71 10.06 138.12 62.91 30.88
## 2014-01-06 31.46 40.04 64.87 26.36 21.88 61.71 10.18 136.87 62.60 31.69
## 2014-01-07 31.86 39.96 64.38 26.36 21.72 63.00 10.35 136.87 62.60 31.82
## 2014-01-08 31.68 39.81 64.05 26.76 21.72 63.40 10.64 137.28 62.99 32.49
## 2014-01-09 30.96 39.14 64.71 26.52 21.67 62.60 10.55 137.28 63.79 33.71
## 2330 2357 2408 2412 2474 2912 3008 3481 6505 9904
## 2014-01-02 89.00 199.29 33.91 73.37 171.06 170.17 1119.08 9.36 66.81 35.77
## 2014-01-03 87.29 199.29 35.99 73.37 169.32 167.29 1128.36 9.40 65.90 35.77
## 2014-01-06 87.29 198.18 38.47 72.97 165.41 168.52 1091.22 9.14 66.73 34.54
## 2014-01-07 86.87 199.29 41.11 72.81 168.89 165.64 1095.86 9.27 66.81 35.36
## 2014-01-08 88.57 206.67 43.91 72.81 168.89 165.64 1054.07 9.23 67.47 34.95
## 2014-01-09 86.02 205.93 46.94 72.81 173.23 165.23 1049.42 9.73 68.05 33.92
tail(tw20.all.1)
## 1101 1216 1301 1402 2002 2105 2303 2308 2317 2327 2330
## 2018-12-22 35.45 69.5 98.3 27.50 24.05 41.00 11.25 128.5 70.9 322.5 221.5
## 2018-12-24 35.80 69.0 98.3 28.25 24.15 41.15 11.20 128.0 71.4 331.0 220.0
## 2018-12-25 35.85 68.0 96.7 27.95 23.80 40.30 11.15 127.0 70.7 328.5 217.5
## 2018-12-26 35.40 68.6 96.3 27.65 23.80 40.15 11.20 126.0 70.2 314.0 216.5
## 2018-12-27 35.40 69.2 97.8 27.50 23.95 40.55 11.25 127.5 70.8 321.0 223.0
## 2018-12-28 35.60 69.8 101.0 27.90 24.25 40.85 11.25 129.5 70.8 319.0 225.5
## 2357 2408 2412 2474 2912 3008 3481 6505 9904
## 2018-12-22 202.0 53.5 108.0 223.5 311.0 3240 9.97 107.5 32.05
## 2018-12-24 202.5 56.0 107.0 227.0 311.0 3270 9.81 104.5 32.40
## 2018-12-25 201.0 57.0 106.5 221.0 308.5 3165 9.62 103.0 31.90
## 2018-12-26 198.5 54.1 106.0 221.5 307.0 3125 9.48 103.0 31.70
## 2018-12-27 200.0 55.8 106.5 225.5 312.0 3135 9.71 109.5 32.15
## 2018-12-28 201.5 55.0 113.0 225.0 311.0 3215 9.72 109.0 32.60
data1<-new.env()
data1$prices<-tw20.all.1$`1101`
prices<-data1$prices
prices
library(TTR)
sma90<-SMA(prices, 90)
head(sma90, 91)
bt.prep(data1, align='keep.all')
names(data1)
data1$dates
data1$prices
data1$prices<-prices
class(data1$dates)
data1$weight
data1$execution.price = prices
data1$weight[] = 1
buy.hold.1101 <- bt.run.share(data1, clean.signal=F, trade.summary = TRUE)
buy.hold.1101 <-bt.run(data1)
sma90<-SMA(prices, 90)
head(sma90, 91)
data1$weight[] <- iif(prices >= sma90, 1, 0)
sma90.1101 <- bt.run(data1, trade.summary=T)
- sma 90 for 1101, short allowed
data1$weight[] <- iif(prices >= sma90, 1, -1)
sma90.1101.short <- bt.run(data1, trade.summary=T)
## Latest weights :
## prices
## 2018-12-28 -100
##
## Performance summary :
## CAGR Best Worst
## -2.9 6.7 -8.1
# summary of investment
models<-list("SMA90"= sma90.1101,
"SMA90_short" = sma90.1101.short,
"BH 1101" = buy.hold.1101)
strategy.performance.snapshoot(models, T)

## NULL
strategy.performance.snapshoot(models, control=list(comparison=T), sort.performance=T)

plotbt.strategy.sidebyside(models, return.table=T)
## SMA90 SMA90_short
## Period "一月2014 - 十二月2018" "一月2014 - 十二月2018"
## Cagr "0.65" "-2.87"
## Sharpe "0.13" "-0.01"
## DVR "0.01" "0"
## Volatility "17.56" "23.83"
## MaxDD "-29.23" "-31.99"
## AvgDD "-6.54" "-9.65"
## VaR "-1.81" "-2.36"
## CVaR "-2.71" "-3.34"
## Exposure "49.15" "99.92"
## BH 1101
## Period "一月2014 - 十二月2018"
## Cagr "1.68"
## Sharpe "0.19"
## DVR "0.01"
## Volatility "23.83"
## MaxDD "-45.04"
## AvgDD "-7.78"
## VaR "-2.35"
## CVaR "-3.28"
## Exposure "99.92"

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.3
all.1101<-merge.xts(sma90.1101$equity,
sma90.1101.short$equity,
buy.hold.1101$equity)
colnames(all.1101)<-c("sma90", "sma90 short", "BH")
head(all.1101)
## sma90 sma90 short BH
## 2014-01-02 1 1.000000 1.0000000
## 2014-01-03 1 1.018620 0.9813797
## 2014-01-06 1 1.040482 0.9603175
## 2014-01-07 1 1.027253 0.9725275
## 2014-01-08 1 1.033056 0.9670330
## 2014-01-09 1 1.056535 0.9450549
all.1101.long<-fortify(all.1101, melt=T)
head(all.1101.long)
## Index Series Value
## 1 2014-01-02 sma90 1
## 2 2014-01-03 sma90 1
## 3 2014-01-06 sma90 1
## 4 2014-01-07 sma90 1
## 5 2014-01-08 sma90 1
## 6 2014-01-09 sma90 1
title = "Cumulative returns of 1101s"
p = ggplot(all.1101.long, aes(x = Index, y = Value)) +
geom_line(aes(linetype = Series, color = Series)) +
#geom_point(aes(shape = Series))+
xlab("year") + ylab("cumulative returns")+
ggtitle(title)
p

1216
data2<-new.env()
data2$prices<-tw20.all.1$`1216`
prices<-data2$prices
prices
sma90<-SMA(prices, 90)
head(sma90, 91)
bt.prep(data2, align='keep.all')
names(data2)
data2$dates
data2$prices
data2$prices<-prices
class(data2$dates)
data2$weight
data2$execution.price = prices
data2$weight[] = 1
buy.hold.1216 <- bt.run.share(data2, clean.signal=F, trade.summary = TRUE)
buy.hold.1216 <-bt.run(data2)
sma90<-SMA(prices, 90)
head(sma90, 91)
data2$weight[] <- iif(prices >= sma90, 1, 0)
sma90.1216 <- bt.run(data2, trade.summary=T)
- sma 90 for 1216, short allowed
data2$weight[] <- iif(prices >= sma90, 1, -1)
sma90.1216.short <- bt.run(data2, trade.summary=T)
## Latest weights :
## prices
## 2018-12-28 -100
##
## Performance summary :
## CAGR Best Worst
## -1.2 7 -6.9
models<-list("SMA90"= sma90.1216,
"SMA90_short" = sma90.1216.short,
"BH 1216" = buy.hold.1216)
strategy.performance.snapshoot(models, T)

## NULL
strategy.performance.snapshoot(models, control=list(comparison=T), sort.performance=T)

plotbt.strategy.sidebyside(models, return.table=T)
## SMA90 SMA90_short
## Period "一月2014 - 十二月2018" "一月2014 - 十二月2018"
## Cagr "5.96" "-1.24"
## Sharpe "0.43" "0.05"
## DVR "0.3" "0"
## Volatility "16.93" "21.89"
## MaxDD "-18.31" "-32.56"
## AvgDD "-3.78" "-6.11"
## VaR "-1.69" "-2.18"
## CVaR "-2.49" "-3.12"
## Exposure "66.72" "99.92"
## BH 1216
## Period "一月2014 - 十二月2018"
## Cagr "11.58"
## Sharpe "0.62"
## DVR "0.54"
## Volatility "21.88"
## MaxDD "-19.9"
## AvgDD "-4.09"
## VaR "-2.09"
## CVaR "-2.98"
## Exposure "99.92"

library(ggplot2)
all.1216<-merge.xts(sma90.1216$equity,
sma90.1216.short$equity,
buy.hold.1216$equity)
colnames(all.1216)<-c("sma90", "sma90 short", "BH")
head(all.1216)
## sma90 sma90 short BH
## 2014-01-02 1 1.000000 1.0000000
## 2014-01-03 1 1.009156 0.9908439
## 2014-01-06 1 1.009156 0.9908439
## 2014-01-07 1 1.011172 0.9888641
## 2014-01-08 1 1.014968 0.9851522
## 2014-01-09 1 1.032050 0.9685721
all.1216.long<-fortify(all.1216, melt=T)
head(all.1216.long)
## Index Series Value
## 1 2014-01-02 sma90 1
## 2 2014-01-03 sma90 1
## 3 2014-01-06 sma90 1
## 4 2014-01-07 sma90 1
## 5 2014-01-08 sma90 1
## 6 2014-01-09 sma90 1
title = "Cumulative returns of 1216s"
p = ggplot(all.1216.long, aes(x = Index, y = Value)) +
geom_line(aes(linetype = Series, color = Series)) +
#geom_point(aes(shape = Series))+
xlab("year") + ylab("cumulative returns")+
ggtitle(title)
p

1301
data3<-new.env()
data3$prices<-tw20.all.1$`1301`
prices<-data3$prices
prices
sma90<-SMA(prices, 90)
head(sma90, 91)
bt.prep(data3, align='keep.all')
names(data3)
data3$dates
data3$prices
data3$prices<-prices
class(data3$dates)
data3$weight
data3$execution.price = prices
data3$weight[] = 1
buy.hold.1301 <- bt.run.share(data3, clean.signal=F, trade.summary = TRUE)
buy.hold.1301 <-bt.run(data3)
sma90<-SMA(prices, 90)
head(sma90, 91)
data3$weight[] <- iif(prices >= sma90, 1, 0)
sma90.1301 <- bt.run(data3, trade.summary=T)
- sma 90 for 1301, short allowed
data3$weight[] <- iif(prices >= sma90, 1, -1)
sma90.1301.short <- bt.run(data3, trade.summary=T)
## Latest weights :
## prices
## 2018-12-28 -100
##
## Performance summary :
## CAGR Best Worst
## -2 6 -9.3
models<-list("SMA90"= sma90.1301,
"SMA90_short" = sma90.1301.short,
"BH 1301" = buy.hold.1301)
strategy.performance.snapshoot(models, T)

## NULL
strategy.performance.snapshoot(models, control=list(comparison=T), sort.performance=T)

plotbt.strategy.sidebyside(models, return.table=T)
## SMA90 SMA90_short
## Period "一月2014 - 十二月2018" "一月2014 - 十二月2018"
## Cagr "4.05" "-2"
## Sharpe "0.34" "-0.01"
## DVR "0.21" "0"
## Volatility "15.65" "19.45"
## MaxDD "-13.65" "-30.83"
## AvgDD "-3.98" "-12.17"
## VaR "-1.47" "-1.89"
## CVaR "-2.36" "-2.86"
## Exposure "64.77" "99.92"
## BH 1301
## Period "一月2014 - 十二月2018"
## Cagr "9.06"
## Sharpe "0.55"
## DVR "0.47"
## Volatility "19.44"
## MaxDD "-21.37"
## AvgDD "-4.05"
## VaR "-1.85"
## CVaR "-2.71"
## Exposure "99.92"

library(ggplot2)
all.1301<-merge.xts(sma90.1301$equity,
sma90.1301.short$equity,
buy.hold.1301$equity)
colnames(all.1301)<-c("sma90", "sma90 short", "BH")
head(all.1301)
## sma90 sma90 short BH
## 2014-01-02 1 1.000000 1.0000000
## 2014-01-03 1 1.003815 0.9961850
## 2014-01-06 1 1.010120 0.9899283
## 2014-01-07 1 1.017750 0.9824508
## 2014-01-08 1 1.022966 0.9774149
## 2014-01-09 1 1.012425 0.9874866
all.1301.long<-fortify(all.1301, melt=T)
head(all.1301.long)
## Index Series Value
## 1 2014-01-02 sma90 1
## 2 2014-01-03 sma90 1
## 3 2014-01-06 sma90 1
## 4 2014-01-07 sma90 1
## 5 2014-01-08 sma90 1
## 6 2014-01-09 sma90 1
title = "Cumulative returns of 1301s"
p = ggplot(all.1301.long, aes(x = Index, y = Value)) +
geom_line(aes(linetype = Series, color = Series)) +
#geom_point(aes(shape = Series))+
xlab("year") + ylab("cumulative returns")+
ggtitle(title)
p
