Initial commit: Django quantitative strategy executor
- Django 5.2.7 project with Python 3.13+ - Quant strategy management system with version control - Strategy implementations using registry pattern - API endpoints for strategy listing and execution - Sample strategy implementations (MovingAverage, RSI, BollingerBand) - Async strategy execution with status tracking 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
81
strategies/models.py
Normal file
81
strategies/models.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from django.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
import json
|
||||
from .base import StrategyRegistry
|
||||
|
||||
|
||||
class QuantStrategy(models.Model):
|
||||
name = models.CharField(max_length=100, unique=True)
|
||||
description = models.TextField(blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
is_active = models.BooleanField(default=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = "Quant Strategies"
|
||||
|
||||
|
||||
class StrategyVersion(models.Model):
|
||||
strategy = models.ForeignKey(QuantStrategy, on_delete=models.CASCADE, related_name='versions')
|
||||
version = models.CharField(max_length=20)
|
||||
implementation_key = models.CharField(max_length=200, default="", help_text="전략 구현체의 레지스트리 키")
|
||||
parameters = models.JSONField(default=dict, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
is_current = models.BooleanField(default=False)
|
||||
|
||||
def clean(self):
|
||||
if self.is_current:
|
||||
StrategyVersion.objects.filter(
|
||||
strategy=self.strategy,
|
||||
is_current=True
|
||||
).exclude(pk=self.pk).update(is_current=False)
|
||||
|
||||
if not self.implementation_key:
|
||||
self.implementation_key = StrategyRegistry.get_strategy_key(
|
||||
self.strategy.name, self.version
|
||||
)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.clean()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_strategy_implementation(self):
|
||||
"""전략 구현체 인스턴스를 반환합니다"""
|
||||
try:
|
||||
name, version = self.implementation_key.split(':')
|
||||
return StrategyRegistry.get_strategy(name, version)
|
||||
except ValueError:
|
||||
raise ValidationError(f"Invalid implementation key: {self.implementation_key}")
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.strategy.name} v{self.version}"
|
||||
|
||||
class Meta:
|
||||
unique_together = ['strategy', 'version']
|
||||
ordering = ['-created_at']
|
||||
|
||||
|
||||
class StrategyExecution(models.Model):
|
||||
EXECUTION_STATUS = [
|
||||
('pending', 'Pending'),
|
||||
('running', 'Running'),
|
||||
('completed', 'Completed'),
|
||||
('failed', 'Failed'),
|
||||
]
|
||||
|
||||
strategy_version = models.ForeignKey(StrategyVersion, on_delete=models.CASCADE)
|
||||
execution_parameters = models.JSONField(default=dict, blank=True)
|
||||
status = models.CharField(max_length=20, choices=EXECUTION_STATUS, default='pending')
|
||||
started_at = models.DateTimeField(auto_now_add=True)
|
||||
completed_at = models.DateTimeField(null=True, blank=True)
|
||||
result = models.JSONField(null=True, blank=True)
|
||||
error_message = models.TextField(blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"Execution of {self.strategy_version} - {self.status}"
|
||||
|
||||
class Meta:
|
||||
ordering = ['-started_at']
|
||||
Reference in New Issue
Block a user