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.tests.helpers import StrategyMixin from core.trading import checks class ChecksTestCase(StrategyMixin, 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", ) super().setUp() @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"))