Add configurable SIM equity (default 10k) and strategy docs.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,12 +2,13 @@ from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from ..config import get_settings
|
||||
from ..models.db import get_db
|
||||
from ..sim.ledger import Ledger
|
||||
from ..sim.matcher import Matcher
|
||||
from .auth import require_user
|
||||
|
||||
router = APIRouter(prefix="/api/settings", tags=["settings"])
|
||||
@@ -38,7 +39,7 @@ class StrategySettingsBody(BaseModel):
|
||||
premium_exit_multiple: float | None = Field(default=None, ge=0.1, le=100)
|
||||
rest_seconds: int | None = Field(default=None, ge=0, le=3600)
|
||||
skip_weekends: bool | None = None
|
||||
initial_equity: float | None = Field(default=None, ge=1000)
|
||||
initial_equity: float | None = Field(default=None, ge=1000, le=10_000_000)
|
||||
leverage: float | None = Field(default=None, ge=1, le=125)
|
||||
min_option_hours: float | None = Field(default=None, ge=1, le=720)
|
||||
min_option_leverage: float | None = Field(default=None, ge=1, le=10000)
|
||||
@@ -116,8 +117,27 @@ async def put_strategy_settings(
|
||||
_user: Annotated[str, Depends(require_user)],
|
||||
) -> dict:
|
||||
db = get_db()
|
||||
s = get_settings()
|
||||
data = body.model_dump(exclude_none=True)
|
||||
equity_to_apply: float | None = None
|
||||
if "initial_equity" in data:
|
||||
new_eq = float(data["initial_equity"])
|
||||
old_eq = float(
|
||||
db.get_setting("initial_equity", str(s.initial_equity)) or s.initial_equity
|
||||
)
|
||||
if abs(new_eq - old_eq) > 1e-9:
|
||||
if Matcher(db).has_open_position():
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="有未平仓,无法重置模拟资金;请先平仓后再改",
|
||||
)
|
||||
equity_to_apply = new_eq
|
||||
for k, v in data.items():
|
||||
if k in KEYS:
|
||||
db.set_setting(k, str(v))
|
||||
if equity_to_apply is not None:
|
||||
Ledger(db).reset_equity(
|
||||
equity_to_apply,
|
||||
note=f"设置模拟资金={equity_to_apply:.2f}",
|
||||
)
|
||||
return _read_settings()
|
||||
|
||||
@@ -38,7 +38,7 @@ class Settings(BaseSettings):
|
||||
index_inst_id: str = "ETH-USD"
|
||||
|
||||
fee_rate: float = 0.0005
|
||||
initial_equity: float = 100_000.0
|
||||
initial_equity: float = 10_000.0 # SIM 模拟初始资金(USDT),设置页可改
|
||||
max_rounds: int = 3 # 已不再强管控,仅兼容旧字段
|
||||
open_hhmm: str = "16:00" # 已废弃开仓窗
|
||||
stop_open_hhmm: str = "08:00" # 已废弃开仓窗
|
||||
|
||||
@@ -48,6 +48,24 @@ class Ledger:
|
||||
self.db._conn.commit()
|
||||
return equity
|
||||
|
||||
def reset_equity(self, amount: float, *, note: str = "重置模拟资金") -> float:
|
||||
"""将权益与可用资金重置为 amount(reserved 清零)。须在无持仓时调用。"""
|
||||
now = int(time.time() * 1000)
|
||||
amt = float(amount)
|
||||
if amt < 0:
|
||||
raise ValueError("模拟资金不能为负")
|
||||
with self.db._lock:
|
||||
self.db._conn.execute(
|
||||
"UPDATE ledger_meta SET equity=?, available=?, reserved=0, updated_at_ms=? WHERE id=1",
|
||||
(amt, amt, now),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"INSERT INTO ledger_entries(group_id, kind, amount, balance_after, note, ts_ms) VALUES (?,?,?,?,?,?)",
|
||||
(None, "reset", amt, amt, note, now),
|
||||
)
|
||||
self.db._conn.commit()
|
||||
return amt
|
||||
|
||||
def get_setting_float(self, key: str, default: float) -> float:
|
||||
v = self.db.get_setting(key)
|
||||
if v is None or v == "":
|
||||
|
||||
Reference in New Issue
Block a user