first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"""API 路由:账户"""
|
||||
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app import crud, schemas
|
||||
|
||||
router = APIRouter(prefix="/api/accounts", tags=["账户"])
|
||||
|
||||
|
||||
@router.get("", response_model=List[schemas.AccountOut])
|
||||
def list_accounts(db: Session = Depends(get_db)):
|
||||
return crud.get_accounts(db)
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.AccountOut, status_code=201)
|
||||
def create_account(data: schemas.AccountCreate, db: Session = Depends(get_db)):
|
||||
return crud.create_account(db, data)
|
||||
|
||||
|
||||
@router.put("/{account_id}", response_model=schemas.AccountOut)
|
||||
def update_account(account_id: int, data: schemas.AccountUpdate, db: Session = Depends(get_db)):
|
||||
obj = crud.update_account(db, account_id, data)
|
||||
if not obj:
|
||||
raise HTTPException(404, "账户不存在")
|
||||
return obj
|
||||
|
||||
|
||||
@router.delete("/{account_id}")
|
||||
def delete_account(account_id: int, db: Session = Depends(get_db)):
|
||||
if not crud.delete_account(db, account_id):
|
||||
raise HTTPException(404, "账户不存在")
|
||||
return {"ok": True}
|
||||
@@ -0,0 +1,31 @@
|
||||
"""API 路由:日常使用 - 前置匹配"""
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app import schemas
|
||||
from app.matcher import run_match
|
||||
|
||||
router = APIRouter(prefix="/api/match", tags=["前置匹配"])
|
||||
|
||||
|
||||
@router.get("", response_model=schemas.MatchResult)
|
||||
def do_match(
|
||||
market_cycle: str = Query(..., description="大盘周期:日线/4H/1H"),
|
||||
market_regime_id: int = Query(..., description="大盘阶段 ID"),
|
||||
trend_strength: str = Query(..., description="趋势强弱:强/弱/震荡"),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""根据人工选择的三项参数,输出前置匹配结果"""
|
||||
req = schemas.MatchRequest(
|
||||
market_cycle=market_cycle,
|
||||
market_regime_id=market_regime_id,
|
||||
trend_strength=trend_strength,
|
||||
)
|
||||
return run_match(db, req)
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.MatchResult)
|
||||
def do_match_post(req: schemas.MatchRequest, db: Session = Depends(get_db)):
|
||||
return run_match(db, req)
|
||||
@@ -0,0 +1,61 @@
|
||||
"""API 路由:匹配绑定"""
|
||||
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app import crud, schemas
|
||||
|
||||
router = APIRouter(prefix="/api/matches", tags=["匹配配置"])
|
||||
|
||||
|
||||
def _enrich_match(m) -> schemas.RegimeMatchOut:
|
||||
"""填充关联名称"""
|
||||
return schemas.RegimeMatchOut(
|
||||
id=m.id,
|
||||
market_regime_id=m.market_regime_id,
|
||||
market_cycle=m.market_cycle,
|
||||
trend_strength=m.trend_strength,
|
||||
account_id=m.account_id,
|
||||
strategy_id=m.strategy_id,
|
||||
force_direction=m.force_direction or "",
|
||||
regime_name=m.regime.name if m.regime else None,
|
||||
account_name=m.account.account_name if m.account else None,
|
||||
strategy_name=m.strategy.strategy_name if m.strategy else None,
|
||||
)
|
||||
|
||||
|
||||
@router.get("", response_model=List[schemas.RegimeMatchOut])
|
||||
def list_matches(db: Session = Depends(get_db)):
|
||||
return [_enrich_match(m) for m in crud.get_matches(db)]
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.RegimeMatchOut, status_code=201)
|
||||
def create_match(data: schemas.RegimeMatchCreate, db: Session = Depends(get_db)):
|
||||
obj = crud.create_match(db, data)
|
||||
# 重新加载关联
|
||||
matches = crud.get_matches(db)
|
||||
for m in matches:
|
||||
if m.id == obj.id:
|
||||
return _enrich_match(m)
|
||||
return schemas.RegimeMatchOut(id=obj.id, **data.model_dump())
|
||||
|
||||
|
||||
@router.put("/{match_id}", response_model=schemas.RegimeMatchOut)
|
||||
def update_match(match_id: int, data: schemas.RegimeMatchUpdate, db: Session = Depends(get_db)):
|
||||
obj = crud.update_match(db, match_id, data)
|
||||
if not obj:
|
||||
raise HTTPException(404, "匹配规则不存在")
|
||||
matches = crud.get_matches(db)
|
||||
for m in matches:
|
||||
if m.id == match_id:
|
||||
return _enrich_match(m)
|
||||
raise HTTPException(404, "匹配规则不存在")
|
||||
|
||||
|
||||
@router.delete("/{match_id}")
|
||||
def delete_match(match_id: int, db: Session = Depends(get_db)):
|
||||
if not crud.delete_match(db, match_id):
|
||||
raise HTTPException(404, "匹配规则不存在")
|
||||
return {"ok": True}
|
||||
@@ -0,0 +1,35 @@
|
||||
"""API 路由:大盘阶段"""
|
||||
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app import crud, schemas
|
||||
|
||||
router = APIRouter(prefix="/api/regimes", tags=["大盘阶段"])
|
||||
|
||||
|
||||
@router.get("", response_model=List[schemas.MarketRegimeOut])
|
||||
def list_regimes(db: Session = Depends(get_db)):
|
||||
return crud.get_regimes(db)
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.MarketRegimeOut, status_code=201)
|
||||
def create_regime(data: schemas.MarketRegimeCreate, db: Session = Depends(get_db)):
|
||||
return crud.create_regime(db, data)
|
||||
|
||||
|
||||
@router.put("/{regime_id}", response_model=schemas.MarketRegimeOut)
|
||||
def update_regime(regime_id: int, data: schemas.MarketRegimeUpdate, db: Session = Depends(get_db)):
|
||||
obj = crud.update_regime(db, regime_id, data)
|
||||
if not obj:
|
||||
raise HTTPException(404, "大盘阶段不存在")
|
||||
return obj
|
||||
|
||||
|
||||
@router.delete("/{regime_id}")
|
||||
def delete_regime(regime_id: int, db: Session = Depends(get_db)):
|
||||
if not crud.delete_regime(db, regime_id):
|
||||
raise HTTPException(404, "大盘阶段不存在")
|
||||
return {"ok": True}
|
||||
@@ -0,0 +1,35 @@
|
||||
"""API 路由:策略"""
|
||||
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app import crud, schemas
|
||||
|
||||
router = APIRouter(prefix="/api/strategies", tags=["策略"])
|
||||
|
||||
|
||||
@router.get("", response_model=List[schemas.StrategyOut])
|
||||
def list_strategies(db: Session = Depends(get_db)):
|
||||
return crud.get_strategies(db)
|
||||
|
||||
|
||||
@router.post("", response_model=schemas.StrategyOut, status_code=201)
|
||||
def create_strategy(data: schemas.StrategyCreate, db: Session = Depends(get_db)):
|
||||
return crud.create_strategy(db, data)
|
||||
|
||||
|
||||
@router.put("/{strategy_id}", response_model=schemas.StrategyOut)
|
||||
def update_strategy(strategy_id: int, data: schemas.StrategyUpdate, db: Session = Depends(get_db)):
|
||||
obj = crud.update_strategy(db, strategy_id, data)
|
||||
if not obj:
|
||||
raise HTTPException(404, "策略不存在")
|
||||
return obj
|
||||
|
||||
|
||||
@router.delete("/{strategy_id}")
|
||||
def delete_strategy(strategy_id: int, db: Session = Depends(get_db)):
|
||||
if not crud.delete_strategy(db, strategy_id):
|
||||
raise HTTPException(404, "策略不存在")
|
||||
return {"ok": True}
|
||||
Reference in New Issue
Block a user