Skip to content

Commit

Permalink
BUG: Fixes asset writer to the select the latest asset to hold a sid (#…
Browse files Browse the repository at this point in the history
…1392)

* BUG: Fixes asset writer to the select the latest asset to hold a sid

When constructing the asset_info dataframe, we were previously taking
the first symbol/sid pair to include, when we should be taking the most
recent.

* Ensure groups are sorted by increasing end_date

* Updates test_lookup_symbol_change_ticker to also cover asset_name
  • Loading branch information
Andrew Daniels committed Aug 16, 2016
1 parent a011634 commit 7719f6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions tests/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,14 @@ def test_lookup_symbol_change_ticker(self):
# sid 0
{
'symbol': 'A',
'asset_name': 'Asset A',
'start_date': T('2014-01-01'),
'end_date': T('2014-01-05'),
'exchange': "TEST",
},
{
'symbol': 'B',
'asset_name': 'Asset B',
'start_date': T('2014-01-06'),
'end_date': T('2014-01-10'),
'exchange': "TEST",
Expand All @@ -537,12 +539,14 @@ def test_lookup_symbol_change_ticker(self):
# sid 1
{
'symbol': 'C',
'asset_name': 'Asset C',
'start_date': T('2014-01-01'),
'end_date': T('2014-01-05'),
'exchange': "TEST",
},
{
'symbol': 'A', # claiming the unused symbol 'A'
'asset_name': 'Asset A',
'start_date': T('2014-01-06'),
'end_date': T('2014-01-10'),
'exchange': "TEST",
Expand Down Expand Up @@ -574,8 +578,9 @@ def test_lookup_symbol_change_ticker(self):
finder.retrieve_asset(0),
msg=str(asof),
)
# the symbol should always be the last symbol
# The symbol and asset_name should always be the last held values
assert_equal(A_result.symbol, 'B')
assert_equal(A_result.asset_name, 'Asset B')

# from 01 through 05 sid 1 held 'C'
C_result = finder.lookup_symbol('C', asof)
Expand All @@ -584,8 +589,9 @@ def test_lookup_symbol_change_ticker(self):
finder.retrieve_asset(1),
msg=str(asof),
)
# the symbol should always be the last symbol
# The symbol and asset_name should always be the last held values
assert_equal(C_result.symbol, 'A')
assert_equal(C_result.asset_name, 'Asset A')

# no one held 'B' before 06
with self.assertRaises(SymbolNotFound):
Expand All @@ -609,6 +615,7 @@ def test_lookup_symbol_change_ticker(self):
msg=str(asof),
)
assert_equal(B_result.symbol, 'B')
assert_equal(B_result.asset_name, 'Asset B')

# from 06 through 10 sid 1 held 'A'
# we test through the 11th because sid 1 is the last to hold 'A'
Expand All @@ -620,6 +627,7 @@ def test_lookup_symbol_change_ticker(self):
msg=str(asof),
)
assert_equal(A_result.symbol, 'A')
assert_equal(A_result.asset_name, 'Asset A')

def test_lookup_symbol(self):

Expand Down
2 changes: 1 addition & 1 deletion zipline/assets/asset_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def _generate_output_dataframe(data_subset, defaults):


def _check_asset_group(group):
row = group.iloc[0]
row = group.sort('end_date').iloc[-1]
row.start_date = group.start_date.min()
row.end_date = group.end_date.max()
row.drop(list(symbol_columns), inplace=True)
Expand Down

0 comments on commit 7719f6e

Please sign in to comment.