Back to Community
Recession Tracker

This is a simple recession tracker that I've started using with my algorithms. It uses the treasury yield curve to predict when we might be in a recession.

Clone Algorithm
38
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
from datetime import timedelta

def initialize(context):
    set_symbol_lookup_date('2005-01-01')
    set_benchmark(symbol('SPY'))
    set_universe(universe.DollarVolumeUniverse(floor_percentile=99.5, ceiling_percentile=100.0))
    fetch_csv('https://www.quandl.com/api/v3/datasets/USTREASURY/YIELD.csv?start_date=2002-01-01',
             date_column='Date',
             usecols=['3 Mo','10 Yr'],
             symbol=symbol('SPY'))
    
    context.RT = RecessionTracker()

def handle_data(context, data):
    prices = history(180, '1d', 'price')
    spy_prices = prices[symbol('SPY')].dropna()
    spy_ret = (spy_prices[-1] - spy_prices[0]) / spy_prices[0]
    record(ret=spy_ret)
    
    if context.RT.in_recession and spy_ret < -0.01:
        for order in get_open_orders():
            cancel_order(order)
        for stock in context.portfolio.positions:
            order_target_percent(stock, 0)
        order_target_percent(symbol('SPY'), -0.5)
    else:            
        for stock in data:
            if data[stock].price > data[stock].mavg(50) > data[stock].mavg(200):
                order_target_percent(stock, 1.0 / len(data))
            
    if '3 Mo' in data[symbol('SPY')] and '10 Yr' in data[symbol('SPY')]:
        val = data[symbol('SPY')]['10 Yr'] - data[symbol('SPY')]['3 Mo']
        record(val=val)
        if val < 0:
            context.RT.update(get_datetime() + timedelta(days=365))
    
class RecessionTracker():
    def __init__(self):
        self.begin_date = None
        self.end_date = None
    
    @property
    def in_recession(self):
        if self.begin_date == None or self.end_date == None:
            return False
        
        return get_datetime() >= self.begin_date and get_datetime() <= self.end_date
    
    def update(self, projection_date, window=180):
        self.begin_date = projection_date - timedelta(days=window)
        self.end_date = projection_date + timedelta(days=window)    
There was a runtime error.
3 responses

Hi. I have a few questions.
Q1. The unit of date had be capitalized when I ran the code. ex> 'Mo' -> 'MO', 'Yr' -> 'YR'
Q2. I'd appreciated if you could add some comments about the logic. For example, when 3 Mo > 10 Yr, you define recession period as 365-180 days from today to 365+180days from today. (if I understood correctly). Is it common sense? If not, can you provide any references?
Q3. Is there any reason why you added US treasury data in 'SPY', rather than defining it as a separate data?

Q4. Can that treasuries data also be used to calculate beta (then dynamically do more/less shorting to target beta zero).

The order canceling won't work there, use one of these instead.

it doesnt work... is quandl still.. supported...??