Back to Community
Pipeline Tutorial Help

I started learning how to use pipeline yesterday but I keep running into the same problem. I always get this:

KeyError: 'tradeable_securities'  
There was a runtime error on line 63.  

My understanding is that this means there are no securities in that dictionary. I feel like at least one of the 1,500 stocks should pass the filter I've created. I think I followed the pipeline tutorial pretty closely but my algorithm doesn't work and theirs does. Can anyone point out what I'm doing wrong?

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume, SimpleMovingAverage  
from quantopian.pipeline.filters.morningstar import Q1500US  
def initialize(context):  
    schedule_function(my_rebalance, date_rules.week_start(), time_rules.market_open(hours=1))  
    my_pipeline = make_pipeline()  
    attach_pipeline(make_pipeline(), 'my_pipeline')  
def make_pipeline():  
    base_universe = Q1500US()  
        # Factors  
    meanHigh_A = SimpleMovingAverage(inputs=[USEquityPricing.high], window_length=9, mask=base_universe)  
    meanLow_A = SimpleMovingAverage(inputs=[USEquityPricing.low], window_length=9, mask=base_universe)  
    meanHigh_B = SimpleMovingAverage(inputs=[USEquityPricing.high],window_length=26, mask=base_universe)  
    meanLow_B = SimpleMovingAverage(inputs=[USEquityPricing.low], window_length=26, mask=base_universe)  
    meanHigh_C = SimpleMovingAverage(inputs=[USEquityPricing.high], window_length=52, mask=base_universe)  
    meanLow_C = SimpleMovingAverage(inputs=[USEquityPricing.low], window_length=52, mask=base_universe)  
    tenkanSen = (meanHigh_A - meanLow_A) / 2  
    kijunSen = (meanHigh_B - meanLow_B) / 2  
    senkouSpanA = (tenkanSen + kijunSen) / 2  
    senkouSpanB = (meanHigh_C - meanLow_C) / 2  
    current_price = SimpleMovingAverage(inputs=[], window_length=2, mask=base_universe)  
        # Filters  
    price_over_green_cloud = current_price > senkouSpanA > senkouSpanB  
    price_over_red_cloud = current_price > senkouSpanB > senkouSpanA  
    uptrend_cloud = senkouSpanA > senkouSpanB  
    price_over_base = current_price > kijunSen  
    base_over_conversion = tenkanSen > kijunSen  
    tradeable_securities = (  
        (price_over_green_cloud | price_over_red_cloud)  
        & uptrend_cloud  
        & price_over_base  
        & base_over_conversion  
        # Pipeline Settings  
    return Pipeline(  
def before_trading_start(context, data):  
    context.output = pipeline_output('my_pipeline')  
    context.weight = my_compute_weights(context, data)  
def my_compute_weights(context, data):  
    weight = 1/len(context.tradeable_securities)  
    return weight  
def my_rebalance(context,data):  
    for security in context.portfolio.positions:  
        if security not in context.tradeable_securities and data.can_trade(security):  
            order_target_percent(security, 0)

    for security in context.tradeable_securities:  
        if data.can_trade(security):  
            order_target_percent(security, context.weight)  
2 responses

Welcome to Quantopian!

in general when you're looking for debugging help, it's easier if you attach a backtest like I have done here. It makes the problem easy to reproduce and removes the fidgety formatting stuff.

The first problem was that you hadn't defined tradeable_securities on context. I added a new line 51
context.tradeable_securities = context.output.index

The second problem is in lines 31 and 32 - you need to articulate the different conditions, you can't just chain them like that.

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
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
# Backtest ID: 58e292e5e4cb9b20a40b4f02
There was a runtime error.

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.

Thank you very much Dan. I'm loving Quantopian so far!