#Install SIT package
#install.packages('devtools')
#install.packages('quantmod')

devtools::install_github('systematicinvestor/SIT.date', force = T)
## Downloading GitHub repo systematicinvestor/SIT.date@HEAD
## 
## ── R CMD build ─────────────────────────────────────────────────────────────────
##   
   checking for file ‘/private/var/folders/d3/dfh0nq_s2bz4x2zjbkrxj49r0000gn/T/RtmpPy49b5/remotes79b32697670/systematicinvestor-SIT.date-6263da6/DESCRIPTION’ ...
  
✔  checking for file ‘/private/var/folders/d3/dfh0nq_s2bz4x2zjbkrxj49r0000gn/T/RtmpPy49b5/remotes79b32697670/systematicinvestor-SIT.date-6263da6/DESCRIPTION’
## 
  
─  preparing ‘SIT.date’:
##    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’
## 
  
   
## 
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')
#rm(list=Is())
# devtools:install_github(joshuaulrich/xts', force = T)
# devtools:install _github('joshuaulrich/quantmod', force = T)
#con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
#source(con)
#close(con)
library(SIT)
## Loading required package: SIT.date
## Loading required package: quantmod
## Loading required package: xts
## Loading required package: zoo
## 
## 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
# Load historical data
tickers = spl('SPY,TLT')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T)
## [1] "SPY" "TLT"
for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)
bt.prep(data, align='remove.na', dates='2005::')



# Setup
 
lookback.len = 60

prices = data$prices

models = list()


# Simple Momentum
 
momentum = prices / mlag(prices, lookback.len)
data$weight[] = NA
data$weight$SPY[] = momentum$SPY > momentum$TLT
data$weight$TLT[] = momentum$SPY <= momentum$TLT
models$Simple  = bt.run.share(data, clean.signal=T)
## Latest weights :
##                     SPY TLT
## 2023-05-12 08:00:00   0 100
## 
## Performance summary :
##  CAGR    Best    Worst   
##  7.2 7.5 -6.7    
 #*****************************************************************
# Probabilistic Momentum
#****************************************************************** 
confidence.level = 60/100
ret = prices / mlag(prices) - 1 
 
ir = sqrt(lookback.len) * runMean(ret$SPY - ret$TLT, lookback.len) / runSD(ret$SPY - ret$TLT, lookback.len)
momentum.p = pt(ir, lookback.len - 1)
     
data$weight[] = NA
    data$weight$SPY[] = iif(cross.up(momentum.p, confidence.level), 1, iif(cross.dn(momentum.p, (1 - confidence.level)), 0,NA))
    data$weight$TLT[] = iif(cross.dn(momentum.p, (1 - confidence.level)), 1, iif(cross.up(momentum.p, confidence.level), 0,NA))
models$Probabilistic  = bt.run.share(data, clean.signal=T)
## Latest weights :
##                     SPY TLT
## 2023-05-12 08:00:00 100   0
## 
## Performance summary :
##  CAGR    Best    Worst   
##  9.8 7.5 -6.7    
# Probabilistic Momentum + SPY Leverage 
#****************************************************************** 
data$weight[] = NA
    data$weight$SPY[] = iif(cross.up(momentum.p, confidence.level), 1, iif(cross.up(momentum.p, (1 - confidence.level)), 0,NA))
    data$weight$TLT[] = iif(cross.dn(momentum.p, (1 - confidence.level)), 1, iif(cross.up(momentum.p, confidence.level), 0,NA))
models$Probabilistic.Leverage = bt.run.share(data, clean.signal=T)  
## Latest weights :
##                     SPY TLT
## 2023-05-12 08:00:00 100   0
## 
## Performance summary :
##  CAGR    Best    Worst   
##  11.3    6.4 -12.7   
#*****************************************************************
# Create Report
#******************************************************************    
strategy.performance.snapshoot(models, T)

## NULL
# Visualize Signal
       
cols = spl('steelblue1,steelblue')
prices = scale.one(data$prices)
 
layout(1:3)
 
plota(prices$SPY, type='l', ylim=range(prices), plotX=F, col=cols[1], lwd=2)
plota.lines(prices$TLT, type='l', plotX=F, col=cols[2], lwd=2)
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "plotX" is not a
## graphical parameter
    plota.legend('SPY,TLT',cols,as.list(prices))
 
highlight = models$Probabilistic$weight$SPY > 0
 #   plota.control$col.x.highlight = iif(highlight, cols[1], cols[2])
plota(models$Probabilistic$equity, type='l', plotX=F, x.highlight = highlight | T)
    plota.legend('Probabilistic,SPY,TLT',c('black',cols))
             
highlight = models$Simple$weight$SPY > 0
  #  plota.control$col.x.highlight = iif(highlight, cols[1], cols[2])
plota(models$Simple$equity, type='l', plotX=T, x.highlight = highlight | T)
    plota.legend('Simple,SPY,TLT',c('black',cols))