Fix handling of Yahoo date rows with nulls.

This commit is contained in:
Chris Berkhout 2021-11-12 13:40:46 +01:00
parent 249ea0b2db
commit afd41da6ef
3 changed files with 21 additions and 1 deletions

View file

@ -84,8 +84,10 @@ class Yahoo(BaseSource):
def _amount(self, row, type): def _amount(self, row, type):
if type == "mid" and row["high"] != "null" and row["low"] != "null": if type == "mid" and row["high"] != "null" and row["low"] != "null":
return sum([Decimal(row["high"]), Decimal(row["low"])]) / 2 return sum([Decimal(row["high"]), Decimal(row["low"])]) / 2
else: elif row[type] != "null":
return Decimal(row[type]) return Decimal(row[type])
else:
return None
def _data(self, series) -> (dict, csv.DictReader): def _data(self, series) -> (dict, csv.DictReader):
base_url = "https://query1.finance.yahoo.com/v7/finance" base_url = "https://query1.finance.yahoo.com/v7/finance"

View file

@ -64,6 +64,13 @@ def long_ok(requests_mock):
yield requests_mock yield requests_mock
@pytest.fixture
def date_with_nulls_ok(requests_mock):
json = (Path(os.path.splitext(__file__)[0]) / "ibm-date-with-nulls.csv").read_text()
requests_mock.add(responses.GET, history_url("IBM"), body=json, status=200)
yield requests_mock
def test_normalizesymbol(src): def test_normalizesymbol(src):
assert src.normalizesymbol("tsla") == "TSLA" assert src.normalizesymbol("tsla") == "TSLA"
@ -163,6 +170,13 @@ def test_fetch_from_before_start(src, type, spark_ok, long_ok):
assert len(series.prices) > 9 assert len(series.prices) > 9
def test_fetch_skips_dates_with_nulls(src, type, spark_ok, date_with_nulls_ok):
series = src.fetch(Series("IBM", "", type, "2021-01-05", "2021-01-07"))
assert series.prices[0] == Price("2021-01-05", Decimal("123.101204"))
assert series.prices[1] == Price("2021-01-07", Decimal("125.882545"))
assert len(series.prices) == 2
def test_fetch_to_future(src, type, spark_ok, recent_ok): def test_fetch_to_future(src, type, spark_ok, recent_ok):
series = src.fetch(Series("TSLA", "", type, "2021-01-04", "2100-01-08")) series = src.fetch(Series("TSLA", "", type, "2021-01-04", "2100-01-08"))
assert len(series.prices) > 0 assert len(series.prices) > 0

View file

@ -0,0 +1,4 @@
Date,Open,High,Low,Close,Adj Close,Volume
2021-01-05,125.010002,126.680000,124.610001,126.139999,123.101204,6114600
2021-01-06,null,null,null,null,null,null
2021-01-07,130.039993,130.460007,128.259995,128.990005,125.882545,4507400
1 Date Open High Low Close Adj Close Volume
2 2021-01-05 125.010002 126.680000 124.610001 126.139999 123.101204 6114600
3 2021-01-06 null null null null null null
4 2021-01-07 130.039993 130.460007 128.259995 128.990005 125.882545 4507400