mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-03 12:40:47 +08:00
MAINT: Standardize reader get value methods.
The daily/session bar reader's `spot_price` took the same parameters and returned the same kind of output as the minute bar reader's `get_value`. Standardize on one method to make a common interface, which may be formally factored out in a later patch; to help enable writing reader implementations or mixins which can be agnostic to the bar frequency.
This commit is contained in:
@@ -512,7 +512,7 @@ class TestResampleSessionBars(WithBcolzFutureMinuteBarReader,
|
||||
|
||||
self.assertEqual(self.END_DATE, session_bar_reader.last_available_dt)
|
||||
|
||||
def test_spot_price(self):
|
||||
def test_get_value(self):
|
||||
calendar = self.trading_calendar
|
||||
session_bar_reader = MinuteResampleSessionBarReader(
|
||||
calendar,
|
||||
@@ -523,7 +523,7 @@ class TestResampleSessionBars(WithBcolzFutureMinuteBarReader,
|
||||
for dt_str, values in expected.iterrows():
|
||||
dt = pd.Timestamp(dt_str, tz='UTC')
|
||||
for col in OHLCV:
|
||||
result = session_bar_reader.spot_price(sid, dt, col)
|
||||
result = session_bar_reader.get_value(sid, dt, col)
|
||||
assert_almost_equal(values[col], result,
|
||||
err_msg="sid={0} col={1} dt={2}".
|
||||
format(sid, col, dt))
|
||||
|
||||
@@ -285,45 +285,45 @@ class BcolzDailyBarTestCase(WithBcolzEquityDailyBarReader, ZiplineTestCase):
|
||||
end_date=self.asset_end(asset),
|
||||
)
|
||||
|
||||
def test_unadjusted_spot_price(self):
|
||||
def test_unadjusted_get_value(self):
|
||||
reader = self.bcolz_equity_daily_bar_reader
|
||||
# At beginning
|
||||
price = reader.spot_price(1, Timestamp('2015-06-01', tz='UTC'),
|
||||
'close')
|
||||
price = reader.get_value(1, Timestamp('2015-06-01', tz='UTC'),
|
||||
'close')
|
||||
# Synthetic writes price for date.
|
||||
self.assertEqual(108630.0, price)
|
||||
|
||||
# Middle
|
||||
price = reader.spot_price(1, Timestamp('2015-06-02', tz='UTC'),
|
||||
'close')
|
||||
price = reader.get_value(1, Timestamp('2015-06-02', tz='UTC'),
|
||||
'close')
|
||||
self.assertEqual(108631.0, price)
|
||||
# End
|
||||
price = reader.spot_price(1, Timestamp('2015-06-05', tz='UTC'),
|
||||
'close')
|
||||
price = reader.get_value(1, Timestamp('2015-06-05', tz='UTC'),
|
||||
'close')
|
||||
self.assertEqual(108634.0, price)
|
||||
|
||||
# Another sid at beginning.
|
||||
price = reader.spot_price(2, Timestamp('2015-06-22', tz='UTC'),
|
||||
'close')
|
||||
price = reader.get_value(2, Timestamp('2015-06-22', tz='UTC'),
|
||||
'close')
|
||||
self.assertEqual(208651.0, price)
|
||||
|
||||
# Ensure that volume does not have float adjustment applied.
|
||||
volume = reader.spot_price(1, Timestamp('2015-06-02', tz='UTC'),
|
||||
'volume')
|
||||
volume = reader.get_value(1, Timestamp('2015-06-02', tz='UTC'),
|
||||
'volume')
|
||||
self.assertEqual(109631, volume)
|
||||
|
||||
def test_unadjusted_spot_price_no_data(self):
|
||||
def test_unadjusted_get_value_no_data(self):
|
||||
table = self.bcolz_daily_bar_ctable
|
||||
reader = BcolzDailyBarReader(table)
|
||||
# before
|
||||
with self.assertRaises(NoDataOnDate):
|
||||
reader.spot_price(2, Timestamp('2015-06-08', tz='UTC'), 'close')
|
||||
reader.get_value(2, Timestamp('2015-06-08', tz='UTC'), 'close')
|
||||
|
||||
# after
|
||||
with self.assertRaises(NoDataOnDate):
|
||||
reader.spot_price(4, Timestamp('2015-06-16', tz='UTC'), 'close')
|
||||
reader.get_value(4, Timestamp('2015-06-16', tz='UTC'), 'close')
|
||||
|
||||
def test_unadjusted_spot_price_empty_value(self):
|
||||
def test_unadjusted_get_value_empty_value(self):
|
||||
reader = self.bcolz_equity_daily_bar_reader
|
||||
|
||||
# A sid, day and corresponding index into which to overwrite a zero.
|
||||
@@ -338,7 +338,7 @@ class BcolzDailyBarTestCase(WithBcolzEquityDailyBarReader, ZiplineTestCase):
|
||||
# This a little hacky, in lieu of changing the synthetic data set.
|
||||
reader._spot_col('close')[zero_ix] = 0
|
||||
|
||||
close = reader.spot_price(zero_sid, zero_day, 'close')
|
||||
close = reader.get_value(zero_sid, zero_day, 'close')
|
||||
self.assertEqual(-1, close)
|
||||
finally:
|
||||
reader._spot_col('close')[zero_ix] = old
|
||||
|
||||
@@ -336,7 +336,7 @@ class MockDailyBarSpotReader(object):
|
||||
"""
|
||||
A BcolzDailyBarReader which returns a constant value for spot price.
|
||||
"""
|
||||
def spot_price(self, sid, day, column):
|
||||
def get_value(self, sid, day, column):
|
||||
return 100.0
|
||||
|
||||
|
||||
|
||||
@@ -358,7 +358,7 @@ class TestMinuteBarData(WithBarDataChecks,
|
||||
elif field == "last_traded":
|
||||
self.assertEqual(last_trading_minute, asset_value)
|
||||
|
||||
def test_spot_price_is_unadjusted(self):
|
||||
def test_get_value_is_unadjusted(self):
|
||||
# verify there is a split for SPLIT_ASSET
|
||||
splits = self.adjustment_reader.get_adjustments_for_sid(
|
||||
"splits",
|
||||
@@ -385,7 +385,7 @@ class TestMinuteBarData(WithBarDataChecks,
|
||||
bar_data.current(self.SPLIT_ASSET, "price")
|
||||
)
|
||||
|
||||
def test_spot_price_is_adjusted_if_needed(self):
|
||||
def test_get_value_is_adjusted_if_needed(self):
|
||||
# on cls.days[1], the first 9 minutes of ILLIQUID_SPLIT_ASSET are
|
||||
# missing. let's get them.
|
||||
day0_minutes = self.trading_calendar.minutes_for_session(
|
||||
@@ -420,7 +420,7 @@ class TestMinuteBarData(WithBarDataChecks,
|
||||
bar_data.current(self.ILLIQUID_SPLIT_ASSET, "price")
|
||||
)
|
||||
|
||||
def test_spot_price_at_midnight(self):
|
||||
def test_get_value_at_midnight(self):
|
||||
# make sure that if we try to get a minute price at a non-market
|
||||
# minute, we use the previous market close's timestamp
|
||||
day = self.equity_minute_bar_days[1]
|
||||
@@ -872,12 +872,12 @@ class TestDailyBarData(WithBarDataChecks,
|
||||
("merger", 2, 3, 3, 1.8),
|
||||
("dividend", 2, 3, 3, 2.88)
|
||||
])
|
||||
def test_spot_price_adjustments(self,
|
||||
adjustment_type,
|
||||
liquid_day_0_price,
|
||||
liquid_day_1_price,
|
||||
illiquid_day_0_price,
|
||||
illiquid_day_1_price_adjusted):
|
||||
def test_get_value_adjustments(self,
|
||||
adjustment_type,
|
||||
liquid_day_0_price,
|
||||
liquid_day_1_price,
|
||||
illiquid_day_0_price,
|
||||
illiquid_day_1_price_adjusted):
|
||||
"""Test the behaviour of spot prices during adjustments."""
|
||||
table_name = adjustment_type + 's'
|
||||
liquid_asset = getattr(self, (adjustment_type.upper() + "_ASSET"))
|
||||
|
||||
@@ -55,14 +55,14 @@ class WithPanelBarReader(WithAssetFinder):
|
||||
|
||||
cls.reader = PanelBarReader(trading_calendar, cls.panel, cls.FREQUENCY)
|
||||
|
||||
def test_spot_price(self):
|
||||
def test_get_value(self):
|
||||
panel = self.panel
|
||||
reader = self.reader
|
||||
|
||||
for asset, date, field in product(*panel.axes):
|
||||
self.assertEqual(
|
||||
panel.loc[asset, date, field],
|
||||
reader.spot_price(asset, date, field),
|
||||
reader.get_value(asset, date, field),
|
||||
)
|
||||
|
||||
def test_duplicate_values(self):
|
||||
|
||||
Reference in New Issue
Block a user