Skip to content

Commit

Permalink
More Fuzzy Symbol Fixes (#1475)
Browse files Browse the repository at this point in the history
* REF: More options before raise MultiFound.

* TST: Checks corner case for fuzzy matching.
  • Loading branch information
kglowinski committed Sep 8, 2016
1 parent 1ccc9e4 commit 4a00e69
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
50 changes: 50 additions & 0 deletions tests/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,56 @@ def test_lookup_mult_are_one(self):
result = finder.lookup_symbol('FOO/B', date + timedelta(1), fuzzy=True)
self.assertEqual(result.sid, 1)

def test_endless_multiple_resolves(self):
"""
Situation:
1. Asset 1 w/ symbol FOOB changes to FOO_B, and then is delisted.
2. Asset 2 is listed with symbol FOO_B.
If someone asks for FOO_B with fuzzy matching after 2 has been listed,
they should be able to correctly get 2.
"""

date = pd.Timestamp('2013-01-01', tz='UTC')

df = pd.DataFrame.from_records(
[
{
'sid': 1,
'symbol': 'FOOB',
'start_date': date.value,
'end_date': date.max.value,
'exchange': 'NYSE',
},
{
'sid': 1,
'symbol': 'FOO_B',
'start_date': (date + timedelta(days=31)).value,
'end_date': (date + timedelta(days=60)).value,
'exchange': 'NYSE',
},
{
'sid': 2,
'symbol': 'FOO_B',
'start_date': (date + timedelta(days=61)).value,
'end_date': date.max.value,
'exchange': 'NYSE',
},

]
)
self.write_assets(equities=df)
finder = self.asset_finder

# If we are able to resolve this with any result, means that we did not
# raise a MultipleSymbolError.
result = finder.lookup_symbol(
'FOO/B',
date + timedelta(days=90),
fuzzy=True
)
self.assertEqual(result.sid, 2)

def test_lookup_generic_handle_missing(self):
data = pd.DataFrame.from_records(
[
Expand Down
7 changes: 5 additions & 2 deletions zipline/assets/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,11 @@ def _lookup_symbol_fuzzy(self, symbol, as_of_date):
return self.retrieve_asset(sid_keys[0])

for sid, sym in options.items():
if sym == symbol:
# look for an exact match on the asof date
# Possible to have a scenario where multiple fuzzy matches have the
# same date. Want to find the one where symbol and share class
# match.
if (company_symbol, share_class_symbol) == \
split_delimited_symbol(sym):
return self.retrieve_asset(sid)

# multiple equities held tickers matching the fuzzy ticker but
Expand Down

0 comments on commit 4a00e69

Please sign in to comment.