From eba040ede95edfd959c5ddc4af32c1cb08d2d804 Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Sun, 16 Dec 2018 14:46:56 -0800 Subject: [PATCH 1/5] Update main.py --- main.py | 91 +++++++++++++++------------------------------------------ 1 file changed, 24 insertions(+), 67 deletions(-) diff --git a/main.py b/main.py index a194fe4..4ec037a 100644 --- a/main.py +++ b/main.py @@ -2,76 +2,33 @@ # 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 +import urllib, requests, json -file_path = "tmp/data.txt" -# directory = os.path.dirname(file_path) +class iex: -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) + def __init__(self, newStock = 'spy') + self.iex -myFile = open('data.json','r') -data = myFile.read() + def printDates(self) + print("Getting data from IEX...") + url = ''.join(('https://api.iextrading.com/1.0/stock/', stock, '/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(json_data) + #print(loaded_json) + #print(loaded_json[0]) -json_string = json.dumps(data) -print("json string:", data) + print("Printing dates given from IEX...") + for i in range (0,len(loaded_json),1): + a = loaded_json[i] + print(a['date']) -''' -myFile =open(file_path,'w') -myFile.write(str(html)) -myFile.close() +def main(): + stock = 'spy' + IEX(stock) - -# 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) -''' - -''' -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() -''' +main() \ No newline at end of file From 1682bb96cef333196249c016b59e7af05f680f40 Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Sun, 16 Dec 2018 17:58:25 -0800 Subject: [PATCH 2/5] Update main.py --- main.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 4ec037a..0b8e002 100644 --- a/main.py +++ b/main.py @@ -1,34 +1,98 @@ # main.py # Andrew Dinh # Python 3.6.1 -# Description: Get, parse, and interpret JSON files from IEX -import urllib, requests, json +# Description: +''' +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) -class iex: +FIRST TESTING WITH EXPENSE RATIO +''' - def __init__(self, newStock = 'spy') - self.iex +# 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 - def printDates(self) +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 = ''): + self.name = newStock + + def getDates(self): # returns beginning and end dates available + print("Getting dates from sources...") + # Gets first and last possible dates from each source + # IEX + print("Getting dates from IEX") + datesIEX = Stock.getDatesIEX(self) + print(datesIEX) + ''' + print("Getting dates from Alpha Vantage") + datesAV = Stock.getDatesAV(self) + print(datesAV) + ''' + + def getDatesIEX(self): + dates = [] + 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) + # 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 = [] + dates.append((firstDate, finalDate)) + return dates + +''' + def printDates(self): print("Getting data from IEX...") - url = ''.join(('https://api.iextrading.com/1.0/stock/', stock, '/chart/5y')) + 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(json_data) - #print(loaded_json) - #print(loaded_json[0]) - print("Printing dates given from IEX...") + print("Printing from IEX...") for i in range (0,len(loaded_json),1): a = loaded_json[i] print(a['date']) +''' def main(): - stock = 'spy' - IEX(stock) + #if __name__ == '__main__': + # install('oauth2client') -main() \ No newline at end of file + stock = 'spy' + spy = Stock(stock) + #print(spy.name) + Stock.getDates(stock) + #Stock.printDates(spy) + +main() From 7027cc9a816857e65abde47f6ceb6c08ee4fbae1 Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Sun, 16 Dec 2018 18:08:10 -0800 Subject: [PATCH 3/5] Create requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt 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 From 2a300a42f2e6e0341b9f4df0b4a653585a34b3c9 Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Sun, 16 Dec 2018 18:12:35 -0800 Subject: [PATCH 4/5] Update main.py --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 0b8e002..77e1baa 100644 --- a/main.py +++ b/main.py @@ -87,7 +87,7 @@ class Stock: def main(): #if __name__ == '__main__': - # install('oauth2client') + # install('requests') stock = 'spy' spy = Stock(stock) From 68f40e5c50291979afd82c355a92379f78bb9aa0 Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Mon, 17 Dec 2018 16:05:36 -0800 Subject: [PATCH 5/5] Moved main.py to StockData.py --- StockData.py | 145 +++++++++++++++++++++++++++ __pycache__/StockData.cpython-37.pyc | Bin 0 -> 2413 bytes main.py | 82 +-------------- 3 files changed, 146 insertions(+), 81 deletions(-) create mode 100644 StockData.py create mode 100644 __pycache__/StockData.cpython-37.pyc 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 0000000000000000000000000000000000000000..92af2c90f8e842581027eb801c728fd4f7edf8c1 GIT binary patch literal 2413 zcmZWqTW{Mo6ee}IoMcYBcD-)iuod-y*^TQoY0z#hGbXC^&Hlrw-+cqj1$xxyJQKb2M4&j{cZ%gPX4jH_sHu;1(~vCXVTwXM_^2?~}T-=GC4EJ!l4rsQS$7RMRZ+o>ztcqVK1f&+E2R zdD?1jJlsE6TwdRBoFBIKohn;c3fR%zyKKR0E_nP-u-sbmo56DPXz^}y`S!ANv*|BE z=XTI!N3CEnXe~8c-lDhQxA-D&G4B=&ZU;*qTYd}YB-G#2^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