仓位管理支持多合约同时开轮

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-26 10:34:25 +08:00
parent 58d99259e8
commit 51be04e5d2
3 changed files with 138 additions and 203 deletions
+20 -15
View File
@@ -20,11 +20,11 @@ def get_active_contracts(db: Session) -> list[str]:
@router.get("/", response_class=HTMLResponse)
def positions_page(request: Request, db: Session = Depends(get_db)):
active_round = (
active_rounds = (
db.query(Round)
.filter(Round.status == "active")
.order_by(Round.started_at.desc())
.first()
.all()
)
template = request.app.state.templates.get_template("positions.html")
@@ -33,7 +33,7 @@ def positions_page(request: Request, db: Session = Depends(get_db)):
request=request,
active_nav="positions",
contracts=get_active_contracts(db),
active_round=active_round,
active_rounds=active_rounds,
)
)
@@ -45,12 +45,15 @@ def create_round(
daily_hands: int = Form(1),
db: Session = Depends(get_db),
):
existing = db.query(Round).filter(Round.status == "active").first()
code = contract_code.upper()
existing = db.query(Round).filter(
Round.status == "active", Round.contract_code == code
).first()
if existing:
return RedirectResponse("/positions/?error=已有活跃轮次", status_code=303)
return RedirectResponse(f"/positions/?error={code} 已有活跃轮次", status_code=303)
r = Round(
contract_code=contract_code.upper(),
contract_code=code,
daily_hands=daily_hands,
status="active",
started_at=date.today(),
@@ -60,21 +63,22 @@ def create_round(
return RedirectResponse("/positions/", status_code=303)
@router.post("/add")
@router.post("/{round_id}/add")
def add_position(
request: Request,
round_id: int,
open_price: int = Form(...),
amp_threshold: int = Form(...),
hands: int = Form(1),
db: Session = Depends(get_db),
):
active_round = db.query(Round).filter(Round.status == "active").first()
if not active_round:
return RedirectResponse("/positions/?error=无活跃轮次", status_code=303)
r = db.query(Round).filter(Round.id == round_id).first()
if not r or r.status != "active":
return RedirectResponse("/positions/?error=轮次不存在或已结束", status_code=303)
lock_price = open_price + amp_threshold
p = Position(
round_id=active_round.id,
round_id=r.id,
open_price=open_price,
amp_threshold=amp_threshold,
lock_price=lock_price,
@@ -104,14 +108,15 @@ def unlock_position(position_id: int, db: Session = Depends(get_db)):
return RedirectResponse("/positions/", status_code=303)
@router.post("/close")
@router.post("/{round_id}/close")
def close_round(
request: Request,
round_id: int,
result: str = Form(...),
db: Session = Depends(get_db),
):
active_round = db.query(Round).filter(Round.status == "active").first()
if active_round:
active_round.status = result
r = db.query(Round).filter(Round.id == round_id).first()
if r and r.status == "active":
r.status = result
db.commit()
return RedirectResponse("/positions/", status_code=303)