Created
August 26, 2020 03:40
-
-
Save cameronShadmehry/5f1ea2b363f2cdc22d6ac8a457d18187 to your computer and use it in GitHub Desktop.
Pull stock data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i=0 | |
while (i < len(tickers)) and (Amount_of_API_Calls < 1800): | |
try: | |
stock = tickers[i] # Gets the current stock ticker | |
temp = yf.Ticker(str(stock)) # Instantiate the ticker as a stock with Yahoo Finance | |
Hist_data = temp.history(period="max") # Tells yfinance what kind of data we want about this stock (In this example, all of the historical data) | |
Hist_data.to_csv("C:\\Users\\Your Desired Path to File") # Saves the historical data in csv format for further processing later | |
time.sleep(2) # Pauses the loop for two seconds so we don't cause issues with Yahoo Finance's backend operations | |
Amount_of_API_Calls += 1 | |
Stock_Failure = 0 | |
i += 1 # Iteration to the next ticker | |
except ValueError: | |
print("Yahoo Finance Back-end Error, Attempting to Fix") # An error occurred on Yahoo Finance's back-end. We will attempt to retreive the data again | |
if Stock_Failure > 5: # Move on to the next ticker if the current ticker fails more than 5 times | |
i+=1 | |
Stocks_Not_Imported += 1 | |
Amount_of_API_Calls += 1 | |
Stock_Failure += 1 | |
print("The amount of stocks we successfully imported: " + str(i - Stocks_Not_Imported)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment