Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
50/200 SMA Golden Cross

Hi,

I'm new to both programming and this site. How would I edit this algorithm to check every stock in the market instead of just GOOG?

def initialize(context):  
    context.sid = sid(26578)  
    context.invested = False

def handle_data(context, data):  
    context.price = data[context.sid].price  
    short = data[context.sid].mavg(50)  
    long = data[context.sid].mavg(200)

    if (short > long) and not context.invested:  
        order(context.sid, 10)  
        context.invested = True  
    elif (short < long) and context.invested:  
        order(context.sid, -10)  
        context.invested = False

    record(short_mavg = short,  
        long_mavg = long,  
        goog_price = context.price)  

Thanks!

9 responses

To work with a list, see attached. API documentation has an example of how to get a list of n% most liquid stocks to pick, or you can use your own picks in init sections.

Although many trading books mention 50 day and 200 day moving average crossing as a buy/sell signal, I have failed miserably to produce any algorithm that would confirm such strategy as profitable. Should you manage to confirm correlation between these indicators and market movements, please let me know. It would be an easy thing to code and exploit.

I'll just expand on Vlatko's answer. You can't check every stock in the market since that would be a ton of data. But you can check a percentage of the top stocks using set_universe. You use set_universe in the initialization to easily store a bunch of sids. Then, in handle_data, you just do what you did for GOOG except set it so it does it for each sid you got from set_universe. I explained it more in the source code below. I know you're new, so let me know if I should make anything else clear.

You can find more on set_universe here: https://www.quantopian.com/help#ide-universe

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'd like to point out that the DollarVolume universe doesn't return "top stocks". It orders all stocks by price * volume and then returns a slice of that (recalculated every quarter).

The purpose is to give your algorithm as wide a variety of stocks as possible without having to process all stocks. If your algo can perform consistently with a range of unknown stocks that is a good thing.

Typically you want to avoid hand picking stocks since that could introduce all kinds of bias and invalidate your results.

Good point Dennis, I should have been clearer about what universe's purpose is.

Let's assume one uses set_universe during initialization then loops over data to pick stocks to trade. If back test starts in 2002, will Google, Facebook and Tesla ever become a part of data set? They will be if the test starts in 2013 as all 3 stocks are at the top of the trading volumes but none existed in 2002.

To put a question in a different way, id the top percentage of stocks traded determined at the beginning of the test and kept as an unchanging set throughout? Or is the percentage of top traded stocks determined each time handle_data is called? Former would lead to a static set that does not include IPO's since the beginning of the trading strategy, latter would lead to some stocks being dropped out of the data set since they are no longer in the top traded list.

Either way, the set_universe() will introduce a bias. Old boys club bias presumably, as I suspect it is implemented as a static data set.

Hi Vlatko,

Our dollar volume universe is recalculated each quarter. See the API docs for more information.

jik

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.

@Vlatko, short answer is yes, GOOG would be picked up.

The long answer is only if they fit into the slice of the universe you are viewing (based on dollar * volume).

Also included will be companies that have since been delisted, possibly during the quarter you are viewing. This is a good thing because it avoids "survivorship bias".

It seems that you are not only trading crosses, but also MA50 above MA200 ?