R Load Data

#con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
#source(con)
#close(con)

# 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   0 100
## 
## Performance summary :
##  CAGR    Best    Worst   
##  7.2 7.5 -6.7    

Model Setup

## Latest weights :
##            SPY TLT
## 2023-05-12 100   0
## 
## Performance summary :
##  CAGR    Best    Worst   
##  9.8 7.5 -6.7    

Performance

# 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 100   0
## 
## Performance summary :
##  CAGR    Best    Worst   
##  11.3    6.4 -12.7   
#*****************************************************************
# Create Report
#******************************************************************    
strategy.performance.snapshoot(models, T)

## NULL

Visualization

# 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))