Note that 'top', 'bottom', and 'percentile_between' are pipeline factor methods. They need to be applied to a factor object and then will return a pipeline filter object. Factor and filter definitions are done exactly once in an algorithm (typically in the 'initialize' method or a method called inside 'initialize').
So, the code below doesn't work
def rebalance(context, data):
mkt_cap_sorted = pipeline_output('pipeline').sort_values('mkt_cap', ascending = True)
longs = mkt_cap_sorted.tail(10)
longs = mkt_cap_sorted.top(10)
longs = mkt_cap_sorted.buttom(10)
longs = mkt_cap_sorted.percentile_between(90, 100)
First, 'mkt_cap' is a pandas dataframe (not a factor object) so it doesn't recognize the 'top', 'bottom', and 'percentile_between' methods. Second, these need to be defined before the 'pipeline_output' is called (typically in 'initialize') .
Put these methods in the pipeline definition. Something like this.
def pipeline(context):
universe = Filters.QTradableStocksUS()
mkt_cap = Factors.MarketCap(mask = universe)
top_10 = mkt_cap.top(10)
bottom_10 = mkt_cap_sorted.bottom(10)
between_90_100 = mkt_cap.percentile_between(90, 100)
pipe = Pipeline(columns = {
'mkt_cap' : mkt_cap,
'top_10' : top_10,
'bottom_10' : bottom_10,
'between_90_100' : between_90_100,
})
return pipe
To then use these when getting the pipeline output (which is usually done in the 'before_trading_start' method), do something like this
def before_trading_start(context, data):
output = pipeline_output('pipeline')
top_10_list = output.query('top_10').index.tolist()
bottom_10_list = output.query('bottom_10').index.tolist()
between_90_100_list = output.query('between_90_100').index.tolist()
Now, one can also use plain old pandas dataframe methods (instead of defining pipeline filters) to get the top and bottom by using the 'nlargest' and 'nsmallest' methods (pandas doesn't have a neat alternative to percent_between)
def before_trading_start(context, data):
output = pipeline_output('pipeline')
top_10_list = output.nlargest(10, 'mkt_cap').index.tolist()
bottom_10_list = output.nsmallest(10, 'mkt_cap').index.tolist()
Hope that helps...