TST: adapting zipline tests to catalyst: risk, events + data (WIP)

This commit is contained in:
Victor Grau Serrat
2017-12-04 23:06:06 -07:00
parent 96a27d083c
commit f279101393
5 changed files with 73 additions and 21 deletions
+7
View File
@@ -11,6 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
# ZIPLINE legacy test: Catalyst does not use DispatchBarReader, and thus
# this test suite is irrelevant, and is commented out in its entirety
from numpy import array, nan
from numpy.testing import assert_almost_equal
from pandas import DataFrame, Timestamp
@@ -330,3 +336,4 @@ class AssetDispatchMinuteBarTestCase(WithBcolzEquityMinuteBarReader,
for i, (sid, expected, msg) in enumerate(expected_per_sid):
for j, result in enumerate(results):
assert_almost_equal(result[:, i], expected[j], err_msg=msg)
'''
+30 -21
View File
@@ -38,8 +38,8 @@ from pandas import (
from catalyst.data.bar_reader import NoDataOnDate
from catalyst.data.minute_bars import (
BcolzMinuteBarMetadata,
BcolzMinuteBarWriter,
BcolzMinuteBarReader,
# BcolzMinuteBarWriter,
# BcolzMinuteBarReader,
BcolzMinuteOverlappingData,
US_EQUITIES_MINUTES_PER_DAY,
BcolzMinuteWriterColumnMismatch,
@@ -47,6 +47,11 @@ from catalyst.data.minute_bars import (
H5MinuteBarUpdateReader,
)
from catalyst.exchange.exchange_bcolz import (
BcolzExchangeBarWriter,
BcolzExchangeBarReader,
)
from catalyst.testing.fixtures import (
WithAssetFinder,
WithInstanceTmpDir,
@@ -57,8 +62,8 @@ from catalyst.testing.fixtures import (
# Calendar is set to cover several half days, to check a case where half
# days would be read out of order in cases of windows which spanned over
# multiple half days.
TEST_CALENDAR_START = Timestamp('2014-06-02', tz='UTC')
TEST_CALENDAR_STOP = Timestamp('2015-12-31', tz='UTC')
TEST_CALENDAR_START = Timestamp('2015-06-02', tz='UTC')
TEST_CALENDAR_STOP = Timestamp('2016-12-31', tz='UTC')
class BcolzMinuteBarTestCase(WithTradingCalendars,
@@ -87,14 +92,14 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
self.dest = self.instance_tmpdir.getpath('minute_bars')
os.makedirs(self.dest)
self.writer = BcolzMinuteBarWriter(
self.dest,
self.trading_calendar,
TEST_CALENDAR_START,
TEST_CALENDAR_STOP,
US_EQUITIES_MINUTES_PER_DAY,
self.writer = BcolzExchangeBarWriter(
rootdir=self.dest,
calendar=self.trading_calendar,
start_session=TEST_CALENDAR_START,
end_session=TEST_CALENDAR_STOP,
data_frequency='minute',
)
self.reader = BcolzMinuteBarReader(self.dest)
self.reader = BcolzExchangeBarReader(self.dest)
def test_version(self):
metadata = self.reader._get_metadata()
@@ -152,7 +157,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
)
# Create a new writer with `ohlc_ratios_per_sid` defined.
writer_with_ratios = BcolzMinuteBarWriter(
writer_with_ratios = BcolzExchangeBarWriter(
self.dest,
self.trading_calendar,
TEST_CALENDAR_START,
@@ -161,7 +166,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
ohlc_ratios_per_sid={sid: 25},
)
writer_with_ratios.write_sid(sid, data)
reader = BcolzMinuteBarReader(self.dest)
reader = BcolzExchangeBarReader(self.dest)
open_price = reader.get_value(sid, minute, 'open')
self.assertEquals(10.0, open_price)
@@ -449,7 +454,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
# of appending new days will be writing to an existing directory.
cday = self.trading_calendar.schedule.index.freq
new_end_session = TEST_CALENDAR_STOP + cday
writer = BcolzMinuteBarWriter.open(self.dest, new_end_session)
writer = BcolzExchangeBarWriter.open(self.dest, new_end_session)
next_day_minute = dt + cday
new_data = DataFrame(
data=ohlcv,
@@ -457,7 +462,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
writer.write_sid(sid, new_data)
# Get a new reader to test updated calendar.
reader = BcolzMinuteBarReader(self.dest)
reader = BcolzExchangeBarReader(self.dest)
second_minute = dt + Timedelta(minutes=1)
@@ -802,7 +807,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
index=minutes)
self.writer.write_sid(sids[1], data_2)
reader = BcolzMinuteBarReader(self.dest)
reader = BcolzExchangeBarReader(self.dest)
columns = ['open', 'high', 'low', 'close', 'volume']
sids = [sids[0], sids[1]]
@@ -854,7 +859,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
index=minutes)
self.writer.write_sid(sids[1], data_2)
reader = BcolzMinuteBarReader(self.dest)
reader = BcolzExchangeBarReader(self.dest)
columns = ['open', 'high', 'low', 'close', 'volume']
sids = [sids[0], sids[1]]
@@ -877,6 +882,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
assert_almost_equal(data[sid].loc[minutes, col],
arrays[i][j][minute_locs])
'''
def test_adjust_non_trading_minutes(self):
start_day = Timestamp('2015-06-01', tz='UTC')
end_day = Timestamp('2015-06-02', tz='UTC')
@@ -922,7 +928,9 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
Timestamp('2015-06-02 20:01:00', tz='UTC'),
'open'
)
'''
'''
def test_adjust_non_trading_minutes_half_days(self):
# half day
start_day = Timestamp('2015-11-27', tz='UTC')
@@ -978,6 +986,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
Timestamp('2015-11-30 21:01:00', tz='UTC'),
'open'
)
'''
def test_set_sid_attrs(self):
"""Confirm that we can set the attributes of a sid's file correctly.
@@ -1023,13 +1032,13 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
# Open a new writer to cover `open` method, also truncating only
# applies to an existing directory.
writer = BcolzMinuteBarWriter.open(self.dest)
writer = BcolzExchangeBarWriter.open(self.dest)
# Truncate to first day with data.
writer.truncate(days[0])
# Refresh the reader since truncate update the metadata.
self.reader = BcolzMinuteBarReader(self.dest)
self.reader = BcolzExchangeBarReader(self.dest)
self.assertEqual(self.writer.last_date_in_output_for_sid(sid), days[0])
@@ -1087,7 +1096,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
self.writer.truncate(self.test_calendar_start)
# Refresh the reader since truncate update the metadata.
self.reader = BcolzMinuteBarReader(self.dest)
self.reader = BcolzExchangeBarReader(self.dest)
self.assertEqual(
self.writer.last_date_in_output_for_sid(sid),
@@ -1198,7 +1207,7 @@ class BcolzMinuteBarTestCase(WithTradingCalendars,
self.writer.write(update_reader.read(minutes, sids))
# Refresh the reader since truncate update the metadata.
reader = BcolzMinuteBarReader(self.dest)
reader = BcolzExchangeBarReader(self.dest)
columns = ['open', 'high', 'low', 'close', 'volume']
sids = [sids[0], sids[1]]
+7
View File
@@ -12,6 +12,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
# ZIPLINE legacy test: Catalyst only uses OPEN calendar, and thus
# this test suite is irrelevant, and is commented out in its entirety
from unittest import TestCase
import pandas as pd
@@ -41,3 +46,5 @@ class TestStatelessRulesCME(StatelessRulesTests, TestCase):
class TestStatefulRulesCME(StatefulRulesTests, TestCase):
CALENDAR_STRING = "CME"
'''
+7
View File
@@ -12,6 +12,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
# ZIPLINE legacy test: Catalyst only uses OPEN calendar, and thus
# this test suite is irrelevant, and is commented out in its entirety
from unittest import TestCase
from datetime import timedelta
import pandas as pd
@@ -162,3 +167,5 @@ class TestStatelessRulesNYSE(StatelessRulesTests, TestCase):
class TestStatefulRulesNYSE(StatefulRulesTests, TestCase):
CALENDAR_STRING = "NYSE"
'''
+22
View File
@@ -232,6 +232,28 @@ class TestRisk(WithTradingEnvironment, ZiplineTestCase):
# The sortino ratio is calculated by a empyrical function so testing
# of period sortino ratios will be limited to determine if the value is
# numerical. This tests for its existence and format.
# This test needs a different result set that, with some
# negative results, otherwise fails in a legitimate way.
RETURNS = (np.random.rand(251) * 0.1) - 0.05
self.algo_returns = factory.create_returns_from_list(
RETURNS,
self.sim_params
)
self.metrics = risk.RiskReport(
self.algo_returns,
self.sim_params,
benchmark_returns=self.benchmark_returns,
trading_calendar=self.trading_calendar,
treasury_curves=self.env.treasury_curves,
)
for x in self.metrics.month_periods:
print (type(x.sortino))
np.testing.assert_equal(
all(isinstance(x.sortino, float)
for x in self.metrics.month_periods),