Пример #1
0
def make_pipeline(context):
    """
    A function to create our pipeline (dynamic stock selector). The pipeline is
    used to rank stocks based on different factors, including builtin factors,
    or custom factors that you can define. Documentation on pipeline can be
    found here:
    https://www.quantopian.com/help#pipeline-title
    """

    # Filter for stocks in the QTradableStocksUS universe. For more detail, see this post:
    # https://www.quantopian.com/posts/working-on-our-best-universe-yet-qtradablestocksus
    universe = QTradableStocksUS()

    # Create a Returns factor with a 5-day lookback window for all
    # securities in our QTradableStocksUS Filter.
    recent_returns = Returns(window_length=context.returns_lookback,
                             mask=universe)

    # Define high and low returns filters to be the bottom 10% and top 10% of
    # securities in the high dollar-volume group.
    low_returns = recent_returns.percentile_between(0, 10)
    high_returns = recent_returns.percentile_between(90, 100)

    # Add a filter to the pipeline such that only high-return and low-return
    # securities are kept.
    securities_to_trade = (low_returns | high_returns)

    # Create a pipeline object with the defined columns and screen.
    pipe = Pipeline(columns={
        'low_returns': low_returns,
        'high_returns': high_returns,
    },
                    screen=securities_to_trade)

    return pipe
Пример #2
0
def make_pipeline(context):
    """
    A function to create our pipeline (dynamic security selector). The pipeline is used
    to rank securities based on different factors, including builtin facotrs, or custom 
    factors that you can define. Documentation on pipeline can be found here:
        https://www.quantopian.com/help#pipeline-title
    """

    # Create a pipeline object.
    pipe = Pipeline()

    # Create a dollar_volume factor using default inputs and window_length.
    # This is a builtin factor.
    dollar_volume = AverageDollarVolume(window_length=1)
    pipe.add(dollar_volume, 'dollar_volume')

    # Create a recent_returns factor with a 5-day returns lookback. This is
    # a custom factor defined below (see RecentReturns class).
    recent_returns = Returns(window_length=context.returns_lookback)
    pipe.add(recent_returns, 'recent_returns')

    # Define high dollar-volume filter to be the top 5% of securities by dollar volume.
    high_dollar_volume = dollar_volume.percentile_between(95, 100)

    # Define high and low returns filters to be the bottom 10% and top 10% of
    # securities in the high dollar-volume group.
    low_returns = recent_returns.percentile_between(0,
                                                    10,
                                                    mask=high_dollar_volume)
    high_returns = recent_returns.percentile_between(90,
                                                     100,
                                                     mask=high_dollar_volume)

    # Factors return a scalar value for each security in the entire universe
    # of securities. Here, we add the recent_returns rank factor to our pipeline
    # and we provide it with a mask such that securities that do not pass the mask
    # (i.e. do not have high dollar-volume), are not considered in the ranking.
    pipe.add(recent_returns.rank(mask=high_dollar_volume),
             'recent_returns_rank')

    # Add a filter to the pipeline such that only high-return and low-return
    # securities are kept.
    pipe.set_screen(low_returns | high_returns)

    # Add the low_returns and high_returns filters as columns to the pipeline so
    # that when we refer to securities remaining in our pipeline later, we know
    # which ones belong to which category.
    pipe.add(low_returns, 'low_returns')
    pipe.add(high_returns, 'high_returns')

    return pipe
Пример #3
0
def make_pipeline(context):
    """
    A function to create our pipeline (dynamic security selector). The pipeline is used
    to rank securities based on different factors, including builtin facotrs, or custom 
    factors that you can define. Documentation on pipeline can be found here:
        https://www.quantopian.com/help#pipeline-title
    """
    
    # Create a pipeline object. 
    pipe = Pipeline()
    
    # Create a dollar_volume factor using default inputs and window_length.
    # This is a builtin factor.
    dollar_volume = AverageDollarVolume(window_length=1)
    pipe.add(dollar_volume, 'dollar_volume')
    
    # Create a recent_returns factor with a 5-day returns lookback. This is
    # a custom factor defined below (see RecentReturns class).
    recent_returns = Returns(window_length=context.returns_lookback)
    pipe.add(recent_returns, 'recent_returns')
    
    # Define high dollar-volume filter to be the top 5% of securities by dollar volume.
    high_dollar_volume = dollar_volume.percentile_between(95, 100)
    
    # Define high and low returns filters to be the bottom 10% and top 10% of
    # securities in the high dollar-volume group.
    low_returns = recent_returns.percentile_between(0,10,mask=high_dollar_volume)
    high_returns = recent_returns.percentile_between(90,100,mask=high_dollar_volume)
    
    # Factors return a scalar value for each security in the entire universe
    # of securities. Here, we add the recent_returns rank factor to our pipeline
    # and we provide it with a mask such that securities that do not pass the mask
    # (i.e. do not have high dollar-volume), are not considered in the ranking.
    pipe.add(recent_returns.rank(mask=high_dollar_volume), 'recent_returns_rank')
    
    # Add a filter to the pipeline such that only high-return and low-return
    # securities are kept.
    pipe.set_screen(low_returns | high_returns)
    
    # Add the low_returns and high_returns filters as columns to the pipeline so
    # that when we refer to securities remaining in our pipeline later, we know
    # which ones belong to which category.
    pipe.add(low_returns, 'low_returns')
    pipe.add(high_returns, 'high_returns')
    
    return pipe
