From 1b6f314a2209d4e7675b656e61b10400487e4d41 Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Mon, 17 Dec 2018 22:19:20 -0800 Subject: [PATCH 1/9] Added find firstLastDate --- .gitignore | 2 ++ StockData.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc90468 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +__pycache__/StockData.cpython-37.pyc diff --git a/StockData.py b/StockData.py index 567401e..c7689aa 100644 --- a/StockData.py +++ b/StockData.py @@ -33,7 +33,7 @@ class Stock: print("Getting available dates for", self, "...") # 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') @@ -49,14 +49,23 @@ class Stock: print("Adding dates available to datesAV") datesAV = Stock.getDatesAV(self, 'all') #print("All dates (recent first):", datesAV, "\n") # Uncomment line to view output - + ''' # Tiingo print("\nDates from Tiingo...") firstLastDatesTiingo = Stock.getDatesTiingo(self, 'firstLast') print("First and last dates:", firstLastDatesTiingo) print("Adding dates available to datesTiingo") - datesTiingo = Stock.getDatesTiingo(self, 'all') + #datesTiingo = Stock.getDatesTiingo(self, 'all') # ADD BACK WHEN DONE #print("All dates (recent first):", datesTiingo, "\n") # Uncomment line to view output + ''' + listOfFirstLastDates = [] + listOfFirstLastDates.append(firstLastDatesIEX) + listOfFirstLastDates.append(firstLastDatesAV) + #listOfFirstLastDates.append(firstLastDatesTiingo) + #print(listOfFirstLastDates) + # Find the min and max date + firstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates) + print("\nAbsolute first and last date:", firstLastDates) def getDatesIEX(self, which): url = ''.join(('https://api.iextrading.com/1.0/stock/', self, '/chart/5y')) @@ -188,6 +197,64 @@ class Stock: dates.append(date) return dates + def getFirstLastDate(self, listOfFirstLastDates): + listOfFirstDates = [] + listOfLastDates = [] + #print(len(listOfFirstLastDates)) + for i in range (0, len(listOfFirstLastDates), 1): + temp = listOfFirstLastDates[i] + datesTemp = temp[0] + #print(datesTemp) + firstDate = datesTemp[0] + #print(firstDate) + lastDate = datesTemp[1] + #print(lastDate) + listOfFirstDates.append(firstDate) + listOfLastDates.append(lastDate) + #print(listOfFirstDates) + #print(listOfLastDates) + for i in range (0, len(listOfFirstDates), 1): + date = listOfFirstDates[i] + if i == 0: + firstDate = date + yearMonthDate = firstDate.split('-') + firstYear = yearMonthDate[0] + firstMonth = yearMonthDate[1] + firstDay = yearMonthDate[2] + else: + yearMonthDate = date.split('-') + year = yearMonthDate[0] + month = yearMonthDate[1] + day = yearMonthDate[2] + if year < firstYear or (year == firstYear and month < firstMonth) or (year == firstYear and month == firstMonth and day < firstDay): + firstDate = date + firstYear = year + firstMonth = month + firstDay = day + #print(firstDate) + for i in range(0, len(listOfLastDates),1): + date = listOfLastDates[i] + if i == 0: + lastDate = date + yearMonthDate = lastDate.split('-') + lastYear = yearMonthDate[0] + lastMonth = yearMonthDate[1] + lastDay = yearMonthDate[2] + else: + yearMonthDate = date.split('-') + year = yearMonthDate[0] + month = yearMonthDate[1] + day = yearMonthDate[2] + if year > lastYear or (year == lastYear and month > lastMonth) or (year == lastYear and month == lastMonth and day > lastDay): + lastDate = date + lastYear = year + lastMonth = month + lastDay = day + #print(lastDate) + firstLastDates = [] + firstLastDates.append((firstDate, lastDate)) + return firstLastDates + def main(): #if __name__ == '__main__': # install('requests') @@ -198,4 +265,4 @@ def main(): Stock.getDates(stock) #Stock.printDates(spy) -main() +main() \ No newline at end of file From e8720e3655dd24e64cf4976cbb09c963d1e1d5c4 Mon Sep 17 00:00:00 2001 From: Andrew Dinh <45807756+andrewkdinh@users.noreply.github.com> Date: Tue, 18 Dec 2018 17:59:31 +0000 Subject: [PATCH 2/9] Update StockData.py --- StockData.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/StockData.py b/StockData.py index c7689aa..bca450d 100644 --- a/StockData.py +++ b/StockData.py @@ -265,4 +265,5 @@ def main(): Stock.getDates(stock) #Stock.printDates(spy) -main() \ No newline at end of file +if __name__ == "__main__": + main() From 49cbc8300ed7970ed9ee0b1d3a4b02a0a00d6abd Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Tue, 18 Dec 2018 11:53:14 -0800 Subject: [PATCH 3/9] Update --- .gitignore | 3 + StockData.py | 149 +++++++++++++-------------- __pycache__/StockData.cpython-37.pyc | Bin 2413 -> 5380 bytes 3 files changed, 76 insertions(+), 76 deletions(-) diff --git a/.gitignore b/.gitignore index dc90468..5c17f9c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ __pycache__/StockData.cpython-37.pyc + +__pycache__/ +*.pyc diff --git a/StockData.py b/StockData.py index bca450d..047d51e 100644 --- a/StockData.py +++ b/StockData.py @@ -16,85 +16,83 @@ 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, "...") - # Gets first and last possible dates from each source - # Also gets all dates from each source +class Stock: + def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newDates = [], newListIEX = [], newListAV = [], newListTiingo = []): + self.name = newName # Name of stock + self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide + self.absFirstLastDates = newAbsFirstLastDates # Absolute first and last dates from all sources + self.dates = newDates # All available dates + + # List from each source containing: ['firstDate', 'lastDate', allDates, values] + self.listIEX = newListIEX # Dates available from IEX + self.listAV = newListAV # Dates available from AV + self.listTiingo = newListTiingo # Dates available from Tiingo + + def main(self): + # Makes list with [firstDate, lastDate, allDates, values] # IEX - print("\nDates from IEX...") - firstLastDatesIEX = Stock.getDatesIEX(self, 'firstLast') - print("First and last dates:", firstLastDatesIEX) - print("Adding dates available to datesIEX") - datesIEX = Stock.getDatesIEX(self, 'all') - #print("All dates (recent first):", datesIEX, "\n") # Uncomment line to view output + print("\nIEX") + listIEX = Stock.getIEX(self) - # Alpha Vantage - print("\nDates from Alpha Vantage...") - firstLastDatesAV = Stock.getDatesAV(self, 'firstLast') - print("First and last dates:", firstLastDatesAV) - print("Adding dates available to datesAV") - datesAV = Stock.getDatesAV(self, 'all') - #print("All dates (recent first):", datesAV, "\n") # Uncomment line to view output - ''' - # Tiingo - print("\nDates from Tiingo...") - firstLastDatesTiingo = Stock.getDatesTiingo(self, 'firstLast') - print("First and last dates:", firstLastDatesTiingo) - print("Adding dates available to datesTiingo") - #datesTiingo = Stock.getDatesTiingo(self, 'all') # ADD BACK WHEN DONE - #print("All dates (recent first):", datesTiingo, "\n") # Uncomment line to view output - ''' - listOfFirstLastDates = [] - listOfFirstLastDates.append(firstLastDatesIEX) - listOfFirstLastDates.append(firstLastDatesAV) - #listOfFirstLastDates.append(firstLastDatesTiingo) - #print(listOfFirstLastDates) - # Find the min and max date - firstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates) - print("\nAbsolute first and last date:", firstLastDates) + def set(self, newName, newfirstLastDates, newAbsFirstLastDates, newDates, newListIEX, newListAV, newListTiingo): + self.name = newName # Name of stock + self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide + self.absFirstLastDates = newAbsFirstLastDates # Absolute first and last dates from all sources + self.dates = newDates # All available dates - def getDatesIEX(self, which): + # List from each source containing: ['firstDate', 'lastDate', allDates, values] + self.listIEX = newListIEX # Dates available from IEX + self.listAV = newListAV # Dates available from AV + self.listTiingo = newListTiingo # Dates available from Tiingo + + def setFirstLastDates(newFirstLastDates): + self.firstLastDates = newFirstLastDates + def setAbsFirstLastDates(newAbsFirstLastDates): + self.absFirstLastDates = newAbsFirstLastDates + def setDates(newDates): + self.dates = newDates + def setListIEX(newListIEX): + self.listIEX = newListIEX + def setListAV(newListAV): + self.listAV = newListAV + def setListTiingo(newListTiingo): + self.listTiingo = newListTiingo + + def getIEX(self): 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) + listIEX = [] + + # Adding (firstDate, lastDate) to list + # Find firstDate (comes first) + firstLine = loaded_json[0] + #print("firstLine:", firstLine) + firstDate = firstLine['date'] + #print("firstDate:",firstDate) + # Find lastDate (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)) - elif which == 'all': -# 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 + lastDate = lastLine['date'] + #print("last date:", lastDate) + listIEX.append(firstDate) + listIEX.append(lastDate) + + allDates = [] +# 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'] + allDates.append(date) + listIEX.append(allDates) + #print(listIEX) + return listIEX def getDatesAV(self, which): url = ''.join(('https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=', self, '&apikey=', apiAV)) @@ -164,15 +162,15 @@ class Stock: print(list1[endNum]) ''' firstDate = loaded_json['startDate'] - finalDate = loaded_json['endDate'] + lastDate = loaded_json['endDate'] #print(firstDate) - #print(finalDate) + #print(lastDate) dates = [] if which == 'firstLast': #print("URL:", url) - dates.append((firstDate, finalDate)) + dates.append((firstDate, lastDate)) elif which == 'all': - url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', finalDate)) + url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', lastDate)) # https://api.tiingo.com/tiingo/daily//prices?startDate=2012-1-1&endDate=2016-1-1 #print("Second URL:", url2) requestResponse2 = requests.get(url2, headers=headers) @@ -256,13 +254,12 @@ class Stock: return firstLastDates def main(): - #if __name__ == '__main__': - # install('requests') - stock = 'spy' spy = Stock(stock) - #print(spy.name) - Stock.getDates(stock) + + Stock.set('spy', 2,2,2,2,2,2,2) + + Stock.main(stock) #Stock.printDates(spy) if __name__ == "__main__": diff --git a/__pycache__/StockData.cpython-37.pyc b/__pycache__/StockData.cpython-37.pyc index 92af2c90f8e842581027eb801c728fd4f7edf8c1..b083a5ec126194059382ec7b507f94d1f0705fed 100644 GIT binary patch literal 5380 zcmcIo&2JmW72nwp?gu|aeZ=usQ^rXwvyDYrmTcLz;wZKfyR~K1R_tV?2uA}wku z&#q#NU7`p3S|I15C={S5&`VL|)Jre@GxpF+bEyA;7RV+2z1bx#(GFk~Ev0=kJM(7V zoA=)Q=FNUoEIJB)U#}cn|L&Bc{DWQwKMj=+@q{-(xZ*0Dxs0o>$~9NxIyXAHtIN`G zjZYMw%RF^VG1|yDb;-MBj>Ik?TR3*c%4qg`@(B? zyp4`u;a=pUE#Q?M(QcsH?23x-HCmN0>Nd72!hf{uhmp@`9JhRDe(vVAPwy`*uH1Cp zpWpt}t$61yHoc8Ym%MY0xpNJEp}9DJ(VuHB&TTAQnp-@-=$@JLFXH8VbI#kCZ!R?F zFV4+378>XLdA`8sy~eE9Jm0+7@D`tAy~=8X=-_9f@*$ou2JscxL4h6Ert;#iX>wTR z8B1-0)$6H6TqCnajypV0R^bI+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7>Akb zUd8PjAP6vel@&X$p>1|sQJ zH`adwWl@^OZ*b9mgmIVTj3D?UuC5d+FnDn7UEkHH*J0HI7H6#O!z=)e4s?z)fk+qS zwm(7+)+p>rXZ4__3yh#MKV4Jq2do^6BhuKayg#^l;Fxl3^+4h|5*99FO=hk_FFgl> z&>7vLGt{Ot+Nhr3G%@vXgZRxsL98TBe?WuNA)o|NRRi@61q3M9az#d48LGJgIyG0H zX{TDI4~<-dzTPIMMozi@ToJuo=f-7nYUU<4p|+rJL0!sAvTx_c(9Ese4qzrq;^kL& zGAk?P`WfY=OPEirGj)yp^66t51rEYxo`QoM|fL8;+&n8+H7Cj)JNXLChh9u!E~Lc`#`{Rh6c z*ss}z_ACiwF@uBFI2)yf3IQy%3|4^gG>Hp6N*GTIgY|lT%nMzxxa}8eMD5I~>UDOB!!5OnKjQueo*I-wUg)H0zC631|LS;e2LW@8r zD&Y75CB!V~7NMkHfFQJ@*uVt?ibgF3ehpaYj!~po|5SF@Q2+OqCMUNEJJ` zhCr7oMYP-mTMOVWjp2SYf&2LQxX$nrz8v2{FKNKLAx^%7Z02QQ65rq3*jjgY*LOD7 zcipY0+j~zof4-5PbdQqg{=m8ce0lDl+#k_laDFR^vrcqU-3z;Zb=Mc6pN=>(4+ob- zso4UaOj79J30D#6AwEWN0R`rD@d=7cAP}H56PKY`D@!tnYdCft#iuAfLs13c%y&mi z3NbzHZwUALR?euV8A*L~4v(P%`JU}EtR3S^kT72nwfzB2+-l1zVBppjzFq7~h` zU|pA54;1pl1cY~2@(OJ^J%xuMC%_JP42JMn;8mAtUD+rPHO0~8trj)# zeP_Tp>k*Q~;HU7mQZE53xjQwAI#Fd_4TQ-aQ32#F&ZVJeZt hjY{8l7MY&3u Date: Tue, 18 Dec 2018 11:54:50 -0800 Subject: [PATCH 4/9] Delete StockData.cpython-37.pyc --- __pycache__/StockData.cpython-37.pyc | Bin 5380 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 __pycache__/StockData.cpython-37.pyc diff --git a/__pycache__/StockData.cpython-37.pyc b/__pycache__/StockData.cpython-37.pyc deleted file mode 100644 index b083a5ec126194059382ec7b507f94d1f0705fed..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5380 zcmcIo&2JmW72nwp?gu|aeZ=usQ^rXwvyDYrmTcLz;wZKfyR~K1R_tV?2uA}wku z&#q#NU7`p3S|I15C={S5&`VL|)Jre@GxpF+bEyA;7RV+2z1bx#(GFk~Ev0=kJM(7V zoA=)Q=FNUoEIJB)U#}cn|L&Bc{DWQwKMj=+@q{-(xZ*0Dxs0o>$~9NxIyXAHtIN`G zjZYMw%RF^VG1|yDb;-MBj>Ik?TR3*c%4qg`@(B? zyp4`u;a=pUE#Q?M(QcsH?23x-HCmN0>Nd72!hf{uhmp@`9JhRDe(vVAPwy`*uH1Cp zpWpt}t$61yHoc8Ym%MY0xpNJEp}9DJ(VuHB&TTAQnp-@-=$@JLFXH8VbI#kCZ!R?F zFV4+378>XLdA`8sy~eE9Jm0+7@D`tAy~=8X=-_9f@*$ou2JscxL4h6Ert;#iX>wTR z8B1-0)$6H6TqCnajypV0R^bI+L}~F7A46&LG9O3j@CiPNGS8>@8z>9>2!9i0kssy9 zP?q>{*zZJQ+#_=&O2dLK^rKS6a}7^80}``XsVlJx%3=*vjdf5hHbC__2WrG7XpSqG zlC4(FL=U`eKPffaB8+Z%VRW6mk&JsA;m3!Y4C$xD?6kw^=E`SDjw;Ld6Nk#Rc01VY zR;@%2{Z7+01OM?V=5@zW8JT(tP0KIMZlN_5?VxrmlVN9dh$&{(RMH<6F^&a)d*#~Y z^-n`zgzFw}2d(w%-Nx>=A4K8$C++CQ?#6m^H{fU60dF_Fs0%h}LkRL_cJ>mhUT+8O zs9t}AELZ|jST6n3sO*&_`^Zy* zy4uqn7_F)$27)|@1kI4-HhnT>RTX)>3i791R!Sy_Mc?+?K?Sui2cqZ}b6C$w)uOHj zO+-H`>5(eY=1zlQ;Akccv}KZ{T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&?H$(`8nELM`o9@%e7?vy0_=*#P1q1^}x3jw8-Q_j<6~7Ob3`$1;4z{ zj%FQY?5#tq`71OD*FhBI7`l3datnZ?CHax0zQl1Q z<8YA0lvwY`a5o+jzFHllPYm>drCv{*dVRagcRN%s)a#FSy-wOACZV1{4a4@E;3VA9+IFhr}hCQ)adj@P|^xht%;33nbQ+ Z)YKnP-WZZmbW{3NEvuzpDcmwG_8$#14%h$y From dbef150f9a346603b0844edc0c225592a7602d9e Mon Sep 17 00:00:00 2001 From: Andrew Dinh <45807756+andrewkdinh@users.noreply.github.com> Date: Tue, 18 Dec 2018 19:55:51 +0000 Subject: [PATCH 5/9] Update .gitignore --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5c17f9c..513fa7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ - __pycache__/StockData.cpython-37.pyc - __pycache__/ *.pyc From 78a20fe11d75d674588408b61da481b7f7e64ecd Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Tue, 18 Dec 2018 14:35:51 -0800 Subject: [PATCH 6/9] Update StockData.py --- StockData.py | 267 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 186 insertions(+), 81 deletions(-) diff --git a/StockData.py b/StockData.py index 047d51e..a702608 100644 --- a/StockData.py +++ b/StockData.py @@ -13,9 +13,10 @@ Returns all available dates and prices for each stock requested. apiAV = 'O42ICUV58EIZZQMU' apiBarchart = 'a17fab99a1c21cd6f847e2f82b592838' -apiTiingo = '2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8' +apiTiingo = '2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8' # Limited to 400 requests/day import requests, json +from datetime import datetime class Stock: def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newDates = [], newListIEX = [], newListAV = [], newListTiingo = []): @@ -23,19 +24,18 @@ class Stock: self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide self.absFirstLastDates = newAbsFirstLastDates # Absolute first and last dates from all sources self.dates = newDates # All available dates - - # List from each source containing: ['firstDate', 'lastDate', allDates, values] + ''' + Format: + # List from each source containing: [firstDate, lastDate, allDates, values, timeFrame] + # firstDate & lastDate = '2018-12-18' (year-month-date) + allDates = ['2018-12-17', '2018-12-14'] (year-month-date) + values (close) = ['164.6307', 164.6307] + timeFrame = [days, weeks, years] + ''' self.listIEX = newListIEX # Dates available from IEX self.listAV = newListAV # Dates available from AV self.listTiingo = newListTiingo # Dates available from Tiingo - def main(self): - # Makes list with [firstDate, lastDate, allDates, values] - - # IEX - print("\nIEX") - listIEX = Stock.getIEX(self) - def set(self, newName, newfirstLastDates, newAbsFirstLastDates, newDates, newListIEX, newListAV, newListTiingo): self.name = newName # Name of stock self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide @@ -63,13 +63,14 @@ class Stock: def getIEX(self): 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) + print("\nSending request to:", url) f = requests.get(url) json_data = f.text loaded_json = json.loads(json_data) listIEX = [] - # Adding (firstDate, lastDate) to list + print("\nFinding first and last date") + # Adding (firstDate, lastDate) to listIEX # Find firstDate (comes first) firstLine = loaded_json[0] #print("firstLine:", firstLine) @@ -83,56 +84,107 @@ class Stock: #print("last date:", lastDate) listIEX.append(firstDate) listIEX.append(lastDate) + print(listIEX[0], ',', listIEX[1]) + print("\nFinding all dates given") allDates = [] # 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): + for i in range(len(loaded_json)-1, -1, -1): line = loaded_json[i] date = line['date'] allDates.append(date) listIEX.append(allDates) - #print(listIEX) + #print(listIEX[1]) + print("Uncomment above line in code to see output") + + print("\nFinding close values for each date") + values = [] +# for i in range(0, len(loaded_json), 1): # If you want to do oldest first + for i in range(len(loaded_json)-1, -1, -1): + line = loaded_json[i] + value = line['close'] + values.append(value) + listIEX.append(values) + #print(listIEX[3]) + print("Uncomment above line in code to see output") + + print("\nFinding time frame given [days, weeks, years]") + timeFrame = [] + d1 = datetime.strptime(firstDate, "%Y-%m-%d") + d2 = datetime.strptime(lastDate, "%Y-%m-%d") + timeFrameDays = abs((d2 - d1).days) + #print(timeFrameDays) + timeFrameYears = float(timeFrameDays / 365) + timeFrameWeeks = float(timeFrameDays / 7) + timeFrame.append(timeFrameDays) + timeFrame.append(timeFrameWeeks) + timeFrame.append(timeFrameYears) + listIEX.append(timeFrame) + print(listIEX[4]) + return listIEX - def getDatesAV(self, which): + def getAV(self): + listAV = [] 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) + print("\nSending request to:", 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)) - elif which == 'all': - dates = listOfDates - return dates - def getDatesTiingo(self, which): + firstDate = listOfDates[-1] + lastDate = listOfDates[0] + #print("firstDate:", firstDate) + #print("lastDate:", lastDate) + listAV.append(firstDate) + listAV.append(lastDate) + listAV.append(listOfDates) + + print("\nFinding first and last date") + print(listAV[0], ',', listAV[1]) + print("\nFinding all dates given") + #print(listAV[2]) + print("Uncomment above line in code to see output") + + print("\nFinding close values for each date") + values = [] + for i in range(0, len(listOfDates), 1): + temp = listOfDates[i] + loaded_json2 = monthlyTimeSeries[temp] + value = loaded_json2['4. close'] + values.append(value) + listAV.append(values) + #print(listOfDates[0]) + #i = listOfDates[0] + #print(monthlyTimeSeries[i]) + #print(listAV[3]) + print("Uncomment above line in code to see output") + + print("\nFinding time frame given [days, weeks, years]") + timeFrame = [] + d1 = datetime.strptime(firstDate, "%Y-%m-%d") + d2 = datetime.strptime(lastDate, "%Y-%m-%d") + timeFrameDays = abs((d2 - d1).days) + #print(timeFrameDays) + timeFrameYears = float(timeFrameDays / 365) + timeFrameWeeks = float(timeFrameDays / 7) + timeFrame.append(timeFrameDays) + timeFrame.append(timeFrameWeeks) + timeFrame.append(timeFrameYears) + listAV.append(timeFrame) + print(listAV[4]) + + return listAV + + def getTiingo(self): ''' - headers = { - 'Content-Type': 'application/json', - 'Authorization' : 'Token ' - } - requestResponse = requests.get("https://api.tiingo.com/api/test/", - headers=headers) - print(requestResponse.json()) - #OR we can use the token directly in the url - headers = { 'Content-Type': 'application/json' } @@ -146,6 +198,7 @@ class Stock: 'Authorization' : token } url = ''.join(('https://api.tiingo.com/tiingo/daily/', self)) + print("\nSending request to:", url) requestResponse = requests.get(url, headers=headers) #print(requestResponse.json()) loaded_json = requestResponse.json() @@ -161,52 +214,70 @@ class Stock: print(list1[startNum]) print(list1[endNum]) ''' + listTiingo = [] + + print("\nFinding first and last date") firstDate = loaded_json['startDate'] lastDate = loaded_json['endDate'] #print(firstDate) #print(lastDate) + listTiingo.append(firstDate) + listTiingo.append(lastDate) + print(listTiingo[0], ',', listTiingo[1]) + + print("\nFinding all dates given") dates = [] - if which == 'firstLast': - #print("URL:", url) - dates.append((firstDate, lastDate)) - elif which == 'all': - url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', lastDate)) - # https://api.tiingo.com/tiingo/daily//prices?startDate=2012-1-1&endDate=2016-1-1 - #print("Second URL:", url2) - requestResponse2 = requests.get(url2, headers=headers) - loaded_json2 = requestResponse2.json() - #print(loaded_json2) - #print(len(loaded_json2)) - ''' - print(loaded_json2[0]) - temp = loaded_json2[0] - temp2 = temp['date'] - temp3 = temp2.split('T00:00:00.000Z') - print(temp2) - print(temp3) - print(temp3[0]) - print(temp3[1]) - ''' - for i in range(len(loaded_json2)-1, 0, -1): - line = loaded_json2[i] - dateWithTime = line['date'] - temp = dateWithTime.split('T00:00:00.000Z') - date = temp[0] - dates.append(date) - return dates + values = [] # Used loop for finding values + url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', lastDate)) + # https://api.tiingo.com/tiingo/daily//prices?startDate=2012-1-1&endDate=2016-1-1 + print("\nSending request to:", url2) + requestResponse2 = requests.get(url2, headers=headers) + loaded_json2 = requestResponse2.json() + #print(loaded_json2) + #print(len(loaded_json2)) + for i in range(len(loaded_json2)-1, -1, -1): + line = loaded_json2[i] + dateWithTime = line['date'] + temp = dateWithTime.split('T00:00:00.000Z') + date = temp[0] + dates.append(date) + + value = line['close'] + values.append(value) + listTiingo.append(dates) + #print(listTiingo[2]) + print("Uncomment above line in code to see output") + + print("Finding close values for each date") + # Used loop from finding dates + listTiingo.append(values) + #print(listTiingo[3]) + print("Uncomment above line in code to see output") + + print("Finding time frame given [days, weeks, years]") + timeFrame = [] + d1 = datetime.strptime(firstDate, "%Y-%m-%d") + d2 = datetime.strptime(lastDate, "%Y-%m-%d") + timeFrameDays = abs((d2 - d1).days) + #print(timeFrameDays) + timeFrameYears = float(timeFrameDays / 365) + timeFrameWeeks = float(timeFrameDays / 7) + timeFrame.append(timeFrameDays) + timeFrame.append(timeFrameWeeks) + timeFrame.append(timeFrameYears) + listTiingo.append(timeFrame) + print(listTiingo[4]) + + return listTiingo def getFirstLastDate(self, listOfFirstLastDates): listOfFirstDates = [] listOfLastDates = [] #print(len(listOfFirstLastDates)) for i in range (0, len(listOfFirstLastDates), 1): - temp = listOfFirstLastDates[i] - datesTemp = temp[0] - #print(datesTemp) - firstDate = datesTemp[0] - #print(firstDate) - lastDate = datesTemp[1] - #print(lastDate) + firstLastDates = listOfFirstLastDates[i] + firstDate = firstLastDates[0] + lastDate = firstLastDates[1] listOfFirstDates.append(firstDate) listOfLastDates.append(lastDate) #print(listOfFirstDates) @@ -249,16 +320,50 @@ class Stock: lastMonth = month lastDay = day #print(lastDate) - firstLastDates = [] - firstLastDates.append((firstDate, lastDate)) - return firstLastDates + absFirstLastDates = [] + absFirstLastDates.append(firstDate) + absFirstLastDates.append(lastDate) + return absFirstLastDates + + def main(self): + # Makes list with ['firstDate', 'lastDate', [allDates], values] + + listOfFirstLastDates = [] + + # IEX + print("\nIEX") + listIEX = Stock.getIEX(self) + #print(listIEX) + listOfFirstLastDates.append((listIEX[0], listIEX[1])) + + # Alpha Vantage + print("\nAlpha Vantage (AV)") + listAV = Stock.getAV(self) + #print(listAV) + listOfFirstLastDates.append((listAV[0], listAV[1])) + + print("\nTiingo") # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY + #listTiingo = Stock.getTiingo(self) + #print(listTiingo) + #listOfFirstLastDates.append((listTiingo[0], listTiingo[1])) + + #print(listOfFirstLastDates) + absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates) + print("\nThe absolute first date was:", absFirstLastDates[0]) + print("The absolute last date was:", absFirstLastDates[1]) + + ''' + FIGURE OUT HOW TO MAKE EACH OF THESE LISTS AN ATTRIBUTE OF CLASS STOCK + self.listIEX = listIEX + self.listAV = listAV + self.listTiingo = listTiingo + + self.absFirstLastDates = absFirstLastDates + ''' def main(): stock = 'spy' spy = Stock(stock) - - Stock.set('spy', 2,2,2,2,2,2,2) - Stock.main(stock) #Stock.printDates(spy) From 1c9d09aebe7efae3e7cbb492111d6ac45b9f33fa Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Tue, 18 Dec 2018 17:45:33 -0800 Subject: [PATCH 7/9] Update StockData.py --- StockData.py | 73 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/StockData.py b/StockData.py index a702608..b10be94 100644 --- a/StockData.py +++ b/StockData.py @@ -1,10 +1,7 @@ # StockData.py # Andrew Dinh # Python 3.6.1 -# Description: -''' -Returns all available dates and prices for each stock requested. -''' +# Description: Returns all available dates and prices for each stock requested. # Alpha Vantage API Key: O42ICUV58EIZZQMU # Barchart API Key: a17fab99a1c21cd6f847e2f82b592838 @@ -19,11 +16,12 @@ import requests, json from datetime import datetime class Stock: - def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newDates = [], newListIEX = [], newListAV = [], newListTiingo = []): + + def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newfinalDatesAndClose = [], newAllLists = []): self.name = newName # Name of stock self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide self.absFirstLastDates = newAbsFirstLastDates # Absolute first and last dates from all sources - self.dates = newDates # All available dates + self.finalDatesAndClose = newfinalDatesAndClose # All available dates ''' Format: # List from each source containing: [firstDate, lastDate, allDates, values, timeFrame] @@ -32,10 +30,8 @@ class Stock: values (close) = ['164.6307', 164.6307] timeFrame = [days, weeks, years] ''' - self.listIEX = newListIEX # Dates available from IEX - self.listAV = newListAV # Dates available from AV - self.listTiingo = newListTiingo # Dates available from Tiingo - + self.allLists = newAllLists + ''' def set(self, newName, newfirstLastDates, newAbsFirstLastDates, newDates, newListIEX, newListAV, newListTiingo): self.name = newName # Name of stock self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide @@ -59,9 +55,10 @@ class Stock: self.listAV = newListAV def setListTiingo(newListTiingo): self.listTiingo = newListTiingo + ''' def getIEX(self): - url = ''.join(('https://api.iextrading.com/1.0/stock/', self, '/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("\nSending request to:", url) f = requests.get(url) @@ -77,7 +74,6 @@ class Stock: firstDate = firstLine['date'] #print("firstDate:",firstDate) # Find lastDate (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) lastDate = lastLine['date'] @@ -126,7 +122,7 @@ class Stock: def getAV(self): listAV = [] - url = ''.join(('https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=', self, '&apikey=', apiAV)) + url = ''.join(('https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=', self.name, '&apikey=', apiAV)) # https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=MSFT&apikey=demo print("\nSending request to:", url) f = requests.get(url) @@ -203,7 +199,6 @@ class Stock: #print(requestResponse.json()) loaded_json = requestResponse.json() #print(loaded_json) - #print(len(loaded_json)) ''' list1 = list(loaded_json) for i in range (0, len(list1), 1): @@ -325,6 +320,20 @@ class Stock: absFirstLastDates.append(lastDate) return absFirstLastDates + def getFinalDatesAndClose(self): + # finalDates and finalClose will coincide (aka i = 1 will correspond to one another) + finalDatesAndClose = () # Will combine finalDates then finalClose + finalDates = [] + finalClose = [] + #print(self.absFirstLastDates) + absFirstDate = self.absFirstLastDates[0] + absLastDate = self.absFirstLastDates[1] + date = absFirstDate + #while date != absLastDate or date == absLastDate: + + + + def main(self): # Makes list with ['firstDate', 'lastDate', [allDates], values] @@ -335,37 +344,45 @@ class Stock: listIEX = Stock.getIEX(self) #print(listIEX) listOfFirstLastDates.append((listIEX[0], listIEX[1])) + self.allLists.append(listIEX) # Alpha Vantage print("\nAlpha Vantage (AV)") listAV = Stock.getAV(self) #print(listAV) listOfFirstLastDates.append((listAV[0], listAV[1])) + self.allLists.append(listAV) print("\nTiingo") # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY #listTiingo = Stock.getTiingo(self) #print(listTiingo) #listOfFirstLastDates.append((listTiingo[0], listTiingo[1])) + # self.allLists.append(listTiingo) #print(listOfFirstLastDates) absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates) - print("\nThe absolute first date was:", absFirstLastDates[0]) - print("The absolute last date was:", absFirstLastDates[1]) - - ''' - FIGURE OUT HOW TO MAKE EACH OF THESE LISTS AN ATTRIBUTE OF CLASS STOCK - self.listIEX = listIEX - self.listAV = listAV - self.listTiingo = listTiingo + print("\nThe absolute first date with close values is:", absFirstLastDates[0]) + print("The absolute last date with close values is:", absFirstLastDates[1]) self.absFirstLastDates = absFirstLastDates - ''' -def main(): - stock = 'spy' - spy = Stock(stock) - Stock.main(stock) - #Stock.printDates(spy) + finalDatesAndClose = Stock.getFinalDatesAndClose(self) # Returns [List of Dates, List of Corresponding Close Values] + #print(finalDatesAndClose) + + #print(self.allLists) + +def main(): # For testing purposes + stockName = 'aapl' + stock1 = Stock(stockName) + print("Finding available dates and close values for", stock1.name) + Stock.main(stock1) + +def imported(): # When this file has been imported + # Add stuff here + return if __name__ == "__main__": main() + +else: + imported() \ No newline at end of file From f0d0fccf8b6597dc2422f69d8de616b70448a49d Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Wed, 19 Dec 2018 15:52:31 -0800 Subject: [PATCH 8/9] Update StockData.py Added function to combine all dates and find the average close values for each date --- StockData.py | 142 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 108 insertions(+), 34 deletions(-) diff --git a/StockData.py b/StockData.py index b10be94..d8fd264 100644 --- a/StockData.py +++ b/StockData.py @@ -193,7 +193,7 @@ class Stock: 'Content-Type': 'application/json', 'Authorization' : token } - url = ''.join(('https://api.tiingo.com/tiingo/daily/', self)) + url = ''.join(('https://api.tiingo.com/tiingo/daily/', self.name)) print("\nSending request to:", url) requestResponse = requests.get(url, headers=headers) #print(requestResponse.json()) @@ -281,15 +281,15 @@ class Stock: date = listOfFirstDates[i] if i == 0: firstDate = date - yearMonthDate = firstDate.split('-') - firstYear = yearMonthDate[0] - firstMonth = yearMonthDate[1] - firstDay = yearMonthDate[2] + yearMonthDay = firstDate.split('-') + firstYear = yearMonthDay[0] + firstMonth = yearMonthDay[1] + firstDay = yearMonthDay[2] else: - yearMonthDate = date.split('-') - year = yearMonthDate[0] - month = yearMonthDate[1] - day = yearMonthDate[2] + yearMonthDay = date.split('-') + year = yearMonthDay[0] + month = yearMonthDay[1] + day = yearMonthDay[2] if year < firstYear or (year == firstYear and month < firstMonth) or (year == firstYear and month == firstMonth and day < firstDay): firstDate = date firstYear = year @@ -300,15 +300,15 @@ class Stock: date = listOfLastDates[i] if i == 0: lastDate = date - yearMonthDate = lastDate.split('-') - lastYear = yearMonthDate[0] - lastMonth = yearMonthDate[1] - lastDay = yearMonthDate[2] + yearMonthDay = lastDate.split('-') + lastYear = yearMonthDay[0] + lastMonth = yearMonthDay[1] + lastDay = yearMonthDay[2] else: - yearMonthDate = date.split('-') - year = yearMonthDate[0] - month = yearMonthDate[1] - day = yearMonthDate[2] + yearMonthDay = date.split('-') + year = yearMonthDay[0] + month = yearMonthDay[1] + day = yearMonthDay[2] if year > lastYear or (year == lastYear and month > lastMonth) or (year == lastYear and month == lastMonth and day > lastDay): lastDate = date lastYear = year @@ -322,17 +322,89 @@ class Stock: def getFinalDatesAndClose(self): # finalDates and finalClose will coincide (aka i = 1 will correspond to one another) - finalDatesAndClose = () # Will combine finalDates then finalClose + finalDatesAndClose = [] # Will combine finalDates then finalClose finalDates = [] finalClose = [] #print(self.absFirstLastDates) absFirstDate = self.absFirstLastDates[0] absLastDate = self.absFirstLastDates[1] date = absFirstDate - #while date != absLastDate or date == absLastDate: - + allLists = self.allLists + while date != absLastDate: # DOESN'T DO LAST DATE + tempListOfClose = [] + found = False + for j in range(0, len(allLists), 1): # Look for date in all lists + list1 = allLists[j] + listOfDates = list1[2] + listOfClose = list1[3] + for k in range(0, len(listOfDates), 1): + if listOfDates[k] == date: + if found == False: + finalDates.append(date) + found = True + #print(listOfDates[k]) + #print(listOfClose[k]) + #print(listOfClose) + tempListOfClose.append(float(listOfClose[k])) + k = len(listOfDates) # Dates don't repeat + if found == True: + sum = 0 + for r in range(0, len(tempListOfClose), 1): + sum = sum + tempListOfClose[r] + close = sum/len(tempListOfClose) + + finalClose.append(close) + #print(close) + + # Go to the next day + yearMonthDay = date.split('-') + year = int(yearMonthDay[0]) + month = int(yearMonthDay[1]) + day = int(yearMonthDay[2]) + + day = day + 1 + if day == 32 and month == 12: # Next year + day = 1 + month = 2 + year = year + 1 + elif day == 32: # Next month + month = month + 1 + day = 1 + if day < 10: + day = ''.join(('0', str(day))) + if month < 10: + month = ''.join(('0', str(month))) + date = ''.join((str(year), '-', str(month), '-', str(day))) + #print(date) + + # For last date + finalDates.append(date) + tempListOfClose = [] + for j in range(0, len(allLists), 1): # Look for date in all lists + list1 = allLists[j] + listOfDates = list1[2] + listOfClose = list1[3] + for k in range(0, len(listOfDates), 1): + if listOfDates[k] == date: + tempListOfClose.append(float(listOfClose[k])) + k = len(listOfDates) # Dates don't repeat + sum = 0 + for r in range(0, len(tempListOfClose), 1): + sum = sum + tempListOfClose[r] + close = sum/len(tempListOfClose) + finalClose.append(close) + #print(finalDates) + #print(finalClose) + + # Want lists from most recent to oldest, comment this out if you don't want that + finalDates = list(reversed(finalDates)) + finalClose = list(reversed(finalClose)) + + finalDatesAndClose.append(finalDates) + finalDatesAndClose.append(finalClose) + return finalDatesAndClose def main(self): # Makes list with ['firstDate', 'lastDate', [allDates], values] @@ -353,23 +425,25 @@ class Stock: listOfFirstLastDates.append((listAV[0], listAV[1])) self.allLists.append(listAV) - print("\nTiingo") # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY - #listTiingo = Stock.getTiingo(self) + # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY + print("\nTiingo") + listTiingo = Stock.getTiingo(self) #print(listTiingo) - #listOfFirstLastDates.append((listTiingo[0], listTiingo[1])) - # self.allLists.append(listTiingo) - - #print(listOfFirstLastDates) - absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates) - print("\nThe absolute first date with close values is:", absFirstLastDates[0]) - print("The absolute last date with close values is:", absFirstLastDates[1]) - - self.absFirstLastDates = absFirstLastDates - - finalDatesAndClose = Stock.getFinalDatesAndClose(self) # Returns [List of Dates, List of Corresponding Close Values] - #print(finalDatesAndClose) + listOfFirstLastDates.append((listTiingo[0], listTiingo[1])) + self.allLists.append(listTiingo) + #print(self.allLists) + #print(listOfFirstLastDates) + self.absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates) + print("\nThe absolute first date with close values is:", self.absFirstLastDates[0]) + print("The absolute last date with close values is:", self.absFirstLastDates[1]) + + print("\nCombining dates and finding average close values") + self.finalDatesAndClose = Stock.getFinalDatesAndClose(self) # Returns [List of Dates, List of Corresponding Close Values] + #print("All dates available:", self.finalDatesAndClose[0]) + #print("All close values:\n", self.finalDatesAndClose[1]) + print("Uncomment above line in code to see output") def main(): # For testing purposes stockName = 'aapl' From 188e095961fb7c4b07a3411bde77f67cec26fed9 Mon Sep 17 00:00:00 2001 From: Andrew Dinh Date: Wed, 19 Dec 2018 16:14:03 -0800 Subject: [PATCH 9/9] Update StockData.py --- StockData.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/StockData.py b/StockData.py index d8fd264..04d9f4d 100644 --- a/StockData.py +++ b/StockData.py @@ -13,6 +13,7 @@ apiBarchart = 'a17fab99a1c21cd6f847e2f82b592838' apiTiingo = '2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8' # Limited to 400 requests/day import requests, json +import importlib.util, sys # To check whether a package is installed from datetime import datetime class Stock: @@ -407,6 +408,12 @@ class Stock: return finalDatesAndClose def main(self): + + package_name = 'requests' + spec = importlib.util.find_spec(package_name) + if spec is None: + print(package_name +" is not installed\nPlease type in 'pip install -r requirements.txt' to install all required packages") + # Makes list with ['firstDate', 'lastDate', [allDates], values] listOfFirstLastDates = [] @@ -426,12 +433,13 @@ class Stock: self.allLists.append(listAV) # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY + ''' print("\nTiingo") listTiingo = Stock.getTiingo(self) #print(listTiingo) listOfFirstLastDates.append((listTiingo[0], listTiingo[1])) self.allLists.append(listTiingo) - + ''' #print(self.allLists) #print(listOfFirstLastDates)