Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Runtime error from Pipeline

I have the following code I am trying to run:

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 Returns  
from quantopian.pipeline.factors import SimpleMovingAverage  
from quantopian.pipeline.filters.morningstar import Q1500US  
import pandas as pd  
import numpy as np  
def initialize(context):  
    schedule_function(buy_open, date_rules.every_day(),time_rules.market_open(minutes=10))  
    schedule_function(sell_close, date_rules.every_day(),time_rules.market_close(minutes=10))  
    my_pipe = make_pipeline()  
    attach_pipeline(my_pipe, name = 'my_pipeline')  
def make_pipeline():  
    base_universe = Q1500US()

    std90 = np.std(Returns(window_length=90))  
    yday_low = USEquityPricing.low.latest  
    current_open = USEquityPricing.open.latest  
    lowret = (current_open-yday_low)/yday_low  
    gapdown_filter = lowret<std90  
    mean_close_20 = SimpleMovingAverage(inputs=[USEquityPricing.close],window_length = 20)  
    narrowing_filter = current_price>mean_close_20  
    comp_filter = gapdown_filter and narrowing_filter  
    smallest = lowret.bottom(10)  
    total_filter = smallest and comp_filter  
    return Pipeline(screen = base_universe and total_filter)

def buy_open(context,data):  
    for s in context.security_list:  
        order_target_percent(s,0.1)  
def sell_close(context,data):  
    for s in context.portofolio.positions:  
        order_target_percent(s,0)  
def before_trading_start(context, data):  
    context.output = pipeline_output('my_pipeline')  
    context.security_list = context.output.index  

And I get a runtime error "ValueError: setting an array element with a sequence." on the line "my_pipe = make_pipeline()". I tried searching through the documentation and everything to see what I'm doing wrong but this seems like the way to use a Pipeline so I am completely lost as to what is not working. Any help would be appreciated.

2 responses

Can't be too certain without attaching the actual algorithm, however, one issue is the statement

std90 = np.std(Returns(window_length=90))

When making a pipeline, one is simply 'defining' it. One is defining what data and any associated calculations the pipeline is to retrieve at run time. The definition can be combinations of factors, filters, and classifiers. These can be combined and manipulated BUT only with operators and methods that are defined for the factor, filter, and classifier objects. One cannot apply arbitrary functions or operations to these objects.

So, no can do the 'np.std' function on a factor. You could write a small custom factor, or use the built in factor for 'ExponentialWeightedMovingStdDev' (set the decay to 1.0 to make it linear), or maybe, since it looks like you are just checking volatility, perhaps use 'AnnualizedVolatility'.

That is the actual algorithm and I was under the impression that numpy methods would work on factors but I guess I was wrong. I will try your suggestions and come back afterwards.