Simplify active management by only specifying trade IDs for violations

This commit is contained in:
2023-02-18 14:36:58 +00:00
parent 466b17400f
commit 15a8bec105
2 changed files with 51 additions and 26 deletions

View File

@@ -3,7 +3,6 @@ from unittest.mock import patch
from django.test import TestCase
from core.exchanges.convert import convert_trades
from core.lib.schemas.oanda_s import parse_time
from core.models import (
Account,
@@ -103,6 +102,9 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
self.ams.get_balance = self.fake_get_balance
# self.ams.trades = self.trades
def get_ids(self, trades):
return [trade["id"] for trade in trades]
def add_trade(self, id, symbol, side, open_time):
trade = {
"id": id,
@@ -179,17 +181,12 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
self.assertEqual(len(violation_calls), len(expected_trades))
if all(expected_trades):
expected_trades = convert_trades(expected_trades)
# expected_trades = convert_trades(expected_trades)
expected_trades = self.get_ids(expected_trades)
for call in violation_calls:
# Ensure the correct action has been called, like close
self.assertEqual(call[0][1], expected_action)
# Ensure the correct trade has been passed to the violation
trade = call[0][2]
if trade:
for field in list(trade.keys()):
if "_usd" in field:
if field in trade.keys():
del trade[field]
self.assertIn(call[0][2], expected_trades)
if expected_args:
self.assertEqual(call[0][3], expected_args)
@@ -454,22 +451,18 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
# trade.
# TODO: Fix this when we have a way of checking the balance at the start of the
# trade.
# Max risk is also mocked as this puts us over the limit, due to the low account
# size.
@patch(
"core.trading.active_management.ActiveManagement.check_max_risk",
return_value=None,
)
@patch(
"core.trading.active_management.ActiveManagement.check_position_size",
return_value=None,
)
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_max_loss_violated(
self, handle_violation, check_position_size, check_max_risk
):
def test_max_loss_violated(self, handle_violation, check_position_size):
self.balance = D("1")
self.balance_usd = D("0.69")
self.trades = []
self.ams.run_checks()
self.check_violation(
@@ -546,3 +539,15 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
"close",
[self.trades[2], self.trades[3]],
)
def test_max_risk_not_violated_after_adjusting_protection(self):
"""
Ensure the max risk check is not violated after adjusting the protection.
"""
pass
def test_max_risk_not_violated_after_adjusting_position_size(self):
"""
Ensure the max risk check is not violated after adjusting the position size.
"""
pass