Begin work on scheduling management command
parent
ffdbcecc8d
commit
da67177a18
@ -0,0 +1,47 @@
|
||||
import asyncio
|
||||
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
from asgiref.sync import sync_to_async
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from core.models import Strategy
|
||||
from core.util import logs
|
||||
from core.trading import active_management
|
||||
|
||||
log = logs.get_logger("scheduling")
|
||||
|
||||
INTERVAL = 5
|
||||
|
||||
|
||||
async def job():
|
||||
"""
|
||||
Run all schedules matching the given interval.
|
||||
:param interval_seconds: The interval to run.
|
||||
"""
|
||||
strategies = await sync_to_async(list)(
|
||||
Strategy.objects.filter(enabled=True, active_management_enabled=True)
|
||||
)
|
||||
log.debug(f"Found {len(strategies)} strategies")
|
||||
for strategy in strategies:
|
||||
log.debug(f"Running strategy {strategy.name}")
|
||||
ams = active_management.ActiveManagement(strategy)
|
||||
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
"""
|
||||
Start the scheduling process.
|
||||
"""
|
||||
scheduler = AsyncIOScheduler()
|
||||
|
||||
log.debug(f"Scheduling checking process job every {INTERVAL} seconds")
|
||||
scheduler.add_job(job, "interval", seconds=INTERVAL)
|
||||
scheduler.start()
|
||||
loop = asyncio.get_event_loop()
|
||||
try:
|
||||
loop.run_forever()
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
log.info("Process terminating")
|
||||
finally:
|
||||
loop.close()
|
@ -0,0 +1,26 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from core.trading import checks
|
||||
|
||||
from core.models import TradingTime, Strategy, OrderSettings, User
|
||||
|
||||
class ChecksTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.user = User.objects.create_user(
|
||||
username="testuser", email="test@example.com", password="test"
|
||||
)
|
||||
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="08:00",
|
||||
end_day=1, # Monday
|
||||
end_time="16:00",
|
||||
)
|
||||
|
||||
|
||||
self.strategy = Strategy.objects.create(user=self.user, name="Test Strategy", )
|
||||
|
||||
def test_within_trading_times(self):
|
||||
pass
|
@ -0,0 +1,3 @@
|
||||
class ActiveManagement(object):
|
||||
def __init__(self, strategy):
|
||||
self.strategy = strategy
|
@ -0,0 +1,19 @@
|
||||
from datetime import datetime
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger("checks")
|
||||
|
||||
|
||||
def within_trading_times(strategy, ts=None):
|
||||
if not ts:
|
||||
ts = datetime.utcnow()
|
||||
# Check if we can trade now!
|
||||
trading_times = strategy.trading_times.all()
|
||||
if not trading_times:
|
||||
log.error("No trading times set for strategy")
|
||||
return False
|
||||
matches = [x.within_range(ts) for x in trading_times]
|
||||
if not any(matches):
|
||||
log.debug("Not within trading time range")
|
||||
return False
|
||||
return True
|
Loading…
Reference in New Issue