Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calling stocks from pipeline output

I want to get the results from my pipeline, and run a calculation on those stocks.

I have a calculation-intensive function that I don't want running every day, so I don't want to include it in the pipeline as a screen. Instead I want to schedule this function to run monthly.

The problem is, I'm having trouble referencing the stocks that my pipeline outputs.

I've got a for loop in my handle_data() that calls the function

for stock in context.output:  
    function(stock)  

where in before_trading_start()
context.output= pipeline_output('stock_screen')

But it doesn't seem to be putting any securities into my function...

9 responses

I'm not stupid, I swear. Just clueless. Someone help a brother out.

I believe you have to do the pipeline output.

Take a look of the example code here.

https://www.quantopian.com/posts/dollar-volume-pipeline

I'm just getting started on this platform so I'm no expert.

Well I have the pipeline output already, that's what the

context.output = pipeline_output('stock_screen')

line is. What I want is a way to take each stock that context.output spits out, and to plug each one individually into a function.

I'm posting a bunch of 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 CustomFactor, SimpleMovingAverage

import pandas as pd  
import numpy as np  
from scipy import stats

#========================================================================================  
#GLOBAL VARIABLES

days = 30


#========================================================================================  
#PIPELINE CUSTOM FACTORS

class AvgDailyDollarVolumeTraded(CustomFactor):  
    inputs = [USEquityPricing.close, USEquityPricing.volume]  
    window_length = days  
    def compute(self, today, assets, out, close_price, volume):  
        out[:] = np.mean(close_price * volume, axis=0)  
#=========================================================================================  
#MAIN PROGRAM FUNCTIONS

#initialization function  
def initialize(context):  
    pipe = Pipeline()  
    pipe = attach_pipeline(pipe, name='stock_screen')

    #create filters  
    sma_200 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=200)  
    dollar_volume = AvgDailyDollarVolumeTraded()  
    #screen out penny stocks and small stocks  
    pipe.set_screen((sma_200 > 5) & (dollar_volume > 10**7))  
    context.spy = sid(8554)  
    schedule_function(rebalance, date_rules.month_start())  
#.........................................................................................

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

#    context.my_universe = [] #put your beta list here  
#    update_universe(context.my_universe)



# .......................................................................................  
#main function  
def handle_data(context, data):  
    for stock in context.output:  
        function(stock)

def function():  
    #function things go here, blah blah blah, secret sauce, etc.  

But it doesn't seem to work. Or it gives me a runtime error. I also tried using context.output.index instead, that didn't work either. So there's probably something really obvious that I'm doing wrong, so if someone could please enlighten me that would be wonderful.

If you use context.output.index, it should work -- please see attached backtest. In addition to that change, I commented out schedule_function (because no rebalance function was defined) and added logging of stock symbols just for verification.

Thanks a bunch Michael! That's exactly what I needed.

Hey Michael,

What if we want to loop through all the stocks, but we also have extra columns in our data frame from Pipeline Factors. So for example, we've added in a factor that tells us the Beta of each stock in our universe, and while we're looping through the stocks, we'd like to access each stock's beta value. How can we do something like that?

Thanks for your help,
Thomas

Total newbie here and to Python. In this case, where is the class defined so that you know you can extract the data: "stock.symbol" from the pipeline_output function return value? I can't find where any of this is defined in the Quantopian reference other than the return value is a "pandas.DataFrame". There's more to the dictionary objects down within that I'm looking for the concise definitions of. For example, there is a section in the documentation that tells you the columns for USEquityPricing. I'm looking for something similar for the default pipeline creation. Thanks.

@Mikan

Specifically, regarding what is returned when calling the 'pipeline_output' function, it is indeed just a basic old pandas dataframe (though that's a pretty powerful object). The documentation is correct (see https://www.quantopian.com/help#using-results). The columns are defined in the pipeline definition. A column will be one of three data types 1) real 2) boolean 3) categorical. These are the outputs from the factors, filters, and classifiers respectively which were added in the pipeline definition.

The index of the dataframe are the asset objects (ie the equities or stocks). It's not a column per se but one can get at it using the various dataframe methods. See https://www.quantopian.com/help#api-sidinfo for info on the '.symbol' property of this object. There are some other interesting properties listed there as well.

If you want to find out more about actual code and class definitions then check out the following links.
http://www.zipline.io/beginner-tutorial.html
https://github.com/quantopian/zipline

Quantopian is based on the open sourced 'zipline' library. You shouldn't really need to dig into the library code but sometimes it's interesting. For example, here is where the asset class is defined https://github.com/quantopian/zipline/blob/master/zipline/assets/_assets.pyx.

I find it very helpful to use the IDE dubugger (https://www.quantopian.com/help#debugger) and set breakpoints. Once the algo pauses one can enter the 'type' method to find out the type of any variable/object in your code (eg enter 'type(my_variable') in the command line). This takes a lot of the mystery out of what's happening.

Hope that helps.