MAINT: Remove environment as an argument to benchmark source. (#1816)

MAINT: Remove environment as an argument to benchmark source.

To allow the BenchmarkSource class to be more easily used in contexts other than
a TradingAlgorithm, remove the TradingEnvironment as an argument to the
benchmark source.

Instead:
- Pass a benchmark Asset, instead of a bencmark sid; so that the asset_finder
does not need to be passed to the benchmark source.
- Pass the pre-calculated benchmark_returns instead of an env,
which contains the benchmark_returns; a consumer can let the benchmark_returns
stay as the default of `None` when using an asset.

We may want to further refactor and make two different classes, instead of
relying on a combination of existence/non-existence of benchmark_asset and
benchmark_returns. That refactoring should be easier to do with this change.
This commit is contained in:
Eddie Hebert
2017-05-25 16:11:25 -04:00
committed by GitHub
parent a3fe687b15
commit c1280daaa3
3 changed files with 47 additions and 33 deletions
+17 -14
View File
@@ -96,7 +96,10 @@ class TestBenchmark(WithDataPortal, WithSimParams, WithTradingCalendars,
days_to_use = self.sim_params.sessions[1:]
source = BenchmarkSource(
1, self.env, self.trading_calendar, days_to_use, self.data_portal
self.env.asset_finder.retrieve_asset(1),
self.trading_calendar,
days_to_use,
self.data_portal
)
# should be the equivalent of getting the price history, then doing
@@ -131,30 +134,28 @@ class TestBenchmark(WithDataPortal, WithSimParams, WithTradingCalendars,
with self.assertRaises(BenchmarkAssetNotAvailableTooEarly) as exc:
BenchmarkSource(
3,
self.env,
benchmark,
self.trading_calendar,
self.sim_params.sessions[1:],
self.data_portal
)
self.assertEqual(
'3 does not exist on %s. It started trading on %s.' %
'Equity(3 [C]) does not exist on %s. It started trading on %s.' %
(self.sim_params.sessions[1], benchmark_start),
exc.exception.message
)
with self.assertRaises(BenchmarkAssetNotAvailableTooLate) as exc2:
BenchmarkSource(
3,
self.env,
benchmark,
self.trading_calendar,
self.sim_params.sessions[120:],
self.data_portal
)
self.assertEqual(
'3 does not exist on %s. It stopped trading on %s.' %
'Equity(3 [C]) does not exist on %s. It stopped trading on %s.' %
(self.sim_params.sessions[-1], benchmark_end),
exc2.exception.message
)
@@ -182,8 +183,7 @@ class TestBenchmark(WithDataPortal, WithSimParams, WithTradingCalendars,
)
source = BenchmarkSource(
2,
self.env,
self.env.asset_finder.retrieve_asset(2),
self.trading_calendar,
self.sim_params.sessions,
data_portal
@@ -214,11 +214,14 @@ class TestBenchmark(WithDataPortal, WithSimParams, WithTradingCalendars,
with self.assertRaises(InvalidBenchmarkAsset) as exc:
BenchmarkSource(
4, self.env, self.trading_calendar,
self.sim_params.sessions, self.data_portal
self.env.asset_finder.retrieve_asset(4),
self.trading_calendar,
self.sim_params.sessions,
self.data_portal
)
self.assertEqual("4 cannot be used as the benchmark because it has a "
"stock dividend on 2006-03-16 00:00:00. Choose "
"another asset to use as the benchmark.",
self.assertEqual("Equity(4 [D]) cannot be used as the benchmark "
"because it has a stock dividend on 2006-03-16 "
"00:00:00. Choose another asset to use as the "
"benchmark.",
exc.exception.message)