Write protection check tests
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
from unittest.mock import Mock, 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, ActiveManagementPolicy, Hook, Signal, User
|
||||
from core.tests.helpers import StrategyMixin, SymbolPriceMock
|
||||
from core.trading.active_management import ActiveManagement
|
||||
from core.models import User, Account, ActiveManagementPolicy, Hook, Signal
|
||||
from unittest.mock import Mock, patch
|
||||
from core.lib.schemas.oanda_s import parse_time
|
||||
|
||||
|
||||
class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
def setUp(self):
|
||||
@@ -43,7 +46,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
"id": "20083",
|
||||
"symbol": "EUR_USD",
|
||||
"price": "1.06331",
|
||||
"openTime": "2023-02-13T11:38:06.302917985Z", # Monday at 11:38
|
||||
"openTime": "2023-02-13T11:38:06.302917985Z", # Monday at 11:38
|
||||
"initialUnits": "10",
|
||||
"initialMarginRequired": "0.2966",
|
||||
"state": "OPEN",
|
||||
@@ -53,9 +56,9 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
"dividendAdjustment": "0.0000",
|
||||
"unrealizedPL": "-0.0008",
|
||||
"marginUsed": "0.2966",
|
||||
"takeProfitOrder": None,
|
||||
"stopLossOrder": None,
|
||||
"trailingStopLossOrder": None,
|
||||
"takeProfitOrder": {"price": "1.06331"},
|
||||
"stopLossOrder": {"price": "1.06331"},
|
||||
"trailingStopLossOrder": {"price": "1.06331"},
|
||||
"trailingStopValue": None,
|
||||
"side": "long",
|
||||
},
|
||||
@@ -63,7 +66,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
"id": "20083",
|
||||
"symbol": "EUR_USD",
|
||||
"price": "1.06331",
|
||||
"openTime": "2023-02-13T11:38:06.302917985Z", # Monday at 11:38
|
||||
"openTime": "2023-02-13T11:38:06.302917985Z", # Monday at 11:38
|
||||
"initialUnits": "10",
|
||||
"initialMarginRequired": "0.2966",
|
||||
"state": "OPEN",
|
||||
@@ -73,12 +76,12 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
"dividendAdjustment": "0.0000",
|
||||
"unrealizedPL": "-0.0008",
|
||||
"marginUsed": "0.2966",
|
||||
"takeProfitOrder": None,
|
||||
"stopLossOrder": None,
|
||||
"trailingStopLossOrder": None,
|
||||
"takeProfitOrder": {"price": "1.06331"},
|
||||
"stopLossOrder": {"price": "1.06331"},
|
||||
"trailingStopLossOrder": {"price": "1.06331"},
|
||||
"trailingStopValue": None,
|
||||
"side": "long",
|
||||
}
|
||||
},
|
||||
]
|
||||
# Run parse_time on all items in trades
|
||||
for trade in self.trades:
|
||||
@@ -86,7 +89,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
self.ams.get_trades = self.fake_get_trades
|
||||
self.ams.get_balance = self.fake_get_balance
|
||||
# self.ams.trades = self.trades
|
||||
|
||||
|
||||
def fake_get_trades(self):
|
||||
self.ams.trades = self.trades
|
||||
return self.trades
|
||||
@@ -105,7 +108,9 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
balance = self.ams.get_balance()
|
||||
self.assertEqual(balance, 10000)
|
||||
|
||||
def check_violation(self, violation, calls, expected_action, expected_trades):
|
||||
def check_violation(
|
||||
self, violation, calls, expected_action, expected_trades, expected_args=None
|
||||
):
|
||||
"""
|
||||
Check that the violation was called with the expected action and trades.
|
||||
Matches the first argument of the call to the violation name.
|
||||
@@ -113,6 +118,7 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
:param: calls: list of calls to the violation
|
||||
:param: expected_action: expected action to be called, close, notify, etc.
|
||||
:param: expected_trades: list of expected trades to be passed to the violation
|
||||
:param: expected_args: optional, expected args to be passed to the violation
|
||||
"""
|
||||
calls = list(calls)
|
||||
violation_calls = []
|
||||
@@ -121,22 +127,28 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
violation_calls.append(call)
|
||||
|
||||
self.assertEqual(len(violation_calls), len(expected_trades))
|
||||
expected_trades = convert_trades(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
|
||||
self.assertIn(call[0][2], expected_trades)
|
||||
if expected_args:
|
||||
self.assertEqual(call[0][3], expected_args)
|
||||
|
||||
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
||||
def test_run_checks(self, handle_violation):
|
||||
self.ams.run_checks()
|
||||
print("handle_violation.call_count", handle_violation.call_args_list)
|
||||
self.assertEqual(handle_violation.call_count, 0)
|
||||
|
||||
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
||||
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.check_violation("trading_time", handle_violation.call_args_list, "close", [self.trades[0]])
|
||||
self.check_violation(
|
||||
"trading_time", handle_violation.call_args_list, "close", [self.trades[0]]
|
||||
)
|
||||
|
||||
def create_hook_signal(self):
|
||||
hook = Hook.objects.create(
|
||||
@@ -158,7 +170,9 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
self.strategy.trends = {"EUR_USD": "sell"}
|
||||
self.strategy.save()
|
||||
self.ams.run_checks()
|
||||
self.check_violation("trends", handle_violation.call_args_list, "close", self.trades)
|
||||
self.check_violation(
|
||||
"trends", handle_violation.call_args_list, "close", self.trades
|
||||
)
|
||||
|
||||
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
||||
def test_trends_violated_none(self, handle_violation):
|
||||
@@ -175,19 +189,27 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
|
||||
self.strategy.trend_signals.set([signal])
|
||||
self.strategy.trends = {"EUR_USD": "sell"}
|
||||
self.strategy.save()
|
||||
|
||||
|
||||
# Change the side of the first trade to match the trends
|
||||
self.trades[0]["side"] = "short"
|
||||
self.ams.run_checks()
|
||||
|
||||
self.check_violation("trends", handle_violation.call_args_list, "close", [self.trades[1]])
|
||||
self.check_violation(
|
||||
"trends", handle_violation.call_args_list, "close", [self.trades[1]]
|
||||
)
|
||||
|
||||
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
||||
def test_position_size_violated(self, handle_violation):
|
||||
self.trades[0]["currentUnits"] = "100000"
|
||||
self.ams.run_checks()
|
||||
|
||||
self.check_violation("position_size", handle_violation.call_args_list, "close", [self.trades[0]])
|
||||
self.check_violation(
|
||||
"position_size",
|
||||
handle_violation.call_args_list,
|
||||
"close",
|
||||
[self.trades[0]],
|
||||
{"size": 50},
|
||||
)
|
||||
|
||||
def test_protection_violated(self):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user