Home > R, Strategy, Trading Strategies > Adjusted Momentum

Adjusted Momentum

David Varadi has published two excellent posts / ideas about cooking with momentum:

I just could not resist the urge to share these ideas with you. Following is implementation using the Systematic Investor Toolbox.

###############################################################################
# Load Systematic Investor Toolbox (SIT)
# https://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)
	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')
		
	tickers = spl('SPY,^VIX')
		
	data <- new.env()
	getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T)
		for(i in data$symbolnames) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)
	bt.prep(data, align='remove.na', fill.gaps = T)

	VIX = Cl(data$VIX)
	bt.prep.remove.symbols(data, 'VIX')
	
	#*****************************************************************
	# Setup
	#*****************************************************************
	prices = data$prices
		
	models = list()

	#*****************************************************************
	# 200 SMA
	#****************************************************************** 
	data$weight[] = NA
		data$weight[] = iif(prices > SMA(prices, 200), 1, 0)
	models$ma200 = bt.run.share(data, clean.signal=T)
	
	#*****************************************************************
	# 200 ROC
	#****************************************************************** 
	roc = prices / mlag(prices) - 1
	
	data$weight[] = NA
		data$weight[] = iif(SMA(roc, 200) > 0, 1, 0)
	models$roc200 = bt.run.share(data, clean.signal=T)
	
	#*****************************************************************
	# 200 VIX MOM
	#****************************************************************** 
	data$weight[] = NA
		data$weight[] = iif(SMA(roc/VIX, 200) > 0, 1, 0)
	models$vix.mom = bt.run.share(data, clean.signal=T)

	#*****************************************************************
	# 200 ER MOM
	#****************************************************************** 
	forecast = SMA(roc,10)
	error = roc - mlag(forecast)
	mae = SMA(abs(error), 10)
	
	data$weight[] = NA
		data$weight[] = iif(SMA(roc/mae, 200) > 0, 1, 0)
	models$er.mom = bt.run.share(data, clean.signal=T)
		
	#*****************************************************************
	# Report
	#****************************************************************** 
	strategy.performance.snapshoot(models, T)

plot1

Please enjoy and share your ideas with David and myself.

To view the complete source code for this example, please have a look at the
bt.adjusted.momentum.test() function in bt.test.r at github.

Categories: R, Strategy, Trading Strategies
  1. Antoine
    August 2, 2014 at 2:31 pm

    Hi, thank you for doing this. Why don’t you have the same results ?

  2. August 2, 2014 at 4:48 pm

    Thanks for posting – this isn’t the first time you’ve published source code that clarifies formulas discussed in others’ posts – well done, again. I note that in Varadi the ER-MOM achieves a 10.7 CAGR vs VIX-MOM’s 10.1, while in the results here the tally is ER-MOM = 9 and VIX-MOM = 9.16, both less and opposite in winning strategy. Do you know if the delta is due to time frame (Varadi starts in Sept 94) or perhaps you’ve included slippage? Any insight appreciated.

    Thanks!
    Carl

  3. dan lulich
    August 21, 2014 at 4:34 am

    #*****************************************************************
    # 200 VIX MOM
    #******************************************************************
    data$weight[] = NA
    data$weight[] = iif(SMA(roc/VIX, 230) > 0, 1, -1)
    models$vix.mom = bt.run.share(data, clean.signal=T)

    #*****************************************************************
    # 200 ER MOM
    #******************************************************************
    forecast = SMA(roc,10)
    error = roc – mlag(forecast)
    mae = SMA(abs(error), 10)

    data$weight[] = NA
    data$weight[] = iif(SMA(roc/mae, 305) > 0, 1, -1)
    models$er.mom = bt.run.share(data, clean.signal=T)

    I hate magic numbers: So, I reject my own tuning except for the fun it gave me to do it. But, shorting appears righteous.

    Thank you Michael and David,
    Dan

  1. August 1, 2014 at 6:15 am

Leave a comment