Python Pandas || Moving Averages and Rolling Window Statistics for Stock Prices

#pandas #python #rolling


Try my Hands-on Python for Finance course on Udemy:

fix_yahoo_finance has been renamed yfinance.

This video was made after changes to many APIs including Yahoo and Google prevented the datareader from connecting. This problem was rectified in pandas_datareader 0.80, however. You may still find yfinance to be a useful library – particularly if for some reason you cannot upgrade pandas_datareader.

Quickly download data for any number of stocks and create a correlation matrix using Python pandas and create a scatter matrix. It may take me 10 minutes to explain, but it will only take you 3 to see the power of Python for downloading and exploring data quickly primarily utilizing NumPy and pandas.

****ATTN: as of pandas_datareader 0.6 downloading from Yahoo API no longer works, and downloading from Google is intermittent. If you have a problem downloading data see this video work-around:

Video tutorial demonstrating the using of the pandas rolling method to calculate moving averages and other rolling window aggregations such as standard deviation often used in determining a securities historical volatility.

You can download the notebook used here:


Xem thêm bài viết khác:


  1. This is helpful. Also, can you do a small tutorial for EMA moving average in Python Pandas?

  2. Hello Matt,

    you said in the end of the video when plotting the moving average std the following:" this would include the 21st observation, the volatility should be used the next day, otherwise it will imply that we know something we don't". What do you mean exactly by this please?
    I presume that you are calculating a 21 days moving average std and that's why you did .shift() but I want to understand the logic behind your previous statement.

    Thanks a lot for your patience.


  3. For some reason, my rolling average estimates get cut off when the index of my main stock data ends (so the current day). Therefore, the data in the dataframe and chart doesn't continue past the current day, meaning all graph points end at the same time spot without the moving averages extending past the current day.

    Do you know why this might be happening?

  4. finally quit watching pandas rolling videos to watch pandas.rolling videos i was searching for

  5. nice work, I hadn't found this method in book i followed

  6. Awesome tutorial – exactly what I was looking for. Thank you!

  7. Thank you, this was a nice little tutorial.

  8. it is really helpful. Thank you

  9. Hi Matt!
    The data in which I am working on is yearly data and I dont know how to do prediction in python. Could u suggest any method.
    Please help 🙁

  10. 1. You got it the opposite in terms of lagging. The rolling mean by default is lagging. When you use "center=True", that actually takes away the lagging. To reiterate, when you specify centering, that takes away lagging. Not the other way around.
    2. Instead of shifting the rolling mean by 1 to exclude the same row, you should use "closed=Left".

  11. Hi Matt, I'm getting
    ModuleNotFoundError: No module named 'pandas_datareader'
    when I try to import pandas-datareader. Any solutions to this?

  12. Found this very helpful, thank you.

  13. very helpful, Thank you Matt

  14. Hey Matt, great video 🙂 Do you have an alternative to Yahoo finance now that it's unstable? Or a workaround of some sort

Leave a Reply

Your email address will not be published. Required fields are marked *