mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-07 01:38:50 +08:00
ENH: can_trade should take restricted list into account
Additionally, create an option for a violation of a 'do not order' trading control to log an error instead of failing
This commit is contained in:
+56
-13
@@ -76,6 +76,11 @@ from zipline.finance.commission import PerShare
|
||||
from zipline.finance.execution import LimitOrder
|
||||
from zipline.finance.order import ORDER_STATUS
|
||||
from zipline.finance.trading import SimulationParameters
|
||||
from zipline.finance.restrictions import (
|
||||
Restriction,
|
||||
HistoricalRestrictions,
|
||||
RESTRICTION_STATES,
|
||||
)
|
||||
from zipline.testing import (
|
||||
FakeDataPortal,
|
||||
create_daily_df_for_asset,
|
||||
@@ -2789,33 +2794,71 @@ class TestTradingControls(WithSimParams, WithDataPortal, ZiplineTestCase):
|
||||
self.check_algo_fails(algo, handle_data, 0)
|
||||
|
||||
def test_set_do_not_order_list(self):
|
||||
# set the restricted list to be the sid, and fail.
|
||||
algo = SetDoNotOrderListAlgorithm(
|
||||
sid=self.sid,
|
||||
restricted_list=[self.sid],
|
||||
sim_params=self.sim_params,
|
||||
env=self.env,
|
||||
)
|
||||
|
||||
def handle_data(algo, data):
|
||||
algo.could_trade = data.can_trade(algo.sid(self.sid))
|
||||
algo.order(algo.sid(self.sid), 100)
|
||||
algo.order_count += 1
|
||||
|
||||
# set the restricted list to be one sid for the entire simulation,
|
||||
# and fail.
|
||||
rlm = HistoricalRestrictions([
|
||||
Restriction(
|
||||
self.sid,
|
||||
self.sim_params.start_session,
|
||||
RESTRICTION_STATES.FROZEN)
|
||||
])
|
||||
algo = SetDoNotOrderListAlgorithm(
|
||||
sid=self.sid,
|
||||
restricted_list=rlm,
|
||||
sim_params=self.sim_params,
|
||||
env=self.env,
|
||||
)
|
||||
self.check_algo_fails(algo, handle_data, 0)
|
||||
self.assertFalse(algo.could_trade)
|
||||
|
||||
# if the restricted list is a static list, then use a shim.
|
||||
rlm = [self.sid]
|
||||
algo = SetDoNotOrderListAlgorithm(
|
||||
sid=self.sid,
|
||||
restricted_list=rlm,
|
||||
sim_params=self.sim_params,
|
||||
env=self.env,
|
||||
)
|
||||
self.check_algo_fails(algo, handle_data, 0)
|
||||
self.assertFalse(algo.could_trade)
|
||||
|
||||
# just log an error on the violation if we choose not to fail.
|
||||
algo = SetDoNotOrderListAlgorithm(
|
||||
sid=self.sid,
|
||||
restricted_list=rlm,
|
||||
sim_params=self.sim_params,
|
||||
env=self.env,
|
||||
on_error='log'
|
||||
)
|
||||
with make_test_handler(self) as log_catcher:
|
||||
self.check_algo_succeeds(algo, handle_data)
|
||||
logs = [r.message for r in log_catcher.records]
|
||||
self.assertIn("Order for 100 shares of Equity(133 [A]) at "
|
||||
"2006-01-03 21:00:00+00:00 violates trading constraint "
|
||||
"RestrictedListOrder({})", logs)
|
||||
self.assertFalse(algo.could_trade)
|
||||
|
||||
# set the restricted list to exclude the sid, and succeed
|
||||
rlm = HistoricalRestrictions([
|
||||
Restriction(
|
||||
sid,
|
||||
self.sim_params.start_session,
|
||||
RESTRICTION_STATES.FROZEN) for sid in [134, 135, 136]
|
||||
])
|
||||
algo = SetDoNotOrderListAlgorithm(
|
||||
sid=self.sid,
|
||||
restricted_list=[134, 135, 136],
|
||||
restricted_list=rlm,
|
||||
sim_params=self.sim_params,
|
||||
env=self.env,
|
||||
)
|
||||
|
||||
def handle_data(algo, data):
|
||||
algo.order(algo.sid(self.sid), 100)
|
||||
algo.order_count += 1
|
||||
|
||||
self.check_algo_succeeds(algo, handle_data)
|
||||
self.assertTrue(algo.could_trade)
|
||||
|
||||
def test_set_max_order_size(self):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user