Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to find the "beta" of individual stocks?

I expected that the beta of a stock could be found using Fundamentals, but it doesn't appear to be listed:

https://www.quantopian.com/help/fundamentals

Is there any way to find the beta of a stock, without using a custom fetcher to Yahoo or similar?

5 responses

This isn't possible in today's world of Quantopian, but we are building a screening architecture that will allow you to filter for stocks based on specific, custom criteria (more info here). I'll see what we can do to include this filter in the options.

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.

Alisa: Thanks. Actually, I am not planning on using it as a filter (although others may want that capability). For my use case, I have an equation with parameters that work well for the overall market, but I want to try trading individual stocks and adjusting my calculation with a per-stock beta multiplier.

You can run a regression and calculate it

here are 3 methods I use to calculate beta. if you want to do it in before_market then you can only do it from bar2 when you have store a copy of data in context

import numpy as np  
from pytz import timezone  
import pandas as pd  
from scipy import stats  
import operator  
from functools import partial  
from scipy  import  polyfit, polyval


def estimateBeta(priceY,priceX,algo = 'standard'):


    X = pd.DataFrame({'x':priceX,'y':priceY})

    if algo=='returns':  
        ret = (X/X.shift(1)-1).dropna().values  
        x = ret[:,0]  
        y = ret[:,1]  
        # filter high values  
        low = np.percentile(x,20)  
        high = np.percentile(x,80)  
        iValid = (x>low) & (x<high)  
        x = x[iValid]  
        y = y[iValid]  
        iteration = 1  
        nrOutliers = 1  
        while iteration < 10 and nrOutliers > 0 :  
            (a,b) = polyfit(x,y,1)  
            yf = polyval([a,b],x)  
            #plot(x,y,'x',x,yf,'r-')  
            err = yf-y  
            idxOutlier = abs(err) > 3*np.std(err)  
            nrOutliers =sum(idxOutlier)  
            beta = a  
            #print 'Iteration: %i beta: %.2f outliers: %i' % (iteration,beta, nrOutliers)  
            x = x[~idxOutlier]  
            y = y[~idxOutlier]  
            iteration += 1  
    elif algo=='quantopian' or algo=='q':  
        ret = (X/X.shift(1)-1).dropna().values  
        x = ret[:,0]  
        y = ret[:,1]  
        returns_matrix = np.vstack([y,x])  
        C = np.cov(returns_matrix, ddof=1)  
        algorithm_covariance = C[0][1]  
        benchmark_variance = C[1][1]  
        beta = algorithm_covariance / benchmark_variance

        return beta  
    elif algo=='standard':  
        ret =np.log(X).diff().dropna()  
        beta = ret['x'].cov(ret['y'])/ret['x'].var()  
    else:  
        raise TypeError("unknown Beta algorithm type, use 'standard', 'q' or 'returns'")

    return beta  

You can also use pd.stats.moments.ewmvar and pd.stats.moments.ewmcov to calculate rolling weighted beta.