0050

data1<-new.env()
data1$prices<-etf4.all.1$`0050`
prices<-data1$prices
prices

sma50<-SMA(prices, 50)
head(sma50, 51)
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$execution.price = data1$prices = etf4.all.1$`0050`
data1$weight[] = 1
buy.hold.0050 <- bt.run.share(data1, clean.signal=F, trade.summary = TRUE)
buy.hold.0050 <-bt.run(data1)
prices<-data1$prices
sma200<-SMA(prices, 200)
head(sma200, 201)
data1$weight[] <- iif(prices >= sma200, 1, 0)
sma200.0050 <- bt.run(data1, trade.summary=T)
sma50<-SMA(prices, 50)
head(sma50, 51)
data1$weight[] <- iif(prices >= sma50, 1, 0)
sma50.0050 <- bt.run(data1, trade.summary=T)
models<-list("SMA50"= sma50.0050, 
             "SMA200"= sma200.0050, 
             "SMA50_short" = sma50.0050.short, 
             "BH 0050" = buy.hold.0050)
strategy.performance.snapshoot(models, T)

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

plotbt.strategy.sidebyside(models, return.table=T)
##            SMA50                     SMA200                   
## Period     "十二月2015 - 十二月2018" "十二月2015 - 十二月2018"
## Cagr       "3.27"                    "2.22"                   
## Sharpe     "0.43"                    "0.33"                   
## DVR        "0.15"                    "0.15"                   
## Volatility "8.57"                    "7.75"                   
## MaxDD      "-14.16"                  "-13.15"                 
## AvgDD      "-2.49"                   "-1.87"                  
## VaR        "-0.9"                    "-0.76"                  
## CVaR       "-1.44"                   "-1.3"                   
## Exposure   "64.31"                   "54.99"                  
##            SMA50_short               BH 0050                  
## Period     "十二月2015 - 十二月2018" "十二月2015 - 十二月2018"
## Cagr       "-2.16"                   "8.1"                    
## Sharpe     "-0.11"                   "0.7"                    
## DVR        "-0.04"                   "0.51"                   
## Volatility "12.56"                   "12.55"                  
## MaxDD      "-26.17"                  "-16.56"                 
## AvgDD      "-5.45"                   "-2.19"                  
## VaR        "-1.3"                    "-1.33"                  
## CVaR       "-1.75"                   "-1.94"                  
## Exposure   "99.87"                   "99.87"

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.3
all.0050<-merge.xts(sma50.0050$equity, 
            sma50.0050.short$equity, 
            sma200.0050$equity, 
            buy.hold.0050$equity)
colnames(all.0050)<-c("sma50", "sma50 short", "sma200", "BH")
head(all.0050)
all.0050.long<-fortify(all.0050, melt=T)
head(all.0050.long)
title = "Cumulative returns of 0050s"
p = ggplot(all.0050.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

0056

data2<-new.env()
data2$prices<-etf4.all.1$`0056`
prices<-data2$prices
prices

sma50<-SMA(prices, 50)
head(sma50, 51)
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.0056 <- bt.run.share(data2, clean.signal=F, trade.summary = TRUE)
buy.hold.0056 <-bt.run(data2)
prices<-data2$prices
sma200<-SMA(prices, 200)
head(sma200, 201)
data2$weight[] <- iif(prices >= sma200, 1, 0)
sma200.0056 <- bt.run(data2, trade.summary=T)
sma50<-SMA(prices, 50)
head(sma50, 51)
data2$weight[] <- iif(prices >= sma50, 1, 0)
sma50.0056 <- bt.run(data2, trade.summary=T)
models<-list("SMA50"= sma50.0056, 
             "SMA200"= sma200.0056,
             "SMA50_short" = sma50.0056.short, 
             "BH 0056" = buy.hold.0056)
strategy.performance.snapshoot(models, T)

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

plotbt.strategy.sidebyside(models, return.table=T)
##            SMA50                     SMA200                   
## Period     "十二月2015 - 十二月2018" "十二月2015 - 十二月2018"
## Cagr       "3.71"                    "-3.14"                  
## Sharpe     "0.53"                    "-0.38"                  
## DVR        "0.29"                    "-0.06"                  
## Volatility "7.61"                    "7.84"                   
## MaxDD      "-7.49"                   "-12.25"                 
## AvgDD      "-1.84"                   "-6.23"                  
## VaR        "-0.6"                    "-0.63"                  
## CVaR       "-1.3"                    "-1.4"                   
## Exposure   "56.86"                   "52.46"                  
##            SMA50_short               BH 0056                  
## Period     "十二月2015 - 十二月2018" "十二月2015 - 十二月2018"
## Cagr       "2.45"                    "4.28"                   
## Sharpe     "0.28"                    "0.44"                   
## DVR        "0.04"                    "0.25"                   
## Volatility "11.22"                   "11.22"                  
## MaxDD      "-14.92"                  "-15.9"                  
## AvgDD      "-3.59"                   "-3.08"                  
## VaR        "-1.1"                    "-1.1"                   
## CVaR       "-1.59"                   "-1.87"                  
## Exposure   "99.87"                   "99.87"

library(ggplot2)
all.0056<-merge.xts(sma50.0056$equity, 
                    sma50.0056.short$equity, 
                    sma200.0056$equity, 
                    buy.hold.0056$equity)
colnames(all.0056)<-c("sma50", "sma50 short","sma200", "BH")
head(all.0056)
all.0056.long<-fortify(all.0056, melt=T)
head(all.0056.long)
title = "Cumulative returns of 0056s"
p = ggplot(all.0056.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