mirror of
https://github.com/wassname/catalyst.git
synced 2026-09-11 12:00:50 +08:00
TST: Make params explicit to calc results.
Change calculate_results to take explicit parameters for sim_params, env and benchmark_events instead of reading those values off of the TestCase instance. This prepares for tests setting specific sim_params in each test case, which is needed for an incoming refactoring of how the test data is set up.
This commit is contained in:
+49
-16
@@ -144,7 +144,9 @@ def benchmark_events_in_range(sim_params, env):
|
||||
]
|
||||
|
||||
|
||||
def calculate_results(host,
|
||||
def calculate_results(sim_params,
|
||||
env,
|
||||
benchmark_events,
|
||||
trade_events,
|
||||
dividend_events=None,
|
||||
splits=None,
|
||||
@@ -175,7 +177,7 @@ def calculate_results(host,
|
||||
txns = txns or []
|
||||
splits = splits or []
|
||||
|
||||
perf_tracker = perf.PerformanceTracker(host.sim_params, host.env)
|
||||
perf_tracker = perf.PerformanceTracker(sim_params, env)
|
||||
|
||||
if dividend_events is not None:
|
||||
dividend_frame = pd.DataFrame(
|
||||
@@ -190,7 +192,7 @@ def calculate_results(host,
|
||||
trade_events = sorted(trade_events, key=lambda ev: (ev.dt, ev.source_id))
|
||||
|
||||
# Add a benchmark event for each date.
|
||||
trades_plus_bm = date_sorted_sources(trade_events, host.benchmark_events)
|
||||
trades_plus_bm = date_sorted_sources(trade_events, benchmark_events)
|
||||
|
||||
# Filter out benchmark events that are later than the last trade date.
|
||||
filtered_trades_plus_bm = (filt_event for filt_event in trades_plus_bm
|
||||
@@ -293,7 +295,9 @@ class TestSplitPerformance(unittest.TestCase):
|
||||
),
|
||||
]
|
||||
|
||||
results = calculate_results(self, events, txns=txns, splits=splits)
|
||||
results = calculate_results(self.sim_params, self.env,
|
||||
self.benchmark_events,
|
||||
events, txns=txns, splits=splits)
|
||||
|
||||
# should have 33 shares (at $60 apiece) and $20 in cash
|
||||
self.assertEqual(2, len(results))
|
||||
@@ -417,7 +421,11 @@ class TestCommissionEvents(unittest.TestCase):
|
||||
|
||||
# Insert a purchase order.
|
||||
txns = [create_txn(events[0], 20, 1)]
|
||||
results = calculate_results(self, events, txns=txns)
|
||||
results = calculate_results(self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
txns=txns)
|
||||
|
||||
# Validate that we lost 320 dollars from our cash pool.
|
||||
self.assertEqual(results[-1]['cumulative_perf']['ending_cash'],
|
||||
@@ -476,7 +484,11 @@ class TestCommissionEvents(unittest.TestCase):
|
||||
|
||||
events.append(cash_adjustment)
|
||||
|
||||
results = calculate_results(self, events, txns=txns)
|
||||
results = calculate_results(self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
txns=txns)
|
||||
# Validate that we lost 300 dollars from our cash pool.
|
||||
self.assertEqual(results[-1]['cumulative_perf']['ending_cash'],
|
||||
9700)
|
||||
@@ -499,7 +511,10 @@ class TestCommissionEvents(unittest.TestCase):
|
||||
cash_adjustment = factory.create_commission(1, 300.0, cash_adj_dt)
|
||||
events.append(cash_adjustment)
|
||||
|
||||
results = calculate_results(self, events)
|
||||
results = calculate_results(self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events)
|
||||
# Validate that we lost 300 dollars from our cash pool.
|
||||
self.assertEqual(results[-1]['cumulative_perf']['ending_cash'],
|
||||
9700)
|
||||
@@ -559,7 +574,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
# Simulate a transaction being filled prior to the ex_date.
|
||||
txns = [create_txn(events[0], 10.0, 100)]
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
txns=txns,
|
||||
@@ -613,7 +630,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
txns = [create_txn(events[0], 10.0, 100)]
|
||||
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
txns=txns,
|
||||
@@ -659,7 +678,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
txns = [create_txn(events[1], 10.0, 100)]
|
||||
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
txns=txns,
|
||||
@@ -702,7 +723,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
txns = [buy_txn, sell_txn]
|
||||
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
txns=txns,
|
||||
@@ -744,7 +767,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
txns = [buy_txn, sell_txn]
|
||||
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
txns=txns,
|
||||
@@ -788,7 +813,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
txns = [create_txn(events[1], 10.0, 100)]
|
||||
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
txns=txns,
|
||||
@@ -833,7 +860,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
txns = [create_txn(events[1], 10.0, -100)]
|
||||
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
txns=txns,
|
||||
@@ -871,7 +900,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
)
|
||||
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
)
|
||||
@@ -919,7 +950,9 @@ class TestDividendPerformance(unittest.TestCase):
|
||||
# Simulate a transaction being filled prior to the ex_date.
|
||||
txns = [create_txn(events[0], 10.0, 100)]
|
||||
results = calculate_results(
|
||||
self,
|
||||
self.sim_params,
|
||||
self.env,
|
||||
self.benchmark_events,
|
||||
events,
|
||||
dividend_events=[dividend],
|
||||
txns=txns,
|
||||
|
||||
Reference in New Issue
Block a user