104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.database import get_db
|
|
from app.models.stock import Strategy
|
|
|
|
router = APIRouter(prefix="/api/strategies", tags=["strategies"])
|
|
|
|
|
|
class StrategyCreate(BaseModel):
|
|
name: str
|
|
strategy_type: str # conditional / technical / periodic
|
|
stock_code: str
|
|
params: dict = {}
|
|
order_type: str = "00"
|
|
qty: int = 1
|
|
|
|
|
|
class StrategyUpdate(BaseModel):
|
|
name: str | None = None
|
|
params: dict | None = None
|
|
order_type: str | None = None
|
|
qty: int | None = None
|
|
is_active: bool | None = None
|
|
|
|
|
|
@router.get("/")
|
|
def list_strategies(db: Session = Depends(get_db)) -> list[dict]:
|
|
strategies = db.query(Strategy).all()
|
|
return [
|
|
{
|
|
"id": s.id,
|
|
"name": s.name,
|
|
"strategy_type": s.strategy_type,
|
|
"stock_code": s.stock_code,
|
|
"params": json.loads(s.params_json) if s.params_json else {},
|
|
"order_type": s.order_type,
|
|
"qty": s.qty,
|
|
"is_active": s.is_active,
|
|
"created_at": s.created_at.isoformat() if s.created_at else None,
|
|
}
|
|
for s in strategies
|
|
]
|
|
|
|
|
|
@router.post("/")
|
|
def create_strategy(data: StrategyCreate, db: Session = Depends(get_db)) -> dict:
|
|
strategy = Strategy(
|
|
name=data.name,
|
|
strategy_type=data.strategy_type,
|
|
stock_code=data.stock_code,
|
|
params_json=json.dumps(data.params, ensure_ascii=False),
|
|
order_type=data.order_type,
|
|
qty=data.qty,
|
|
)
|
|
db.add(strategy)
|
|
db.commit()
|
|
db.refresh(strategy)
|
|
return {
|
|
"id": strategy.id,
|
|
"name": strategy.name,
|
|
"strategy_type": strategy.strategy_type,
|
|
"stock_code": strategy.stock_code,
|
|
"params": data.params,
|
|
"is_active": strategy.is_active,
|
|
}
|
|
|
|
|
|
@router.put("/{strategy_id}")
|
|
def update_strategy(strategy_id: int, data: StrategyUpdate, db: Session = Depends(get_db)) -> dict:
|
|
strategy = db.query(Strategy).filter(Strategy.id == strategy_id).first()
|
|
if not strategy:
|
|
raise HTTPException(status_code=404, detail="전략을 찾을 수 없습니다")
|
|
|
|
if data.name is not None:
|
|
strategy.name = data.name
|
|
if data.params is not None:
|
|
strategy.params_json = json.dumps(data.params, ensure_ascii=False)
|
|
if data.order_type is not None:
|
|
strategy.order_type = data.order_type
|
|
if data.qty is not None:
|
|
strategy.qty = data.qty
|
|
if data.is_active is not None:
|
|
strategy.is_active = data.is_active
|
|
|
|
db.commit()
|
|
return {"message": "전략이 업데이트되었습니다", "id": strategy_id}
|
|
|
|
|
|
@router.delete("/{strategy_id}")
|
|
def delete_strategy(strategy_id: int, db: Session = Depends(get_db)) -> dict:
|
|
strategy = db.query(Strategy).filter(Strategy.id == strategy_id).first()
|
|
if not strategy:
|
|
raise HTTPException(status_code=404, detail="전략을 찾을 수 없습니다")
|
|
|
|
db.delete(strategy)
|
|
db.commit()
|
|
return {"message": "전략이 삭제되었습니다", "id": strategy_id}
|