Back to Community
Need help with update_universe

import numpy as np
import pandas as pd
import scipy.stats as stats
import time
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.factors import EWMA, Latest, SimpleMovingAverage
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.algorithm import attach_pipeline, pipeline_output

def initialize(context):
context.limit = 500.0
# Set possible number of random stocks meeting fundamental data requirements

def before_trading_start(context):
fundamental_df = get_fundamentals(
query(fundamentals.valuation_ratios.peg_ratio,
fundamentals.valuation_ratios.pb_ratio
)
.filter(fundamentals.valuation_ratios.peg_ratio > 2

    )  
    .filter(fundamentals.valuation_ratios.peg_ratio < 1  

    )  
    .filter(fundamentals.valuation_ratios.pb_ratio < 1  

    )  
    .filter(fundamentals.valuation_ratios.pb_ratio > 5  

    )  



    .limit = context.limit  

 # Screening fudamental data  

update_universe(context.fundamental_df.columns.values)

Updating the universe *****Doesn't Work******

def handle_data(context, data):
cash = context.portfolio.cash
position = context.portfolio.positions
for stock in data:
position = context.portfolio[stock].amount # Set position for reference
price = context.portfolio[stock].price #Long/Short price, price to base stop losses

    stop_long = price - (price * 0.05)    #Stop loss at -5%  
    stop_short = price + (price * 0.05)   #Stop loss at -5%  


    SMA5 = SimpleMovingAverage(inputs=[context.limit], window_length=5)    # Get SMA for 5 days  
    SMA5 = SimpleMovingAverage(inputs=[context.limit], window_length=20)   # Get SMA for 20 days  

        if SMA5 < SMA20  
            if context.fundamentals[stock][fundamentals.valuation_ratios.pb_ratio] > 5       # Stock must meet fundamental data  
                if context.fundamentals[stock][fundamentals.valuation_ratios.peg_ratio] > 2  # Stock must meet fundamental data  
                    if position == 0 # Must not already have a position  
                        order(stock, -500 style=StopOrder(stop_short)) # Market order at current price  
                                                                       # Stop loss order at current price  
                        profit_short = price - (price * 0.10) # Allows take profits order to reference short order price,                                                                           not current price  
                            else order_target(stock, 0)       # If criteria not met, order nothing  
                                if price >= profit_short order_target_value(stock, 0) # Take profits at + 10%  





                                # ***Basically the exact opposite for shorts***  
        if SMA5 > SMA20  
            if context.fundamentals[stock][fundamentals.valuation_ratios.pb_ratio] < 1  
                if context.fundamentals[stock][fundamentals.valuation_ratios.peg_ratio] < 1  
                    if position == 0  
                        order(stock, 500 style=StopOrder(stop_long))  
                        profit_long = price + (price * 0.10)  
                            else order_target(stock, 0)  
                                if price >= profit_long order_target_value(stock, 0)  
5 responses

Yeah so I cant even attach the algorithm, but if you copy and paste it in to a black algorithm page it will be exactly the same. The universe update won't work, "Syntax Error". I followed a few tutorials very closely but after like 2 days I still cant get it. I'm also having a lot of trouble trying to filter out low volume, getting SMA's to work, actually having an order work, to name a few problems. Basically anything I try to do doesn't work haha. Any help would be appreciated.

Having the exact same issue. My .filters are different as is my context.limit(25), but the syntax error has me boggled.

I suspect you are missing a closed parenthesis somewhere with your get_fundamentals call such that the syntax error is being thrown on update_universe(). Tough to tell from inspection here due to the formatting of the code but that's my guess. Hope that helps.

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.

Yeah I fixed that. It doesn't have any Syntax errors it just doesn't buy or sell anything.

Josh - thanks for the help. I left an 's' off...tired eyes.