Write crossfilter, asset groups and max open trades implementation and tests

This commit is contained in:
2023-02-17 22:11:46 +00:00
parent 67117f0978
commit d262f208b5
7 changed files with 587 additions and 82 deletions

View File

@@ -16,69 +16,104 @@ class CommonTestCase(TestCase):
"""
Test that the TP price to percent conversion works for long trades.
"""
tp_price = 1.1 # 10%
tp_price = D("1.1") # 10%
current_price = 1.0
current_units = 1
unrealised_pl = 0
expected_percent = 10
percent = tp_price_to_percent(
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_initial_short(self):
"""
Test that the TP price to percent conversion works for short trades.
"""
tp_price = 0.9 # 10%
tp_price = D("0.9") # 10%
current_price = 1.0
current_units = 1
unrealised_pl = 0
expected_percent = 10
percent = tp_price_to_percent(
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_change_long(self):
"""
Test that the TP price to percent conversion works for long trades
when the price has changed.
"""
tp_price = 1.2 # 20%
tp_price = D("1.2") # 20%
current_price = 1.1 # + 10%
current_units = 1
unrealised_pl = 0.1 # 10%
expected_percent = 20
percent = tp_price_to_percent(
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_change_long_loss(self):
"""
Test that the TP price to percent conversion works for long trades
when the price has changed and the TP is at a loss.
"""
tp_price = 0.8 # -20%
tp_price = D("0.8") # -20%
current_price = 0.9 # - 10%
current_units = 1
unrealised_pl = -0.1 # -10%
expected_percent = -20
percent = tp_price_to_percent(
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, -20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_change_short_loss(self):
"""
Test that the TP price to percent conversion works for short trades
when the price has changed and the TP is at a loss.
"""
tp_price = 1.2 # -20%
tp_price = D("1.2") # -20%
current_price = 1.1 # - 10%
current_units = 1
unrealised_pl = -0.1 # -10%
expected_percent = -20
percent = tp_price_to_percent(
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, -20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
tp_price,
)
# For multiple units
def test_tp_price_to_percent_initial_long_multi(self):
@@ -86,139 +121,209 @@ class CommonTestCase(TestCase):
Test that the TP price to percent conversion works for long trades
with multiple units.
"""
tp_price = 1.1 # 10%
tp_price = D("1.1") # 10%
current_price = 1.0
current_units = 10
unrealised_pl = 0
expected_percent = 10
percent = tp_price_to_percent(
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_initial_short_multi(self):
"""
Test that the TP price to percent conversion works for short trades
with multiple units.
"""
tp_price = 0.9 # 10%
tp_price = D("0.9") # 10%
current_price = 1.0
current_units = 10
unrealised_pl = 0
expected_percent = 10
percent = tp_price_to_percent(
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_change_long_multi(self):
"""
Test that the TP price to percent conversion works for long trades
when the price has changed, with multiple units.
"""
tp_price = 1.2 # +20%
tp_price = D("1.2") # +20%
current_price = 1.1 # +10%
current_units = 10
unrealised_pl = 1 # 10%
expected_percent = 20
percent = tp_price_to_percent(
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_change_short_multi(self):
"""
Test that the TP price to percent conversion works for short trades
when the price has changed, with multiple units.
"""
tp_price = 0.8 # -20%
tp_price = D("0.8") # -20%
current_price = 0.9 # -10%
current_units = 10
unrealised_pl = 1 # 10%
expected_percent = 20
percent = tp_price_to_percent(
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_change_long_multi_loss(self):
"""
Test that the TP price to percent conversion works for long trades
when the price has changed, with multiple units, and the TP is at a loss.
"""
tp_price = 0.8 # -20%
tp_price = D("0.8") # -20%
current_price = 0.9 # -10%
current_units = 10
unrealised_pl = -1 # -10%
expected_percent = -20
percent = tp_price_to_percent(
tp_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, -20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
tp_price,
)
def test_tp_price_to_percent_change_short_multi_loss(self):
"""
Test that the TP price to percent conversion works for short trades
when the price has changed, with multiple units, and the TP is at a loss.
"""
tp_price = 1.2 # -20%
tp_price = D("1.2") # -20%
current_price = 1.1 # -10%
current_units = 10
unrealised_pl = -1 # 10%
expected_percent = -20
percent = tp_price_to_percent(
tp_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, -20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
tp_price,
)
# SL
def test_sl_price_to_percent_initial_long(self):
"""
Test that the SL price to percent conversion works for long trades.
"""
sl_price = 0.9 # 10%
sl_price = D("0.9") # 10%
current_price = 1.0
current_units = 1
unrealised_pl = 0
expected_percent = 10
percent = sl_price_to_percent(
sl_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
self.assertEqual(percent, expected_percent)
self.assertEqual(
sl_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
sl_price,
)
def test_sl_price_to_percent_initial_short(self):
"""
Test that the SL price to percent conversion works for short trades.
"""
sl_price = 1.1 # 10%
sl_price = D("1.1") # 10%
current_price = 1.0
current_units = 1
unrealised_pl = 0
expected_percent = 10
percent = sl_price_to_percent(
sl_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
self.assertEqual(percent, expected_percent)
self.assertEqual(
sl_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
sl_price,
)
def test_sl_price_to_percent_change_long_profit(self):
"""
Test that the SL price to percent conversion works for long trades
when the price has changed and the SL is at a profit.
"""
sl_price = 1.2 # +20%
sl_price = D("1.2") # +20%
current_price = 1.1 # +10%
current_units = 1
unrealised_pl = 0.1 # +10%
expected_percent = -20
percent = sl_price_to_percent(
sl_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, -20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
sl_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
sl_price,
)
def test_sl_price_to_percent_change_short_profit(self):
"""
Test that the SL price to percent conversion works for short trades
when the price has changed and the SL is at a profit.
"""
sl_price = 0.8 # +20%
sl_price = D("0.8") # +20%
current_price = 0.9 # +10%
current_units = 1
unrealised_pl = 0.1 # +10%
expected_percent = -20
percent = sl_price_to_percent(
sl_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, -20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
sl_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
sl_price,
)
# For multiple units
def test_sl_price_to_percent_initial_long_multi(self):
@@ -226,35 +331,49 @@ class CommonTestCase(TestCase):
Test that the SL price to percent conversion works for long trades
with multiple units.
"""
sl_price = 0.9 # -10%
sl_price = D("0.9") # -10%
current_price = 1.0
current_units = 10
unrealised_pl = 0
expected_percent = 10
percent = sl_price_to_percent(
sl_price, "long", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 10)
self.assertEqual(percent, expected_percent)
self.assertEqual(
sl_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
sl_price,
)
def test_sl_price_to_percent_initial_short_multi(self):
"""
Test that the SL price to percent conversion works for short trades
with multiple units.
"""
sl_price = 1.2 # -20%
sl_price = D("1.2") # -20%
current_price = 1.0
current_units = 10
unrealised_pl = 0
expected_percent = 20
percent = sl_price_to_percent(
sl_price, "short", current_price, current_units, unrealised_pl
)
self.assertEqual(percent, 20)
self.assertEqual(percent, expected_percent)
self.assertEqual(
sl_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
sl_price,
)
def test_sl_price_to_percent_change_long_multi_profit(self):
"""
Test that the SL price to percent conversion works for long trades
when the price has changed, with multiple units, and the SL is at a profit.
"""
sl_price = D(1.2) # +20%
sl_price = D("1.2") # +20%
current_price = 1.1 # +10%
current_units = 10
unrealised_pl = 1 # +10%
@@ -264,7 +383,7 @@ class CommonTestCase(TestCase):
)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
sl_percent_to_price(
expected_percent, "long", current_price, current_units, unrealised_pl
),
sl_price,
@@ -275,7 +394,7 @@ class CommonTestCase(TestCase):
Test that the SL price to percent conversion works for short trades
when the price has changed, with multiple units, and the SL is at a profit.
"""
sl_price = D(0.8) # -20%
sl_price = D("0.8") # -20%
current_price = 0.9 # +10%
current_units = 10
unrealised_pl = 1 # +10%
@@ -285,7 +404,7 @@ class CommonTestCase(TestCase):
)
self.assertEqual(percent, expected_percent)
self.assertEqual(
tp_percent_to_price(
sl_percent_to_price(
expected_percent, "short", current_price, current_units, unrealised_pl
),
sl_price,

View File

@@ -1,10 +1,19 @@
from decimal import Decimal as D
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.models import (
Account,
ActiveManagementPolicy,
AssetGroup,
AssetRule,
Hook,
Signal,
User,
)
from core.tests.helpers import StrategyMixin, SymbolPriceMock
from core.trading.active_management import ActiveManagement
@@ -56,17 +65,17 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
"dividendAdjustment": "0.0000",
"unrealizedPL": "-0.0008",
"marginUsed": "0.2966",
"takeProfitOrder": {"price": "1.06331"},
"stopLossOrder": {"price": "1.06331"},
"trailingStopLossOrder": {"price": "1.06331"},
"takeProfitOrder": {"price": "1.07934"},
"stopLossOrder": {"price": "1.05276"},
"trailingStopLossOrder": None,
"trailingStopValue": None,
"side": "long",
},
{
"id": "20083",
"id": "20084",
"symbol": "EUR_USD",
"price": "1.06331",
"openTime": "2023-02-13T11:38:06.302917985Z", # Monday at 11:38
"openTime": "2023-02-13T11:39:06.302917985Z", # Monday at 11:38
"initialUnits": "10",
"initialMarginRequired": "0.2966",
"state": "OPEN",
@@ -76,9 +85,9 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
"dividendAdjustment": "0.0000",
"unrealizedPL": "-0.0008",
"marginUsed": "0.2966",
"takeProfitOrder": {"price": "1.06331"},
"stopLossOrder": {"price": "1.06331"},
"trailingStopLossOrder": {"price": "1.06331"},
"takeProfitOrder": {"price": "1.07934"},
"stopLossOrder": {"price": "1.05276"},
"trailingStopLossOrder": None,
"trailingStopValue": None,
"side": "long",
},
@@ -90,6 +99,30 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
self.ams.get_balance = self.fake_get_balance
# self.ams.trades = self.trades
def add_trade(self, id, symbol, side, open_time):
trade = {
"id": id,
"symbol": symbol,
"price": "1.06331",
"openTime": open_time,
"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": {"price": "1.07934"},
"stopLossOrder": {"price": "1.05276"},
"trailingStopLossOrder": None,
"trailingStopValue": None,
"side": side,
}
trade["openTime"] = parse_time(trade)
self.trades.append(trade)
def fake_get_trades(self):
self.ams.trades = self.trades
return self.trades
@@ -211,14 +244,154 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
{"size": 50},
)
def test_protection_violated(self):
pass
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_protection_violated_absent(self, handle_violation):
self.trades[0]["takeProfitOrder"] = None
self.trades[0]["stopLossOrder"] = None
self.ams.run_checks()
def test_asset_groups_violated(self):
pass
expected_args = {
"take_profit_price": D("1.07934"),
"stop_loss_price": D("1.05276"),
}
self.check_violation(
"protection",
handle_violation.call_args_list,
"close",
[self.trades[0]],
expected_args,
)
def test_max_open_trades_violated(self):
pass
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_protection_violated_absent_not_required(self, handle_violation):
self.strategy.order_settings.take_profit_percent = 0
self.strategy.order_settings.stop_loss_percent = 0
self.strategy.order_settings.save()
self.trades[0]["takeProfitOrder"] = None
self.trades[0]["stopLossOrder"] = None
self.ams.run_checks()
print("CALLS", handle_violation.call_args_list)
self.assertEqual(handle_violation.call_count, 0)
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_asset_groups_violated(self, handle_violation):
asset_group = AssetGroup.objects.create(
user=self.user,
name="Test Asset Group",
)
AssetRule.objects.create(
user=self.user,
asset="USD",
group=asset_group,
status=2, # Bullish
)
self.strategy.asset_group = asset_group
self.strategy.save()
self.ams.run_checks()
self.check_violation(
"asset_group",
handle_violation.call_args_list,
"close",
self.trades, # All trades should be closed, since all are USD quote
)
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_asset_groups_violated_invert(self, handle_violation):
self.trades[0]["side"] = "short"
self.trades[1]["side"] = "short"
asset_group = AssetGroup.objects.create(
user=self.user,
name="Test Asset Group",
)
AssetRule.objects.create(
user=self.user,
asset="USD",
group=asset_group,
status=3, # Bullish
)
self.strategy.asset_group = asset_group
self.strategy.save()
self.ams.run_checks()
self.check_violation(
"asset_group",
handle_violation.call_args_list,
"close",
self.trades, # All trades should be closed, since all are USD quote
)
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_crossfilter_violated_side(self, handle_violation):
self.trades[1]["side"] = "short"
self.ams.run_checks()
self.check_violation(
"crossfilter",
handle_violation.call_args_list,
"close",
[self.trades[1]], # Only close newer trade
)
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_crossfilter_violated_side_multiple(self, handle_violation):
self.add_trade("20085", "EUR_USD", "short", "2023-02-13T12:39:06.302917985Z")
self.add_trade("20086", "EUR_USD", "short", "2023-02-14T12:39:06.302917985Z")
self.add_trade("20087", "EUR_USD", "short", "2023-02-10T12:39:06.302917985Z")
self.ams.run_checks()
self.check_violation(
"crossfilter",
handle_violation.call_args_list,
"close",
self.trades[0:4], # Only close newer trades
)
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_crossfilter_violated_symbol(self, handle_violation):
# Change symbol to conflict with long on EUR_USD
self.trades[1]["symbol"] = "USD_EUR"
self.ams.run_checks()
self.check_violation(
"crossfilter",
handle_violation.call_args_list,
"close",
[self.trades[1]], # Only close newer trade
)
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_crossfilter_violated_symbol_multiple(self, handle_violation):
self.add_trade("20085", "USD_EUR", "long", "2023-02-13T12:39:06.302917985Z")
self.add_trade("20086", "USD_EUR", "long", "2023-02-14T12:39:06.302917985Z")
self.add_trade("20087", "USD_EUR", "long", "2023-02-10T12:39:06.302917985Z")
self.ams.run_checks()
self.check_violation(
"crossfilter",
handle_violation.call_args_list,
"close",
self.trades[0:4], # Only close newer trades
)
@patch("core.trading.active_management.ActiveManagement.handle_violation")
def test_max_open_trades_violated(self, handle_violation):
for x in range(9):
self.add_trade(
str(x),
"EUR_USD",
"long",
f"2023-02-13T12:39:1{x}.302917985Z",
)
self.ams.run_checks()
self.check_violation(
"max_open_trades",
handle_violation.call_args_list,
"close",
self.trades[10:], # Only close newer trades
)
def test_max_open_trades_per_symbol_violated(self):
pass
@@ -228,6 +401,3 @@ class ActiveManagementTestCase(StrategyMixin, SymbolPriceMock, TestCase):
def test_max_risk_violated(self):
pass
def test_crossfilter_violated(self):
pass