You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.3 KiB
Python
107 lines
3.3 KiB
Python
from datetime import time
|
|
|
|
import freezegun
|
|
from django.test import TestCase
|
|
|
|
from core.models import (
|
|
Account,
|
|
Hook,
|
|
OrderSettings,
|
|
RiskModel,
|
|
Signal,
|
|
Strategy,
|
|
TradingTime,
|
|
User,
|
|
)
|
|
from core.trading import checks
|
|
|
|
|
|
class ChecksTestCase(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"
|
|
)
|
|
self.account = Account.objects.create(
|
|
user=self.user,
|
|
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()
|
|
|
|
@freezegun.freeze_time("2023-02-13T09:00:00") # Monday at 09:00
|
|
def test_within_trading_times_pass(self):
|
|
self.assertTrue(checks.within_trading_times(self.strategy))
|
|
|
|
@freezegun.freeze_time("2023-02-13T17:00:00") # Monday at 17:00
|
|
def test_within_trading_times_fail(self):
|
|
self.assertFalse(checks.within_trading_times(self.strategy))
|
|
|
|
def test_within_callback_price_deviation_fail(self):
|
|
price_callback = 100
|
|
current_price = 200
|
|
self.assertFalse(
|
|
checks.within_callback_price_deviation(
|
|
self.strategy, price_callback, current_price
|
|
)
|
|
)
|
|
|
|
def test_within_callback_price_deviation_pass(self):
|
|
price_callback = 100
|
|
current_price = 100.5
|
|
self.assertTrue(
|
|
checks.within_callback_price_deviation(
|
|
self.strategy, price_callback, current_price
|
|
)
|
|
)
|
|
|
|
def test_within_trends(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": "buy"}
|
|
self.strategy.save()
|
|
self.assertTrue(checks.within_trends(self.strategy, "EUR_USD", "buy"))
|
|
self.assertFalse(checks.within_trends(self.strategy, "EUR_USD", "sell"))
|
|
|
|
self.assertIsNone(checks.within_trends(self.strategy, "EUR_XXX", "buy"))
|
|
self.assertIsNone(checks.within_trends(self.strategy, "EUR_XXX", "sell"))
|