Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
When I list specific stocks this algorithm runs fine, but when i run it after I use a get_fundamentals filter to get the S&P500 I get an error any idea why?
# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
import numpy as np  
import math  
from sqlalchemy import or_  
def initialize(context):  
    context.limit = 500  
    schedule_function(rebalance,  
                      date_rule = date_rules.month_start(),  
                      time_rule = time_rules.market_open())  
def before_trading_start(context):  
    context.security = get_fundamentals(  
                        query(fundamentals.valuation.market_cap,  
                             fundamentals.company_reference.primary_exchange_id)  
                        .filter(fundamentals.valuation.market_cap > 4e9)  
                        .filter(fundamentals.company_reference.country_id == "USA")  
                        .filter(or_(fundamentals.company_reference.primary_exchange_id == "NAS", fundamentals.company_reference.primary_exchange_id == "NYS"))  
                        .order_by(fundamentals.valuation.market_cap.desc())  
                        .limit(500)  
                       )  # S&P 500 rebalances on an as needed basis, so we'll just use today  
    update_universe(context.security.columns.values)  
def rebalance(context,data):  
    for stock in data:  
        prices = history(bar_count = 253, frequency = '1d', field = 'price')  
        pct_change = (prices.ix[-252] - prices.ix[232]) / prices.ix[232]  
        pct_change2 = (prices.ix[-126]-prices.ix[-42]) / prices.ix[-42]  
        theory1 = pct_change+pct_change2  
        uptheory1 = np.percentile(theory1, 50)  
        most_theory = theory1[theory1>uptheory1]  
        amount = context.portfolio.positions[stock].amount  
        if stock not in most_theory:  
            order(stock, -amount)


# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    prices = history(bar_count = 253, frequency = '1d', field = 'price')  
    pct_change = (prices.ix[-252] - prices.ix[232]) / prices.ix[232]  
    pct_change2 = (prices.ix[-126]-prices.ix[-42]) / prices.ix[-42]  
    theory1 = pct_change+pct_change2  
    uptheory1 = np.percentile(theory1, 50)  
    most_theory = theory1[theory1>uptheory1]  
    #print uptheory1  
    print most_theory  
    #print pct_change  
    #print pct_change2  
    for stock in context.stocks:  
        cash = context.portfolio.cash  
        price = data[stock].price  
        if stock in most_theory:  
            if cash > 0:  
                hope = (math.floor(cash/price))/4  
                order(stock, hope)  
4 responses

Hey so I wrote the above code for getting the S&P 500 facsimile, now as you may have noticed the code is written in Research, however this code can easily be imported into the IDE by removing one line, the line having to do with exchange filtering, we take care of this for you in the IDE automatically. Remove that line and you should be good to go, provided your code has no other errors.

Good Luck

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.

I'm still getting this error 'Error Runtime exception: IndexError: cannot do a non-empty take from an empty axes.' The thing is I'm getting it on line 39 when I have the same code on line 26 and it isn't returning an error. Any suggestions?

What does 4e9 do?

4e9 means 4,000,000 or 4 billion.

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CCwQygQwAWoVChMI_KLy3KXExwIVwx0eCh0WrAQ2&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FScientific_notation%23E_notation&ei=lmfcVfz_D8O7eJbYkrAD&usg=AFQjCNHwtFf5Y45PmvMoV0XjZYpHaZUnOg