Back to Community
Q: How do you filter Pipeline output by SID/Ticker?

I am trying to buy and sell SPY using simple moving average (sma) triggers. However, the context.sma_short and context.sma_long vectors being created with the pipeline data include a large number of securities. Could someone tell me how to simply select the sma for SPY? Would it involve selecting the row for SPY out of the "results" data frame that is created? Is there a way to filter the Pipeline output by SID/Ticker? I have seen Pipeline output filtered by other factors such as market cap before using pipe.set_screen().

Thank you! :)

import numpy as np
import pandas as pd
from scipy import stats
from pytz import timezone
import datetime
import math
import time
import functools
import random
import itertools
from statsmodels.stats.moment_helpers import cov2corr, corr2cov, se_cov
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import SimpleMovingAverage, Latest, EWMSTD, EWMA
from quantopian.pipeline import CustomFactor

def initialize(context):

context.stock=symbol('SPY')  

context.buy_flag=False  
context.sell_flag=False  

pipe = Pipeline()  
pipe = attach_pipeline(pipe, name='pipeline')  
context.sma_short= SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=50)  
pipe.add(context.sma_short, "sma_short")  
context.sma_long= SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=100)  
pipe.add(context.sma_long, "sma_long")  

schedule_function(trading, date_rule=date_rules.every_day(),            time_rule=time_rules.market_open(hours=1))  

def before_trading_start(context,data):

results = pipeline_output('pipeline')  
print results.head(5)  

if context.sma_short > context.sma_long and context.portfolio.positions_value == 0:  
    context.buy_flag=True  
else:  
    context.buy_flag=False  
if context.sma_short < context.sma_long and context.portfolio.positions_value>0:  
    context.sell_flag=True  
else:  
    context.sell_flag=False

def trading(context, data):
if context.buy_flag == True:
order_value(context.stock, context.portfolio.cash)
if context.sell_flag == True:
order_target(context.stock, 0)

def handle_data(context, data):
record(positions=context.portfolio.positions_value,
cash=context.portfolio.cash,
buy_flag=context.buy_flag,
sell_flag=context.sell_flag)

5 responses

You can add a filter by the primary symbol:

.filter(fundamentals.company_reference.primary_symbol == 'SPY')

Or you can filter by the sid:

.filter(fundamentals.valuation.sid == 8554)
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.

EDIT: I misread your question, I realize now you're asking about adding this filter in pipeline. You can do this using a screen: https://www.quantopian.com/help/#quantopian_pipeline_Pipeline

Hi Alisa,

Thanks for your response. I was able to successfully set up a pipeline screen for market cap; this is commented out in the code below. However, when I tried to modify this to filter based on primary symbol I got the following error message: "Runtime exception: NotPipelineCompatible: 0079 company_reference.primary_symbol is not currently available in the Pipeline API". Do you have any recommendation on how to filter based on symbol/sid using a pipeline screen?

Thanks!

import numpy as np
import pandas as pd
from scipy import stats
from pytz import timezone
import datetime
import math
import time
import functools
import random
import itertools
from statsmodels.stats.moment_helpers import cov2corr, corr2cov, se_cov
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import SimpleMovingAverage, Latest, EWMSTD, EWMA
from quantopian.pipeline import CustomFactor

class MarketCap(CustomFactor):

#inputs = [morningstar.valuation.market_cap]  
#window_length = 1

#def compute(self, today, assets, out, market_cap):  
    #out[:] = market_cap  

class Primary_symbol(CustomFactor):

inputs = [morningstar.company_reference.primary_symbol]  
window_length = 1

def compute(self, today, assets, out, primary_symbol):  
    out[:] = primary_symbol  

def initialize(context):

context.stock=symbol('SPY')  

context.buy_flag=False  
context.sell_flag=False  

pipe = Pipeline()  
pipe = attach_pipeline(pipe, name='pipeline')  
context.sma_short= SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=50)  
pipe.add(context.sma_short, "sma_short")  
context.sma_long= SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=100)  
pipe.add(context.sma_long, "sma_long")  

#pipe.add(MarketCap(), "Market_Cap")  
pipe.add(Primary_symbol(), "Primary_symbol")  

#market_cap = morningstar.valuation.market_cap.latest  
primary_symbol=morningstar.company_reference.primary_symbol.latest  

#market_cap_filter = (market_cap > 200000000000)  
name_filter = (primary_symbol == 'SPY')  
#pipe.set_screen(market_cap_filter)  
pipe.set_screen(name_filter)  

schedule_function(trading, date_rule=date_rules.every_day(),            time_rule=time_rules.market_open(hours=1))  

def before_trading_start(context,data):

results = pipeline_output('pipeline')  
print results.head(5)  

if context.sma_short > context.sma_long and context.portfolio.positions_value == 0:  
    context.buy_flag=True  
else:  
    context.buy_flag=False  
if context.sma_short < context.sma_long and context.portfolio.positions_value>0:  
    context.sell_flag=True  
else:  
    context.sell_flag=False

def trading(context, data):
if context.buy_flag == True:
order_value(context.stock, context.portfolio.cash)
if context.sell_flag == True:
order_target(context.stock, 0)

def handle_data(context, data):
record(positions=context.portfolio.positions_value,
cash=context.portfolio.cash,
buy_flag=context.buy_flag,
sell_flag=context.sell_flag)

Tommy,
Attached is an example backtest that filters the pipeline output for a single stock.

KR

Clone Algorithm
58
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import SimpleMovingAverage


# Create custom factor subclass to calculate a market cap based on yesterday's
# close
class MarketCap(CustomFactor):

    # Pre-declare inputs and window_length
    inputs = [morningstar.valuation.market_cap]
    window_length = 1

    # Compute market cap value
    def compute(self, today, assets, out, mkt_cap):
        out[:] = mkt_cap[-1]


def initialize(context):

    pipe = Pipeline()
    attach_pipeline(pipe, 'example')

    # Construct the custom factor
    mkt_cap = MarketCap()
    pipe.add(mkt_cap, 'mkt_cap')

    # Create and apply a filter representing the top 500 equities by MarketCap
    mkt_cap_top_500 = mkt_cap.top(500)

    # Use multiple screens to narrow the universe
    pipe.set_screen(mkt_cap_top_500)


def before_trading_start(context, data):
    # get the output of your pipeline
    context.output = pipeline_output('example')

    # index into it searching for just the security you are looking for
    aapl = context.output.loc[symbols('AAPL')]
    log.info(aapl)

    update_universe(context.output.index)
    
def handle_data(context, data):
    pass
There was a runtime error.
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.

Hey Karen,

Thanks! That worked perfectly. Would you be able to take a look at my latest post (same code, new problem):

https://www.quantopian.com/posts/having-trouble-creating-trading-signals-with-pipeline