Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
error with stddev?

Hello, i found this site a couple of days ago. Very cool! Ive just been starting with investing and now with python and im finding a problem which I want to blame on the stddev function. This will start to run, then it will say "run time error". So my idea was to filter stocks with high standard deviations (which i hope will indicate volatility) and then trade them based on their averages.

def initialize(context):  
     set_universe(universe.DollarVolumeUniverse(98, 99))  
def handle_data(context, data):  
    #the idea is to find stocks with high standard deviation,  
    #and trade them above and below their moving averages.  
    volatileStocks = list()  
    for stock in data.keys():  
        if data[stock].stddev(3) > 2:  
            volatileStocks .append(stock)  
    for stock in volatileStocks:  
        price = data[stock].price  
        avg = data[stock].vwap(3)  
        if price < avg * .98:  
            order(stock, +context.portfolio.cash/price)  
        elif price > avg * 1.02:  
            order(stock, -context.portfolio.cash/price)  

Thanks for reading

6 responses

sorry, meant to attach this in first post

Chris,

I tried your algorithm...also got the error and could not sort out what's going on. Runs fine for a bit and then crashes. I suggest an e-mail to Quantopian feedback: [email protected].

Grant

Hi Chris,

I think we found the bug that's causing this problem. We're testing the fix and it should be ready tomorrow.

Dan

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 still get the error (with slightly modified code from what Chris posted). See below and attached. --Grant

def initialize(context):  
     set_universe(universe.DollarVolumeUniverse(98, 99))  
def handle_data(context, data):  
    #the idea is to find stocks with high standard deviation,  
    #and trade them above and below their moving averages.  
    for stock in data.keys():  
        if data[stock].stddev(3) > 2:  
            price = data[stock].price  
            avg = data[stock].vwap(3)  
            if price < avg * .99:  
                order(stock, +context.portfolio.cash/price)  
            elif price > avg * 1.01:  
                order(stock, -context.portfolio.cash/price)  

Chris, Grant:

The fix is now live.

For some more color, the bug was due to floating point rounding resulting in a very small negative number being passed to the math.sqrt call in the stddev calculation.

Here's a link to the commit in Zipline that fixed that case, https://github.com/quantopian/zipline/commit/414e12ecc99885ff41ab54fed91d0336b7c1ac65

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 also just confirmed that it works now (see attached). Thanks, Grant