I am trying to make a program that emails alerts when a stock price crosses a moving average and I am using the library yahoo_fin
(Here are the docs)
I am trying to get the moving average data from yahoo_fin.stock_info.get_stats('a')
but I am getting the following error:
File "lib\site-packages\yahoo_fin\stock_info.py", line 241, in get_stats
table.columns = ["Attribute" , "Value"]
File "lib\site-packages\pandas\core\generic.py", line 5287, in __setattr__
return object.__setattr__(self, name, value)
File "pandas\_libs\properties.pyx", line 67, in pandas._libs.properties.AxisProperty.__set__
File "lib\site-packages\pandas\core\generic.py", line 661, in _set_axis
self._data.set_axis(axis, labels)
File "lib\site-packages\pandas\core\internals\managers.py", line 178, in set_axis
f"Length mismatch: Expected axis has {old_len} elements, new "
ValueError: Length mismatch: Expected axis has 9 elements, new values have 2 elements
Any help on fixing this would be great!
If you don't know how to get this particular function to work, another alternative I tried and which seems to work is to use the method yahoo_fin.stock_info.get_data('a')
, but I would need help to know how to calculate the moving average from this data.
you can calculate the 50 day moving average from get_data
using the code:
import yahoo_fin
from yahoo_fin import stock_info
yahoo_fin.stock_info.get_data('a', interval='1d')['close'][-50:].mean()
if you want yo calculate it over a certain period:
df = yahoo_fin.stock_info.get_data('a', interval='1d')
moving_average = [df['close'][i-50:i].mean() for i in range(50, df.shape[0]+1)]