Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problems controlling exposure to "short term reversals"

Hi there

I am struggling to control the exposure to "short term reversals" trading style metric.

I've tried to follow the advice to constrain the portfolio with "order_optimal_portfolio" from lesson 9, but am getting build errors.
https://www.quantopian.com/tutorials/contest#lesson9

When I amend my code to take advantage of "order_optimal_portfolio", I get the error message:
"NameError: global name 'algo' is not defined"

But surely 'algo' is definied in the 3rd line of code below? Sorry for such a basic question. Just stumped.

The full code was functioning before I tried to add the "order_optimal_portfolio" stuff.

Below is the stripped down code, showing just bare essentials.


from quantopian.pipeline import Pipeline, CustomFactor  
import quantopian.pipeline.filters as Filters  
import quantopian.algorithm as algo  
import quantopian.optimize as opt  
#from quantopian.pipeline.filters import  StaticAssets  
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.filters import QTradableStocksUS  
from quantopian.pipeline.experimental import risk_loading_pipeline

import numpy as np  # needed for NaN handling  
import math  # ceil and floor are useful for rounding


from itertools import cycle  


def initialize(context):

    algo.attach_pipeline(make_pipeline(), 'long_short_equity_template')

    # Attach the pipeline for the risk model factors that we  
    # want to neutralize in the optimization step. The 'risk_factors' string is  
    # used to retrieve the output of the pipeline in before_trading_start below.  
    algo.attach_pipeline(risk_loading_pipeline(), 'risk_factors')  


    # Rebalance  
    schedule_function(  
        my_rebalance,  
        date_rules.every_day(),  
        time_rules.market_close(minutes=15))

Thanks for any hint or tips!