diff --git a/StockData.py b/StockData.py new file mode 100644 index 0000000..e8cf182 --- /dev/null +++ b/StockData.py @@ -0,0 +1,145 @@ +# StockData.py +# Andrew Dinh +# Python 3.6.1 +# Description: +''' +Returns all available dates and prices for each stock requested. +''' + +# Alpha Vantage API Key: O42ICUV58EIZZQMU +# Barchart API Key: a17fab99a1c21cd6f847e2f82b592838 +# Tiingo API Key: 2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8 +# If you're going to take these API keys and abuse it, you should really reconsider your life priorities + +apiAV = 'O42ICUV58EIZZQMU' +apiBarchart = 'a17fab99a1c21cd6f847e2f82b592838' +apiTiingo = '2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8' + +import requests, json +''' +def install(package): + if hasattr(pip, 'main'): + pip.main(['install', package]) + else: + pip._internal.main(['install', package]) +''' +class Stock: + def __init__(self, newStock = '', newfirstLastDates = [], newDates = []): + self.name = newStock + self.firstLastDates = newfirstLastDates + self.dates = newDates + + def getDates(self): # returns beginning and end dates available + print("Getting available dates for", self, "...\n") + # Gets first and last possible dates from each source + # Also gets all dates from each source + + # IEX + print("\nDates from IEX...") + firstLastDatesIEX = Stock.getDatesIEX(self, 'firstLast') + print("\nFirst and last dates:", firstLastDatesIEX) + datesIEX = Stock.getDatesIEX(self, 'all') + #print("All dates (recent first):", datesIEX, "\n") + + # Alpha Vantage + print("\nDates from Alpha Vantage...") + firstLastDatesAV = Stock.getDatesAV(self, 'firstLast') + print("\nFirst and last dates:", firstLastDatesAV) + datesAV = Stock.getDatesAV(self, 'all') + #print("All dates (recent first):", datesAV, "\n") + + # + + def getDatesIEX(self, which): + url = ''.join(('https://api.iextrading.com/1.0/stock/', self, '/chart/5y')) + #link = "https://api.iextrading.com/1.0/stock/spy/chart/5y" + print("URL:", url) + f = requests.get(url) + json_data = f.text + loaded_json = json.loads(json_data) + dates = [] + if which == 'firstLast': + # Find firstDate (comes first) + firstLine = loaded_json[0] + #print("firstLine:", firstLine) + firstDate = firstLine['date'] + #print("firstDate:",firstDate) + # Find finalDate (comes last) + #print("Length:", len(loaded_json)) + lastLine = loaded_json[-1] # Returns last value of the list (Equivalent to len(loaded_json)-1) + #print("lastLine:", lastLine) + finalDate = lastLine['date'] + #print("Final date:", finalDate) + dates.append((firstDate, finalDate)) + else: +# for i in range(0, len(loaded_json), 1): # If you want to do oldest first + for i in range(len(loaded_json)-1, 0, -1): + line = loaded_json[i] + date = line['date'] + dates.append(date) + return dates + + def getDatesAV(self, which): + url = ''.join(('https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=', self, '&apikey=', apiAV)) + # https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=MSFT&apikey=demo + print("URL:", url) + f = requests.get(url) + json_data = f.text + loaded_json = json.loads(json_data) + #print(loaded_json['Monthly Time Series']) + monthlyTimeSeries = loaded_json['Monthly Time Series'] + #print(monthlyTimeSeries) + #print(len(monthlyTimeSeries)) + #length = len(monthlyTimeSeries) + #print(monthlyTimeSeries['2018-12-17']) + #print(type(monthlyTimeSeries)) + listOfDates = list(monthlyTimeSeries) + #print(listOfDates) + dates = [] + if which == 'firstLast': + firstDate = listOfDates[-1] + lastDate = listOfDates[0] + #print("firstDate:", firstDate) + #print("lastDate:", lastDate) + dates.append((firstDate, lastDate)) + else: + dates = listOfDates + return dates +''' + # Find finalDate (comes first) + print("Length:", len(loaded_json)) + lastLine = loaded_json[-1] # Returns last value of the list (Equivalent to len(loaded_json)-1) + print("lastLine:", lastLine) + finalDate = lastLine['date'] + print("Final date:", finalDate) + dates = [] + dates.append((firstDate, finalDate)) +''' +''' + def printDates(self): + print("Getting data from IEX...") + url = ''.join(('https://api.iextrading.com/1.0/stock/', self.name, '/chart/5y')) + #link = "https://api.iextrading.com/1.0/stock/spy/chart/5y" + print("URL:", url) + f = requests.get(url) + #print(f.text) + json_data = f.text + loaded_json = json.loads(json_data) + + print("Printing from IEX...") + for i in range (0,len(loaded_json),1): + a = loaded_json[i] + print(a['date']) +''' + +def main(): + #if __name__ == '__main__': + # install('requests') + + stock = 'spy' + spy = Stock(stock) + #print(spy.name) + Stock.getDates(stock) + #Stock.printDates(spy) + +main() diff --git a/__pycache__/StockData.cpython-37.pyc b/__pycache__/StockData.cpython-37.pyc new file mode 100644 index 0000000..92af2c9 Binary files /dev/null and b/__pycache__/StockData.cpython-37.pyc differ diff --git a/main.py b/main.py index a194fe4..415775e 100644 --- a/main.py +++ b/main.py @@ -1,77 +1,18 @@ # main.py # Andrew Dinh # Python 3.6.1 -# Description: Get, parse, and interpret JSON files from IEX -import urllib.request -import re -#import os, errno -import json - -file_path = "tmp/data.txt" -# directory = os.path.dirname(file_path) - -url = urllib.request.urlopen("https://api.iextrading.com/1.0/stock/aapl/chart") -#print("url =", url) -html = url.read() # this is not a string, must convert it for findall() -# print("the html =", html) - -myFile = open('data.json','r') -data = myFile.read() - -json_string = json.dumps(data) -print("json string:", data) - +# Description: ''' -myFile =open(file_path,'w') -myFile.write(str(html)) -myFile.close() +Asks users for mutual funds/stocks to compare +Asks to be compared (expense ratio, turnover, market capitalization, or persistence) +Asks for time period (Possibly: 1 year, 5 years, 10 years) +Makes the mutual funds as class Stock +Gets data from each API +Compare and contrast dates and end changeOverTime for set time period + NOTES: Later can worry about getting close values to make a graph or something +Gives correlation value using equation at the end (from 0 to 1) - -# Create tmp folder -try: - os.makedirs(directory) -except OSError as e: - if e.errno != errno.EEXIST: - raise - -myFile = open(file_path,'r') -listOfLines = myFile.read().split('}') -#print("fileAsList:\n",listOfLines) - -print(listOfLines[0],"\n") -print(listOfLines[1]) -''' -''' -for j in range(0,len(listOfLines),1): - aLine = listOfLines[j] - #print(aLine) - lineData = aLine.split() - #print(lineData) +FIRST TESTING WITH EXPENSE RATIO ''' -''' -url = urllib.request.urlopen("http://www.gavilan.edu/staff/dir.php") -print("url =", url) -html = url.read() # this is not a string, must convert it for findall() -# print("-----------------------------------------------") -# print("the html =", html) - -myFile = open('dataFile.txt','w') -myFile.write(str(html)) -myFile.close() - -myFile = open('dataFile.txt','r') -listOfLines = myFile.readlines() # returns a list of lines,USE for search() -#print("fileAsList:\n",listOfLines) -listOfEmails = re.findall(r'\w+@gavilan.edu', str(html)) -# print(listOfEmails) -myFile.close() - -myFile = open('dataFile.txt','r') -listOfLines = myFile.readlines() -for line in listOfLines: - match = re.search('stoykov',str(line)) - if match: - print(line) -myFile.close() -''' +import StockData \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..da3fa4c --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests==2.21.0