def setUp(self): self.benchmark_returns, self.treasury_curves = \ factory.load_market_data() for n in range(100): random_index = random.randint( 0, len(self.treasury_curves) ) self.dt = self.treasury_curves.keys()[random_index] self.end_dt = self.dt + datetime.timedelta(days=365) now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) if self.end_dt <= now: break assert self.end_dt <= now, """ failed to find a date suitable daterange after 100 attempts. please double check treasury and benchmark data in findb, and re-run the test.""" self.trading_environment = TradingEnvironment( self.benchmark_returns, self.treasury_curves, period_start=self.dt, period_end=self.end_dt ) self.onesec = datetime.timedelta(seconds=1) self.oneday = datetime.timedelta(days=1) self.tradingday = datetime.timedelta(hours=6, minutes=30) self.dt = self.trading_environment.trading_days[random_index]
def setUp(self): self.benchmark_returns, self.treasury_curves = \ factory.load_market_data() for n in range(100): random_index = random.randint(0, len(self.treasury_curves)) self.dt = self.treasury_curves.keys()[random_index] self.end_dt = self.dt + datetime.timedelta(days=365) now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) if self.end_dt <= now: break assert self.end_dt <= now, """ failed to find a date suitable daterange after 100 attempts. please double check treasury and benchmark data in findb, and re-run the test.""" self.trading_environment = TradingEnvironment(self.benchmark_returns, self.treasury_curves, period_start=self.dt, period_end=self.end_dt) self.onesec = datetime.timedelta(seconds=1) self.oneday = datetime.timedelta(days=1) self.tradingday = datetime.timedelta(hours=6, minutes=30) self.dt = self.trading_environment.trading_days[random_index]
def setUp(self): start_date = datetime.datetime( year=2006, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_date = datetime.datetime( year=2006, month=12, day=31, tzinfo=pytz.utc) self.benchmark_returns, self.treasury_curves = \ factory.load_market_data() self.trading_env = TradingEnvironment( self.benchmark_returns, self.treasury_curves, period_start=start_date, period_end=end_date ) self.onesec = datetime.timedelta(seconds=1) self.oneday = datetime.timedelta(days=1) self.tradingday = datetime.timedelta(hours=6, minutes=30) self.dt = datetime.datetime.utcnow() self.algo_returns_06 = factory.create_returns_from_list( RETURNS, self.trading_env ) self.metrics_06 = risk.RiskReport( self.algo_returns_06, self.trading_env ) start_08 = datetime.datetime( year=2008, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) end_08 = datetime.datetime( year=2008, month=12, day=31, tzinfo=pytz.utc ) self.trading_env08 = TradingEnvironment( self.benchmark_returns, self.treasury_curves, period_start=start_08, period_end=end_08 )
def test_trading_environment(self): benchmark_returns, treasury_curves = \ factory.load_market_data() env = TradingEnvironment( benchmark_returns, treasury_curves, period_start=datetime(2008, 1, 1, tzinfo=pytz.utc), period_end=datetime(2008, 12, 31, tzinfo=pytz.utc), capital_base=100000, ) #holidays taken from: http://www.nyse.com/press/1191407641943.html new_years = datetime(2008, 1, 1, tzinfo=pytz.utc) mlk_day = datetime(2008, 1, 21, tzinfo=pytz.utc) presidents = datetime(2008, 2, 18, tzinfo=pytz.utc) good_friday = datetime(2008, 3, 21, tzinfo=pytz.utc) memorial_day = datetime(2008, 5, 26, tzinfo=pytz.utc) july_4th = datetime(2008, 7, 4, tzinfo=pytz.utc) labor_day = datetime(2008, 9, 1, tzinfo=pytz.utc) tgiving = datetime(2008, 11, 27, tzinfo=pytz.utc) christmas = datetime(2008, 5, 25, tzinfo=pytz.utc) a_saturday = datetime(2008, 8, 2, tzinfo=pytz.utc) a_sunday = datetime(2008, 10, 12, tzinfo=pytz.utc) holidays = [ new_years, mlk_day, presidents, good_friday, memorial_day, july_4th, labor_day, tgiving, christmas, a_saturday, a_sunday ] for holiday in holidays: self.assertTrue(not env.is_trading_day(holiday)) first_trading_day = datetime(2008, 1, 2, tzinfo=pytz.utc) last_trading_day = datetime(2008, 12, 31, tzinfo=pytz.utc) workdays = [first_trading_day, last_trading_day] for workday in workdays: self.assertTrue(env.is_trading_day(workday)) self.assertTrue(env.last_close.month == 12) self.assertTrue(env.last_close.day == 31)
def setUp(self): self.start_date = datetime.datetime( year=2006, month=1, day=1, hour=0, minute=0, tzinfo=pytz.utc) self.end_date = datetime.datetime( year=2006, month=12, day=31, tzinfo=pytz.utc) self.benchmark_returns, self.treasury_curves = \ factory.load_market_data() self.trading_env = TradingEnvironment( self.benchmark_returns, self.treasury_curves, period_start=self.start_date, period_end=self.end_date, capital_base=1000.0 ) self.oneday = datetime.timedelta(days=1)
def test_trading_environment_days_in_period(self): benchmark_returns, treasury_curves = \ factory.load_market_data() # January 2008 # Su Mo Tu We Th Fr Sa # 1 2 3 4 5 # 6 7 8 9 10 11 12 # 13 14 15 16 17 18 19 # 20 21 22 23 24 25 26 # 27 28 29 30 31 env = TradingEnvironment( benchmark_returns, treasury_curves, period_start=datetime(2007, 12, 31, tzinfo=pytz.utc), period_end=datetime(2008, 1, 7, tzinfo=pytz.utc), capital_base=100000, ) expected_trading_days = ( datetime(2007, 12, 31, tzinfo=pytz.utc), # Skip new years #holidays taken from: http://www.nyse.com/press/1191407641943.html datetime(2008, 1, 2, tzinfo=pytz.utc), datetime(2008, 1, 3, tzinfo=pytz.utc), datetime(2008, 1, 4, tzinfo=pytz.utc), # Skip Saturday # Skip Sunday datetime(2008, 1, 7, tzinfo=pytz.utc) ) num_expected_trading_days = 5 self.assertEquals(num_expected_trading_days, env.days_in_period) np.testing.assert_array_equal(expected_trading_days, env.period_trading_days)
def create_env(self, start_dt=None): benchmark_returns, treasury_curves = \ factory.load_market_data() if not start_dt: for n in range(100): random_index = random.randint( 0, len(treasury_curves) ) start_dt = treasury_curves.keys()[random_index] end_dt = start_dt + datetime.timedelta(days=365) now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) if end_dt <= now: break else: end_dt = start_dt + datetime.timedelta(days=365) now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) assert end_dt <= now, """ failed to find a date suitable daterange after 100 attempts. please double check treasury and benchmark data in findb, and re-run the test.""" assert start_dt < end_dt, "start_dt must be less than end_dt" trading_environment = TradingEnvironment( benchmark_returns, treasury_curves, period_start=start_dt, period_end=end_dt ) return trading_environment, start_dt, end_dt
def test_tracker(self, parameter_comment, days_to_delete): """ @days_to_delete - configures which days in the data set we should remove, used for ensuring that we still return performance messages even when there is no data. """ # This date range covers Columbus day, # however Columbus day is not a market holiday # # October 2008 # Su Mo Tu We Th Fr Sa # 1 2 3 4 # 5 6 7 8 9 10 11 # 12 13 14 15 16 17 18 # 19 20 21 22 23 24 25 # 26 27 28 29 30 31 start_dt = datetime.datetime(year=2008, month=10, day=9, tzinfo=pytz.utc) end_dt = datetime.datetime(year=2008, month=10, day=16, tzinfo=pytz.utc) trade_count = 6 sid = 133 price = 10.1 price_list = [price] * trade_count volume = [100] * trade_count trade_time_increment = datetime.timedelta(days=1) benchmark_returns, treasury_curves = \ factory.load_market_data() trading_environment = TradingEnvironment( benchmark_returns, treasury_curves, period_start=start_dt, period_end=end_dt ) trade_history = factory.create_trade_history( sid, price_list, volume, trade_time_increment, trading_environment, source_id="factory1" ) sid2 = 134 price2 = 12.12 price2_list = [price2] * trade_count trade_history2 = factory.create_trade_history( sid2, price2_list, volume, trade_time_increment, trading_environment, source_id="factory2" ) # 'middle' start of 3 depends on number of days == 7 middle = 3 # First delete from middle if days_to_delete.middle: del trade_history[middle:(middle + days_to_delete.middle)] del trade_history2[middle:(middle + days_to_delete.middle)] # Delete start if days_to_delete.start: del trade_history[:days_to_delete.start] del trade_history2[:days_to_delete.start] # Delete from end if days_to_delete.end: del trade_history[-days_to_delete.end:] del trade_history2[-days_to_delete.end:] trading_environment.first_open = \ trading_environment.calculate_first_open() trading_environment.last_close = \ trading_environment.calculate_last_close() trading_environment.capital_base = 1000.0 trading_environment.frame_index = [ 'sid', 'volume', 'dt', 'price', 'changed'] perf_tracker = perf.PerformanceTracker( trading_environment ) events = date_sorted_sources(trade_history, trade_history2) events = [self.event_with_txn(event, trade_history[0].dt) for event in events] # Extract events with transactions to use for verification. events_with_txns = [event for event in events if event.TRANSACTION] perf_messages = \ [msg for date, snapshot in perf_tracker.transform( itertools.groupby(events, attrgetter('dt'))) for event in snapshot for msg in event.perf_messages] end_perf_messages, risk_message = perf_tracker.handle_simulation_end() perf_messages.extend(end_perf_messages) #we skip two trades, to test case of None transaction self.assertEqual(perf_tracker.txn_count, len(events_with_txns)) cumulative_pos = perf_tracker.cumulative_performance.positions[sid] expected_size = len(events_with_txns) / 2 * -25 self.assertEqual(cumulative_pos.amount, expected_size) self.assertEqual(perf_tracker.last_close, perf_tracker.cumulative_risk_metrics.end_date) self.assertEqual(len(perf_messages), trading_environment.days_in_period)