Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Top Volume within a Price Range

First I would like to apologize if this is an easy/stupid question. I am a beginner level programmer and would like to know if there's a way to set the trading universe to look for top volume within a given price range.

So far my code begins as follows:

set_universe(universe.DollarVolumeUniverse(floor_percentile=99.9, ceiling_percentile=100))

Is there a way to add on to that to only look for tickers within a given price range? Maybe another universe.function?

Any help and suggestions are greatly appreciated!

3 responses

Hi Kit,

Thanks for posting. Quantopian is moving on from using set_universe to narrow down a universe of equities; a much more efficient and flexible tool to do the same job is Pipeline. I would recommend reading through Pipeline's section of the help page for an introduction. Once you're through that, you'll be able to narrow down your trading universe as you suggested on this post by creating two filters, one using AverageDollarVolume and the other by comparing USEquityPricing.close.latest with two thresholds of your choice. After that, just use set_screen with the two filters anded together (& operator).

Please ask if you have any more questions.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Nathan,

Thank you for the reply! I've been looking at the Pipeline documentation and played around with the coding for a bit but can't seem to get output I want. I'm confused as to how to use the USEquityPricing.close.latest and comparing to which 2 thresholds. Here is my code so far:

from quantopian.pipeline import Pipeline
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume

def initialize(context):
pipe = Pipeline()
attach_pipeline(pipe, 'my_pipeline')

# Create a dollar volume factor.  
dollar_volume = AverageDollarVolume(window_length=1)  

# Pick the top 1% of stocks ranked by dollar volume.  
high_dollar_volume = dollar_volume.percentile_between(99, 100)  
pipe.add(high_dollar_volume, 'high_dollar_volume')  

# Construct a Filter.  
prices_under_5 = (high_dollar_volume < 5)  
pipe.set_screen(high_dollar_volume & prices_under_5)  

def before_trading_start(context, data):
output = pipeline_output('my_pipeline')
# These are the securities that we are interested in trading each day.
context.my_universe = output.sort('high_dollar_volume', ascending=False).iloc[:5]

# Define a universe with the results of a Pipeline.  
context.assets = context.my_universe.index

def handle_data(context, data):
log.info('\n'+str(context.my_universe.head()))

Please let me know what I've used wrongly and what correction I need to make. Or if there are video resources that are up to date that I can watch and further my learning. And again thank you for your help!!

i've written something that might work for you:

def my_pipeline(context):  
    print("pipe")  
    """  
    A function to create our dynamic stock selector (pipeline). Documentation on  
    pipeline can be found here: https://www.quantopian.com/help#pipeline-title  
    """  
    pipe = Pipeline()  
    # Create a dollar volume factor.  
    dollar_volume = AverageDollarVolume(window_length=1)  
    pipe.add(dollar_volume, 'dollar_volume')  
    high_dollar_volume = dollar_volume.percentile_between(60, 100)  
    price = Latest(inputs=[USEquityPricing.close])  
    #price = USEquityPricing.close(window_length=1)  
    lower_screen = (price>15.0)  
    upper_screen = (price<99.0)  
    pipe.add(price, "price")  
    pipe.set_screen(high_dollar_volume&upper_screen&lower_screen)  
    return pipe