From ba8eb693093392f3491dbc332af0a570c88d397e Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 20 Feb 2023 23:57:20 +0000 Subject: [PATCH] Begin protection checks --- core/tests/trading/test_live.py | 54 ++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/core/tests/trading/test_live.py b/core/tests/trading/test_live.py index a6a8278..61e534f 100644 --- a/core/tests/trading/test_live.py +++ b/core/tests/trading/test_live.py @@ -175,7 +175,59 @@ class ActiveManagementMixinTestCase(StrategyMixin): self.close_trade(complex_trade) def test_ams_protection_violated(self): - pass + self.active_management_policy.when_protection_violated = "close" + self.active_management_policy.save() + # Don't violate position size check + trade_size = market.get_trade_size_in_base( + "buy", self.account, self.strategy, self.account.client.get_balance(), "EUR" + ) + trade_size = round(trade_size, 0) + print("TRADE SIZE", trade_size) + print("TYPE", type(trade_size)) + complex_trade = self.create_complex_trade("buy", trade_size, "EUR_USD", 5, 5) + self.open_trade(complex_trade) + + self.ams.run_checks() + self.assertEqual(len(self.ams.actions["close"]), 1) + expected = { + "close": [ + { + "id": complex_trade.id, + "check": "protection", + + } + ] + } + + del self.ams.checks["close"][0]["extra"] + + self.assertEqual(self.ams.checks, expected) + + def test_ams_protection_violated_adjust(self): + # Don't violate position size check + trade_size = market.get_trade_size_in_base( + "buy", self.account, self.strategy, self.account.client.get_balance(), "EUR" + ) + trade_size = round(trade_size, 0) + print("TRADE SIZE", trade_size) + print("TYPE", type(trade_size)) + complex_trade = self.create_complex_trade("buy", trade_size, "EUR_USD", 5, 5) + self.open_trade(complex_trade) + + self.ams.run_checks() + expected = { + "adjust": [ + { + "id": "21381", + "check": "protection", + "extra": { + "stop_loss_price": D("1.05812"), + "take_profit_price": D("1.08484"), + }, + } + ] + } + print("CHECKS", self.ams.actions) def test_ams_asset_groups_violated(self): pass