Back to Community
custom factors and screening with pipe.set_screen()

Hey guys. So i'm trying to get all stocks that have a new 52 week high, have a relative volume of 1.5 (todays volume / 3 month average volume >= 1.5) & the average daily trading volume is over 200,000. I have made so custom factors to accomplish this, though I am a beginner so am not 100% sure if i have done it correctly. Anyways I am having a issue with screening the stocks with the pipe.set_screen() function. The code below (very bottom of post) will execute but will come back with no stocks. When i try the same code and just simplify what goes into the pipe.screen() as

pipe.set_screen( (todays_high == highest_high) )  

I get the error:

" TypeError: zipline.pipeline.pipeline.set_screen() expected a value of type zipline.pipeline.filters.filter.Filter for argument 'screen', but got bool instead. "

The error occurs when I set the screen. I guess i'm not sure how to properly create a filter. :-/
also when I try code below:

pipe.set_screen( (volume > 2000))  

it still returns 0 stocks. Below is my code:

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume, SimpleMovingAverage, CustomFactor  
import numpy as np  
from numpy import nanmin, nanmax

def initialize(context):  
    schedule_function(order_stocks, date_rules.every_day(), time_rules.market_close())  


    # Create our dynamic stock selector.  
    pipe = attach_pipeline(my_pipeline(context), 'my_pipeline')  


def my_pipeline(context):  
    pipe = Pipeline( (volume > 1000) )


    #get average volume for past 3 months  
    volume = DailyAverageVolume()  
    pipe.add(volume, 'average_volume')  


    #get the volume for the past day  
    today_volume = TodayVolume()  
    pipe.add(today_volume, 'today_volume')  


    # get highest high for past year  
    highest_high = HighestHigh()  
    pipe.add(highest_high, 'highest_high')  


    #get todays high  
    todays_high = TodaysHigh()  
    pipe.add(todays_high, 'todays_high')


    #screening  
    screen = ((todays_high == highest_high) & ((today_volume / volume) > 1.5) & (volume > 200000))  
    pipe.set_screen(screen)

    return pipe  


class DailyAverageVolume(CustomFactor):  
    inputs = [USEquityPricing.volume]  
    window_length = 90


    def compute(self, today, assets, out, volume):  
        out[:] = volume.mean()

class HighestHigh(CustomFactor):  
    inputs = [USEquityPricing.high]  
    window_length = 365


    def compute(self, today, assets, out, highs):  
        highest_highs = nanmax(highs, axis=0)  
        out[:] = highest_highs  

class TodaysHigh(CustomFactor):  
    inputs = [USEquityPricing.high]  
    window_length = 1


    def compute(self, today, assets, out, high):  
       out[:] = nanmax(high, axis=0)  


class TodayVolume(CustomFactor):  
    inputs = [USEquityPricing.volume]  
    window_length = 1  


    def compute(self, today, assets, out, volume):  
        out[:] = volume


def before_trading_start(context, data):  
    context.output = pipeline_output('my_pipeline')  

    # These are the securities that we are interested in trading each day.  
    context.security_list = context.output.index  


def order_stocks(context, data):  
    context.my_securities = context.output.sort('todays_high', ascending=False).iloc[:50]  
    print len(context.my_securities)  


def handle_data(context,data):  
    pass

1 response

Read Factor's description and notes, near the end.

You can get the equivalent of your TodayVolume custom factor by directly using USEquityPricing.volume.latest.

I think your DailyAverageVolume can be replaced by builtin factors as well with SimpleMovingAverage(inputs=[USEquityPricing.volume]….

edit: fix typo 'volume' in place of 'close'