#Installing SIT
devtools::install_github('systematicinvestor/SIT.date', force = T)
## WARNING: Rtools is required to build R packages, but is not currently installed.
##
## Please download and install Rtools 4.2 from https://cran.r-project.org/bin/windows/Rtools/ or https://www.r-project.org/nosvn/winutf8/ucrt3/.
## Downloading GitHub repo systematicinvestor/SIT.date@HEAD
## rlang (1.1.0 -> 1.1.1 ) [CRAN]
## cli (3.4.1 -> 3.6.1 ) [CRAN]
## vctrs (0.6.0 -> 0.6.2 ) [CRAN]
## stringi (1.7.8 -> 1.7.12) [CRAN]
## Installing 4 packages: rlang, cli, vctrs, stringi
## Installing packages into 'C:/Users/DELL/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'rlang' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'rlang'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\DELL\AppData\Local\R\win-library\4.2\00LOCK\rlang\libs\x64\rlang.dll
## to C:\Users\DELL\AppData\Local\R\win-library\4.2\rlang\libs\x64\rlang.dll:
## Permission denied
## Warning: restored 'rlang'
## package 'cli' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'cli'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\DELL\AppData\Local\R\win-library\4.2\00LOCK\cli\libs\x64\cli.dll to
## C:\Users\DELL\AppData\Local\R\win-library\4.2\cli\libs\x64\cli.dll: Permission
## denied
## Warning: restored 'cli'
## package 'vctrs' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'vctrs'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\DELL\AppData\Local\R\win-library\4.2\00LOCK\vctrs\libs\x64\vctrs.dll
## to C:\Users\DELL\AppData\Local\R\win-library\4.2\vctrs\libs\x64\vctrs.dll:
## Permission denied
## Warning: restored 'vctrs'
## package 'stringi' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'stringi'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\DELL\AppData\Local\R\win-library\4.2\00LOCK\stringi\libs\x64\stringi.dll
## to C:\Users\DELL\AppData\Local\R\win-library\4.2\stringi\libs\x64\stringi.dll:
## Permission denied
## Warning: restored 'stringi'
##
## The downloaded binary packages are in
## C:\Users\DELL\AppData\Local\Temp\RtmpaGC3YR\downloaded_packages
## ── R CMD build ─────────────────────────────────────────────────────────────────
## WARNING: Rtools is required to build R packages, but is not currently installed.
##
## Please download and install Rtools 4.2 from https://cran.r-project.org/bin/windows/Rtools/ or https://www.r-project.org/nosvn/winutf8/ucrt3/.
##
checking for file 'C:\Users\DELL\AppData\Local\Temp\RtmpaGC3YR\remotes2b7832a96f0c\systematicinvestor-SIT.date-6263da6/DESCRIPTION' ...
✔ checking for file 'C:\Users\DELL\AppData\Local\Temp\RtmpaGC3YR\remotes2b7832a96f0c\systematicinvestor-SIT.date-6263da6/DESCRIPTION'
##
─ preparing 'SIT.date':
## checking DESCRIPTION meta-information ...
checking DESCRIPTION meta-information ...
✔ checking DESCRIPTION meta-information
##
─ checking for LF line-endings in source and make files and shell scripts
##
─ checking for empty or unneeded directories
##
Omitted 'LazyData' from DESCRIPTION
##
─ building 'SIT.date_0.1.tar.gz'
##
##
## Installing package into 'C:/Users/DELL/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
curl::curl_download('https://github.com/systematicinvestor/SIT/raw/master/SIT.tar.gz', 'SIT.tar.gz',mode = 'wb',quiet=T)
install.packages('SIT.tar.gz', repos = NULL, type='source')
## Installing package into 'C:/Users/DELL/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
library(SIT)
## Loading required package: SIT.date
## Loading required package: quantmod
## Warning: package 'quantmod' was built under R version 4.2.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
##
## Attaching package: 'SIT'
## The following object is masked from 'package:TTR':
##
## DVI
## The following object is masked from 'package:base':
##
## close
library(quantmod)
tickers = c('0050.TW')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
## Warning: 0050.TW contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## [1] "0050.TW"
head(data$`0050.TW`)
## 0050.TW.Open 0050.TW.High 0050.TW.Low 0050.TW.Close 0050.TW.Volume
## 2008-01-02 60.01 60.01 60.01 60.01 0
## 2008-01-03 58.89 58.89 58.89 58.89 0
## 2008-01-04 59.01 59.01 59.01 59.01 0
## 2008-01-07 56.39 56.39 56.39 56.39 0
## 2008-01-08 56.98 56.98 56.98 56.98 0
## 2008-01-09 58.20 58.20 58.20 58.20 0
## 0050.TW.Adjusted
## 2008-01-02 60.01
## 2008-01-03 58.89
## 2008-01-04 59.01
## 2008-01-07 56.39
## 2008-01-08 56.98
## 2008-01-09 58.20
tail(data$`0050.TW`)
## 0050.TW.Open 0050.TW.High 0050.TW.Low 0050.TW.Close 0050.TW.Volume
## 2023-05-05 117.70 118.05 117.40 117.80 2218751
## 2023-05-08 118.50 118.90 118.35 118.55 6011209
## 2023-05-09 118.60 119.00 118.30 118.90 4636210
## 2023-05-10 118.80 118.80 117.65 117.80 4075119
## 2023-05-11 118.25 118.45 117.55 117.55 3684964
## 2023-05-12 117.15 117.50 116.75 117.20 3813349
## 0050.TW.Adjusted
## 2023-05-05 117.80
## 2023-05-08 118.55
## 2023-05-09 118.90
## 2023-05-10 117.80
## 2023-05-11 117.55
## 2023-05-12 117.20
bt.prep(data, align ='keep.all', dates='2010::2022')
lookback.len=60
models <- list()
prices = data$prices
data$weight[] = 1
models$buy.hold = bt.run(data)
## Latest weights :
## Warning: object index class (Date) does not support timezones.
## Expected 'UTC' timezone, but tzone is 'Asia/Taipei'
## 0050.TW
## 2022-12-30 100
##
## Performance summary :
## CAGR Best Worst
## 5.3 8 -7
sma = bt.apply(data, function(x) {SMA(Cl(x), 200)})
head(sma, 200)
## Warning: object index class (Date) does not support timezones.
## Expected 'UTC' timezone, but tzone is 'Asia/Taipei'
## X0050.TW
## 2010-01-04 NA
## 2010-01-05 NA
## 2010-01-06 NA
## 2010-01-07 NA
## 2010-01-08 NA
## 2010-01-11 NA
## 2010-01-12 NA
## 2010-01-13 NA
## 2010-01-14 NA
## 2010-01-15 NA
## ...
## 2010-10-11 NA
## 2010-10-12 NA
## 2010-10-13 NA
## 2010-10-14 NA
## 2010-10-15 NA
## 2010-10-18 NA
## 2010-10-19 NA
## 2010-10-20 NA
## 2010-10-21 NA
## 2010-10-22 53.869
head(prices, 200)
## Warning: object index class (Date) does not support timezones.
## Expected 'UTC' timezone, but tzone is 'Asia/Taipei'
## X0050.TW
## 2010-01-04 56.50
## 2010-01-05 56.50
## 2010-01-06 57.55
## 2010-01-07 57.40
## 2010-01-08 57.75
## 2010-01-11 57.90
## 2010-01-12 57.60
## 2010-01-13 56.75
## 2010-01-14 57.45
## 2010-01-15 57.80
## ...
## 2010-10-11 56.95
## 2010-10-12 56.20
## 2010-10-13 56.55
## 2010-10-14 57.45
## 2010-10-15 57.35
## 2010-10-18 56.20
## 2010-10-19 56.05
## 2010-10-20 56.70
## 2010-10-21 56.75
## 2010-10-22 57.10
data$weight[] = NA
data$weight[] = iif(prices >= sma, 1, 0)
head(data$weight, 200)
## Warning: object index class (Date) does not support timezones.
## Expected 'UTC' timezone, but tzone is 'Asia/Taipei'
## X0050.TW
## 2010-01-04 0
## 2010-01-05 0
## 2010-01-06 0
## 2010-01-07 0
## 2010-01-08 0
## 2010-01-11 0
## 2010-01-12 0
## 2010-01-13 0
## 2010-01-14 0
## 2010-01-15 0
## ...
## 2010-10-11 0
## 2010-10-12 0
## 2010-10-13 0
## 2010-10-14 0
## 2010-10-15 0
## 2010-10-18 0
## 2010-10-19 0
## 2010-10-20 0
## 2010-10-21 0
## 2010-10-22 1
names(data)
## [1] "prices" "dates" "0050.TW" "weight"
## [5] ".getSymbols" "symbolnames" "execution.price"
models$sma.cross = bt.run(data, do.lag = 1, trade.summary=T)
## Latest weights :
## Warning: object index class (Date) does not support timezones.
## Expected 'UTC' timezone, but tzone is 'Asia/Taipei'
## 0050.TW
## 2022-12-30 0
##
## Performance summary :
## CAGR Best Worst
## 3.9 4.9 -7
names(models$sma.cross)
## [1] "weight" "type" "ret" "best"
## [5] "worst" "equity" "cagr" "dates.index"
## [9] "trade.summary"
str(models$sma.cross)
## List of 9
## $ weight :An xts object on 2010-01-04 / 2022-12-30 containing:
## Data: double [3195, 1]
## Columns: 0050.TW
## Index: Date [3195] (TZ: "Asia/Taipei")
## $ type : chr "weight"
## $ ret :An xts object on 2010-01-04 / 2022-12-30 containing:
## Data: double [3195, 1]
## Columns: 0050.TW
## Index: Date [3195] (TZ: "Asia/Taipei")
## $ best : num 0.0487
## $ worst : num -0.0703
## $ equity :An xts object on 2010-01-04 / 2022-12-30 containing:
## Data: double [3195, 1]
## Columns: 0050.TW
## Index: Date [3195] (TZ: "Asia/Taipei")
## $ cagr : num 0.0393
## $ dates.index : int [1:3195] 1 2 3 4 5 6 7 8 9 10 ...
## $ trade.summary:List of 2
## ..$ stats : num [1:11, 1:3] 48 0.0135 42.4167 0.2292 0.0975 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:11] "ntrades" "avg.pnl" "len" "win.prob" ...
## .. .. ..$ : chr [1:3] "All" "Long" "Short"
## ..$ trades: chr [1:48, 1:8] "0050.TW" "0050.TW" "0050.TW" "0050.TW" ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : NULL
## .. .. ..$ : chr [1:8] "symbol" "weight" "entry.date" "exit.date" ...
models$sma.cross$trade.summary
## $stats
## All Long Short
## ntrades 4.800000e+01 4.800000e+01 NA
## avg.pnl 1.345274e-02 1.345274e-02 NA
## len 4.241667e+01 4.241667e+01 NA
## win.prob 2.291667e-01 2.291667e-01 NA
## win.avg.pnl 9.754864e-02 9.754864e-02 NA
## win.len 1.544545e+02 1.544545e+02 NA
## loss.prob 7.708333e-01 7.708333e-01 NA
## loss.avg.pnl -1.154875e-02 -1.154875e-02 NA
## loss.len 9.108108e+00 9.108108e+00 NA
## expectancy 1.345274e-04 1.345274e-04 NA
## profitfactor 2.511177e+00 2.511177e+00 NA
##
## $trades
## symbol weight entry.date exit.date nhold entry.price exit.price
## [1,] "0050.TW" "100" "2010-10-22" "2011-06-20" "241" "57.1" "58.6"
## [2,] "0050.TW" "100" "2011-06-22" "2011-06-23" "1" "59.2" "58.65"
## [3,] "0050.TW" "100" "2011-07-01" "2011-07-12" "11" "59.7" "58.7"
## [4,] "0050.TW" "100" "2011-07-20" "2011-08-02" "13" "60.45" "59.55"
## [5,] "0050.TW" "100" "2012-02-15" "2012-02-16" "1" "54.9" "54.25"
## [6,] "0050.TW" "100" "2012-02-17" "2012-02-21" "4" "54.75" "54.5"
## [7,] "0050.TW" "100" "2012-02-22" "2012-04-09" "47" "54.85" "53.1"
## [8,] "0050.TW" "100" "2012-04-13" "2012-04-17" "4" "53.95" "52.9"
## [9,] "0050.TW" "100" "2012-05-02" "2012-05-07" "5" "53.35" "52.05"
## [10,] "0050.TW" "100" "2012-08-06" "2012-10-24" "79" "51.55" "51"
## [11,] "0050.TW" "100" "2012-11-27" "2013-06-20" "205" "52.4" "54.25"
## [12,] "0050.TW" "100" "2013-06-27" "2013-08-20" "54" "54.35" "54.6"
## [13,] "0050.TW" "100" "2013-08-23" "2013-08-27" "4" "54.85" "54.6"
## [14,] "0050.TW" "100" "2013-08-29" "2013-11-13" "76" "55.5" "55.75"
## [15,] "0050.TW" "100" "2013-11-15" "2013-11-21" "6" "56.15" "55.45"
## [16,] "0050.TW" "100" "2013-11-25" "2014-02-05" "72" "56.25" "55.8"
## [17,] "0050.TW" "100" "2014-02-11" "2014-10-24" "255" "56.95" "63.05"
## [18,] "0050.TW" "100" "2014-10-27" "2014-12-16" "50" "63.25" "64.55"
## [19,] "0050.TW" "100" "2014-12-19" "2015-01-06" "18" "65.2" "64.9"
## [20,] "0050.TW" "100" "2015-01-08" "2015-07-08" "181" "66.5" "67.5"
## [21,] "0050.TW" "100" "2015-07-13" "2015-07-22" "9" "68.1" "67.8"
## [22,] "0050.TW" "100" "2016-03-14" "2016-03-15" "1" "64" "63.05"
## [23,] "0050.TW" "100" "2016-03-16" "2016-04-06" "21" "63.6" "62.4"
## [24,] "0050.TW" "100" "2016-04-13" "2016-04-20" "7" "64.05" "62.65"
## [25,] "0050.TW" "100" "2016-04-21" "2016-04-28" "7" "62.95" "62.4"
## [26,] "0050.TW" "100" "2016-05-25" "2018-02-06" "622" "62" "79.65"
## [27,] "0050.TW" "100" "2018-02-23" "2018-03-01" "6" "82" "81.75"
## [28,] "0050.TW" "100" "2018-03-06" "2018-03-07" "1" "82.15" "81.8"
## [29,] "0050.TW" "100" "2018-03-08" "2018-03-23" "15" "82.75" "82.1"
## [30,] "0050.TW" "100" "2018-03-27" "2018-03-28" "1" "83.4" "82.25"
## [31,] "0050.TW" "100" "2018-03-30" "2018-04-02" "3" "82.85" "82.25"
## [32,] "0050.TW" "100" "2018-04-11" "2018-04-12" "1" "82.85" "82.5"
## [33,] "0050.TW" "100" "2018-06-06" "2018-06-08" "2" "83.3" "82.55"
## [34,] "0050.TW" "100" "2018-06-13" "2018-06-14" "1" "83" "81.75"
## [35,] "0050.TW" "100" "2018-07-12" "2018-07-17" "5" "82.65" "82.55"
## [36,] "0050.TW" "100" "2018-07-18" "2018-10-08" "82" "83.05" "83"
## [37,] "0050.TW" "100" "2018-10-09" "2018-10-11" "2" "83.25" "77.4"
## [38,] "0050.TW" "100" "2019-04-02" "2019-05-14" "42" "80.15" "79.65"
## [39,] "0050.TW" "100" "2019-06-11" "2019-06-13" "2" "79.4" "79.05"
## [40,] "0050.TW" "100" "2019-06-19" "2020-03-09" "264" "80.75" "85.85"
## [41,] "0050.TW" "100" "2020-06-04" "2020-06-15" "11" "87.6" "87"
## [42,] "0050.TW" "100" "2020-06-16" "2021-10-01" "472" "88.9" "133.95"
## [43,] "0050.TW" "100" "2021-10-07" "2021-10-08" "1" "135.35" "134.2"
## [44,] "0050.TW" "100" "2021-10-15" "2021-10-29" "14" "136.25" "135.8"
## [45,] "0050.TW" "100" "2021-11-01" "2021-11-02" "1" "136.5" "136.25"
## [46,] "0050.TW" "100" "2021-11-03" "2021-11-04" "1" "136.6" "136.15"
## [47,] "0050.TW" "100" "2021-11-05" "2022-02-24" "111" "138.1" "138.8"
## [48,] "0050.TW" "100" "2022-03-01" "2022-03-04" "3" "140.45" "138.45"
## return
## [1,] "2.63"
## [2,] "-0.93"
## [3,] "-1.68"
## [4,] "-1.49"
## [5,] "-1.18"
## [6,] "-0.46"
## [7,] "-3.19"
## [8,] "-1.95"
## [9,] "-2.44"
## [10,] "-1.07"
## [11,] "3.53"
## [12,] "0.46"
## [13,] "-0.46"
## [14,] "0.45"
## [15,] "-1.25"
## [16,] "-0.8"
## [17,] "10.71"
## [18,] "2.06"
## [19,] "-0.46"
## [20,] "1.5"
## [21,] "-0.44"
## [22,] "-1.48"
## [23,] "-1.89"
## [24,] "-2.19"
## [25,] "-0.87"
## [26,] "28.47"
## [27,] "-0.3"
## [28,] "-0.43"
## [29,] "-0.79"
## [30,] "-1.38"
## [31,] "-0.72"
## [32,] "-0.42"
## [33,] "-0.9"
## [34,] "-1.51"
## [35,] "-0.12"
## [36,] "-0.06"
## [37,] "-7.03"
## [38,] "-0.62"
## [39,] "-0.44"
## [40,] "6.32"
## [41,] "-0.68"
## [42,] "50.67"
## [43,] "-0.85"
## [44,] "-0.33"
## [45,] "-0.18"
## [46,] "-0.33"
## [47,] "0.51"
## [48,] "-1.42"
bt.detail.summary(models$sma.cross)
## $System
## $System$Period
## [1] "Jan2010 - Dec2022"
##
## $System$Cagr
## [1] 3.93
##
## $System$Sharpe
## [1] 0.4
##
## $System$DVR
## [,1]
## 0050.TW 0.26
##
## $System$Volatility
## [1] 11.55
##
## $System$MaxDD
## [1] -23.67
##
## $System$AvgDD
## [1] -3.12
##
## $System$VaR
## 5%
## -1.17
##
## $System$CVaR
## [1] -1.86
##
## $System$Exposure
## [1] 63.72
##
##
## $Trade
## $Trade$Win.Percent
## [1] 22.9
##
## $Trade$Avg.Trade
## [1] 1.3
##
## $Trade$Avg.Win
## [1] 9.8
##
## $Trade$Avg.Loss
## [1] -1.2
##
## $Trade$Best.Trade
## [1] 50.67
##
## $Trade$Worst.Trade
## [1] -7.03
##
## $Trade$WinLoss.Ratio
## [1] 8.45
##
## $Trade$Avg.Len
## [1] 42.42
##
## $Trade$Num.Trades
## [1] 48
##
##
## $Period
## $Period$Win.Percent.Day
## [1] 31.9
##
## $Period$Best.Day
## [1] 4.9
##
## $Period$Worst.Day
## [1] -7
##
## $Period$Win.Percent.Month
## [1] 41.7
##
## $Period$Best.Month
## [1] 15.5
##
## $Period$Worst.Month
## [1] -11.2
##
## $Period$Win.Percent.Year
## [1] 53.8
##
## $Period$Best.Year
## [1] 20.9
##
## $Period$Worst.Year
## [1] -15.7
plotbt.transition.map(models$sma.cross$weight)
plotbt.monthly.table(models$sma.cross$equity)
## Jan Feb Mar Apr May Jun Jul Aug Sep
## 2010 " NA" " 0.0" " 0.0" " 0.0" " 0.0" " 0.0" " 0.0" " 0.0" " 0.0"
## 2011 " 2.6" " -5.6" " 0.1" " 4.1" " 0.0" " -6.4" " -2.6" " -0.6" " 0.0"
## 2012 " 0.0" " 0.4" " -2.0" " -5.2" " -2.4" " 0.0" " 0.0" " 2.3" " 4.5"
## 2013 " 1.5" " 1.1" " -0.2" " 2.4" " 1.0" " -3.4" " 2.7" " -3.4" " 2.3"
## 2014 " -1.9" " -0.8" " 3.1" " 1.5" " 3.4" " 4.5" " 1.2" " 3.7" " -5.6"
## 2015 " -0.7" " 3.4" " -1.8" " 3.5" " -0.5" " -2.4" " -3.2" " 0.0" " 0.0"
## 2016 " 0.0" " 0.0" " 0.1" " -6.4" " 1.9" " 3.6" " 4.7" " 1.2" " 2.2"
## 2017 " 2.1" " -0.2" " 0.8" " 1.2" " 2.6" " 5.1" " 1.2" " 2.0" " -2.8"
## 2018 " 3.0" " -5.5" " -3.3" " -1.1" " 0.0" " -2.4" " 2.9" " 1.6" " -0.1"
## 2019 " 0.0" " 0.0" " 0.0" " 3.3" " -3.8" " -0.3" " 2.3" " -1.1" " 3.4"
## 2020 " -7.2" " -1.4" " -3.2" " 0.0" " 0.0" " 0.4" " 15.5" " -1.9" " 1.2"
## 2021 " 4.9" " 4.1" " 1.0" " 4.3" " -2.5" " 1.3" " -2.1" " 3.2" " -2.4"
## 2022 " -2.7" " -1.9" " -1.4" " 0.0" " 0.0" " 0.0" " 0.0" " 0.0" " 0.0"
## Avg " 0.1" " -0.5" " -0.5" " 0.6" " 0.0" " 0.0" " 1.7" " 0.5" " 0.2"
## Oct Nov Dec Year MaxDD
## 2010 " -1.9" " 2.0" " 7.5" " 7.5" " -2.7"
## 2011 " 0.0" " 0.0" " 0.0" " -8.4" "-10.7"
## 2012 " -7.5" " 2.2" " 0.5" " -7.5" "-10.3"
## 2013 " 1.3" " -2.7" " 2.0" " 4.4" " -8.3"
## 2014 " -0.3" " 3.4" " -2.0" " 10.1" " -9.7"
## 2015 " 0.0" " 0.0" " 0.0" " -1.9" " -7.6"
## 2016 " 2.3" " -1.2" " 0.1" " 8.5" " -6.5"
## 2017 " 5.6" " -3.5" " -0.1" " 14.4" " -5.5"
## 2018 "-11.2" " 0.0" " 0.0" "-15.7" "-21.5"
## 2019 " 6.5" " 1.6" " 6.0" " 18.8" " -6.9"
## 2020 " 0.0" " 10.0" " 7.9" " 20.9" "-13.6"
## 2021 " -3.4" " -0.6" " 5.4" " 13.5" "-11.5"
## 2022 " 0.0" " 0.0" " 0.0" " -6.0" "-10.0"
## Avg " -0.7" " 0.9" " 2.1" " 4.5" " -9.6"
dates = '2010::2022'
buy.hold.equity = models$buy.hold$equity[dates] / as.double(models$buy.hold$equity[dates][1])
sma.cross.equity = models$sma.cross$equity[dates] / as.double(models$sma.cross$equity[dates][1])
chartSeries(buy.hold.equity, TA = c(addTA(sma.cross.equity, on=1, col='red')),
theme ='white', yrange = range(buy.hold.equity, sma.cross.equity) )
confidence.level = 60/100
ret = prices / mlag(prices) - 1
ir = sqrt(lookback.len) * runMean(ret$'0050.TW', lookback.len) / runSD(ret$'0050.TW', lookback.len)
momentum.p = pt(ir, lookback.len - 1)
data$weight[] = NA
data$weight$'0050.TW'[] = iif(cross.up(momentum.p, confidence.level), 1, iif(cross.dn(momentum.p, (1 - confidence.level)), 0,NA))
models$Probabilistic = bt.run.share(data, clean.signal=T)
## Latest weights :
## Warning: object index class (Date) does not support timezones.
## Expected 'UTC' timezone, but tzone is 'Asia/Taipei'
## 0050.TW
## 2022-12-30 100
##
## Performance summary :
## CAGR Best Worst
## 2.5 4.9 -7
data$weight[] = NA
data$weight$'0050.TW'[] = iif(cross.up(momentum.p, confidence.level), 1, iif(cross.up(momentum.p, (1 - confidence.level)), 0,NA))
models$Probabilistic.Leverage = bt.run.share(data, clean.signal=T)
## Latest weights :
## Warning: object index class (Date) does not support timezones.
## Expected 'UTC' timezone, but tzone is 'Asia/Taipei'
## 0050.TW
## 2022-12-30 100
##
## Performance summary :
## CAGR Best Worst
## 4.1 4.9 -7
strategy.performance.snapshoot(models, T)
## NULL
cols = spl('steelblue1,steelblue')
prices = scale.one(data$prices)
layout(1:3)
plota(prices$'0050.TW', type='l', ylim=range(prices), plotX=F, col=cols[1], lwd=2)
plota.legend('0050.TW',cols,as.list(prices))
highlight = models$Probabilistic$weight$'0050.TW' > 0
plota(models$Probabilistic$equity, type='l', plotX=F, x.highlight = highlight | T)
plota.legend('Probabilistic,0050.TW',c('black',cols))