TST: Ensure that create_trade_history uses midnight for daily trades.

Prepare for implementation of backtest loop that depends on daily
trades being grouped by midnight.
This commit is contained in:
Eddie Hebert
2013-04-25 13:25:50 -04:00
parent 2b0a91e568
commit 4b33d6ea4b
2 changed files with 12 additions and 4 deletions
+5 -3
View File
@@ -80,6 +80,7 @@ class TestDividendPerformance(unittest.TestCase):
)
self.assertEqual(after.hour, 13)
@trading.use_environment(trading.TradingEnvironment())
def test_long_position_receives_dividend(self):
#post some trades in the market
events = factory.create_trade_history(
@@ -126,11 +127,12 @@ class TestDividendPerformance(unittest.TestCase):
perf_messages, risk = perf_tracker.handle_simulation_end()
results.append(perf_messages[0])
self.assertEqual(results[0]['daily_perf']['period_open'], events[0].dt)
self.assertEqual(
results[0]['daily_perf']['period_open'],
trading.environment.get_open_and_close(events[0].dt)[0])
self.assertEqual(
results[-1]['daily_perf']['period_open'],
events[-1].dt
)
trading.environment.get_open_and_close(events[-1].dt)[0])
self.assertEqual(len(results), 5)
cumulative_returns = \
+7 -1
View File
@@ -145,8 +145,14 @@ def create_trade_history(sid, prices, amounts, interval, sim_params,
trades = []
current = sim_params.first_open
oneday = timedelta(days=1)
use_midnight = interval >= oneday
for price, amount in zip(prices, amounts):
trade = create_trade(sid, price, amount, current, source_id)
if use_midnight:
trade_dt = current.replace(hour=0, minute=0)
else:
trade_dt = current
trade = create_trade(sid, price, amount, trade_dt, source_id)
trades.append(trade)
current = get_next_trading_dt(current, interval)