feat: include stock names in snapshot, transaction, and backtest transaction API responses
This commit is contained in:
parent
b6c22f70ae
commit
628b431171
@ -17,6 +17,7 @@ from app.schemas.backtest import (
|
|||||||
EquityCurvePoint, RebalanceHoldings, HoldingItem, TransactionItem,
|
EquityCurvePoint, RebalanceHoldings, HoldingItem, TransactionItem,
|
||||||
)
|
)
|
||||||
from app.services.backtest import submit_backtest
|
from app.services.backtest import submit_backtest
|
||||||
|
from app.services.rebalance import RebalanceService
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/backtest", tags=["backtest"])
|
router = APIRouter(prefix="/api/backtest", tags=["backtest"])
|
||||||
|
|
||||||
@ -228,11 +229,17 @@ async def get_transactions(
|
|||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Resolve stock names
|
||||||
|
tickers = list({t.ticker for t in transactions})
|
||||||
|
name_service = RebalanceService(db)
|
||||||
|
names = name_service.get_stock_names(tickers)
|
||||||
|
|
||||||
return [
|
return [
|
||||||
TransactionItem(
|
TransactionItem(
|
||||||
id=t.id,
|
id=t.id,
|
||||||
date=t.date,
|
date=t.date,
|
||||||
ticker=t.ticker,
|
ticker=t.ticker,
|
||||||
|
name=names.get(t.ticker),
|
||||||
action=t.action,
|
action=t.action,
|
||||||
shares=t.shares,
|
shares=t.shares,
|
||||||
price=t.price,
|
price=t.price,
|
||||||
|
|||||||
@ -231,7 +231,25 @@ async def get_transactions(
|
|||||||
.limit(limit)
|
.limit(limit)
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
return transactions
|
|
||||||
|
# Resolve stock names
|
||||||
|
tickers = list({tx.ticker for tx in transactions})
|
||||||
|
service = RebalanceService(db)
|
||||||
|
names = service.get_stock_names(tickers)
|
||||||
|
|
||||||
|
return [
|
||||||
|
TransactionResponse(
|
||||||
|
id=tx.id,
|
||||||
|
ticker=tx.ticker,
|
||||||
|
name=names.get(tx.ticker),
|
||||||
|
tx_type=tx.tx_type.value,
|
||||||
|
quantity=tx.quantity,
|
||||||
|
price=tx.price,
|
||||||
|
executed_at=tx.executed_at,
|
||||||
|
memo=tx.memo,
|
||||||
|
)
|
||||||
|
for tx in transactions
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{portfolio_id}/transactions", response_model=TransactionResponse, status_code=status.HTTP_201_CREATED)
|
@router.post("/{portfolio_id}/transactions", response_model=TransactionResponse, status_code=status.HTTP_201_CREATED)
|
||||||
|
|||||||
@ -16,6 +16,7 @@ from app.schemas.portfolio import (
|
|||||||
ReturnsResponse, ReturnDataPoint,
|
ReturnsResponse, ReturnDataPoint,
|
||||||
)
|
)
|
||||||
from app.services.price_service import PriceService
|
from app.services.price_service import PriceService
|
||||||
|
from app.services.rebalance import RebalanceService
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/portfolios", tags=["snapshots"])
|
router = APIRouter(prefix="/api/portfolios", tags=["snapshots"])
|
||||||
|
|
||||||
@ -110,6 +111,10 @@ async def create_snapshot(
|
|||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(snapshot)
|
db.refresh(snapshot)
|
||||||
|
|
||||||
|
# Get stock names
|
||||||
|
name_service = RebalanceService(db)
|
||||||
|
names = name_service.get_stock_names(tickers)
|
||||||
|
|
||||||
return SnapshotResponse(
|
return SnapshotResponse(
|
||||||
id=snapshot.id,
|
id=snapshot.id,
|
||||||
portfolio_id=snapshot.portfolio_id,
|
portfolio_id=snapshot.portfolio_id,
|
||||||
@ -118,6 +123,7 @@ async def create_snapshot(
|
|||||||
holdings=[
|
holdings=[
|
||||||
SnapshotHoldingResponse(
|
SnapshotHoldingResponse(
|
||||||
ticker=h.ticker,
|
ticker=h.ticker,
|
||||||
|
name=names.get(h.ticker),
|
||||||
quantity=h.quantity,
|
quantity=h.quantity,
|
||||||
price=h.price,
|
price=h.price,
|
||||||
value=h.value,
|
value=h.value,
|
||||||
@ -150,6 +156,11 @@ async def get_snapshot(
|
|||||||
if not snapshot:
|
if not snapshot:
|
||||||
raise HTTPException(status_code=404, detail="Snapshot not found")
|
raise HTTPException(status_code=404, detail="Snapshot not found")
|
||||||
|
|
||||||
|
# Get stock names
|
||||||
|
tickers = [h.ticker for h in snapshot.holdings]
|
||||||
|
name_service = RebalanceService(db)
|
||||||
|
names = name_service.get_stock_names(tickers)
|
||||||
|
|
||||||
return SnapshotResponse(
|
return SnapshotResponse(
|
||||||
id=snapshot.id,
|
id=snapshot.id,
|
||||||
portfolio_id=snapshot.portfolio_id,
|
portfolio_id=snapshot.portfolio_id,
|
||||||
@ -158,6 +169,7 @@ async def get_snapshot(
|
|||||||
holdings=[
|
holdings=[
|
||||||
SnapshotHoldingResponse(
|
SnapshotHoldingResponse(
|
||||||
ticker=h.ticker,
|
ticker=h.ticker,
|
||||||
|
name=names.get(h.ticker),
|
||||||
quantity=h.quantity,
|
quantity=h.quantity,
|
||||||
price=h.price,
|
price=h.price,
|
||||||
value=h.value,
|
value=h.value,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user