# import pipeline stuff
from quantopian.pipeline import CustomFactor, Pipeline
from quantopian.research import run_pipeline
# import built in factors and filters
import quantopian.pipeline.factors as Factors
import quantopian.pipeline.filters as Filters
import quantopian.pipeline.classifiers as Classifiers
# import data
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
# import these just in case we need them
import numpy as np
import pandas as pd
import scipy as scp
Below is the original code. I'd use the built in 'make_us_equity_universe' filter unless there is some reason not to. See https://www.quantopian.com/help#built-in-filters
# Filter for primary share equities. IsPrimaryShare is a built-in filter.
primary_share = Filters.morningstar.IsPrimaryShare()
# Equities listed as common stock (as opposed to, say, preferred stock).
# 'ST00000001' indicates common stock.
common_stock = morningstar.share_class_reference.security_type.latest.eq('ST00000001')
# Non-depositary receipts. Recall that the ~ operator inverts filters,
# turning Trues into Falses and vice versa
not_depositary = ~morningstar.share_class_reference.is_depositary_receipt.latest
# Equities not trading over-the-counter.
not_otc = ~morningstar.share_class_reference.exchange_id.latest.startswith('OTC')
# Not when-issued equities.
not_wi = ~morningstar.share_class_reference.symbol.latest.endswith('.WI')
# Equities without LP in their name, .matches does a match using a regular
# expression
not_lp_name = ~morningstar.company_reference.standard_name.latest.matches('.* L[. ]?P.?$')
# Equities with a null value in the limited_partnership Morningstar
# fundamental field.
not_lp_balance_sheet = morningstar.balance_sheet.limited_partnership.latest.isnull()
# Equities whose most recent Morningstar market cap is not null have
# fundamental data and therefore are not ETFs.
have_market_cap = morningstar.valuation.market_cap.latest.notnull()
# Filter for stocks that pass all of our previous filters.
tradeable_stocks = (
primary_share
& common_stock
& not_depositary
& not_otc
& not_wi
& not_lp_name
& not_lp_balance_sheet
& have_market_cap
)
original_universe = Factors.AverageDollarVolume(window_length=30, mask=tradeable_stocks).percentile_between(98, 100)
Let's make a universe of approximately 2% of the most liquid securities using 'make_us_equity_universe'. There's roughly 3000 tradable securities so set target size to 60. 'max_group_weight' is set to 30% to ensure no more than a 30% weighting in any one sector. This can be ommited if desired.
new_universe = Filters.make_us_equity_universe(
target_size = 60,
rankby = Factors.AverageDollarVolume(window_length=200),
mask = Filters.default_us_equity_universe_mask(),
groupby = Classifiers.morningstar.Sector(),
max_group_weight = 0.3,
smoothing_func = lambda f: f.downsample('month_start'),
)
# a little faster implementation of log returns
class Log_Returns(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 2
def compute(self, today, asset_ids, out, close_prices):
out[:] = np.nan_to_num(np.diff(np.log(close_prices), axis=0))
# Wouldn't bother using Log_Returns as an input. Just do the math here.
class Distribution_Estimation(CustomFactor):
inputs = [USEquityPricing.close]
outputs = ['mu','gam','H','GIGparam'] #Mu, Gam is a Nx1 Vector, H is a NxN Matrix and GIGparam is a 3x1
window_length = 2
def specified_function(log_returns, type_model_distribution, mu, gam, H, GIGparam):
# do whatever logic is needed
return mu, gam, H, GIGparam
def compute(self, today, asset_ids, out, close_prices):
# Mu, gam, H, GIGparam
mu = []; gam = []; H = []; GIGparam = [];
# below will replace any NaNs with zeros. Maybe this isn't desired?
log_returns = np.nan_to_num(np.diff(np.log(close_prices), axis=0))
type_model_distribution = 'Distribution';
mu, gam, H, GIGparam = specified_function(log_returns, type_model_distribution, mu, gam, H, GIGparam)
out.mu = mu; out.gam = gam; out.H = H; out.GIGparam = GIGparam
def make_pipeline():
window = 2
#log Returns are preferred
log_returns = Log_Returns(window_length = window, mask = new_universe)
#regular Returns
returns = Factors.Returns(inputs = [USEquityPricing.close], window_length = window, mask = new_universe)
output = Distribution_Estimation(mask = new_universe)
return Pipeline(
columns = {
'log_returns': log_returns,
},
screen = new_universe
)
result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-06')
result