Archive

Archive for August, 2013

7Twelve Back-test

August 15, 2013 3 comments

I recently came across the The 7Twelve Portfolio strategy. I like the catchy name and the strategy report, “An Introduction to 7Twelve.” Following is some additional info about the The 7Twelve Portfolio strategy that I found useful:

Today I want to show how to back-test the The 7Twelve Portfolio strategy using the Systematic Investor Toolbox.

Let’s start by loading historical data

###############################################################################
# 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('VFINX,VIMSX,NAESX,VDMIX,VEIEX,VGSIX,FNARX,QRAAX,VBMFX,VIPSX,OIBAX,BIL') 
	
    data <- new.env()
    getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)

    #--------------------------------   
    # BIL     30-May-2007 
    # load 3-Month Treasury Bill from FRED
    TB3M = quantmod::getSymbols('DTB3', src='FRED', auto.assign = FALSE)		
    TB3M[] = ifna.prev(TB3M)	
    TB3M = processTBill(TB3M, timetomaturity = 1/4, 261)	
    #--------------------------------       	

    # extend	
    data$BIL = extend.data(data$BIL, TB3M, scale=T)	
	
    for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)		
				
    bt.prep(data, align='remove.na')

Next, let’s make the The 7Twelve Portfolio strategy with annual/ quarterly and monthly rebalancing.

    #*****************************************************************
    # Code Strategies
    #****************************************************************** 
    models = list()
	
    # Vanguard 500 Index Investor (VFINX)
    data$weight[] = NA
        data$weight$VFINX[] = 1
    models$VFINX  = bt.run.share(data, clean.signal=F) 
		
    #*****************************************************************
    # Code Strategies
    #****************************************************************** 	
    obj = portfolio.allocation.helper(data$prices, periodicity = 'years',
        min.risk.fns = list(EW=equal.weight.portfolio)
    ) 	
    models$year = create.strategies(obj, data)$models$EW

    obj = portfolio.allocation.helper(data$prices, periodicity = 'quarters',
        min.risk.fns = list(EW=equal.weight.portfolio)
    ) 	
    models$quarter = create.strategies(obj, data)$models$EW
		
    obj = portfolio.allocation.helper(data$prices, periodicity = 'months',
        min.risk.fns = list(EW=equal.weight.portfolio)
    ) 	
    models$month = create.strategies(obj, data)$models$EW
	
    #*****************************************************************
    # Create Report
    #****************************************************************** 
    strategy.performance.snapshoot(models, T)

plot1

The strategy does better than the Vanguard 500 Index benchmark, but still suffers a huge draw-down in 2008-2009 period.

How would you make it a better strategy? Please share your ideas.

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

Advertisement

Calendar-based Sector Strategy

August 5, 2013 1 comment

I recently came across the Kaeppel’s Sector Seasonality Strategy which is described in Kaeppel’s Corner: Sector Seasonality and updated in Kaeppel’s Corner: Get Me Back, Clarence.

Today I want to show how to back-test the Kaeppel’s Sector Seasonality Strategy using the Systematic Investor Toolbox. Following are the strategy rules:

  • Buy Fidelity Select Technology (FSPTX) at the October close.
  • Switch from FSPTX to Fidelity Select Energy (FSENX) at the January close.
  • Switch from FSENX to cash at the May close.
  • Switch from cash to Fidelity Select Gold (FSAGX) at the August close.
  • Switch from FSAGX to cash at the September close.
  • Repeat by switching from cash to FSPTX at the October close.

Let’s start by loading historical data

###############################################################################
# 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('FSPTX,FSENX,FSAGX,VFINX,BIL') 

    data <- new.env()
        getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)

    #--------------------------------   
    # BIL     30-May-2007 
    # load 3-Month Treasury Bill from FRED
    TB3M = getSymbols('DTB3', src='FRED', auto.assign = FALSE)		
    TB3M[] = ifna.prev(TB3M)	
    TB3M = processTBill(TB3M, timetomaturity = 1/4, 261)	
    #--------------------------------       	

    # extend BIL with 3-Month Treasury Bills
    data$BIL = extend.data(data$BIL, TB3M, scale=T)	
	

        for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)						
    bt.prep(data, align='remove.na')

Next let’s create 2 benchmark strategies:

  • Vanguard 500 Index Investor (VFINX)
  • VFINX from the October close through the May close and cash otherwise (VFINX /Cash)
    #*****************************************************************
    # Code Strategies
    #****************************************************************** 
    prices = data$prices 
    dates = data$dates	
	        
    models = list()

    # find period ends
    period.ends = endpoints(prices, 'months')
        period.ends = period.ends[period.ends > 0]	
	
    months = date.month(dates[period.ends])
	    	
    #*****************************************************************
    # Code Strategies
    #****************************************************************** 
    # Vanguard 500 Index Investor (VFINX)
    data$weight[] = NA
        data$weight$VFINX[] = 1
    models$VFINX  = bt.run.share(data, clean.signal=F) 
	
	
    # VFINX from the October[10] close through the May[5] close and cash otherwise (VFINX /Cash)
    data$weight[] = NA
        data$weight$VFINX[period.ends] = iif( months >= 10 | months <= 5, 1, 0)
        data$weight$BIL[period.ends] = iif( !(months >= 10 | months <= 5), 1, 0)
    models$VFINX_Cash  = bt.run.share(data, clean.signal=F) 	

And finally, let’s create the Kaeppel’s Sector Seasonality Strategy and make reports

    #*****************************************************************
    # Calendar-based sector strategy
    #****************************************************************** 	
    # Buy Fidelity Select Technology (FSPTX) at the October close.
    # Switch from FSPTX to Fidelity Select Energy (FSENX) at the January close.
    # Switch from FSENX to cash at the May close.
    # Switch from cash to Fidelity Select Gold (FSAGX) at the August close.
    # Switch from FSAGX to cash at the September close.
    # Repeat by switching from cash to FSPTX at the October close.
    data$weight[] = NA
        # Buy Fidelity Select Technology (FSPTX) at the October close.
        data$weight$FSPTX[period.ends] = iif( months >= 10 | months < 1, 1, 0)
		
        # Switch from FSPTX to Fidelity Select Energy (FSENX) at the January close.
        data$weight$FSENX[period.ends] = iif( months >= 1 & months < 5, 1, 0)
				
        # Switch from cash to Fidelity Select Gold (FSAGX) at the August close.
        data$weight$FSAGX[period.ends] = iif( months >= 8 & months < 9, 1, 0)

        # Rest time in Cash
        data$weight$BIL[period.ends] = 1 - rowSums(data$weight[period.ends], na.rm = T)
    models$Sector  = bt.run.share(data, clean.signal=F) 	
		           
    #*****************************************************************
    # Create Report
    #****************************************************************** 
    strategy.performance.snapshoot(models, T)

plot1

The Technology exposure is surely made a big difference from 1998 to 2000. But looking at the strategy perfromance since 2002, the strategy is still doing better than our benchmarks.

plot2

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

Categories: Backtesting, R