QTrade/src/core/models/pairing.py

168 lines
4.7 KiB
Python
Raw Normal View History

"""
股票-策略配对 ORM 模型
包含:
- PairingStatus: 配对状态枚举
- Pairing: 股票-策略配对表
"""
from datetime import datetime
from decimal import Decimal
from enum import Enum
from sqlalchemy import Index, JSON, Numeric, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base, TimestampMixin
class PairingStatus(str, Enum):
"""配对状态"""
ACTIVE = "active" # 运行中
PAUSED = "paused" # 已暂停
STOPPED = "stopped" # 已停止
class Pairing(TimestampMixin, Base):
"""
股票-策略配对
一只股票可以配对多个策略,每个配对有独立的资金和持仓管理
"""
__tablename__ = "pairings"
# 主键
id: Mapped[str] = mapped_column(String(32), primary_key=True, comment="配对ID")
# 股票信息
symbol: Mapped[str] = mapped_column(String(20), nullable=False, comment="股票代码")
symbol_name: Mapped[str | None] = mapped_column(String(50), comment="股票名称")
# 策略信息
strategy_id: Mapped[str] = mapped_column(
String(50), nullable=False, comment="策略ID"
)
strategy_name: Mapped[str | None] = mapped_column(String(100), comment="策略名称")
# 资金配置
initial_capital: Mapped[Decimal] = mapped_column(
Numeric(16, 2), nullable=False, comment="初始资金"
)
available_capital: Mapped[Decimal] = mapped_column(
Numeric(16, 2), nullable=False, comment="可用资金"
)
frozen_capital: Mapped[Decimal] = mapped_column(
Numeric(16, 2), default=Decimal(0), comment="冻结资金"
)
# 状态
status: Mapped[str] = mapped_column(
String(20), default=PairingStatus.STOPPED.value, comment="配对状态"
)
# 策略参数 (JSON)
params: Mapped[dict | None] = mapped_column(JSON, default=dict, comment="策略参数")
# 统计信息
total_trades: Mapped[int] = mapped_column(default=0, comment="总交易次数")
win_trades: Mapped[int] = mapped_column(default=0, comment="盈利次数")
loss_trades: Mapped[int] = mapped_column(default=0, comment="亏损次数")
total_pnl: Mapped[Decimal] = mapped_column(
Numeric(16, 2), default=Decimal(0), comment="累计盈亏"
)
max_drawdown: Mapped[Decimal] = mapped_column(
Numeric(10, 4), default=Decimal(0), comment="最大回撤率"
)
# 备注
remark: Mapped[str | None] = mapped_column(Text, comment="备注")
__table_args__ = (
Index("ix_pairings_symbol", "symbol"),
Index("ix_pairings_strategy_id", "strategy_id"),
Index("ix_pairings_status", "status"),
Index("ix_pairings_symbol_strategy", "symbol", "strategy_id"),
)
@property
def current_capital(self) -> Decimal:
"""当前总资金 = 可用 + 冻结"""
return self.available_capital + self.frozen_capital
@property
def win_rate(self) -> float | None:
"""胜率"""
if self.total_trades == 0:
return None
return self.win_trades / self.total_trades
@property
def return_rate(self) -> float:
"""收益率 (%)"""
if self.initial_capital == 0:
return 0.0
return float(
(self.current_capital - self.initial_capital) / self.initial_capital * 100
)
def is_active(self) -> bool:
"""是否运行中"""
return self.status == PairingStatus.ACTIVE.value
def freeze_capital(self, amount: Decimal) -> bool:
"""
冻结资金
Args:
amount: 冻结金额
Returns:
是否成功
"""
if amount > self.available_capital:
return False
self.available_capital -= amount
self.frozen_capital += amount
self.updated_at = datetime.now()
return True
def unfreeze_capital(self, amount: Decimal) -> bool:
"""
解冻资金
Args:
amount: 解冻金额
Returns:
是否成功
"""
if amount > self.frozen_capital:
return False
self.frozen_capital -= amount
self.available_capital += amount
self.updated_at = datetime.now()
return True
def add_capital(self, amount: Decimal) -> None:
"""增加可用资金"""
self.available_capital += amount
self.updated_at = datetime.now()
def deduct_capital(self, amount: Decimal) -> bool:
"""
扣减可用资金
Args:
amount: 扣减金额
Returns:
是否成功
"""
if amount > self.available_capital:
return False
self.available_capital -= amount
self.updated_at = datetime.now()
return True