Пример #4
0
def make_pipeline(context):
    """
    A function to create our pipeline (dynamic stock selector). The pipeline is used
    to rank stocks based on different factors, including builtin factors, or custom
    factors that you can define. Documentation on pipeline can be found here:
    https://www.quantopian.com/help#pipeline-title
    """
    # Create a pipeline object.

    # Create a dollar_volume factor using default inputs and window_length.
    # This is a builtin factor.
    dollar_volume = AverageDollarVolume(window_length=1)

    # Define high dollar-volume filter to be the top 5% of stocks by dollar volume.
    high_dollar_volume = dollar_volume.percentile_between(95, 100)

    # Create a recent_returns factor with a 5-day returns lookback for all securities
    # in our high_dollar_volume Filter. This is a custom factor defined below (see
    # RecentReturns class).
    recent_returns = Returns(window_length=context.returns_lookback,
                             mask=high_dollar_volume)

    # Define high and low returns filters to be the bottom 10% and top 10% of
    # securities in the high dollar-volume group.
    low_returns = recent_returns.percentile_between(0, 10)
    high_returns = recent_returns.percentile_between(90, 100)

    # Define a column dictionary that holds all the Factors
    pipe_columns = {
        'low_returns': low_returns,
        'high_returns': high_returns,
        'recent_returns': recent_returns,
        'dollar_volume': dollar_volume
    }

    # Add a filter to the pipeline such that only high-return and low-return
    # securities are kept.
    pipe_screen = (low_returns | high_returns)

    # Create a pipeline object with the defined columns and screen.
    pipe = Pipeline(columns=pipe_columns, screen=pipe_screen)

    return pipe
Пример #5
0
def make_pipeline():

    # Base universe set to the Q500US
    universe = Q500US()

    roe = Fundamentals.roe.latest

    new_returns = Returns(window_length=5, mask=universe)

    new_returns = new_returns.zscore()
    returns_range = new_returns.percentile_between(1, 30)

    new_volatility = AnnualizedVolatility(mask=universe)

    new_volatility = new_volatility.zscore()
    volatility_range = new_volatility.percentile_between(1, 30)

    pipe = Pipeline(columns={
        'roe': roe,
        'returns': returns_range,
        'volatility': volatility_range
    },
                    screen=universe)
    return pipe
def make_pipeline(context):
    #create pipeline
    pipe = Pipeline()

    #use built in factor AverageDollarVolume to screen for liquid stocks
    avg_dollar_volume = AverageDollarVolume(window_length=1)
    pipe.add(avg_dollar_volume, 'avg_dollar_volume')

    #use built in factor Returns to get returns over the recent_lookback window
    recent_returns = Returns(window_length=context.returns_lookback)
    pipe.add(recent_returns, 'recent_returns')

    #filter out stocks in the top % of dollar volume
    high_dollar_volume = avg_dollar_volume.percentile_between(95, 100)

    #rank the recent returns in the high dollar volume group and add to pipe
    pipe.add(recent_returns.rank(mask=high_dollar_volume),
             'recent_returns_rank')

    #get stocks with the highest and lowest returns in the high dollar volume         group
    low_returns = recent_returns.percentile_between(0,
                                                    10,
                                                    mask=high_dollar_volume)

    high_returns = recent_returns.percentile_between(90,
                                                     100,
                                                     mask=high_dollar_volume)

    #add high and low returns as columns to pipeline for easier data mgmt
    pipe.add(high_returns, 'high_returns')
    pipe.add(low_returns, 'low_returns')

    ######Earnings Announcement Risk Framework#############
    # https://www.quantopian.com/data/eventvestor/earnings_calendar
    # EarningsCalendar.X is the actual date of the announcement
    # E.g. 9/12/2015
    pipe.add(EarningsCalendar.next_announcement.latest, 'next')
    pipe.add(EarningsCalendar.previous_announcement.latest, 'prev')

    # BusinessDaysX is the integer days until or after the closest
    # announcement. So if AAPL had an earnings announcement yesterday,
    # prev_earnings would be 1. If it's the day of, it will be 0.
    # For BusinessDaysUntilNextEarnings(), it is common that the value
    # is NaaN because we typically don't know the precise date of an
    # earnings announcement until about 15 days before
    ne = BusinessDaysUntilNextEarnings()
    pe = BusinessDaysSincePreviousEarnings()
    pipe.add(ne, 'next_earnings')
    pipe.add(pe, 'prev_earnings')

    # The number of days before/after an announcement that you want to
    # avoid an earnings for.
    # pipe.set_screen ( (ne.isnan() | (ne > 3)) & (pe > 3) )

    #####Set Pipe based on the desired framework######
    #screen the pipeline by the high and low returns(and high dollar volume, implicitly
    pipe.set_screen(high_returns | low_returns
                    | ((ne.isnan() | (ne > 3)) & (pe > 3)))
    # pipe.set_screen(high_returns | low_returns)

    return pipe