|
|
@ -189,19 +189,20 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
# Ensure the correct trade has been passed to the violation
|
|
|
|
# Ensure the correct trade has been passed to the violation
|
|
|
|
self.assertIn(call[0][2], expected_trades)
|
|
|
|
self.assertIn(call[0][2], expected_trades)
|
|
|
|
if expected_args:
|
|
|
|
if expected_args:
|
|
|
|
self.assertEqual(call[0][3], expected_args)
|
|
|
|
_, kwargs = call
|
|
|
|
|
|
|
|
self.assertEqual(kwargs, expected_args)
|
|
|
|
|
|
|
|
# self.assertEqual(call[0][3], expected_args)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_run_checks(self):
|
|
|
|
def test_run_checks(self, handle_violation):
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.assertEqual(handle_violation.call_count, 0)
|
|
|
|
self.assertEqual(len(self.ams.actions), 0)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_trading_time_violated(self):
|
|
|
|
def test_trading_time_violated(self, handle_violation):
|
|
|
|
|
|
|
|
self.trades[0]["openTime"] = "2023-02-17T11:38:06.302917Z" # Friday
|
|
|
|
self.trades[0]["openTime"] = "2023-02-17T11:38:06.302917Z" # Friday
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"trading_time", handle_violation.call_args_list, "close", [self.trades[0]]
|
|
|
|
self.ams.actions,
|
|
|
|
|
|
|
|
{"close": [{"id": "20083", "check": "trading_time", "extra": {}}]},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def create_hook_signal(self):
|
|
|
|
def create_hook_signal(self):
|
|
|
@ -217,8 +218,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return signal
|
|
|
|
return signal
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_trends_violated(self):
|
|
|
|
def test_trends_violated(self, handle_violation):
|
|
|
|
|
|
|
|
signal = self.create_hook_signal()
|
|
|
|
signal = self.create_hook_signal()
|
|
|
|
self.strategy.trend_signals.set([signal])
|
|
|
|
self.strategy.trend_signals.set([signal])
|
|
|
|
self.strategy.trends = {"EUR_USD": "sell"}
|
|
|
|
self.strategy.trends = {"EUR_USD": "sell"}
|
|
|
@ -226,18 +226,24 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
self.strategy.save()
|
|
|
|
self.strategy.save()
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"trends", handle_violation.call_args_list, "close", self.trades
|
|
|
|
self.ams.actions,
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"close": [
|
|
|
|
|
|
|
|
{"id": "20084", "check": "trends", "extra": {}},
|
|
|
|
|
|
|
|
{"id": "20083", "check": "trends", "extra": {}},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_trends_violated_none(self):
|
|
|
|
def test_trends_violated_none(self, handle_violation):
|
|
|
|
|
|
|
|
signal = self.create_hook_signal()
|
|
|
|
signal = self.create_hook_signal()
|
|
|
|
self.strategy.trend_signals.set([signal])
|
|
|
|
self.strategy.trend_signals.set([signal])
|
|
|
|
self.strategy.trends = {"EUR_USD": "buy"}
|
|
|
|
self.strategy.trends = {"EUR_USD": "buy"}
|
|
|
|
self.strategy.save()
|
|
|
|
self.strategy.save()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.check_violation("trends", handle_violation.call_args_list, "close", [])
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(self.ams.actions, {})
|
|
|
|
|
|
|
|
|
|
|
|
# Mock crossfilter here since we want to allow this conflict in order to test that
|
|
|
|
# Mock crossfilter here since we want to allow this conflict in order to test that
|
|
|
|
# trends only close trades that are in the wrong direction
|
|
|
|
# trends only close trades that are in the wrong direction
|
|
|
@ -245,8 +251,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
"core.trading.active_management.ActiveManagement.check_crossfilter",
|
|
|
|
"core.trading.active_management.ActiveManagement.check_crossfilter",
|
|
|
|
return_value=None,
|
|
|
|
return_value=None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_trends_violated_partial(self, check_crossfilter):
|
|
|
|
def test_trends_violated_partial(self, handle_violation, check_crossfilter):
|
|
|
|
|
|
|
|
signal = self.create_hook_signal()
|
|
|
|
signal = self.create_hook_signal()
|
|
|
|
self.strategy.trend_signals.set([signal])
|
|
|
|
self.strategy.trend_signals.set([signal])
|
|
|
|
self.strategy.trends = {"EUR_USD": "sell"}
|
|
|
|
self.strategy.trends = {"EUR_USD": "sell"}
|
|
|
@ -257,25 +262,53 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"trends", handle_violation.call_args_list, "close", [self.trades[1]]
|
|
|
|
self.ams.actions,
|
|
|
|
|
|
|
|
{"close": [{"id": "20084", "check": "trends", "extra": {}}]},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_position_size_violated(self):
|
|
|
|
def test_position_size_violated(self, handle_violation):
|
|
|
|
|
|
|
|
self.trades[0]["currentUnits"] = "100000"
|
|
|
|
self.trades[0]["currentUnits"] = "100000"
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
self.ams.actions,
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"close": [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"id": "20083",
|
|
|
|
|
|
|
|
"check": "position_size",
|
|
|
|
|
|
|
|
"extra": {"size": D("500.000")},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
def test_protection_violated(self):
|
|
|
|
"position_size",
|
|
|
|
self.trades[0]["takeProfitOrder"] = {"price": "0.0001"}
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
self.trades[0]["stopLossOrder"] = {"price": "0.0001"}
|
|
|
|
"close",
|
|
|
|
self.ams.run_checks()
|
|
|
|
[self.trades[0]],
|
|
|
|
|
|
|
|
{"size": 500},
|
|
|
|
expected_args = {
|
|
|
|
|
|
|
|
"take_profit_price": D("1.07934"),
|
|
|
|
|
|
|
|
"stop_loss_price": D("1.05276"),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
self.ams.actions,
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"close": [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"id": "20083",
|
|
|
|
|
|
|
|
"check": "protection",
|
|
|
|
|
|
|
|
"extra": {
|
|
|
|
|
|
|
|
**expected_args,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_protection_violated_absent(self):
|
|
|
|
def test_protection_violated_absent(self, handle_violation):
|
|
|
|
|
|
|
|
self.trades[0]["takeProfitOrder"] = None
|
|
|
|
self.trades[0]["takeProfitOrder"] = None
|
|
|
|
self.trades[0]["stopLossOrder"] = None
|
|
|
|
self.trades[0]["stopLossOrder"] = None
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
@ -284,16 +317,22 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
"take_profit_price": D("1.07934"),
|
|
|
|
"take_profit_price": D("1.07934"),
|
|
|
|
"stop_loss_price": D("1.05276"),
|
|
|
|
"stop_loss_price": D("1.05276"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"protection",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{
|
|
|
|
"close",
|
|
|
|
"close": [
|
|
|
|
[self.trades[0]],
|
|
|
|
{
|
|
|
|
expected_args,
|
|
|
|
"id": "20083",
|
|
|
|
|
|
|
|
"check": "protection",
|
|
|
|
|
|
|
|
"extra": {
|
|
|
|
|
|
|
|
**expected_args,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_protection_violated_absent_not_required(self):
|
|
|
|
def test_protection_violated_absent_not_required(self, handle_violation):
|
|
|
|
|
|
|
|
self.strategy.order_settings.take_profit_percent = 0
|
|
|
|
self.strategy.order_settings.take_profit_percent = 0
|
|
|
|
self.strategy.order_settings.stop_loss_percent = 0
|
|
|
|
self.strategy.order_settings.stop_loss_percent = 0
|
|
|
|
self.strategy.order_settings.save()
|
|
|
|
self.strategy.order_settings.save()
|
|
|
@ -301,10 +340,9 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
self.trades[0]["stopLossOrder"] = None
|
|
|
|
self.trades[0]["stopLossOrder"] = None
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(handle_violation.call_count, 0)
|
|
|
|
self.assertEqual(self.ams.actions, {})
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_asset_groups_violated(self):
|
|
|
|
def test_asset_groups_violated(self, handle_violation):
|
|
|
|
|
|
|
|
asset_group = AssetGroup.objects.create(
|
|
|
|
asset_group = AssetGroup.objects.create(
|
|
|
|
user=self.user,
|
|
|
|
user=self.user,
|
|
|
|
name="Test Asset Group",
|
|
|
|
name="Test Asset Group",
|
|
|
@ -319,15 +357,17 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
self.strategy.save()
|
|
|
|
self.strategy.save()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"asset_group",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{
|
|
|
|
"close",
|
|
|
|
"close": [
|
|
|
|
self.trades, # All trades should be closed, since all are USD quote
|
|
|
|
{"id": "20084", "check": "asset_group", "extra": {}},
|
|
|
|
|
|
|
|
{"id": "20083", "check": "asset_group", "extra": {}},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_asset_groups_violated_invert(self):
|
|
|
|
def test_asset_groups_violated_invert(self, handle_violation):
|
|
|
|
|
|
|
|
self.trades[0]["side"] = "short"
|
|
|
|
self.trades[0]["side"] = "short"
|
|
|
|
self.trades[1]["side"] = "short"
|
|
|
|
self.trades[1]["side"] = "short"
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
@ -345,28 +385,27 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
self.strategy.save()
|
|
|
|
self.strategy.save()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"asset_group",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{
|
|
|
|
"close",
|
|
|
|
"close": [
|
|
|
|
self.trades, # All trades should be closed, since all are USD quote
|
|
|
|
{"id": "20084", "check": "asset_group", "extra": {}},
|
|
|
|
|
|
|
|
{"id": "20083", "check": "asset_group", "extra": {}},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_crossfilter_violated_side(self):
|
|
|
|
def test_crossfilter_violated_side(self, handle_violation):
|
|
|
|
|
|
|
|
self.trades[1]["side"] = "short"
|
|
|
|
self.trades[1]["side"] = "short"
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"crossfilter",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{"close": [{"id": "20084", "check": "crossfilter", "extra": {}}]},
|
|
|
|
"close",
|
|
|
|
|
|
|
|
[self.trades[1]], # Only close newer trade
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_crossfilter_violated_side_multiple(self):
|
|
|
|
def test_crossfilter_violated_side_multiple(self, handle_violation):
|
|
|
|
|
|
|
|
self.add_trade(
|
|
|
|
self.add_trade(
|
|
|
|
"20085", "EUR_USD", "short", "2023-02-13T12:39:06.302917985Z"
|
|
|
|
"20085", "EUR_USD", "short", "2023-02-13T12:39:06.302917985Z"
|
|
|
|
) # 2:
|
|
|
|
) # 2:
|
|
|
@ -375,28 +414,28 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"crossfilter",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{
|
|
|
|
"close",
|
|
|
|
"close": [
|
|
|
|
self.trades[2:], # Only close newer trades
|
|
|
|
{"id": "20087", "check": "crossfilter", "extra": {}},
|
|
|
|
|
|
|
|
{"id": "20086", "check": "crossfilter", "extra": {}},
|
|
|
|
|
|
|
|
{"id": "20085", "check": "crossfilter", "extra": {}},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_crossfilter_violated_symbol(self):
|
|
|
|
def test_crossfilter_violated_symbol(self, handle_violation):
|
|
|
|
|
|
|
|
# Change symbol to conflict with long on EUR_USD
|
|
|
|
# Change symbol to conflict with long on EUR_USD
|
|
|
|
self.trades[1]["symbol"] = "USD_EUR"
|
|
|
|
self.trades[1]["symbol"] = "USD_EUR"
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"crossfilter",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{"close": [{"id": "20084", "check": "crossfilter", "extra": {}}]},
|
|
|
|
"close",
|
|
|
|
|
|
|
|
[self.trades[1]], # Only close newer trade
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_crossfilter_violated_symbol_multiple(self):
|
|
|
|
def test_crossfilter_violated_symbol_multiple(self, handle_violation):
|
|
|
|
|
|
|
|
self.add_trade(
|
|
|
|
self.add_trade(
|
|
|
|
"20085", "USD_EUR", "long", "2023-02-13T12:39:06.302917985Z"
|
|
|
|
"20085", "USD_EUR", "long", "2023-02-13T12:39:06.302917985Z"
|
|
|
|
) # 2:
|
|
|
|
) # 2:
|
|
|
@ -404,15 +443,18 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
self.add_trade("20087", "USD_EUR", "long", "2023-02-13T14:39:06.302917985Z")
|
|
|
|
self.add_trade("20087", "USD_EUR", "long", "2023-02-13T14:39:06.302917985Z")
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"crossfilter",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{
|
|
|
|
"close",
|
|
|
|
"close": [
|
|
|
|
self.trades[2:], # Only close newer trades
|
|
|
|
{"id": "20087", "check": "crossfilter", "extra": {}},
|
|
|
|
|
|
|
|
{"id": "20086", "check": "crossfilter", "extra": {}},
|
|
|
|
|
|
|
|
{"id": "20085", "check": "crossfilter", "extra": {}},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_max_open_trades_violated(self):
|
|
|
|
def test_max_open_trades_violated(self, handle_violation):
|
|
|
|
|
|
|
|
for x in range(9):
|
|
|
|
for x in range(9):
|
|
|
|
self.add_trade(
|
|
|
|
self.add_trade(
|
|
|
|
str(x),
|
|
|
|
str(x),
|
|
|
@ -422,15 +464,12 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"max_open_trades",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{"close": [{"id": "8", "check": "max_open_trades", "extra": {}}]},
|
|
|
|
"close",
|
|
|
|
|
|
|
|
self.trades[10:], # Only close newer trades
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_max_open_trades_per_symbol_violated(self):
|
|
|
|
def test_max_open_trades_per_symbol_violated(self, handle_violation):
|
|
|
|
|
|
|
|
for x in range(4):
|
|
|
|
for x in range(4):
|
|
|
|
self.add_trade(
|
|
|
|
self.add_trade(
|
|
|
|
str(x),
|
|
|
|
str(x),
|
|
|
@ -440,11 +479,14 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.check_violation(
|
|
|
|
|
|
|
|
"max_open_trades_per_symbol",
|
|
|
|
self.assertEqual(
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
self.ams.actions,
|
|
|
|
"close",
|
|
|
|
{
|
|
|
|
self.trades[5:], # Only close newer trades
|
|
|
|
"close": [
|
|
|
|
|
|
|
|
{"id": "3", "check": "max_open_trades_per_symbol", "extra": {}}
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Mock position size as we have no way of checking the balance at the start of the
|
|
|
|
# Mock position size as we have no way of checking the balance at the start of the
|
|
|
@ -456,8 +498,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
"core.trading.active_management.ActiveManagement.check_position_size",
|
|
|
|
"core.trading.active_management.ActiveManagement.check_position_size",
|
|
|
|
return_value=None,
|
|
|
|
return_value=None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_max_loss_violated(self, check_position_size):
|
|
|
|
def test_max_loss_violated(self, handle_violation, check_position_size):
|
|
|
|
|
|
|
|
self.balance = D("1")
|
|
|
|
self.balance = D("1")
|
|
|
|
self.balance_usd = D("0.69")
|
|
|
|
self.balance_usd = D("0.69")
|
|
|
|
|
|
|
|
|
|
|
@ -465,11 +506,9 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"max_loss",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{"close": [{"id": None, "check": "max_loss", "extra": {}}]},
|
|
|
|
"close",
|
|
|
|
|
|
|
|
[None],
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch(
|
|
|
|
@patch(
|
|
|
@ -480,10 +519,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
"core.trading.active_management.ActiveManagement.check_protection",
|
|
|
|
"core.trading.active_management.ActiveManagement.check_protection",
|
|
|
|
return_value=None,
|
|
|
|
return_value=None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_max_risk_violated(self, check_protection, check_position_size):
|
|
|
|
def test_max_risk_violated(
|
|
|
|
|
|
|
|
self, handle_violation, check_protection, check_position_size
|
|
|
|
|
|
|
|
):
|
|
|
|
|
|
|
|
self.add_trade(
|
|
|
|
self.add_trade(
|
|
|
|
"20085",
|
|
|
|
"20085",
|
|
|
|
"EUR_USD",
|
|
|
|
"EUR_USD",
|
|
|
@ -495,11 +531,9 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"max_risk",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{"close": [{"id": "20085", "check": "max_risk", "extra": {}}]},
|
|
|
|
"close",
|
|
|
|
|
|
|
|
[self.trades[2]],
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch(
|
|
|
|
@patch(
|
|
|
@ -510,10 +544,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
"core.trading.active_management.ActiveManagement.check_protection",
|
|
|
|
"core.trading.active_management.ActiveManagement.check_protection",
|
|
|
|
return_value=None,
|
|
|
|
return_value=None,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
|
|
|
def test_max_risk_violated_multiple(self, check_protection, check_position_size):
|
|
|
|
def test_max_risk_violated_multiple(
|
|
|
|
|
|
|
|
self, handle_violation, check_protection, check_position_size
|
|
|
|
|
|
|
|
):
|
|
|
|
|
|
|
|
self.add_trade(
|
|
|
|
self.add_trade(
|
|
|
|
"20085",
|
|
|
|
"20085",
|
|
|
|
"EUR_USD",
|
|
|
|
"EUR_USD",
|
|
|
@ -533,11 +564,14 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
self.check_violation(
|
|
|
|
self.assertEqual(
|
|
|
|
"max_risk",
|
|
|
|
self.ams.actions,
|
|
|
|
handle_violation.call_args_list,
|
|
|
|
{
|
|
|
|
"close",
|
|
|
|
"close": [
|
|
|
|
[self.trades[2], self.trades[3]],
|
|
|
|
{"id": "20086", "check": "max_risk", "extra": {}},
|
|
|
|
|
|
|
|
{"id": "20085", "check": "max_risk", "extra": {}},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@patch(
|
|
|
|
@patch(
|
|
|
@ -574,7 +608,17 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_reduce_actions(self):
|
|
|
|
def test_reduce_actions(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
|
|
|
|
Test that closing actions precede adjusting actions.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.ams.add_action("close", "trading_time", "fake_trade_id")
|
|
|
|
|
|
|
|
self.ams.add_action("adjust", "position_size", "fake_trade_id", size=1000)
|
|
|
|
|
|
|
|
self.assertEqual(len(self.ams.actions["close"]), 1)
|
|
|
|
|
|
|
|
self.assertEqual(len(self.ams.actions["adjust"]), 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.reduce_actions()
|
|
|
|
|
|
|
|
self.assertEqual(len(self.ams.actions["close"]), 1)
|
|
|
|
|
|
|
|
self.assertEqual(len(self.ams.actions["adjust"]), 0)
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_close_trades")
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_close_trades")
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_notify")
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.bulk_notify")
|
|
|
@ -688,26 +732,263 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
|
|
|
close_trade.assert_any_call("id1")
|
|
|
|
close_trade.assert_any_call("id1")
|
|
|
|
close_trade.assert_any_call("id2")
|
|
|
|
close_trade.assert_any_call("id2")
|
|
|
|
|
|
|
|
|
|
|
|
def test_bulk_notify(self):
|
|
|
|
@patch("core.trading.active_management.sendmsg")
|
|
|
|
pass
|
|
|
|
def test_bulk_notify_plain(self, sendmsg):
|
|
|
|
|
|
|
|
self.ams.bulk_notify("close", [{"id": "id1", "check": "check1", "extra": {}}])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sendmsg.assert_called_once_with(
|
|
|
|
|
|
|
|
self.user,
|
|
|
|
|
|
|
|
"ACTION: 'close' on trade ID 'id1'\nVIOLATION: 'check1'\n=========\n",
|
|
|
|
|
|
|
|
title="AMS: close",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.sendmsg")
|
|
|
|
|
|
|
|
def test_bulk_notify_extra(self, sendmsg):
|
|
|
|
|
|
|
|
self.ams.bulk_notify(
|
|
|
|
|
|
|
|
"close", [{"id": "id1", "check": "check1", "extra": {"field1": "value1"}}]
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sendmsg.assert_called_once_with(
|
|
|
|
|
|
|
|
self.user,
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
"ACTION: 'close' on trade ID 'id1'\nVIOLATION: 'check1'\nEXTRA:"
|
|
|
|
|
|
|
|
" field1: value1\n=========\n"
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
title="AMS: close",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_protection")
|
|
|
|
|
|
|
|
def test_position_size_reduced(self, check_protection):
|
|
|
|
|
|
|
|
self.active_management_policy.when_position_size_violated = "adjust"
|
|
|
|
|
|
|
|
self.active_management_policy.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.trades[0]["currentUnits"] = "100000"
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
call_args = check_protection.call_args[0][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(call_args["amount"], 500)
|
|
|
|
|
|
|
|
self.assertEqual(call_args["units"], 500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_asset_groups")
|
|
|
|
|
|
|
|
def test_protection_added(self, check_asset_groups):
|
|
|
|
|
|
|
|
self.active_management_policy.when_protection_violated = "adjust"
|
|
|
|
|
|
|
|
self.active_management_policy.save()
|
|
|
|
|
|
|
|
self.trades[0]["takeProfitOrder"] = None
|
|
|
|
|
|
|
|
self.trades[0]["stopLossOrder"] = None
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
call_args = check_asset_groups.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(call_args["take_profit_price"], D("1.07934"))
|
|
|
|
|
|
|
|
self.assertEqual(call_args["stop_loss_price"], D("1.05276"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_asset_groups")
|
|
|
|
|
|
|
|
def test_protection_amended(self, check_asset_groups):
|
|
|
|
|
|
|
|
self.active_management_policy.when_protection_violated = "adjust"
|
|
|
|
|
|
|
|
self.active_management_policy.save()
|
|
|
|
|
|
|
|
self.trades[0]["takeProfitOrder"] = {"price": "0.0001"}
|
|
|
|
|
|
|
|
self.trades[0]["stopLossOrder"] = {"price": "0.0001"}
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
call_args = check_asset_groups.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(call_args["take_profit_price"], D("1.07934"))
|
|
|
|
|
|
|
|
self.assertEqual(call_args["stop_loss_price"], D("1.05276"))
|
|
|
|
|
|
|
|
|
|
|
|
def test_max_risk_not_violated_after_adjusting_protection(self):
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.close_trade")
|
|
|
|
|
|
|
|
def test_max_risk_not_violated_after_adjusting_protection(self, close_trade):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Ensure the max risk check is not violated after adjusting the protection.
|
|
|
|
Ensure the max risk check is not violated after adjusting the protection.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
self.active_management_policy.when_protection_violated = "adjust"
|
|
|
|
|
|
|
|
self.active_management_policy.save()
|
|
|
|
|
|
|
|
|
|
|
|
def test_max_risk_not_violated_after_adjusting_position_size(self):
|
|
|
|
self.add_trade(
|
|
|
|
|
|
|
|
"20085",
|
|
|
|
|
|
|
|
"EUR_USD",
|
|
|
|
|
|
|
|
"long",
|
|
|
|
|
|
|
|
"2023-02-13T15:39:19.302917985Z",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
self.trades[2]["stopLossOrder"]["price"] = "0.001"
|
|
|
|
|
|
|
|
self.trades[2]["currentUnits"] = "13000"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(close_trade.call_count, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.close_trade")
|
|
|
|
|
|
|
|
def test_max_risk_not_violated_after_adjusting_position_size(self, close_trade):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Ensure the max risk check is not violated after adjusting the position size.
|
|
|
|
Ensure the max risk check is not violated after adjusting the position size.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
self.active_management_policy.when_position_size_violated = "adjust"
|
|
|
|
|
|
|
|
self.active_management_policy.save()
|
|
|
|
|
|
|
|
|
|
|
|
def test_position_size_reduced(self):
|
|
|
|
self.trades[0]["currentUnits"] = "100000"
|
|
|
|
pass
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
def test_protection_added(self):
|
|
|
|
self.assertEqual(close_trade.call_count, 0)
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_crossfilter")
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_trends")
|
|
|
|
|
|
|
|
def test_trading_time_mutation(self, check_trends, check_crossfilter):
|
|
|
|
|
|
|
|
self.trades[0]["openTime"] = "2023-02-17T11:38:06.302917Z" # Friday
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
def test_protection_amended(self):
|
|
|
|
self.assertEqual(check_trends.call_count, 1)
|
|
|
|
|
|
|
|
call_args = check_trends.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
call_args["id"], "20084"
|
|
|
|
|
|
|
|
) # Only run trends on the second trade
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
crossfilter_call_args = check_crossfilter.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(len(crossfilter_call_args), 1)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
crossfilter_call_args[0]["id"], "20084"
|
|
|
|
|
|
|
|
) # Same for crossfilter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_crossfilter")
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_position_size")
|
|
|
|
|
|
|
|
def test_check_trends_mutation(self, check_position_size, check_crossfilter):
|
|
|
|
|
|
|
|
signal = self.create_hook_signal()
|
|
|
|
|
|
|
|
self.strategy.trend_signals.set([signal])
|
|
|
|
|
|
|
|
self.strategy.trends = {"EUR_USD": "sell"}
|
|
|
|
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
|
|
|
|
self.strategy.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(check_position_size.call_count, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
crossfilter_call_args = check_crossfilter.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(len(crossfilter_call_args), 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_crossfilter")
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_protection")
|
|
|
|
|
|
|
|
def test_check_position_size_mutation(self, check_protection, check_crossfilter):
|
|
|
|
|
|
|
|
self.trades[0]["currentUnits"] = "100000"
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(check_protection.call_count, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
call_args = check_protection.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
call_args["id"], "20084"
|
|
|
|
|
|
|
|
) # Only run protection on the second trade
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
crossfilter_call_args = check_crossfilter.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(len(crossfilter_call_args), 1)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
crossfilter_call_args[0]["id"], "20084"
|
|
|
|
|
|
|
|
) # Same for crossfilter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_crossfilter")
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_protection")
|
|
|
|
|
|
|
|
def test_check_protection_mutation(self, check_protection, check_crossfilter):
|
|
|
|
|
|
|
|
self.trades[0]["currentUnits"] = "100000"
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(check_protection.call_count, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
call_args = check_protection.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
call_args["id"], "20084"
|
|
|
|
|
|
|
|
) # Only run protection on the second trade
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
crossfilter_call_args = check_crossfilter.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(len(crossfilter_call_args), 1)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
|
|
|
crossfilter_call_args[0]["id"], "20084"
|
|
|
|
|
|
|
|
) # Same for crossfilter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This may look similar but check_crossfilter is called with the whole trade list.
|
|
|
|
|
|
|
|
# Check that the trade that is removed from the list is not checked.
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_crossfilter")
|
|
|
|
|
|
|
|
def test_check_asset_groups_mutation(self, check_crossfilter):
|
|
|
|
|
|
|
|
asset_group = AssetGroup.objects.create(
|
|
|
|
|
|
|
|
user=self.user,
|
|
|
|
|
|
|
|
name="Test Asset Group",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
AssetRule.objects.create(
|
|
|
|
|
|
|
|
user=self.user,
|
|
|
|
|
|
|
|
asset="USD",
|
|
|
|
|
|
|
|
group=asset_group,
|
|
|
|
|
|
|
|
status=2, # Bullish
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
self.strategy.asset_group = asset_group
|
|
|
|
|
|
|
|
self.strategy.save()
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
check_crossfilter.assert_called_once_with([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_max_open_trades")
|
|
|
|
|
|
|
|
def test_check_crossfilter_mutation(self, check_max_open_trades):
|
|
|
|
|
|
|
|
self.trades[1]["side"] = "short"
|
|
|
|
|
|
|
|
self.amend_tp_sl_flip_side()
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(check_max_open_trades.call_count, 1)
|
|
|
|
|
|
|
|
call_args = check_max_open_trades.call_args[0][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(len(call_args), 1)
|
|
|
|
|
|
|
|
self.assertEqual(call_args[0]["id"], "20083")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch( # When the string is just too damn long
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
"core.trading.active_management.ActiveManagement."
|
|
|
|
|
|
|
|
"check_max_open_trades_per_symbol"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_check_max_open_trades_mutation(self, check_max_open_trades_per_symbol):
|
|
|
|
|
|
|
|
for x in range(9):
|
|
|
|
|
|
|
|
self.add_trade(
|
|
|
|
|
|
|
|
str(x),
|
|
|
|
|
|
|
|
f"EUR_USD{x}", # Vary symbol to prevent max open trades per symbol
|
|
|
|
|
|
|
|
"long",
|
|
|
|
|
|
|
|
f"2023-02-13T12:39:1{x}.302917985Z",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(check_max_open_trades_per_symbol.call_count, 1)
|
|
|
|
|
|
|
|
call_args = check_max_open_trades_per_symbol.call_args[0][0]
|
|
|
|
|
|
|
|
called_with_ids = [x["id"] for x in call_args]
|
|
|
|
|
|
|
|
self.assertListEqual(
|
|
|
|
|
|
|
|
called_with_ids, ["20083", "20084", "0", "1", "2", "3", "4", "5", "6", "7"]
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_max_loss")
|
|
|
|
|
|
|
|
def test_check_max_open_trades_per_symbol_mutation(self, check_max_loss):
|
|
|
|
|
|
|
|
for x in range(4):
|
|
|
|
|
|
|
|
self.add_trade(
|
|
|
|
|
|
|
|
str(x),
|
|
|
|
|
|
|
|
"EUR_USD",
|
|
|
|
|
|
|
|
"long",
|
|
|
|
|
|
|
|
f"2023-02-13T12:39:1{x}.302917985Z",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(check_max_loss.call_count, 1)
|
|
|
|
|
|
|
|
call_args = check_max_loss.call_args[0][0]
|
|
|
|
|
|
|
|
called_with_ids = [x["id"] for x in call_args]
|
|
|
|
|
|
|
|
self.assertListEqual(called_with_ids, ["20083", "20084", "0", "1", "2"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("core.trading.active_management.ActiveManagement.check_max_risk")
|
|
|
|
|
|
|
|
def test_check_max_loss_mutation(self, check_max_risk):
|
|
|
|
|
|
|
|
self.balance = D("1")
|
|
|
|
|
|
|
|
self.balance_usd = D("0.69")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.trades = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(check_max_risk.call_count, 1)
|
|
|
|
|
|
|
|
call_args = check_max_risk.call_args[0][0]
|
|
|
|
|
|
|
|
called_with_ids = [x["id"] for x in call_args]
|
|
|
|
|
|
|
|
self.assertListEqual(called_with_ids, [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_max_risk_mutation(self):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
This cannot be tested as there are no hooks after it.
|
|
|
|
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|