I calculate the "Earning Yield" factor but I cannot figure out how to check for NaN/Inf values
import pandas as pd
import numpy as np
import math
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import SimpleMovingAverage
# Create custom factor subclass to calculate a market cap based on yesterday's
# close
class EarningYield(CustomFactor):
# Pre-declare inputs and window_length
inputs = [morningstar.valuation.enterprise_value, morningstar.income_statement.ebit]
window_length = 1
# Compute market cap value
def compute(self, today, assets, out, ev, ebit):
out[:] = ev[-1] / ebit[-1]
# Put any initialization logic here. The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
pipe = Pipeline()
attach_pipeline(pipe, name='my_pipeline')
# Construct the custom factor
earning_yield = EarningYield()
pipe.add(earning_yield, 'earning_yield')
remove_nan_inf = np.isfinite(earning_yield)
# Use multiple screens to narrow the universe
pipe.set_screen(remove_nan_inf)
def before_trading_start(context, data):
# Access results using the name passed to `attach_pipeline`.
results = pipeline_output('my_pipeline')
print results.sort('earning_yield', ascending=False, na_position='last').head(100)
# Define a universe with the results of a Pipeline.
# Take the first ten assets by 30-day SMA.
update_universe(results.sort('earning_yield').index[:10])