initial commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
import dependencies
|
||||
|
||||
router = APIRouter(prefix="/portfolio")
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
|
||||
@router.get("", response_class=HTMLResponse)
|
||||
async def portfolio_page(request: Request):
|
||||
return templates.TemplateResponse(request, "portfolio.html", {})
|
||||
|
||||
|
||||
@router.get("/data")
|
||||
async def portfolio_data():
|
||||
try:
|
||||
ib = dependencies.get_ib()
|
||||
positions = ib.positions()
|
||||
return [
|
||||
{
|
||||
"symbol": pos.contract.symbol,
|
||||
"position": pos.position,
|
||||
"avg_cost": pos.avgCost,
|
||||
}
|
||||
for pos in positions
|
||||
]
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
|
||||
@router.get("/pnl")
|
||||
async def portfolio_pnl():
|
||||
try:
|
||||
ib = dependencies.get_ib()
|
||||
summary = await ib.reqAccountSummaryAsync()
|
||||
keys = {"NetLiquidation", "TotalCashValue", "UnrealizedPnL", "RealizedPnL"}
|
||||
result = {}
|
||||
for item in summary:
|
||||
if item.tag in keys:
|
||||
try:
|
||||
result[item.tag] = float(item.value)
|
||||
except (ValueError, TypeError):
|
||||
result[item.tag] = item.value
|
||||
return result
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
Reference in New Issue
Block a user