December 17, 2024
Download historical stock data from Yahoo Finance using Python
 #Finance

Download historical stock data from Yahoo Finance using Python #Finance


hey how’s it going guys in this CashNews.co i’m going to show you how to download stock historical data from yahoo Finance all right so this is a script i

used pretty frequently back in the day when i’m doing uh trading let me show you my Portfolio so basically my investment Portfolio i didn’t start investing stock until a couple years ago when one of my friends suggested i should use python to automate

some of my Financial Analysis so i wrote script to extra historical data for one to two thousand companies from yahoo Finance and i wrote an algorithm to

pick the stock for me and so far i’ve made over a hundred thousand dollar which is pretty good in my opinion so i tell why not to share the script with you guys all right so we’ll be using uh yahoo text-decoration: none;">Finance to uh extra the data set so first you want to go to Finance.evo.com slash quote and i’ll be using a tesla for this

demonstration here let me go ahead and import the python library first so for this exercise i’ll be using three different libraries the time library data library and the pandas library and the reason why i’m using pandas live is because this library allows you to easily save the data

set in two different files such as excel file csv file text file or even json file now let me go back to yahoo Finance so if you want to go to historical data and you have a

couple options or parameters and let’s change the start dates to december first so here’s this is going to be my uh beginning period 2020. so basically the month of december of 2020 so this is going to be my time here for the uh data type i’m going to keep that as historical

prices and frequencies i’m going to change that to weekly for now so we’ll change that back later now what i want to do is i want to hover my mouse to this download link right click i want to copy the link location i’m going to create a variable i’ll name this variable query

string now copy paste the url now if we look at this url here’s my uh taker and after the question mark is my parameters so this is going to be player 1 period 2 my interval value and the rest is basically the default settings and using this url we can make a request code to extract the

historical data so what i’m going to do is i’m going to convert the string to f string so this allows me to directly insert the variable name in the query i want to be able to manipulate p1 value period 2 value and noticing that here the period values are in seconds so when we provide

our dates range we need to convert those difference values into seconds and here’s my interval value oh and uh ticker symbol right so here let me go ahead and create my variables first the first parameter is the uh ticker symbol i’m going to assign tesla period 1 period 2. and to insert

the date value so i’m going to use the datetime.daytime method i’m going to assign the year value to 2020 december 1st and because we’re dealing with gmt time so for the hour value i like to use 23 for the minute value i’ll set that to 59. i’ll convert the daytime

object to time to pull now i can take the time module the mk type this function will convert this output into second value so let me let me do this i’m going to highlight the code i want to execute individually now i’m going to highlight this name right here alright and that’s

going to give me the second value and noticing that he will have a one decimal point so i need to remove the decimal point by converting the value into integer i can take this statement copy and paste i’ll change the date value to 31st and the last parameter is the interval i’ll set

that to one week so for one week it’s going to be 1wk if you want to set the interval to dla then it’s going to be 1d and for monthly it’s going to be 1m now we can uh assign the variables to our query string and it’s going to be ticker and it’s going to be period one

period two and interval now let me uh run uh this code block all right so if i print the query string and if i open the url in a new browser it’s going to give me this pop-up dialog asked me to save the data set in a csg file i’m going to cancel this now what i can do is i can basically

treat this as an api code i’ll insert the pandas module always the re-csv method to read the query string and save the output sdf if i run line 12 and if i print the df object and it’s going to gives me the historical data set now let me change the interval value to one day so basically

daily i’ll change the ticker to apple in case if i want to save the data set as a csv file or excel file so i can use the method to csv o to excel and followed by the file path i’m going to skip in this exercise and i’ll simply just print the df object now i’m going to

terminate this session and i’ll run the script by itself it should be pretty quick yeah and here’s my historical data set for apple from december 1st to december 31st of 2020 all right so this is someone to share in this CashNews.co and hopefully you guys found the CashNews.co useful in

the swiss thank you guys for watching i’ll see you guys on the next CashNews.co

Now that you’re fully informed, don’t miss this insightful video on Download historical stock data from Yahoo Finance using Python.
With over 77799 views, this video offers valuable insights into Finance.

CashNews, your go-to portal for financial news and insights.

20 thoughts on “Download historical stock data from Yahoo Finance using Python #Finance

  1. nooby programmer here! I got a long list of errors on my machine: please help :)) –
    urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)>

  2. 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?

  3. 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.

  4. 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?

Comments are closed.