Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions internal/api/order_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,48 @@ func (h *OrderHandler) PlaceMarketOrder(c *gin.Context) {
}

book := h.bookManager.GetOrCreate(req.Symbol)

// Pre-execution validation for Market Orders:
// Verify that the user has sufficient funds/assets before hitting the engine.
p, err := h.portfolioMgr.LoadOrFetch(c.Request.Context(), userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load portfolio: " + err.Error()})
return
}

if side == engine.SideBuy {
bestAsk := book.BestAsk()
if bestAsk <= 0 {
// If no liquidity, we can't estimate cost.
// Rejection is safer for massive orders like 5B.
c.JSON(http.StatusBadRequest, gin.H{"error": "insufficient liquidity in the order book"})
return
}
estCost := (float64(bestAsk) / 100.0) * float64(req.Quantity)
if p.AvailableCash < estCost {
c.JSON(http.StatusBadRequest, gin.H{
"error": "insufficient funds",
"available_cash": p.AvailableCash,
"estimated_cost": estCost,
})
return
}
} else if side == engine.SideSell {
pos, ok := p.Positions[req.Symbol]
if !ok || pos.Quantity < float64(req.Quantity) {
avail := 0.0
if ok {
avail = pos.Quantity
}
c.JSON(http.StatusBadRequest, gin.H{
"error": "insufficient assets",
"available_shares": avail,
"required_shares": req.Quantity,
})
return
}
}

orderID, status, result := book.MarketAutoID(side, int(req.Quantity))
if status != engine.StatusOK {
c.JSON(http.StatusInternalServerError, gin.H{"error": "engine error: " + status.Error()})
Expand Down
Loading