I recently created a beta calculator which pulls data from yahoo. But for some reason, I can't change the time interval. And what i mean by interval is when i pull data from a start time and a end time, it pulls all daily values. So for example, if i code df=pdr.get_data_yahoo(stock,start,now) itll give me all the prices of the stock but in daily intervals. I want to be able to change this to monthly price data or yearly price data. Every time I input the interval command, it gives me an error. I dont really know how to fix this error.
Traceback (most recent call last):
File "C:/Users/Owner/pyproj/project 1/380projects/Beta Calculator.py", line 34, in <module>
slope, intercept, r, p, std_err = stats.linregress(market_pct,stock_pct)
File "C:\Users\Owner\pyproj\project 1\lib\site-packages\scipy\stats\_stats_mstats_common.py", line 116, in linregress
ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat
File "<__array_function__ internals>", line 5, in cov
File "C:\Users\Owner\pyproj\project 1\lib\site-packages\numpy\lib\function_base.py", line 2415, in cov
X = np.concatenate((X, y), axis=0)
File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 13 and the array at index 1 has size 12
import datetime as dt
import numpy as np
import yfinance as yf
from pandas_datareader import data as pdr
from scipy import stats
yf.pdr_override()
stock = input("Enter a Stock Ticker Symbol: ")
print(stock)
startyear = 2020
startmonth = 1
startday = 1
start = dt.datetime(startyear,startmonth,startday)
now=dt.datetime.now()
df=pdr.get_data_yahoo(stock,start,now, interval = "1mo")
market=input('Enter Market Index to run regression: ')
de=pdr.get_data_yahoo(market,start,now, interval = "1mo")
stock_pct = df['Adj Close'].pct_change().values
market_pct = de['Adj Close'].pct_change().values
stock_pct = np.delete(stock_pct,0)
market_pct = np.delete(market_pct,0)
slope, intercept, r, p, std_err = stats.linregress(market_pct,stock_pct)
print('Standard Error = ', std_err)
print('Interecept = ', intercept)
print ('Beta = ', slope)
plt.plot(stock_pct, market_pct, 'b. ', alpha = 0.2)
plt.show()
x = np.linspace(np.amin(stock_pct), np.amax(stock_pct))
y = slope * x + intercept