fisk/core/tests/trading/test_checks.py

66 lines
2.2 KiB
Python
Raw Normal View History

import freezegun
from django.test import TestCase
from core.models import Account, Hook, Signal, User
2023-02-17 07:20:15 +00:00
from core.tests.helpers import StrategyMixin
from core.trading import checks
2023-02-17 07:20:15 +00:00
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",
)
2023-02-17 07:20:15 +00:00
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"))