Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Fundamental Data on Portfolio stocks

For two days now I have been trying to figure out how to use Fundamental Data on my Portfolio Stocks. I give up and could use some help.

Here are the things I have tried:
1) Storing the CustomFactor in the Pipeline and referencing when I got to sell with context.output.loc[stock, 'custom_factor']. The problem with this is the stock in my portfolio sometimes falls off my universe.

2) I tried taking off all filters on my pipeline so that the stock doesn't fall off, it still falls off using my universe which is default_us_equity_universe_mask(). My guess is stocks can just fall off this universe too.

3) I tried making sure the portfolio is in the pipeline with a assets = filters.StaticAssets(context.portfolio.positions) then ORing the assets with my universe. This didn't work.

4) I tried creating a second pipeline with just StaticAssets in it. Quantopian only allows one pipeline.

5) I googled everything I could think of.

I just want to ask a few fundamental questions about the stock before I sell it, seems simple but I don't know how to make sure it works. I am sure I am missing something simple.

5 responses

A few suggestions... First, please include a backtest. It makes it easier for others to troubleshoot.

1) this should work

2) you didn't take ALL the filters off since you use a mask. Eliminate that too.

3) correct. this won't work

4) correct, only one pipeline

If you want to do some logic on existing holdings using pipe data, then you need to ensure your holdings are in the pipe. If you have a fixed static set of holdings, that can be done with the 'StaticAsset' filter. Otherwise, if there is the potential to hold ANY security then simply have the pipe return ALL securities. Don't use any masks or screens.

Dan,

Thank you for your reply. I was afraid you were going to say that. I really think something is fundamentally wrong with the API if this is the answer. I spent a lot of time working on filtering my pipeline to find the stocks I want to BUY, and it works great. Now in order to determine what I want to sell, I have to completely destroy that solution, in order to write could to determine when to SELL. I mean what is the point of filtering at all if what your doing is limiting the data you can access. There really should be a way to ask fundamental questions at any point, or allow multiple pipelines for different reasons.

So I found a way to do this. Its basically the 2 pipeline method, but for the second pipeline just use get_fundementals...

def before_trading_start(context, data):  
    context.output = pipeline_output('pipeline')  
    sids = []  
    for stock in context.portfolio.positions:  
        sids.append(stock.sid)  
    if sids:  
        context.portfolio_fd = get_fundamentals(  
            query(  
                fundamentals.company_reference.sid,  
                fundamentals.operation_ratios.roa  
            )  
            .filter(fundamentals.company_reference.sid.in_(sids))  
        )  

@Jacob, you posited the question "...what is the point of filtering at all if what your doing is limiting the data you can access.".

It took me awhile to wrap my head around the pipeline paradigm. I feel some of the tutorials stress the filtering ability too much. How I approach ALL of my pipeline algorithms now is to think of the pipeline exclusively as the method for fetching data and putting it into one very nice 2D dataframe. I don't do any logic in the pipeline (eg don't expect it to determine what to buy or sell). I do ALL that logic in code. I simply collect any needed data into the pipeline output, THEN use that data for any logic.

Think of the pipeline definition as defining the rows and columns of a spreadsheet. The rows are securities. The columns are any pieces of data you want associated with those securities. Determine what securities and what data you will need in your algorithm and define the pipeline accordingly. The caveat is that these are fixed. You can't change the columns, and, unless you set a screen you can't change the rows. The "pipeline_output" simply returns this "spreadsheet" in the form of a Pandas dataframe updated every day with fresh data. Your logic can then use that data any way you wish. I wouldn't start thinking of "multiple pipelines". Its just a single "spreadsheet" or "database" of data your algo will be using.

Take a look at this post for why pipeline was developed https://www.quantopian.com/posts/need-help-with-pipeline-just-a-beginner .

I've attached a simple algo showing how I use the pipeline output for both buys and sells. Notice a few things.

  • after I get the pipeline output I then add any other data I'll be using to that same dataframe (in this case "days_held"). I store ALL my data in the one dataframe for easy access.
  • use the "query" method for easy intuitive filtering of the pipeline data
  • use the "get_value" and "set_value" methods for an easy intuitive ways to get and set dataframe data
  • I don't do any logic in the pipeline and don't set a screen. All the logic is done in the algo.

This is just my approach but I have found it very intuitive separating the data (ie the pipeline definition and output) from the logic (ie the algo code).

Dan,

I understand what your getting at. However, here is where it gets tricky. I want to start with a "Tradable" universe. Apparently lots of people do which is why they created Q500US() and Q1500US(), heck even you use them in your code. But what happens when you go to sell something in your portfolio that you have held on to and its fallen off Q500US(). This is what was happening to me, and when I asked for my pipeline factors I was getting an exception.

Yes I agree you can move all the filters out of Q500US() into your buy code, but that is a seriously annoying thing to do and defeats all the benefits of creating the universe in the first place.

One can argue as soon as a stock falls out of your universe you should sell. Okay but maybe I think the market was crazy and the fundamentals still look good to me, maybe I should check the fundamentals oh wait its no longer in my universe! You see the problem, really I just want anything I own to remain in my universe. Regardless I found a way to do that, and guess what it didn't improve my algo :)