So I have been working on this algorithm and recently it has stopped working. When building and running, it only runs for approx 7 days and then freezes. I've been trying to remove the pipe.add for morningstar factors and that seems to make it run smoothly again which is why I suspect the custom factors using morningstar is the trouble here. Does anyone have the same issue or any suggestions?
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data import morningstar
import numpy as np
class DividendYield(CustomFactor):
inputs = [morningstar.valuation_ratios.dividend_yield]
window_length = 1
def compute(self, today, assets, out, dy):
out[:] = dy[-1]*100
class Debt_Ratio(CustomFactor):
inputs = [morningstar.balance_sheet.current_debt, morningstar.income_statement.total_revenue]
window_length = 1
def compute(self, today, assets, out, debt, revenue):
out[:] = 2 #debt[-1] / revenue[-1] * 100
# DollarVolume will calculate yesterday's dollar volume for each stock in the universe.
class DollarVolume(CustomFactor):
# We need close price and trade volume for this calculation.
inputs = [USEquityPricing.close, USEquityPricing.volume]
window_length = 1
# Dollar volume is volume * closing price.
def compute(self, today, assets, out, close, volume):
out[:] = (close[-1] * volume[-1])
def initialize(context):
"""
Called once at the start of the algorithm.
"""
# User defined variables
context.leverage_long = 0.5
context.leverage_short = 0.5
context.long_number = 10
context.short_number = 10
# Create and attach pipeline
pipe = Pipeline()
attach_pipeline(pipe,name='pipeline')
# Add dividend yield to pipeline
dividend_yield = DividendYield()
pipe.add(dividend_yield, 'dividend_yield')
dy_filter = (dividend_yield > 2) & (dividend_yield < 6)
# Add price to book to pipeline
debt_ratio = Debt_Ratio()
pipe.add(debt_ratio,'debt_ratio')
dr_filter = (debt_ratio > 0)
# Add price to book to pipeline
#valuation = Valuation()
#pipe.add(valuation,'valuation')
#v_filter = (valuation < 20) & (valuation > -10)
# Create the dollar_volume factor using default inputs and window_length
dollar_volume = DollarVolume()
dollar_filter = (dollar_volume > 5000000)
pipe.add(dollar_volume, 'dollar_volume')
#pipe.set_screen(dy_filter & dollar_filter & dr_filter)
pipe.set_screen(dollar_filter)
# Rebalance every day, 1 hour after market open.
schedule_function(rebalance, date_rules.month_start(), time_rules.market_open(hours=1))