|
|
|
@ -1,15 +1,204 @@
|
|
|
|
|
from datetime import time
|
|
|
|
|
from decimal import Decimal as D
|
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
|
from core.exchanges.convert import convert_trades
|
|
|
|
|
from core.models import RiskModel, Trade
|
|
|
|
|
from core.tests.helpers import ElasticMock, LiveBase
|
|
|
|
|
from core.models import (
|
|
|
|
|
ActiveManagementPolicy,
|
|
|
|
|
Hook,
|
|
|
|
|
RiskModel,
|
|
|
|
|
Signal,
|
|
|
|
|
Trade,
|
|
|
|
|
TradingTime,
|
|
|
|
|
)
|
|
|
|
|
from core.tests.helpers import ElasticMock, LiveBase, StrategyMixin
|
|
|
|
|
from core.trading import market, risk
|
|
|
|
|
from core.trading.active_management import ActiveManagement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LiveTradingTestCase(ElasticMock, LiveBase, TestCase):
|
|
|
|
|
class ActiveManagementMixinTestCase(StrategyMixin):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
super().setUp()
|
|
|
|
|
self.trading_time_all = TradingTime.objects.create(
|
|
|
|
|
user=self.user,
|
|
|
|
|
name="All",
|
|
|
|
|
start_day=1,
|
|
|
|
|
start_time=time(0, 0, 0),
|
|
|
|
|
end_day=7,
|
|
|
|
|
end_time=time(23, 59, 59),
|
|
|
|
|
)
|
|
|
|
|
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="adjust",
|
|
|
|
|
when_protection_violated="adjust",
|
|
|
|
|
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.trading_times.set([self.trading_time_all])
|
|
|
|
|
self.strategy.save()
|
|
|
|
|
self.ams = ActiveManagement(self.strategy)
|
|
|
|
|
|
|
|
|
|
def test_ams_success(self):
|
|
|
|
|
complex_trade = self.create_complex_trade("buy", 10, "EUR_USD", 1.5, 1.0)
|
|
|
|
|
self.open_trade(complex_trade)
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
self.assertEqual(self.ams.actions, {})
|
|
|
|
|
|
|
|
|
|
self.close_trade(complex_trade)
|
|
|
|
|
|
|
|
|
|
def test_ams_trading_time_violated(self):
|
|
|
|
|
# Forex market is closed on Saturday and Sunday.
|
|
|
|
|
# All of these tests will fail then anyway, so it's the only time
|
|
|
|
|
# we can use for this.
|
|
|
|
|
trading_time_weekend = TradingTime.objects.create(
|
|
|
|
|
user=self.user,
|
|
|
|
|
name="Weekend",
|
|
|
|
|
start_day=6,
|
|
|
|
|
start_time=time(0, 0, 0),
|
|
|
|
|
end_day=7,
|
|
|
|
|
end_time=time(23, 59, 59),
|
|
|
|
|
)
|
|
|
|
|
self.strategy.trading_times.set([trading_time_weekend])
|
|
|
|
|
self.strategy.save()
|
|
|
|
|
|
|
|
|
|
complex_trade = self.create_complex_trade("buy", 10, "EUR_USD", 1.5, 1.0)
|
|
|
|
|
self.open_trade(complex_trade)
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
expected_actions = {
|
|
|
|
|
"close": [
|
|
|
|
|
{"id": complex_trade.order_id, "check": "trading_time", "extra": {}}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
self.assertEqual(self.ams.actions, expected_actions)
|
|
|
|
|
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
|
self.assertEqual(len(trades), 1)
|
|
|
|
|
|
|
|
|
|
self.ams.execute_actions()
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
|
self.assertEqual(len(trades), 0)
|
|
|
|
|
|
|
|
|
|
# Don't need to close the trade, it's already closed.
|
|
|
|
|
# Otherwise the test would fail.
|
|
|
|
|
|
|
|
|
|
def test_ams_trends_violated(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",
|
|
|
|
|
)
|
|
|
|
|
self.strategy.trend_signals.set([signal])
|
|
|
|
|
self.strategy.trends = {"EUR_USD": "sell"}
|
|
|
|
|
self.strategy.save()
|
|
|
|
|
|
|
|
|
|
complex_trade = self.create_complex_trade("buy", 10, "EUR_USD", 1.5, 1.0)
|
|
|
|
|
self.open_trade(complex_trade)
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
expected_actions = {
|
|
|
|
|
"close": [{"id": complex_trade.order_id, "check": "trends", "extra": {}}]
|
|
|
|
|
}
|
|
|
|
|
self.assertEqual(self.ams.actions, expected_actions)
|
|
|
|
|
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
|
self.assertEqual(len(trades), 1)
|
|
|
|
|
|
|
|
|
|
self.ams.execute_actions()
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
|
self.assertEqual(len(trades), 0)
|
|
|
|
|
|
|
|
|
|
# Don't need to close the trade, it's already closed.
|
|
|
|
|
# Otherwise the test would fail.
|
|
|
|
|
|
|
|
|
|
def test_ams_position_size_violated(self):
|
|
|
|
|
self.active_management_policy.when_position_size_violated = "close"
|
|
|
|
|
self.active_management_policy.save()
|
|
|
|
|
|
|
|
|
|
complex_trade = self.create_complex_trade("buy", 600, "EUR_USD", 1.5, 1.0)
|
|
|
|
|
self.open_trade(complex_trade)
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
expected_actions = {
|
|
|
|
|
"close": [
|
|
|
|
|
{"id": complex_trade.order_id, "check": "position_size", "extra": {}}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
self.assertEqual(len(self.ams.actions["close"]), 1)
|
|
|
|
|
del self.ams.actions["close"][0]["extra"]["size"] # We don't know the size
|
|
|
|
|
self.assertEqual(self.ams.actions, expected_actions)
|
|
|
|
|
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
|
self.assertEqual(len(trades), 1)
|
|
|
|
|
|
|
|
|
|
self.ams.execute_actions()
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
|
self.assertEqual(len(trades), 0)
|
|
|
|
|
|
|
|
|
|
def test_ams_position_size_violated_adjust(self):
|
|
|
|
|
complex_trade = self.create_complex_trade("buy", 600, "EUR_USD", 1.5, 1.0)
|
|
|
|
|
self.open_trade(complex_trade)
|
|
|
|
|
|
|
|
|
|
self.ams.run_checks()
|
|
|
|
|
# Convert to int to make a whole number
|
|
|
|
|
expected = round(self.ams.actions["adjust"][0]["extra"]["size"], 0)
|
|
|
|
|
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
|
self.assertEqual(len(trades), 1)
|
|
|
|
|
self.assertEqual(trades[0]["currentUnits"], "600")
|
|
|
|
|
|
|
|
|
|
self.ams.execute_actions()
|
|
|
|
|
trades = self.account.client.get_all_open_trades()
|
|
|
|
|
self.assertEqual(len(trades), 1)
|
|
|
|
|
self.assertEqual(trades[0]["currentUnits"], str(expected))
|
|
|
|
|
|
|
|
|
|
complex_trade.amount = expected
|
|
|
|
|
complex_trade.save()
|
|
|
|
|
|
|
|
|
|
self.close_trade(complex_trade)
|
|
|
|
|
|
|
|
|
|
def test_ams_protection_violated(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_ams_asset_groups_violated(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_ams_crossfilter_violated(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_ams_max_open_trades_violated(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_ams_max_open_trades_per_symbol_violated(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_ams_max_loss_violated(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_ams_max_risk_violated(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LiveTradingTestCase(
|
|
|
|
|
ElasticMock, ActiveManagementMixinTestCase, LiveBase, TestCase
|
|
|
|
|
):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
super(LiveTradingTestCase, self).setUp()
|
|
|
|
|
self.trade = Trade.objects.create(
|
|
|
|
|