1
votes

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.

2
which version of python and yahoo_fin are you using? it works on python 3.6 and yahoo_fin 0.8.4. Also, could you give a bit more code context? what modules are you importing?user7440787
I am using python 3.7.7 and yahoo_fin 0.8.4! I am just trying to get the get_stats('a') to work and it's giving an error with that. Once I get the get_stats('a') to show me some data then I want to get the moving average from that data. Just typing yahoo_fin.stock_info.get_stats('a') gives me the error above. I am also using import yahoo_fin & from yahoo_fin import stock_infozqshmvbs
sorry, I'm not able to replicate the issue. it works just fine python -c "import yahoo_fin; from yahoo_fin import stock_info; print(yahoo_fin.stock_info.get_data('a'))". Try a different version of pandas? which version of pandas are you using?user7440787
i am using pandas 1.0.3! i'm trying to use the get_stats('a') and not the get_data('a'). i can try get_data('a') but how would you get a 50 day moving average from that data? also, how would you make it not pick up unnecessary data in the table?zqshmvbs
get_stats also works python -c "import yahoo_fin; from yahoo_fin import stock_info; print(yahoo_fin.stock_info.get_stats('a'))" and I am using the same version of pandas. Sorryuser7440787

2 Answers

1
votes

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)]
1
votes

I pushed a patch for this - if you upgrade your version of yahoo_fin to 0.8.5, this should be fixed now.

from yahoo_fin import stock info as si
si.get_stats("a")

From this, you can get the 50-day and 200-day moving averages. However, as mentioned in @user7440787's response, you can use the get_data method to pull the price history, and then calculate whatever window-size moving average you need to (10-day, 100-day, 150-day, etc.).