mirror of
https://github.com/lux-apps/papers.git
synced 2026-11-03 05:07:46 +00:00
Restructured from flat .tex/.pdf files into per-paper subdirectories. Added Antje Worring as co-author on all papers. Synced PDFs to pdfs/ for download site.
68 lines
2.0 KiB
TeX
68 lines
2.0 KiB
TeX
\section{Order Matching}
|
|
\label{sec:matching}
|
|
|
|
\subsection{Matching Engine}
|
|
|
|
The matching engine implements price-time priority for continuous order matching:
|
|
|
|
\begin{lstlisting}[language=Go,caption={Matching engine core.}]
|
|
type MatchEngine struct {
|
|
books map[SecurityID]*OrderBook
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
type OrderBook struct {
|
|
Bids *PriorityQueue // max-heap by price, FIFO by time
|
|
Asks *PriorityQueue // min-heap by price, FIFO by time
|
|
}
|
|
|
|
func (e *MatchEngine) Match(secID SecurityID) []Execution {
|
|
e.mu.Lock()
|
|
defer e.mu.Unlock()
|
|
|
|
book := e.books[secID]
|
|
var executions []Execution
|
|
|
|
for book.Bids.Len() > 0 && book.Asks.Len() > 0 {
|
|
bestBid := book.Bids.Peek()
|
|
bestAsk := book.Asks.Peek()
|
|
|
|
if bestBid.Price.LessThan(bestAsk.Price) {
|
|
break
|
|
}
|
|
|
|
qty := min(bestBid.Remaining, bestAsk.Remaining)
|
|
price := bestAsk.Price // price-time priority: aggressor pays
|
|
|
|
executions = append(executions, Execution{
|
|
BuyOrder: bestBid.ID,
|
|
SellOrder: bestAsk.ID,
|
|
Quantity: qty,
|
|
Price: price,
|
|
Timestamp: time.Now().UTC(),
|
|
})
|
|
|
|
bestBid.Remaining -= qty
|
|
bestAsk.Remaining -= qty
|
|
|
|
if bestBid.Remaining == 0 { book.Bids.Pop() }
|
|
if bestAsk.Remaining == 0 { book.Asks.Pop() }
|
|
}
|
|
return executions
|
|
}
|
|
\end{lstlisting}
|
|
|
|
\subsection{Order Types}
|
|
|
|
The ATS supports the following order types, consistent with standard equity market conventions:
|
|
|
|
\begin{itemize}[nosep]
|
|
\item \textbf{Limit}: Execute at specified price or better.
|
|
\item \textbf{Market}: Execute immediately at best available price.
|
|
\item \textbf{IOC} (Immediate-or-Cancel): Fill what is available, cancel remainder.
|
|
\item \textbf{GTC} (Good-Till-Cancelled): Remain on book until filled or cancelled.
|
|
\item \textbf{GTD} (Good-Till-Date): Remain on book until specified date.
|
|
\item \textbf{RFQ} (Request-for-Quote): Solicit quotes from registered market makers.
|
|
\end{itemize}
|
|
|