Last active
July 12, 2020 04:52
-
-
Save cameronShadmehry/a9e4a8a5aae4650ff3b45648dfad9122 to your computer and use it in GitHub Desktop.
Importing historical stock today
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
# If you have a list of your own you would like to use just create a new list instead of using this, for example: tickers = ["FB", "AMZN", ...] | |
tickers = gt.get_tickers_filtered(mktcap_min=5000, mktcap_max=10000000) | |
# Check that the amount of tickers isn't more than 2000 | |
print("The amount of stocks chosen to observe: " + str(len(tickers))) | |
# These two lines remove the Stocks folder and then recreate it in order to remove old stocks. Make sure you have created a Stocks Folder the first time you run this. | |
shutil.rmtree("<Your Path>\\SMA_Analysis\\Stocks\\") | |
os.mkdir("<Your Path>\\SMA_Analysis\\Stocks\\") | |
# Holds the amount of API calls we executed | |
Amount_of_API_Calls = 0 | |
# This while loop is reponsible for storing the historical data for each ticker in our list. Note that yahoo finance sometimes incurs json.decode errors and because of this we are sleeping for 2 | |
# seconds after each iteration, also if a call fails we are going to try to execute it again. | |
# Also, do not make more than 2,000 calls per hour or 48,000 calls per day or Yahoo Finance may block your IP. The clause "(Amount_of_API_Calls < 1800)" below will stop the loop from making | |
# too many calls to the yfinance API. | |
# Prepare for this loop to take some time. It is pausing for 2 seconds after importing each stock. | |
# Used to make sure we don't waste too many API calls on one Stock ticker that could be having issues | |
Stock_Failure = 0 | |
Stocks_Not_Imported = 0 | |
# Used to iterate through our list of tickers | |
i=0 | |
while (i < len(tickers)) and (Amount_of_API_Calls < 1800): | |
try: | |
print("Iteration = " + str(i)) | |
stock = tickers[i] # Gets the current stock ticker | |
temp = yf.Ticker(str(stock)) | |
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("<Your Path>\\SMA_Analysis\\Stocks\\"+stock+".csv") # 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 Backend Error, Attempting to Fix") # An error occured on Yahoo Finance's backend. 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 | |
# Handle SSL error | |
except requests.exceptions.SSLError as e: | |
print("Yahoo Finance Backend Error, Attempting to Fix SSL") # An error occured on Yahoo Finance's backend. 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