Download historical stock data from Yahoo Finance using Python

In this tutorial, I will share a script I use to easily download historical stock data from Yahoo finance using just a few lines of code in Python to help me analyze different stocks.
There are several Python libraries you can use these days to download stock data from Yahoo finance, but for me, I prefer directly to go to the source. In this tutorial, I will show you how we can make a request call to Yahoo Finance's back-end to access the stock data and export them as CSV files.
📑 Source Code: learndataanalysis.org/source-...
📃 If you want to export multiple historical data to a single Excel spreadsheet: • Download Multiple Hist...
► Buy Me a Coffee? Your support is much appreciated!
-------------------------------------------------------------------------------------------
☕ Paypal: www.paypal.me/jiejenn/5
☕ Venmo: @Jie-Jenn
💸 Join Robinhood with my link and we'll both get a free stock: bit.ly/3iWr7LC
► Support my channel so I can continue making free contents
---------------------------------------------------------------------------------------------------------------
🛒 By shopping on Amazon → amzn.to/2JkGeMD
👩‍💻 Follow me on Linked: / jiejenn
🌳 Becoming a Patreon supporter: / jiejenn
✉️ Business Inquiring: KZread@LearnDataAnalysis.org
#Stock #YahooFinance #HistoricalStockData #Python

Пікірлер: 106

  • @jiejenn
    @jiejenn2 жыл бұрын

    To import price for for monthly interval, Yahoo Finance has change the parameter value from "1m" to "1mo".

  • @Dan-uf2vh
    @Dan-uf2vh3 жыл бұрын

    such tremendous value in an 8 minute video! exactly what I needed, only a few lines of code

  • @priyamd4759
    @priyamd47592 жыл бұрын

    Great tutorial! The step by step way you explain the time conversion functions is superb. Hope you continue doing this as a series. Liked and subscribed.

  • @lpc1231000
    @lpc1231000 Жыл бұрын

    Thank you. I have been trying to script this for a long time!

  • @ki5ngau
    @ki5ngau3 жыл бұрын

    Crazy good. Thank you for creating this vid.

  • @sid9137
    @sid91372 жыл бұрын

    Hello Sir, This video tutorial was great, learned something new. Thanks a lot! :)

  • @dariourdaneta5443
    @dariourdaneta54433 жыл бұрын

    Thanks bro! I learn a lot!

  • @EpoKeins
    @EpoKeins3 жыл бұрын

    Thank you for this great introduction! Could you please make a series on this topic?! How do you build your portfolio? How do you choose stocks - based on which criteria?

  • @jiejenn

    @jiejenn

    3 жыл бұрын

    I will eventually make a video on how I use data analytics and Python to determine my stock purchasing decision.

  • @amarshah1230

    @amarshah1230

    2 жыл бұрын

    @@jiejenn thanks for this, let me know when you have a video for stock purchasing - thanks

  • @patrickmueller9658
    @patrickmueller96582 жыл бұрын

    Nice!!! Did this with the data on redfin

  • @danielwang1970
    @danielwang19703 жыл бұрын

    Great and useful content! Nice tutorial

  • @jiejenn

    @jiejenn

    3 жыл бұрын

    Thanks!

  • @gbabayan
    @gbabayan2 ай бұрын

    good job, just what i needed

  • @eddiehillergrand8882
    @eddiehillergrand8882 Жыл бұрын

    Thank you so much Jie! I got it right first try lol

  • @jiejenn

    @jiejenn

    Жыл бұрын

    Glad you got the script to work on the 1st try!

  • @patrickjay6434
    @patrickjay64342 жыл бұрын

    Awesome, thanks for sharing! 👍🏿

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Glad the video helped.

  • @lfz1570
    @lfz1570 Жыл бұрын

    Thank you for sharing.

  • @paraswadel5541
    @paraswadel5541 Жыл бұрын

    thank you so much for this

  • @alexisgc19
    @alexisgc192 жыл бұрын

    Excellent!!!

  • @indiaeye
    @indiaeye2 жыл бұрын

    Awesome tutorial! It is the simplest and best way to download the price data. Thanks a lot. Do you have any tips for downloading the historical fundamentals number such as PE, PB etc?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Something like this? kzread.info/dash/bejne/pqaTsLOPhcfTprw.html

  • @oleksiidrozdov3186
    @oleksiidrozdov31863 ай бұрын

    great one, thank you

  • @jiejenn

    @jiejenn

    3 ай бұрын

    👍

  • @macaeallen7938
    @macaeallen79382 жыл бұрын

    When I print 'query_string', I am able to get the link. However, it automatically downloads the csv straight to my computer rather than asking me. Because of this, my data isn't on my python IDE. Any suggestions on how to fix this?

  • @cvillejin
    @cvillejin2 жыл бұрын

    Hi thanks for the upload! Do you think you can do a video tutorial for showing the stock option data using yahoo finance api or any other api? Also, is there a way to change the periods so that it automatically pulls the most recent X years of data as opposed to having to go back into the code and changing the dates each time?

  • @lhassan1499
    @lhassan1499 Жыл бұрын

    thank you ❤

  • @joerocks1477
    @joerocks14772 жыл бұрын

    I basically copied the entire thing.. and then "query_string in my terminal but a hyperlink doesnt show up for me too look at it... I need help ! Also, any time i write code and run the module i never see any results. Newb here.

  • @johnvalentino576
    @johnvalentino5762 жыл бұрын

    Fantastic

  • @alastermyst
    @alastermyst2 жыл бұрын

    This wasn't working for me, I was getting a 404 error. After a bit of fruitlessness and frustration I have found the issue. I was forgetting to use the download link, not the webpage address. Hopefully this will help anyone else having similar PEBKAC issues.

  • @rahmaaja6600
    @rahmaaja6600 Жыл бұрын

    thanks for making this video. good job 👏

  • @thegamechanger7157
    @thegamechanger71572 жыл бұрын

    It's starting.👄👍

  • @krishnangdalal7425
    @krishnangdalal74252 жыл бұрын

    Thanks for the video. Is there a way to get the intraday data between the given two dates?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Not with Yahoo's API. Storage is costly so that's why most of the services only provide daily data. If you are looking for intradat data, Google might be your best friend.

  • @ahubuni
    @ahubuni3 жыл бұрын

    Awesome video with clear explanation!! I ran your code, however I'm missing my middle "Low" Column. Instead of numbers, each item in the column just says "..." any thoughts?

  • @jiejenn

    @jiejenn

    3 жыл бұрын

    By default, pandas doesn't display all the columns on the console. Insert this line before your print statement pd.set_option('display.max_columns', 100)

  • @samjockel5848
    @samjockel58482 жыл бұрын

    Thanks for the video! If I wanted to take just the numbers from the 'Close' column, how would I do that? When I try (using df['Close']) it outputs more than just the actual number values of that column.

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Can you be more specific? What do you mean by outputs more than just the actual number?

  • @samjockel5848

    @samjockel5848

    2 жыл бұрын

    @@jiejenn If I run your code using print(df['Close']), I get an output of: 0 123.080002 1 122.940002 2 122.250000 3 123.750000 and so on... Is it possible to only get an output of just the closing prices, instead of having them numbered 0-3? To be more specific, I'm trying to put these closing prices into an array so I can manipulate them and do some equations. I'm not sure if this approach will actually work, but let me know what you think!

  • @jd5787
    @jd57872 жыл бұрын

    Hi there! is there a way to pull other fields such as EV, Revenue, market cap, forward revenue estimates on a monthly periodicity please? (revenue is published every quarter and not every month so there could be 3x the same Revenue number to represent a quarter) . thanks!

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Check out these 2 videos for reference: 📚 kzread.info/dash/bejne/pqaTsLOPhcfTprw.html 📚 kzread.info/dash/bejne/iYCsqtued5aTY7Q.html

  • @jd5787

    @jd5787

    2 жыл бұрын

    @@jiejenn thanks, viewing now!

  • @mtstockmarketupdates5022
    @mtstockmarketupdates50222 жыл бұрын

    can u teach me how to get data in minuteframe

  • @jsh111
    @jsh1113 жыл бұрын

    I can see the download link for a regular stock, but there is no download link for S&P500 on yahoo finance. Am I doing something wrong?

  • @jiejenn

    @jiejenn

    3 жыл бұрын

    What are you trying to download? If you don't see the option on Yahoo Finance, then it is probably not available.

  • @paulhoppe8581
    @paulhoppe85813 жыл бұрын

    Good video. How about modifying the script for a list of symbols? Thank you.

  • @jiejenn

    @jiejenn

    3 жыл бұрын

    Create a list of symbols and iterate each one.

  • @meetshah4800
    @meetshah48002 жыл бұрын

    What if I want historical data of 1 min (^NSEBANK) of about 5 Years ??

  • @lera722
    @lera7225 ай бұрын

    What Python program do you use? I'm a complete beginner to PyCharm and can't seem to find where to actually type the code!

  • @jiejenn

    @jiejenn

    5 ай бұрын

    I'm using VS Code.

  • @patrickpartikian2542
    @patrickpartikian25422 жыл бұрын

    This was very good video. Im getting started with python and will be coming to your site a lot to learn. I wish you would share with us the algorithm you used to make 100k! Can you share? :)

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Maybe one day. It's 200k as of today now.

  • @AndyChen1127
    @AndyChen11272 жыл бұрын

    hello, when I use the API try to take history data , I get response : forbindan the API isn't to be access now?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    I just tried the script, it is working fine for me snipboard.io/5dCu39.jpg. Makes sure your url is constructed correctly.

  • @JohnsDysfunction
    @JohnsDysfunction2 жыл бұрын

    For some reason I am having a problem calling pd.read_csv(query_string). I am getting HTTP Error 404: Not Found. I am suspecting that this is because it is not returning a CSV file. I did click on the printed query_string that I created and it does take me to the expected site with data. Am I missing a parameter?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    I just tested it and it is exporting the file just fine. Have you tried copy paste the code snippet from the post too? learndataanalysis.org/source-code-download-historical-stock-data-from-yahoo-finance-using-python/

  • @niaguilar1994
    @niaguilar19942 жыл бұрын

    Can you make a loop for downloading one file with the stock history for several stocks?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Try this video kzread.info/dash/bejne/oop8sNCrgbjdZ6g.html

  • @AmruthaSRISAIDURGA
    @AmruthaSRISAIDURGA9 ай бұрын

    Great Job sir, How to download NSE & BSE IPO Historical data with stock code, Listing price, OHLC, Listing Date. Thank you

  • @Pump_and_Dump_strategy
    @Pump_and_Dump_strategy2 жыл бұрын

    How to download 1 hour ?

  • @Deathpsyche3000
    @Deathpsyche30002 жыл бұрын

    I tried to scrape over 900 tickers using a for-loop. After about 30% through the list of tickers, encountered HTTP Error 401: Unauthorized ... Is this a YahooFinance restriction to prevent user from hoarding resources? Is there a workaround solution?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    You probably made too many requests in a short period amount of time and now your IP is permanently blocked by the host.

  • @colebutkovsky1477
    @colebutkovsky14773 жыл бұрын

    Hey, I'm getting a syntax error on line 12 during the query _string... I don't know what I did wrong, I copied everything you had

  • @jiejenn

    @jiejenn

    3 жыл бұрын

    Syntax error means you have a typo somewhere in your script.

  • @damianwysokinski3285

    @damianwysokinski3285

    3 жыл бұрын

    maybe wrong date combinations

  • @williamkim8297
    @williamkim82972 жыл бұрын

    Hi Jie, can you get historical data for Crypto as well? Thanks

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    This is something I never thought of doing. Let me look into it!

  • @carlossierra919
    @carlossierra9192 жыл бұрын

    Thanks a lot for this video. I tried daily period and working fine but it does not work for weekly period. using the option 1w. Any idea how can I solve it ?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Look like Yahoo Finance API made the change to the parameter value. Use 1mo instead of 1m.

  • @carlossierra919

    @carlossierra919

    Жыл бұрын

    @@jiejenn what is the parameter to use for weekly period ?

  • @KingLiongWong
    @KingLiongWong Жыл бұрын

    Great to learn new way to download the stock historical data from Yahoo finance, thanks. But I would like to know what is the different from using "yfinance" library or "pandas_datareader" library? Is this method better than those library?

  • @jiejenn

    @jiejenn

    Жыл бұрын

    Should be the same endpoint since there is only one. I prefer directly work with the endpoint just so if something changes, I don't have to wait till the author fix the package.

  • @arogantmushroom
    @arogantmushroom2 жыл бұрын

    How do you get the import panda part

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Do you mean installing pandas package? In your terminal, pip install pandas.

  • @senayg.5864
    @senayg.5864Ай бұрын

    Why does it skip some dates? Is it due to not having the information for that date?

  • @jiejenn

    @jiejenn

    Ай бұрын

    Weekends and holidays.

  • @lordstrading6327
    @lordstrading63273 жыл бұрын

    can you download a 2 hour time interval?

  • @jiejenn

    @jiejenn

    3 жыл бұрын

    If you want to download stock price every x hours, you will have to create a scraper.

  • @lordstrading6327

    @lordstrading6327

    3 жыл бұрын

    @@jiejenn Thanks Jen. Please can I email you? I am trying to find/scrap the volume data on plus500 website. But I can't find it.

  • @buraiki
    @buraiki2 жыл бұрын

    Hello, Thank you for your effort and posting such a usufl video. I have followed the code however; when I run the code i'm getting urllib.error.HTTPError: HTTP Error 422: Unprocessable Entity. Anyone facing this issue?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    I just tested the script, and I am not getting the 422 error.

  • @leichen1842
    @leichen18422 жыл бұрын

    Hi Jie, I tried your code but got the followed error: HTTPError: HTTP Error 422: Unprocessable Entity How to fix this? Thanks in advance!

  • @leichen1842

    @leichen1842

    2 жыл бұрын

    Solved. For monthly data, we should use '1mo'

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Glad to see you solved your issue.

  • @coconut7135
    @coconut71353 жыл бұрын

    Hi Jie, thanks for your video, I found it very helpful. I am wondering if you know how to full financial statements data into excel? It would make the fundamental analysis much easier. Thanks heaps in advance

  • @jiejenn

    @jiejenn

    3 жыл бұрын

    Are you referring to the Financial Statements on the Financial tab? If so, then the only ways are either sign up for their Premium service or manually create a script to scrape the information.

  • @coconut7135

    @coconut7135

    3 жыл бұрын

    @@jiejenn Yes, thanks you very much!

  • @marcusrecarus3692
    @marcusrecarus36922 жыл бұрын

    Hi, sir. What broker are you in?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    I do my own investing.

  • @marcusrecarus3692

    @marcusrecarus3692

    2 жыл бұрын

    @@jiejenn How about the screenshot of your portfolio? Was it "I do my own investing."?

  • @Emma-eb5zb
    @Emma-eb5zb Жыл бұрын

    Download button is missing now :c

  • @jiejenn

    @jiejenn

    Жыл бұрын

    I just checked, it's still there.

  • @rverm1000
    @rverm10002 жыл бұрын

    How did you make money with that data?

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    By doing some data analysis.

  • @mtstockmarketupdates5022
    @mtstockmarketupdates50222 жыл бұрын

    i want in minutes time frame

  • @jiejenn

    @jiejenn

    2 жыл бұрын

    Okay.

  • @ErtizaAbbas
    @ErtizaAbbas Жыл бұрын

    hey Jei, cannot fetch yahoo finance stock (daily closing data) now, i think yahoo finance restricted, any good solution for this problem?

  • @jiejenn

    @jiejenn

    Жыл бұрын

    I just tested the script, and it is working fine for me. Perhaps you have a typo somewhere? Either way, there should be an error message indicates what is causing the issue.

  • @ErtizaAbbas

    @ErtizaAbbas

    Жыл бұрын

    @@jiejenn hmm, actually, i am trying fetch data via yfinance , while yahoo finance historical data page doesn't present 'Donaload' clickable button, and this is the issue and i can't solve it

  • @jiejenn

    @jiejenn

    Жыл бұрын

    Then you will have to reach out to yfinance.

  • @monkeybread612
    @monkeybread612 Жыл бұрын

    nooby programmer here! I got a long list of errors on my machine: please help :)) - urllib.error.URLError:

  • @jiejenn

    @jiejenn

    Жыл бұрын

    Maybe this post might help stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error