Adding Unemployment as a Filter before Momentum Indicators

Got the idea from here and here and am trying to implement it on QC. Currently have the FRED Civilian Unemployment rate as filter and used the Upbias Tactical Switch Strategy as the base. 

I've gotten the algorithm to work on my desktop IDE, but I'm not sure how to properly import the data to QC. I've currently got it as a csv, but you could just as well get it right from Quandl. I've read the posts on the university for importing custom data, but I'm not following them. Any tips or instruction on this would be much appreciated!


Update Backtest

import numpy as np
import pandas as pd
from datetime import datetime

### <summary>
### Upbias Tactical Switch Strategy - a simple strategy that can beat the stock market.
### detailed explanations at
### </summary>
class UpbiasTacticalSwitch(QCAlgorithm):

def __init__(self):
self.previous = None
self._sma = None
self.position = None
self.lastMonth = -1

def Initialize(self):
self.SetStartDate(2005,01,03) #Set Start Date
self.SetEndDate(2017,12,29) #Set End Date
self.SetCash(100000) #Set Strategy Cash
self.AddSecurity(SecurityType.Equity, "SPY", Resolution.Daily)
self.AddSecurity(SecurityType.Equity, "IEF", Resolution.Daily)

self._sma = self.SMA("SPY", 200, Resolution.Daily)

def OnData(self, data):

# wait for the sma to fully initialize
if not self._sma.IsReady:

if not data.ContainsKey("SPY"):

if self.lastMonth == self.Time.month:

# if statement to go here for the unemployment rate and moving average before entering into the
# the moving average area

# doesn't look like we can do the add data with fred data

fred_unemployment = pd.read_csv("C:\Users\canaccord2\Desktop\Python Courses\Finance & Algo Trading\TEST")

signals = pd.DataFrame(index=fred_unemployment.index)

signals["signal"] = 0
signals["Unemployment_Rate"] = fred_unemployment["Value"]
signals["12 MMA"] = fred_unemployment["Value"].rolling(window=12, min_periods=1, center=False).mean()

# if unemployment rate is below its 12 mma (positive signal)
signals["signal"][12:] = np.where(signals["Unemployment_Rate"][12:] < signals["12 MMA"][12:], 1.0, 0.0)

if signals == 1:
# if we don't have a position set it to SPY - and if we already do keep holdings
if self.position == None or self.position == "SPY":
self.SetHoldings("SPY", 1)

# if unemployment is above 12 mma (bad signal)
if signals == 0:

if data["SPY"].Close > self._sma.Current.Value:
if self.position == None:
self.SetHoldings("SPY", 1)
if self.position == "IEF":
self.SetHoldings("SPY", 1)
self.position = "SPY"
if self.position == None:
self.SetHoldings("IEF", 1)
if self.position == "SPY":
self.SetHoldings("IEF", 1)
self.position = "IEF"

self.lastMonth = self.Time.month

Update Backtest


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This discussion is closed