Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
StaticAssets Error

Every time I try to use StaticAssets it returns the following error:

TypeError: MaximizeAlpha() expected a value with dtype 'float64' or 'int64' for argument 'alphas', but got 'object' instead.
There was a runtime error on line 100.

I've tried using universe=Filters.StaticAssets(context.stock) but that returns a key error 'stock'. How would I go about fixing this.

def make_pipeline(context):  
    """  
    A function to create our dynamic stock selector (pipeline). Documentation  
    on pipeline can be found here:  
    https://www.quantopian.com/help#pipeline-title  
    """  
    my_etfs = (StaticAssets(symbols('IVV','EFA','AGG','IJH','IWM','IWD','IWF','LQD','EEM','EZU',  
)))
    #first factor + winsorizing to prevent outliers  
    fac = Fundamentals.equity_per_share_growth.latest  
    fac_wins = fac.winsorize(min_percentile=0.05, max_percentile=0.95)

    #putting it into z-score form  
    finalfactor = fac_wins.zscore()  
    #deciding shorts and longs  
    longs = finalfactor.top(TOTAL_POSITIONS//2, mask=my_etfs)  
    shorts = finalfactor.bottom(TOTAL_POSITIONS//2, mask=my_etfs)  
    #adding another screen  
    finalscreen = (longs | shorts)  
    pipe = Pipeline(  
        columns={  
            'longs': longs,  
            'shorts': shorts,  
            'finalfactor': finalfactor  
        },  
        screen=(finalscreen & my_etfs)  
    )  
    return pipe


def before_trading_start(context, data):  
    """  
    Called every day before market open.  
    """  
    context.output = algo.pipeline_output('pipeline')  
    context.risk_loadings = algo.pipeline_output('risk_factors')

    # These are the securities that we are interested in trading each day.  
    context.security_list = context.output.index


def rebalance(context, data):  
    """  
    Execute orders according to our schedule_function() timing.  
    """  
    pipeline_data = context.output

    risk_loadings = context.risk_loadings

    objective = opt.MaximizeAlpha(pipeline_data.finalfactor)

    constraints = []  
    constraints.append(opt.MaxGrossExposure(MAX_GROSS_LEVERAGE))

    constraints.append(opt.DollarNeutral())

    neutralize_risk_factors = opt.experimental.RiskModelExposure(  
        risk_model_loadings=risk_loadings,  
        version=0)  
    constraints.append(neutralize_risk_factors)

    constraints.append(  
        opt.PositionConcentration.with_equal_bounds(min=-MAX_SHORT_POSITION_SIZE,  
            max=MAX_LONG_POSITION_SIZE))  
    algo.order_optimal_portfolio(objective=objective, constraints=constraints)
5 responses

I can maybe help but could you attach a backtest It's much easier to debug that way.

Here you go. Thanks!

This is how I format my StaticAssets in the research notebook

StaticAssets(symbols(['NKE', 'JPM']))

It has the brackets around symbols, maybe that will help

Evan Kim, the problem with that is in the algorithm IDE the brackets return the error of "arguments must be strings".

The issue isn't with the implementation of the StaticAssets method. That works fine.

The error "TypeError: MaximizeAlpha() expected a value with dtype 'float64' or 'int64' for argument 'alphas', but got 'object' instead" is a bit misleading. The issue is indeed with the argument for alphas, but the issue is that it is an empty series. MaximizeAlpha doesn't like it when it's not given any alphas to maximize. So one way to avoid getting the error is to check if "pipeline_data.finalfactor" has any securities. This can be done perhaps like this

if not pipeline_data.finalfactor.empty:  
    objective = opt.MaximizeAlpha(pipeline_data.finalfactor)  
    ... the rest of the code...

However, while this eliminates the error, it doesn't solve the problem. The algo now just never orders anything. The problem actually stems from the fact that all the assets are ETFs (defined in the StaticAssets filter). The algo then goes on to filter based upon "Fundamentals.equity_per_share_growth". The issue is this field is only populated for stocks (not ETFs). Therefore the filter doesn't return anything and nothing gets ordered.

Attached is the algo with the error fixed but without placing orders. Not sure where to go with that.