Skip to content

Commit

Permalink
Yahoo: Fix parsing when final price in series is null
Browse files Browse the repository at this point in the history
  • Loading branch information
mbafford committed Feb 28, 2022
1 parent b05203e commit a409184
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion beanprice/sources/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def get_price_series(ticker: str,
timestamp_array = result['timestamp']
close_array = result['indicators']['quote'][0]['close']
series = [(datetime.fromtimestamp(timestamp, tz=tzone), Decimal(price))
for timestamp, price in zip(timestamp_array, close_array)]
for timestamp, price in zip(timestamp_array, close_array)
if price is not None]

currency = result['meta']['currency']
return series, currency
Expand Down
40 changes: 40 additions & 0 deletions beanprice/sources/yahoo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,45 @@ def test_parse_response_no_timestamp(self):
'XSP.TO', datetime.datetime(2017, 11, 1, 16, 0, 0, tzinfo=tz.tzutc()))


def test_parse_null_prices_in_series(self):
response = MockResponse(textwrap.dedent("""
{"chart": {"result":[ {"meta":{
"currency":"USD","symbol":"FBIIX",
"exchangeName":"NAS","instrumentType":"MUTUALFUND",
"firstTradeDate":1570714200,"regularMarketTime":1646053572,
"gmtoffset":-18000,"timezone":"EST",
"exchangeTimezoneName":"America/New_York",
"regularMarketPrice":9.69,"chartPreviousClose":9.69,
"priceHint":2,
"currentTradingPeriod":{
"pre":{"timezone":"EST","start":1646038800,"end":1646058600,"gmtoffset":-18000},
"regular":{"timezone":"EST","start":1646058600,"end":1646082000,"gmtoffset":-18000},
"post":{"timezone":"EST","start":1646082000,"end":1646096400,"gmtoffset":-18000}
},
"dataGranularity":"1d","range":"",
"validRanges":["1mo","3mo","6mo","ytd","1y","2y","5y","10y","max"]},
"timestamp":[1645626600,1645713000,1645799400,1646058600],
"indicators":{
"quote":[
{"open":[9.6899995803833,9.710000038146973,9.6899995803833,null],
"low":[9.6899995803833,9.710000038146973,9.6899995803833,null],
"high":[9.6899995803833,9.710000038146973,9.6899995803833,null],
"volume":[0,0,0,null],
"close":[9.6899995803833,9.710000038146973,9.6899995803833,null]}
],"adjclose":[
{"adjclose":[9.6899995803833,9.710000038146973,9.6899995803833,null]}
]
}}],"error":null}}
"""))

with mock.patch('requests.get', return_value=response):
srcprice = yahoo.Source().get_historical_price(
'XSP.TO', datetime.datetime(2022, 2, 28, 16, 0, 0, tzinfo=tz.tzutc()))
self.assertTrue(isinstance(srcprice.price, Decimal))
self.assertEqual(Decimal('9.6899995803833'), srcprice.price)
timezone = datetime.timezone(datetime.timedelta(hours=-5), 'America/New_York')
self.assertEqual(datetime.datetime(2022, 2, 25, 9, 30, tzinfo=timezone),
srcprice.time)
self.assertEqual('USD', srcprice.quote_currency)
if __name__ == '__main__':
unittest.main()

0 comments on commit a409184

Please sign in to comment.