The intention of the function below is to calcualte the beta of a security. 


I have used Investopedia to verify I'm calculating beta correctly.(trying to at least)


correlation*(STD_deviation_of_return_of_spy/STD_of_return_of_stock) = beta


The function below is my attemtp to calcualte beta: 

import numpy as np
from scipy import stats

def get_beta(self, security, period):
        if not self.Securities.ContainsKey(security):return 'no data'
        security_data = self.History([security], period, Resolution.Daily)
        spy_data = self.History(['SPY'],period,Resolution.Daily)
        if 'close' not in security_data:return 'no close'

        y = security_data['close'].pct_change().dropna().values

        x = spy_data['close'].pct_change().dropna().values
        slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
        return r_value * ((np.std(y))/(np.std(x)))

Can someone please tell me where my error is ? Thanks.