2023-02-17 07:20:19 +00:00
|
|
|
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.trading import active_management
|
2023-02-17 07:20:28 +00:00
|
|
|
from core.util import logs
|
2023-02-17 07:20:19 +00:00
|
|
|
|
|
|
|
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}")
|
2023-02-17 07:20:28 +00:00
|
|
|
ams = active_management.ActiveManagement(strategy) # noqa
|
2023-02-17 07:20:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|