Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added get_historical_52_week_high method #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions test_ystockquote.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ def test_get_historical_prices(self):
self.assertGreater(float(prices[end_date]['Volume']), 0.0)
self.assertGreater(float(prices[end_date]['Adj Close']), 0.0)

def test_get_historical_52_week_high(self):
symbol = 'GOOG'
date = '2014-07-10'
prices = ystockquote.get_historical_52_week_high(symbol, date)
self.assertIsInstance(prices, dict)
self.assertGreater(len(prices), 1)
item = prices.popitem()
self.assertEqual(item[0], '2014-04-02')
self.assertEqual(item[1]['High'], 604.83)

if __name__ == '__main__':
unittest.main()
23 changes: 23 additions & 0 deletions ystockquote.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# ystockquote : Python module - retrieve stock quote data from Yahoo Finance
#
# Copyright (c) 2007,2008,2013 Corey Goldberg ([email protected])
# Copyright (c) 2014 Ilya Usvyatsky ([email protected])
#
# license: GNU LGPL
#
Expand Down Expand Up @@ -498,3 +499,25 @@ def get_historical_prices(symbol, start_date, end_date):
keys[5]: day_data[5],
keys[6]: day_data[6]}
return hist_dict

def get_historical_52_week_high(symbol, date):
"""
Compute historical 52-week high for the given ticker symbol
at given date.
Date format is 'YYYY-MM-DD'

Returns a nested dictionary (dict of dicts).
outer dict keys are dates ('YYYY-MM-DD')
"""
start_date = str(int(date[0:4])-1) + date[4:]
prices = get_historical_prices(symbol, start_date, date)
max_high = 0
for d in prices:
if prices[d]['High'] > max_high:
max_high = prices[d]['High']
result = {}
for d in prices:
if prices[d]['High'] == max_high:
result[d] = prices[d]
return result