mirror of
https://github.com/wassname/catalyst.git
synced 2026-09-10 11:50:32 +08:00
ENH: Adds fuzzy symbol look-up and makes it distinct from delimited symbols
This commit is contained in:
+44
-7
@@ -286,7 +286,7 @@ class AssetFinderTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.env = TradingEnvironment()
|
||||
|
||||
def test_lookup_symbol_fuzzy(self):
|
||||
def test_lookup_symbol_delimited(self):
|
||||
as_of = pd.Timestamp('2013-01-01', tz='UTC')
|
||||
frame = pd.DataFrame.from_records(
|
||||
[
|
||||
@@ -302,23 +302,60 @@ class AssetFinderTestCase(TestCase):
|
||||
]
|
||||
)
|
||||
self.env.write_data(equities_df=frame)
|
||||
finder = AssetFinder(self.env.engine, fuzzy_char='@')
|
||||
finder = AssetFinder(self.env.engine)
|
||||
asset_0, asset_1, asset_2 = (
|
||||
finder.retrieve_asset(i) for i in range(3)
|
||||
)
|
||||
|
||||
# we do it twice to catch caching bugs
|
||||
for i in range(2):
|
||||
# Shouldn't find this with no fuzzy_str passed.
|
||||
self.assertIsNone(finder.lookup_symbol('test', as_of))
|
||||
self.assertIsNone(finder.lookup_symbol('test1', as_of))
|
||||
self.assertEqual(asset_1, finder.lookup_symbol('test.1', as_of))
|
||||
# '@' is not a supported delimiter
|
||||
self.assertIsNone(finder.lookup_symbol('test@1', as_of))
|
||||
|
||||
# Adding an unnecessary fuzzy shouldn't matter.
|
||||
self.assertEqual(asset_1, finder.lookup_symbol('test/1', as_of))
|
||||
for fuzzy_char in ['-', '/', '_', '.']:
|
||||
self.assertEqual(
|
||||
asset_1,
|
||||
finder.lookup_symbol('test%s1' % fuzzy_char, as_of)
|
||||
)
|
||||
|
||||
# Should find exact match.
|
||||
self.assertEqual(asset_1, finder.lookup_symbol('test-1', as_of))
|
||||
def test_lookup_symbol_fuzzy(self):
|
||||
metadata = {
|
||||
0: {'symbol': 'PRTY_HRD'},
|
||||
1: {'symbol': 'BRKA'},
|
||||
2: {'symbol': 'BRK_A'},
|
||||
}
|
||||
self.env.write_data(equities_data=metadata)
|
||||
finder = self.env.asset_finder
|
||||
dt = pd.Timestamp('2013-01-01', tz='UTC')
|
||||
|
||||
# Try combos of looking up PRTYHRD with and without a time or fuzzy
|
||||
# Both non-fuzzys get no result
|
||||
self.assertIsNone(finder.lookup_symbol('PRTYHRD', None))
|
||||
self.assertIsNone(finder.lookup_symbol('PRTYHRD', dt))
|
||||
# Both fuzzys work
|
||||
self.assertEqual(0, finder.lookup_symbol('PRTYHRD', None, fuzzy=True))
|
||||
self.assertEqual(0, finder.lookup_symbol('PRTYHRD', dt, fuzzy=True))
|
||||
|
||||
# Try combos of looking up PRTY_HRD, all returning sid 0
|
||||
self.assertEqual(0, finder.lookup_symbol('PRTY_HRD', None))
|
||||
self.assertEqual(0, finder.lookup_symbol('PRTY_HRD', dt))
|
||||
self.assertEqual(0, finder.lookup_symbol('PRTY_HRD', None, fuzzy=True))
|
||||
self.assertEqual(0, finder.lookup_symbol('PRTY_HRD', dt, fuzzy=True))
|
||||
|
||||
# Try combos of looking up BRKA, all returning sid 1
|
||||
self.assertEqual(1, finder.lookup_symbol('BRKA', None))
|
||||
self.assertEqual(1, finder.lookup_symbol('BRKA', dt))
|
||||
self.assertEqual(1, finder.lookup_symbol('BRKA', None, fuzzy=True))
|
||||
self.assertEqual(1, finder.lookup_symbol('BRKA', dt, fuzzy=True))
|
||||
|
||||
# Try combos of looking up BRK_A, all returning sid 2
|
||||
self.assertEqual(2, finder.lookup_symbol('BRK_A', None))
|
||||
self.assertEqual(2, finder.lookup_symbol('BRK_A', dt))
|
||||
self.assertEqual(2, finder.lookup_symbol('BRK_A', None, fuzzy=True))
|
||||
self.assertEqual(2, finder.lookup_symbol('BRK_A', dt, fuzzy=True))
|
||||
|
||||
def test_lookup_symbol(self):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user