Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How To Filter Stocks By Price And Percent Gain

Hi, I'm pretty new to Quantopian and have a few parameters that I'm having trouble implementing. First I would like to search the top stocks based on their percentage gain. It appears that I may be able to use Zipline for this or import an external CSV, though I don't really understand Zipline or where to get a live feed CSV. Once that's done I would like to sort through all of those stocks and exclude any that go above $20. Suggestions would be greatly appreciated

6 responses

Read about history on the Quantopian help page, and the DataFrame class and its pct_change method on the Pandas documentation page.

Hi Travis,

Welcome to Quantopian! This sounds like a job for the Pipeline API. With Pipeline, you can create custom factors such as percent returns and then rank and filter securities as you like. Take a look at this example that filters based on market cap and simple moving average.

There's also a Getting Started tutorial series to help you get familiarized with the API. The tutorials don't yet cover Pipeline but they should help you get the hang of coding an algo on Quantopian. The history function that André mentioned is covered in one of the videos!

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.

Much appreciated André and Jamie I've been looking over those answers all day. I'm starting to get a grasp on the pipeline API but don't have the functionality that I want quite yet. I have all weekend to work on my algo so ill keep everyone update with snippets and progress.

I got the screener set up to calculate percent change of the current price and yesterday's close. Problem is that I want to use pipeline to load 8000+ equities but I'm not sure how to do it. Can anybody help?

from quantopian.pipeline.data.builtin import USEquityPricing  
import pandas as pd  
import datetime

def initialize(context):  
    context.stock = sid(7408)  
    context.yesterday_high = None  


def handle_data(context, data):  
    # print yesterday high if set (non None)  
    #if context.yesterday_high:  
        #log.info("Yesterday high: %f" %  context.yesterday_high)  
    yesterdays_close = history(bar_count=2, frequency='1d', field='close_price')  
    log.info(yesterdays_close)  
    # print today high  
    todays_high = data[context.stock].open_price  
    log.info("current price: %f" % todays_high)

    # Save today high in context for tomorrow's use  
    context.yesterday_high = todays_high  
    pct_change = (todays_high - yesterdays_close) / todays_high  
    log.info(pct_change*100)  

Travis - Read the Pipeline manual. Start at the address Jamie posted, https://www.quantopian.com/help#pipeline-title .

Figured out how to sort through the entire universe and get the previous day close for every stock.
This code isn't perfect and it sorts through the stock alphabetically but it does what it's supposed to do. :D

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 SimpleMovingAverage  
from quantopian.pipeline import CustomFactor  
import numpy as np  
import pandas as pd  
import datetime  
class PriceRange(CustomFactor):  
    # define inputs for the compute method  
    inputs = [USEquityPricing.close]  
    def compute(self, today, assets, out, close):  
        out[:] = close[-1]  
def initialize(context):  
    stock_prices = PriceRange(window_length=10)  
    #attaches a name to the pipeline  
    pipe = attach_pipeline(Pipeline(), name = 'pipeline')  
    # adds the stock_prices variable to the stock_prices pipeline  
    pipe.add(stock_prices, 'pipeline')

def before_trading_start(context, data):  
    # Access results using the name passed to `attach_pipeline`.  
    results = pipeline_output('pipeline')  
    print results.head(30)

    # Define a universe with the results of a Pipeline.  
    update_universe(results.sort('pipeline').index[:10])  
def handle_data(context, data):  
    # placeholder variable  
    variable = int