Skip to content

Instantly share code, notes, and snippets.

@clintmjohnson
Created October 10, 2017 22:33
Show Gist options
  • Save clintmjohnson/e6cb1fad8774d3542b1f165ca78edab7 to your computer and use it in GitHub Desktop.
Save clintmjohnson/e6cb1fad8774d3542b1f165ca78edab7 to your computer and use it in GitHub Desktop.
This Function downloads Historic stock prices for a Symbol, and Saves them to CSV file, using Pandas and Yahoo finance API
"""
This function combines data from Pandas data on stocks, and the Yahoo Finance API
"""
def stockhistory(stock2check):
import pandas_datareader.data as web
import datetime
from yahoo_finance import Share
stock = Share(stock2check)
ylp = stock.get_year_low() # Returns the Year Low price for the stock
end = datetime.datetime.today() # Use Todays date as the End Date to Download.
start = (end - datetime.timedelta(days=365)) # Use todays date minus 365 Days as the Start date to download.
#year #month #day
#end = datetime.datetime(2017,10,10) #Uncomment this if you would prefer to use a Custom End date, other than today.
#start = datetime.datetime(2017,10,10) #Uncomment this if you would prefer to use a Custom Start date, other than today.
f = web.DataReader(stock2check, 'yahoo', start, end) #This specifies the Data source as Yahoo for Pandas to use.
f['YearLow'] = f['Close']-float(ylp) # Calculates the current price against the Year Low
f.to_csv('HMNY.csv') # Output to a CSV File, if already exists it will be overwritten.
print(f)
stockhistory(input('Stock to Check :'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment