From 192fea22b0fc4f2e5a4acdaa8308bcc39308a1d5 Mon Sep 17 00:00:00 2001 From: Dale Nguyen Date: Sat, 27 Feb 2021 22:06:50 -0500 Subject: [PATCH] Get all historical data since 01/01/2000 --- changelog.md | 12 ++++++++++++ setup.py | 2 +- stockai/__init__.py | 2 +- stockai/utils.py | 33 +++++++++++++++++--------------- stockai/wealthsimple/__init__.py | 2 +- stockai/yahoo/__init__.py | 2 +- stockai/yahoo/base.py | 16 +++++++--------- tests/yahoo.py | 2 +- 8 files changed, 42 insertions(+), 29 deletions(-) diff --git a/changelog.md b/changelog.md index 0821486..bbd6fcb 100644 --- a/changelog.md +++ b/changelog.md @@ -11,6 +11,18 @@ --- +## [1.4.0] - 2021-02-27 + +#### - :bug: [Bug Fix] + +- Get all historical data since 01/01/2000 + +## [1.3.0] - 2021-02-27 + +#### - :nail_care: [Polish] + +- Use unix timestamp for date + ## [1.2.0] - 2021-02-27 #### - :rocket: [New Feature] diff --git a/setup.py b/setup.py index 1ae9533..47375ee 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name="stockai", - version="1.2.0", + version="1.4.0", author="Dale Nguyen", author_email="dale@dalenguyen.me", description="Get stock info from Yahoo! Finance", diff --git a/stockai/__init__.py b/stockai/__init__.py index 3ab1531..3242ce0 100644 --- a/stockai/__init__.py +++ b/stockai/__init__.py @@ -2,7 +2,7 @@ Stockai - get stock information from Yahoo! Finance """ -__version__ = '1.2.0' +__version__ = '1.4.0' __author__ = 'Dale Nguyen' __name__ = 'stockai' diff --git a/stockai/utils.py b/stockai/utils.py index 1b7ce84..6fd23ea 100644 --- a/stockai/utils.py +++ b/stockai/utils.py @@ -1,23 +1,26 @@ -from time import mktime +from time import mktime, time from ciso8601 import parse_datetime from datetime import datetime def date_to_timestamp(date): - """ - Convert date to timestamp + """ + Convert date to timestamp - :param date: date string '2019-04-30' - :return: timestamp int 1546318800 - """ - ts = parse_datetime(date) - # to get time in seconds: - return int(mktime(ts.timetuple())) + :param date: date string '2019-04-30' + :return: timestamp int 1546318800 + """ + ts = parse_datetime(date) + # to get time in seconds: + return int(mktime(ts.timetuple())) def timestamp_to_date(timestamp): - """ - Convert timestamp to readable date + """ + Convert timestamp to readable date - :param timestamp: timestamp string '1546318800' - :return: date string '2019-04-30' - """ - return datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d') \ No newline at end of file + :param timestamp: timestamp string '1546318800' + :return: date string '2019-04-30' + """ + return datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d') + +def get_current_timestamp(): + return int(time()) diff --git a/stockai/wealthsimple/__init__.py b/stockai/wealthsimple/__init__.py index 7a688cc..c422a8c 100644 --- a/stockai/wealthsimple/__init__.py +++ b/stockai/wealthsimple/__init__.py @@ -1,5 +1,5 @@ """ -StockAI v1.2.0 +StockAI Copyright (c) Dale Nguyen """ diff --git a/stockai/yahoo/__init__.py b/stockai/yahoo/__init__.py index 3ee66f5..24c24b4 100644 --- a/stockai/yahoo/__init__.py +++ b/stockai/yahoo/__init__.py @@ -1,5 +1,5 @@ """ -StockAI v1.2.0 +StockAI Copyright (c) Dale Nguyen """ diff --git a/stockai/yahoo/base.py b/stockai/yahoo/base.py index 197095d..7cca46b 100644 --- a/stockai/yahoo/base.py +++ b/stockai/yahoo/base.py @@ -1,5 +1,5 @@ from requests import get -from ..utils import timestamp_to_date +from ..utils import timestamp_to_date, get_current_timestamp class Base(object): def __init__(self, symbol): @@ -38,12 +38,10 @@ def get_historical(self, start_date, end_date): return self.__process_historical_result(data) def get_all_historical(self): - url = 'https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?range=max'.format( - symbol = self.symbol - ) - - data = get(url) - return self.__process_historical_result(data) + # Start from 01/01/2000 + start_date = 946702800 + end_date = get_current_timestamp() + return self.get_historical(start_date, end_date) def refresh(self): """ @@ -63,7 +61,7 @@ def __process_historical_result(self, data): result['adjclose'] = data.json()['chart']['result'][0]['indicators']['adjclose'][0]['adjclose'] result['meta'] = data.json()['chart']['result'][0]['meta'] - for index, date in enumerate(result['date']): - result['date'][index] = timestamp_to_date(result['date'][index]) + # for index, date in enumerate(result['date']): + # result['date'][index] = timestamp_to_date(result['date'][index]) return result diff --git a/tests/yahoo.py b/tests/yahoo.py index 59a57c1..84d187b 100644 --- a/tests/yahoo.py +++ b/tests/yahoo.py @@ -14,7 +14,7 @@ def setUp(self): self.td = Stock('TD.TO') def test_td_summary_profile(self): - print(self.td.get_summary_profile()) + # print(self.td.get_summary_profile()) self.assertEqual(self.td.get_summary_profile()['city'], 'Toronto') def test_td_financial_data(self):