how do you get the average price and total volume within a certain window period, of our portfolio consisting of roughly 30 stocks? we want to use that to calculate the z score of the stocks. This is what we have so far, but there seems to be an error with the pipeline
def initialize(context):
schedule_function(my_rebalance,
date_rules.every_day,
time_rules.market_open(hours=1))
attach_pipeline(make_pipeline(), 'my_pipeline')
def make_pipeline():
universe = QTradableStocksUS()
sma_50 = Factors.SimpleMovingAverage(inputs = [USEquityPricing.close],
window_length = 50,
mask = universe)
sma_200 = Factors.SimpleMovingAverage(inputs = [USEquityPricing.close],
window_length = 200,
mask = universe)
mktcap = Fundamentals.market_cap.latest
universe &= (Fundamentals.morningstar_sector_code.latest != 103)
universe &= (Fundamentals.morningstar_sector_code.latest != 104)
universe &= (Fundamentals.morningstar_sector_code.latest != 207)
universe &= (Fundamentals.ebit.latest > 0)
universe &= (Fundamentals.enterprise_value.latest > 0)
universe &= (Fundamentals.net_ppe.latest > 0)
mktcap = Fundamentals.market_cap.latest
evebit = Fundamentals.enterprise_value.latest / Fundamentals.ebit.latest
roc = Fundamentals.ebit.latest / (Fundamentals.net_ppe.latest +
Fundamentals.working_capital.latest)
universe &= (evebit > 0)
universe &= (roc > 0)
evebit_rank = evebit.rank(ascending=True, mask=universe)
roc_rank = roc.rank(ascending=False, mask=universe)
mf = evebit_rank + roc_rank
mf_rank = mf.rank(ascending=True, mask=universe) #best first
mf_rank2 = mf.rank(ascending=False, mask=universe) #worst first
universe &= (mf_rank <= 100) | (mf_rank2 <= 100) #top 100 for respective rank
universe1 = universe
universe1 &= (mf_rank <= 100) #universe for top 100
universe2 = universe
universe2 &= (mf_rank2 <= 100 #universe for bottom 100
return Pipeline(
columns = {'sma_50': sma_50,
'sma_200': sma_200,
'mktcap': mktcap,
'evebit': evebit,
'evebit_rank': evebit_rank,
'roc': roc,
'roc_rank': roc_rank,
'mf': mf,
'mf_rank': mf_rank,
'mf_rank2': mf_rank2,
},
screen=universe,
)