def test_normal(self): days_to_use = self.sim_params.sessions[1:] source = BenchmarkSource(self.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 # a pct_change on it manually_calculated = self.data_portal.get_history_window( [1], days_to_use[-1], len(days_to_use), "1d", "close", "daily", )[1].pct_change() # compare all the fields except the first one, for which we don't have # data in manually_calculated for idx, day in enumerate(days_to_use[1:]): self.assertEqual(source.get_value(day), manually_calculated[idx + 1]) # compare a slice of the data assert_series_equal(source.get_range(days_to_use[1], days_to_use[10]), manually_calculated[1:11])
def test_asset_IPOed_same_day(self): # gotta get some minute data up in here. # add sid 4 for a couple of days minutes = self.env.minutes_for_days_in_range( self.sim_params.trading_days[0], self.sim_params.trading_days[5]) path = write_minute_data(self.env, self.tempdir, minutes, [2]) self.data_portal._minutes_equities_path = path source = BenchmarkSource(2, self.env, self.sim_params.trading_days, self.data_portal) days_to_use = self.sim_params.trading_days # first value should be 0.0, coming from daily data self.assertAlmostEquals(0.0, source.get_value(days_to_use[0])) manually_calculated = self.data_portal.get_history_window( [2], days_to_use[-1], len(days_to_use), "1d", "close")[2].pct_change() for idx, day in enumerate(days_to_use[1:]): self.assertEqual(source.get_value(day), manually_calculated[idx + 1])
def test_asset_not_trading(self): benchmark = self.env.asset_finder.retrieve_asset(3) benchmark_start = benchmark.start_date benchmark_end = benchmark.end_date with self.assertRaises(BenchmarkAssetNotAvailableTooEarly) as exc: BenchmarkSource( 3, self.env, self.trading_calendar, self.sim_params.sessions[1:], self.data_portal ) self.assertEqual( '3 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, self.trading_calendar, self.sim_params.sessions[120:], self.data_portal ) self.assertEqual( '3 does not exist on %s. It stopped trading on %s.' % (self.sim_params.sessions[-1], benchmark_end), exc2.exception.message )
def test_asset_not_trading(self): with self.assertRaises(BenchmarkAssetNotAvailableTooEarly) as exc: BenchmarkSource( 3, self.env, self.sim_params.trading_days[1:], self.data_portal ) self.assertEqual( '3 does not exist on 2006-01-04 00:00:00+00:00. ' 'It started trading on 2006-05-26 00:00:00+00:00.', exc.exception.message ) with self.assertRaises(BenchmarkAssetNotAvailableTooLate) as exc2: BenchmarkSource( 3, self.env, self.sim_params.trading_days[120:], self.data_portal ) self.assertEqual( '3 does not exist on 2006-06-26 00:00:00+00:00. ' 'It stopped trading on 2006-08-09 00:00:00+00:00.', exc2.exception.message )
def test_normal(self): days_to_use = self.sim_params.sessions[1:] source = BenchmarkSource( 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 # a pct_change on it manually_calculated = self.data_portal.get_history_window( [1], days_to_use[-1], len(days_to_use), "1d", "close", "daily", )[1].pct_change() # compare all the fields except the first one, for which we don't have # data in manually_calculated for idx, day in enumerate(days_to_use[1:]): self.assertEqual( source.get_value(day), manually_calculated[idx + 1] ) # compare a slice of the data assert_series_equal( source.get_range(days_to_use[1], days_to_use[10]), manually_calculated[1:11] )
def test_asset_not_trading(self): benchmark = self.asset_finder.retrieve_asset(3) benchmark_start = benchmark.start_date benchmark_end = benchmark.end_date expected_msg = ( f"Equity(3 [C]) does not exist on {self.sim_params.sessions[1]}. " f"It started trading on {benchmark_start}.") with pytest.raises(BenchmarkAssetNotAvailableTooEarly, match=re.escape(expected_msg)): BenchmarkSource( benchmark, self.trading_calendar, self.sim_params.sessions[1:], self.data_portal, ) expected_msg = ( f"Equity(3 [C]) does not exist on {self.sim_params.sessions[-1]}. " f"It stopped trading on {benchmark_end}.") with pytest.raises(BenchmarkAssetNotAvailableTooLate, match=re.escape(expected_msg)): BenchmarkSource( benchmark, self.trading_calendar, self.sim_params.sessions[120:], self.data_portal, )
def test_asset_IPOed_same_day(self): # gotta get some minute data up in here. # add sid 4 for a couple of days minutes = self.trading_calendar.minutes_for_sessions_in_range( self.sim_params.sessions[0], self.sim_params.sessions[5] ) tmp_reader = tmp_bcolz_equity_minute_bar_reader( self.trading_calendar, self.trading_calendar.all_sessions, create_minute_bar_data(minutes, [2]), ) with tmp_reader as reader: data_portal = DataPortal( self.env.asset_finder, self.trading_calendar, first_trading_day=reader.first_trading_day, equity_minute_reader=reader, equity_daily_reader=self.bcolz_equity_daily_bar_reader, adjustment_reader=self.adjustment_reader, ) source = BenchmarkSource( 2, self.env, self.trading_calendar, self.sim_params.sessions, data_portal ) days_to_use = self.sim_params.sessions # first value should be 0.0, coming from daily data self.assertAlmostEquals(0.0, source.get_value(days_to_use[0])) manually_calculated = data_portal.get_history_window( [2], days_to_use[-1], len(days_to_use), "1d", "close", "daily", )[2].pct_change() for idx, day in enumerate(days_to_use[1:]): self.assertEqual( source.get_value(day), manually_calculated[idx + 1] )
def test_normal(self): days_to_use = self.sim_params.trading_days[1:] source = BenchmarkSource(1, self.env, days_to_use, self.data_portal) # should be the equivalent of getting the price history, then doing # a pct_change on it manually_calculated = self.data_portal.get_history_window( [1], days_to_use[-1], len(days_to_use), "1d", "close")[1].pct_change() # compare all the fields except the first one, for which we don't have # data in manually_calculated for idx, day in enumerate(days_to_use[1:]): self.assertEqual(source.get_value(day), manually_calculated[idx + 1])
def test_asset_IPOed_same_day(self): # gotta get some minute data up in here. # add sid 4 for a couple of days minutes = self.trading_calendar.minutes_for_sessions_in_range( self.sim_params.sessions[0], self.sim_params.sessions[5] ) tmp_reader = tmp_bcolz_equity_minute_bar_reader( self.trading_calendar, self.trading_calendar.all_sessions, create_minute_bar_data(minutes, [2]), ) with tmp_reader as reader: data_portal = DataPortal( self.env.asset_finder, self.trading_calendar, first_trading_day=reader.first_trading_day, equity_minute_reader=reader, equity_daily_reader=self.bcolz_equity_daily_bar_reader, adjustment_reader=self.adjustment_reader, ) source = BenchmarkSource( 2, self.env, self.trading_calendar, self.sim_params.sessions, data_portal ) days_to_use = self.sim_params.sessions # first value should be 0.0, coming from daily data self.assertAlmostEquals(0.0, source.get_value(days_to_use[0])) manually_calculated = data_portal.get_history_window( [2], days_to_use[-1], len(days_to_use), "1d", "close", )[2].pct_change() for idx, day in enumerate(days_to_use[1:]): self.assertEqual( source.get_value(day), manually_calculated[idx + 1] )
def test_normal(self): days_to_use = self.sim_params.trading_days[1:] source = BenchmarkSource( 1, self.env, days_to_use, self.data_portal ) # should be the equivalent of getting the price history, then doing # a pct_change on it manually_calculated = self.data_portal.get_history_window( [1], days_to_use[-1], len(days_to_use), "1d", "close" )[1].pct_change() # compare all the fields except the first one, for which we don't have # data in manually_calculated for idx, day in enumerate(days_to_use[1:]): self.assertEqual( source.get_value(day), manually_calculated[idx + 1] )
def test_no_stock_dividends_allowed(self): # try to use sid(4) as benchmark, should blow up due to the presence # of a stock dividend with self.assertRaises(InvalidBenchmarkAsset) as exc: BenchmarkSource(4, self.env, self.sim_params.trading_days, 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.", exc.exception.message)
def test_no_stock_dividends_allowed(self): # try to use sid(4) as benchmark, should blow up due to the presence # of a stock dividend with self.assertRaises(InvalidBenchmarkAsset) as exc: BenchmarkSource(self.asset_finder.retrieve_asset(4), self.trading_calendar, self.sim_params.sessions, self.data_portal) 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)
def test_asset_IPOed_same_day(self): # gotta get some minute data up in here. # add sid 4 for a couple of days minutes = self.env.minutes_for_days_in_range( self.sim_params.trading_days[0], self.sim_params.trading_days[5] ) path = write_minute_data( self.env, self.tempdir, minutes, [2] ) self.data_portal._minutes_equities_path = path source = BenchmarkSource( 2, self.env, self.sim_params.trading_days, self.data_portal ) days_to_use = self.sim_params.trading_days # first value should be 0.0, coming from daily data self.assertAlmostEquals(0.0, source.get_value(days_to_use[0])) manually_calculated = self.data_portal.get_history_window( [2], days_to_use[-1], len(days_to_use), "1d", "close" )[2].pct_change() for idx, day in enumerate(days_to_use[1:]): self.assertEqual( source.get_value(day), manually_calculated[idx + 1] )
def test_no_stock_dividends_allowed(self): # try to use sid(4) as benchmark, should blow up due to the presence # of a stock dividend err_msg = ("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.") with pytest.raises(InvalidBenchmarkAsset, match=re.escape(err_msg)): BenchmarkSource( self.asset_finder.retrieve_asset(4), self.trading_calendar, self.sim_params.sessions, self.data_portal, )