Add more hooks to active management
This commit is contained in:
@@ -1,7 +1,17 @@
|
||||
from datetime import time
|
||||
from os import getenv
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from core.models import Account, User
|
||||
from core.models import (
|
||||
Account,
|
||||
Hook,
|
||||
OrderSettings,
|
||||
RiskModel,
|
||||
Signal,
|
||||
Strategy,
|
||||
TradingTime,
|
||||
User,
|
||||
)
|
||||
|
||||
|
||||
# Create patch mixin to mock out the Elastic client
|
||||
@@ -31,6 +41,21 @@ class ElasticMock:
|
||||
cls.patcher.stop()
|
||||
|
||||
|
||||
class SymbolPriceMock:
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(SymbolPriceMock, cls).setUpClass()
|
||||
cls.patcher = patch("core.exchanges.common.get_symbol_price")
|
||||
patcher = cls.patcher.start()
|
||||
|
||||
patcher.return_value = 1
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
super(SymbolPriceMock, cls).tearDownClass()
|
||||
cls.patcher.stop()
|
||||
|
||||
|
||||
class LiveBase:
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
@@ -92,3 +117,40 @@ If you have done this, please see the following line for more information:
|
||||
def setUp(self):
|
||||
if self.fail:
|
||||
self.skipTest("Live tests aborted")
|
||||
|
||||
|
||||
class StrategyMixin:
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.time_8 = time(8, 0, 0)
|
||||
self.time_16 = time(16, 0, 0)
|
||||
self.order_settings = OrderSettings.objects.create(
|
||||
user=self.user, name="Default"
|
||||
)
|
||||
self.trading_time_now = TradingTime.objects.create(
|
||||
user=self.user,
|
||||
name="Test Trading Time",
|
||||
start_day=1, # Monday
|
||||
start_time=self.time_8,
|
||||
end_day=1, # Monday
|
||||
end_time=self.time_16,
|
||||
)
|
||||
self.risk_model = RiskModel.objects.create(
|
||||
user=self.user,
|
||||
name="Test Risk Model",
|
||||
max_loss_percent=50,
|
||||
max_risk_percent=10,
|
||||
max_open_trades=10,
|
||||
max_open_trades_per_symbol=5,
|
||||
)
|
||||
|
||||
self.strategy = Strategy.objects.create(
|
||||
user=self.user,
|
||||
name="Test Strategy",
|
||||
account=self.account,
|
||||
order_settings=self.order_settings,
|
||||
risk_model=self.risk_model,
|
||||
active_management_enabled=True,
|
||||
)
|
||||
self.strategy.trading_times.set([self.trading_time_now])
|
||||
self.strategy.save()
|
||||
|
||||
211
core/tests/trading/test_active_management.py
Normal file
211
core/tests/trading/test_active_management.py
Normal file
@@ -0,0 +1,211 @@
|
||||
from django.test import TestCase
|
||||
|
||||
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):
|
||||
self.user = User.objects.create_user(
|
||||
username="testuser", email="test@example.com", password="test"
|
||||
)
|
||||
self.account = Account.objects.create(
|
||||
user=self.user,
|
||||
name="Test Account",
|
||||
exchange="fake",
|
||||
currency="USD",
|
||||
)
|
||||
self.account.supported_symbols = ["EUR_USD", "EUR_XXX", "USD_EUR", "XXX_EUR"]
|
||||
self.account.save()
|
||||
super().setUp()
|
||||
self.active_management_policy = ActiveManagementPolicy.objects.create(
|
||||
user=self.user,
|
||||
name="Test Policy",
|
||||
when_trading_time_violated="close",
|
||||
when_trends_violated="close",
|
||||
when_position_size_violated="close",
|
||||
when_protection_violated="close",
|
||||
when_asset_groups_violated="close",
|
||||
when_max_open_trades_violated="close",
|
||||
when_max_open_trades_per_symbol_violated="close",
|
||||
when_max_loss_violated="close",
|
||||
when_max_risk_violated="close",
|
||||
when_crossfilter_violated="close",
|
||||
)
|
||||
|
||||
self.strategy.active_management_policy = self.active_management_policy
|
||||
self.strategy.save()
|
||||
self.ams = ActiveManagement(self.strategy)
|
||||
self.trades = [
|
||||
{
|
||||
"id": "20083",
|
||||
"symbol": "EUR_USD",
|
||||
"price": "1.06331",
|
||||
"openTime": "2023-02-13T11:38:06.302917985Z", # Monday at 11:38
|
||||
"initialUnits": "10",
|
||||
"initialMarginRequired": "0.2966",
|
||||
"state": "OPEN",
|
||||
"currentUnits": "10",
|
||||
"realizedPL": "0.0000",
|
||||
"financing": "0.0000",
|
||||
"dividendAdjustment": "0.0000",
|
||||
"unrealizedPL": "-0.0008",
|
||||
"marginUsed": "0.2966",
|
||||
"takeProfitOrder": None,
|
||||
"stopLossOrder": None,
|
||||
"trailingStopLossOrder": None,
|
||||
"trailingStopValue": None,
|
||||
"side": "long",
|
||||
},
|
||||
{
|
||||
"id": "20083",
|
||||
"symbol": "EUR_USD",
|
||||
"price": "1.06331",
|
||||
"openTime": "2023-02-13T11:38:06.302917985Z", # Monday at 11:38
|
||||
"initialUnits": "10",
|
||||
"initialMarginRequired": "0.2966",
|
||||
"state": "OPEN",
|
||||
"currentUnits": "10",
|
||||
"realizedPL": "0.0000",
|
||||
"financing": "0.0000",
|
||||
"dividendAdjustment": "0.0000",
|
||||
"unrealizedPL": "-0.0008",
|
||||
"marginUsed": "0.2966",
|
||||
"takeProfitOrder": None,
|
||||
"stopLossOrder": None,
|
||||
"trailingStopLossOrder": None,
|
||||
"trailingStopValue": None,
|
||||
"side": "long",
|
||||
}
|
||||
]
|
||||
# Run parse_time on all items in trades
|
||||
for trade in self.trades:
|
||||
trade["openTime"] = parse_time(trade)
|
||||
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
|
||||
|
||||
def fake_get_balance(self):
|
||||
return 10000
|
||||
|
||||
def fake_get_currencies(self, symbols):
|
||||
pass
|
||||
|
||||
def test_get_trades(self):
|
||||
trades = self.ams.get_trades()
|
||||
self.assertEqual(trades, self.trades)
|
||||
|
||||
def test_get_balance(self):
|
||||
balance = self.ams.get_balance()
|
||||
self.assertEqual(balance, 10000)
|
||||
|
||||
def check_violation(self, violation, calls, expected_action, expected_trades):
|
||||
"""
|
||||
Check that the violation was called with the expected action and trades.
|
||||
Matches the first argument of the call to the violation name.
|
||||
:param: violation: type of the violation to check against
|
||||
: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
|
||||
"""
|
||||
calls = list(calls)
|
||||
violation_calls = []
|
||||
for call in calls:
|
||||
if call[0][0] == violation:
|
||||
violation_calls.append(call)
|
||||
|
||||
self.assertEqual(len(violation_calls), len(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)
|
||||
|
||||
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
||||
def test_run_checks(self, handle_violation):
|
||||
self.ams.run_checks()
|
||||
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.ams.run_checks()
|
||||
self.check_violation("trading_time", handle_violation.call_args_list, "close", [self.trades[0]])
|
||||
|
||||
def create_hook_signal(self):
|
||||
hook = Hook.objects.create(
|
||||
user=self.user,
|
||||
name="Test Hook",
|
||||
)
|
||||
signal = Signal.objects.create(
|
||||
user=self.user,
|
||||
name="Test Signal",
|
||||
hook=hook,
|
||||
type="trend",
|
||||
)
|
||||
return signal
|
||||
|
||||
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
||||
def test_trends_violated(self, handle_violation):
|
||||
signal = self.create_hook_signal()
|
||||
self.strategy.trend_signals.set([signal])
|
||||
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)
|
||||
|
||||
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
||||
def test_trends_violated_none(self, handle_violation):
|
||||
signal = self.create_hook_signal()
|
||||
self.strategy.trend_signals.set([signal])
|
||||
self.strategy.trends = {"EUR_USD": "buy"}
|
||||
self.strategy.save()
|
||||
self.ams.run_checks()
|
||||
self.check_violation("trends", handle_violation.call_args_list, "close", [])
|
||||
|
||||
@patch("core.trading.active_management.ActiveManagement.handle_violation")
|
||||
def test_trends_violated_partial(self, handle_violation):
|
||||
signal = self.create_hook_signal()
|
||||
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]])
|
||||
|
||||
@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]])
|
||||
|
||||
def test_protection_violated(self):
|
||||
pass
|
||||
|
||||
def test_asset_groups_violated(self):
|
||||
pass
|
||||
|
||||
def test_max_open_trades_violated(self):
|
||||
pass
|
||||
|
||||
def test_max_open_trades_per_symbol_violated(self):
|
||||
pass
|
||||
|
||||
def test_max_loss_violated(self):
|
||||
pass
|
||||
|
||||
def test_max_risk_violated(self):
|
||||
pass
|
||||
|
||||
def test_crossfilter_violated(self):
|
||||
pass
|
||||
@@ -13,13 +13,12 @@ from core.models import (
|
||||
TradingTime,
|
||||
User,
|
||||
)
|
||||
from core.tests.helpers import StrategyMixin
|
||||
from core.trading import checks
|
||||
|
||||
|
||||
class ChecksTestCase(TestCase):
|
||||
class ChecksTestCase(StrategyMixin, TestCase):
|
||||
def setUp(self):
|
||||
self.time_8 = time(8, 0, 0)
|
||||
self.time_16 = time(16, 0, 0)
|
||||
self.user = User.objects.create_user(
|
||||
username="testuser", email="test@example.com", password="test"
|
||||
)
|
||||
@@ -28,36 +27,7 @@ class ChecksTestCase(TestCase):
|
||||
name="Test Account",
|
||||
exchange="fake",
|
||||
)
|
||||
self.order_settings = OrderSettings.objects.create(
|
||||
user=self.user, name="Default"
|
||||
)
|
||||
self.trading_time_now = TradingTime.objects.create(
|
||||
user=self.user,
|
||||
name="Test Trading Time",
|
||||
start_day=1, # Monday
|
||||
start_time=self.time_8,
|
||||
end_day=1, # Monday
|
||||
end_time=self.time_16,
|
||||
)
|
||||
self.risk_model = RiskModel.objects.create(
|
||||
user=self.user,
|
||||
name="Test Risk Model",
|
||||
max_loss_percent=50,
|
||||
max_risk_percent=10,
|
||||
max_open_trades=10,
|
||||
max_open_trades_per_symbol=5,
|
||||
)
|
||||
|
||||
self.strategy = Strategy.objects.create(
|
||||
user=self.user,
|
||||
name="Test Strategy",
|
||||
account=self.account,
|
||||
order_settings=self.order_settings,
|
||||
risk_model=self.risk_model,
|
||||
active_management_enabled=True,
|
||||
)
|
||||
self.strategy.trading_times.set([self.trading_time_now])
|
||||
self.strategy.save()
|
||||
super().setUp()
|
||||
|
||||
@freezegun.freeze_time("2023-02-13T09:00:00") # Monday at 09:00
|
||||
def test_within_trading_times_pass(self):
|
||||
|
||||
Reference in New Issue
Block a user