|
|
|
@ -540,6 +540,157 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
|
[self.trades[2], self.trades[3]],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@patch(
|
|
|
|
|
"core.trading.active_management.ActiveManagement.add_action",
|
|
|
|
|
)
|
|
|
|
|
def test_handle_violation(self, add_action):
|
|
|
|
|
self.ams.handle_violation("max_loss", "close", "trade_id")
|
|
|
|
|
self.ams.handle_violation("position_size", "adjust", "trade_id2", size=1000)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(add_action.call_count, 2)
|
|
|
|
|
add_action.assert_any_call("close", "max_loss", "trade_id")
|
|
|
|
|
add_action.assert_any_call("adjust", "position_size", "trade_id2", size=1000)
|
|
|
|
|
|
|
|
|
|
def test_add_action(self):
|
|
|
|
|
protection_args = {
|
|
|
|
|
"take_profit_price": D("1.07934"),
|
|
|
|
|
"stop_loss_price": D("1.05276"),
|
|
|
|
|
}
|
|
|
|
|
self.ams.add_action("close", "trading_time", "fake_trade_id")
|
|
|
|
|
self.ams.add_action("close", "protection", "fake_trade_id2", **protection_args)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
self.ams.actions,
|
|
|
|
|
{
|
|
|
|
|
"close": [
|
|
|
|
|
{"id": "fake_trade_id", "check": "trading_time", "extra": {}},
|
|
|
|
|
{
|
|
|
|
|
"id": "fake_trade_id2",
|
|
|
|
|
"check": "protection",
|
|
|
|
|
"extra": protection_args,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_reduce_actions(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_close_trades")
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_notify")
|
|
|
|
|
def test_run_actions(self, bulk_notify, bulk_close_trades):
|
|
|
|
|
protection_args = {
|
|
|
|
|
"take_profit_price": D("1.07934"),
|
|
|
|
|
"stop_loss_price": D("1.05276"),
|
|
|
|
|
}
|
|
|
|
|
self.ams.add_action("close", "trading_time", "fake_trade_id")
|
|
|
|
|
self.ams.add_action("close", "protection", "fake_trade_id2", **protection_args)
|
|
|
|
|
self.ams.run_actions()
|
|
|
|
|
|
|
|
|
|
expected_action_cast = [
|
|
|
|
|
{"id": "fake_trade_id", "check": "trading_time", "extra": {}},
|
|
|
|
|
{
|
|
|
|
|
"id": "fake_trade_id2",
|
|
|
|
|
"check": "protection",
|
|
|
|
|
"extra": {
|
|
|
|
|
**protection_args,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
bulk_notify.assert_called_once_with(
|
|
|
|
|
"close",
|
|
|
|
|
expected_action_cast,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
bulk_close_trades.assert_called_once_with(
|
|
|
|
|
["fake_trade_id", "fake_trade_id2"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_close_trades")
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_notify")
|
|
|
|
|
def test_run_actions_notify_only(self, bulk_notify, bulk_close_trades):
|
|
|
|
|
protection_args = {
|
|
|
|
|
"take_profit_price": D("1.07934"),
|
|
|
|
|
"stop_loss_price": D("1.05276"),
|
|
|
|
|
}
|
|
|
|
|
self.ams.add_action("notify", "trading_time", "fake_trade_id")
|
|
|
|
|
self.ams.add_action("notify", "protection", "fake_trade_id2", **protection_args)
|
|
|
|
|
self.ams.run_actions()
|
|
|
|
|
|
|
|
|
|
expected_action_cast = [
|
|
|
|
|
{"id": "fake_trade_id", "check": "trading_time", "extra": {}},
|
|
|
|
|
{
|
|
|
|
|
"id": "fake_trade_id2",
|
|
|
|
|
"check": "protection",
|
|
|
|
|
"extra": {
|
|
|
|
|
**protection_args,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
bulk_notify.assert_called_once_with(
|
|
|
|
|
"notify",
|
|
|
|
|
expected_action_cast,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
bulk_close_trades.assert_not_called()
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_close_trades")
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_adjust")
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_notify")
|
|
|
|
|
def test_run_actions_notify_adjust_only(
|
|
|
|
|
self, bulk_notify, bulk_adjust, bulk_close_trades
|
|
|
|
|
):
|
|
|
|
|
protection_args = {
|
|
|
|
|
"take_profit_price": D("1.07934"),
|
|
|
|
|
"stop_loss_price": D("1.05276"),
|
|
|
|
|
}
|
|
|
|
|
self.ams.add_action("adjust", "position_size", "fake_trade_id", size=1000)
|
|
|
|
|
self.ams.add_action("adjust", "protection", "fake_trade_id2", **protection_args)
|
|
|
|
|
self.ams.run_actions()
|
|
|
|
|
|
|
|
|
|
expected_action_cast = [
|
|
|
|
|
{"id": "fake_trade_id", "check": "position_size", "extra": {"size": 1000}},
|
|
|
|
|
{
|
|
|
|
|
"id": "fake_trade_id2",
|
|
|
|
|
"check": "protection",
|
|
|
|
|
"extra": {
|
|
|
|
|
**protection_args,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
bulk_notify.assert_called_once_with(
|
|
|
|
|
"adjust",
|
|
|
|
|
expected_action_cast,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
bulk_adjust.assert_called_once_with(expected_action_cast)
|
|
|
|
|
bulk_close_trades.assert_not_called()
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.adjust_position_size")
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.adjust_protection")
|
|
|
|
|
def test_bulk_adjust(self, adjust_protection, adjust_position_size):
|
|
|
|
|
expected_protection = {"take_profit_price": 1.10, "stop_loss_price": 1.05}
|
|
|
|
|
cast_list = [
|
|
|
|
|
{"id": "id1", "check": "position_size", "extra": {"size": 1000}},
|
|
|
|
|
{"id": "id2", "check": "protection", "extra": expected_protection},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
self.ams.bulk_adjust(cast_list)
|
|
|
|
|
|
|
|
|
|
adjust_position_size.assert_called_once_with("id1", 1000)
|
|
|
|
|
adjust_protection.assert_called_once_with("id2", expected_protection)
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.close_trade")
|
|
|
|
|
def test_bulk_close_trades(self, close_trade):
|
|
|
|
|
self.ams.bulk_close_trades(["id1", "id2"])
|
|
|
|
|
|
|
|
|
|
self.assertEqual(close_trade.call_count, 2)
|
|
|
|
|
close_trade.assert_any_call("id1")
|
|
|
|
|
close_trade.assert_any_call("id2")
|
|
|
|
|
|
|
|
|
|
def test_bulk_notify(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_max_risk_not_violated_after_adjusting_protection(self):
|
|
|
|
|
"""
|
|
|
|
|
Ensure the max risk check is not violated after adjusting the protection.
|
|
|
|
@ -551,3 +702,12 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
|
Ensure the max risk check is not violated after adjusting the position size.
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_position_size_reduced(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_protection_added(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_protection_amended(self):
|
|
|
|
|
pass
|
|
|
|
|