과거 거래 기록 조회 추가

좌측 사이드메뉴 추가
ggitignore 업데이트 : *.pyc
This commit is contained in:
2026-07-17 13:03:35 +09:00
parent 2fcef54b23
commit d3f3931c38
6 changed files with 431 additions and 118 deletions
+49 -21
View File
@@ -1,9 +1,12 @@
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException
from datetime import date, timedelta
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from sqlalchemy.orm import Session
from app.core.auth import token_manager
from app.core.database import get_db
from app.models.stock import Trade
from app.services.trading import trading_service
@@ -62,32 +65,57 @@ async def cancel_order(data: CancelRequest) -> dict:
@router.get("/history")
def get_trade_history(
async def get_trade_history(
stock_code: str | None = None,
limit: int = 50,
start_date: str | None = None,
end_date: str | None = None,
limit: int = 100,
db: Session = Depends(get_db),
) -> list[dict]:
) -> dict:
source = "local"
if token_manager.is_authenticated:
if not start_date:
start_date = (date.today() - timedelta(days=30)).strftime("%Y%m%d")
if not end_date:
end_date = date.today().strftime("%Y%m%d")
try:
kis_trades = await trading_service.get_daily_ccled(
start_date=start_date,
end_date=end_date,
stock_code=stock_code or "",
)
if kis_trades:
source = "kis"
return {"source": source, "trades": kis_trades[:limit]}
except Exception:
pass
query = db.query(Trade).order_by(Trade.created_at.desc())
if stock_code:
query = query.filter(Trade.stock_code == stock_code)
trades = query.limit(limit).all()
return [
{
"id": t.id,
"order_no": t.order_no,
"stock_code": t.stock_code,
"stock_name": t.stock_name,
"side": t.side,
"qty": t.qty,
"price": t.price,
"order_type": t.order_type,
"status": t.status,
"strategy_id": t.strategy_id,
"created_at": t.created_at.isoformat() if t.created_at else None,
"filled_at": t.filled_at.isoformat() if t.filled_at else None,
}
for t in trades
]
return {
"source": source,
"trades": [
{
"id": t.id,
"order_no": t.order_no,
"stock_code": t.stock_code,
"stock_name": t.stock_name,
"side": t.side,
"qty": t.qty,
"price": t.price,
"order_type": t.order_type,
"status": t.status,
"strategy_id": t.strategy_id,
"created_at": t.created_at.isoformat() if t.created_at else None,
"filled_at": t.filled_at.isoformat() if t.filled_at else None,
}
for t in trades
],
}
@router.get("/account")