Flow 8.4 list trading working
This commit is contained in:
@ -1,21 +1,24 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/shopspring/decimal"
|
||||
|
||||
"quantex.com/qfixdpl/quickfix"
|
||||
"quantex.com/qfixdpl/quickfix/gen/enum"
|
||||
"quantex.com/qfixdpl/quickfix/gen/field"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/executionack"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/executionreport"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/quote"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/quoteack"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/quoterequest"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/quoteresponse"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/quotestatusreport"
|
||||
"quantex.com/qfixdpl/quickfix/store/file"
|
||||
"quantex.com/qfixdpl/src/app"
|
||||
@ -23,12 +26,28 @@ import (
|
||||
"quantex.com/qfixdpl/src/domain"
|
||||
)
|
||||
|
||||
type listTrade struct {
|
||||
QuoteReqID string
|
||||
ListID string
|
||||
Symbol string
|
||||
SecurityIDSrc enum.SecurityIDSource
|
||||
Currency string
|
||||
Side enum.Side
|
||||
OrderQty decimal.Decimal
|
||||
SettlDate string
|
||||
Price decimal.Decimal
|
||||
OwnerTraderID string
|
||||
SessionID quickfix.SessionID
|
||||
}
|
||||
|
||||
// Manager wraps the QuickFIX initiator and implements domain.FIXSender.
|
||||
type Manager struct {
|
||||
initiator *quickfix.Initiator
|
||||
app *application
|
||||
sessionsMu sync.RWMutex
|
||||
sessions map[string]quickfix.SessionID
|
||||
tradesMu sync.RWMutex
|
||||
trades map[string]*listTrade
|
||||
orderStore domain.OrderStore
|
||||
notify domain.Notifier
|
||||
cfg app.FIXConfig
|
||||
@ -37,6 +56,7 @@ type Manager struct {
|
||||
func NewManager(cfg app.FIXConfig, orderStore domain.OrderStore, notify domain.Notifier) *Manager {
|
||||
return &Manager{
|
||||
sessions: make(map[string]quickfix.SessionID),
|
||||
trades: make(map[string]*listTrade),
|
||||
orderStore: orderStore,
|
||||
notify: notify,
|
||||
cfg: cfg,
|
||||
@ -48,6 +68,10 @@ func (m *Manager) Start() error {
|
||||
fixApp.onLogon = m.onLogon
|
||||
fixApp.onLogout = m.onLogout
|
||||
fixApp.onQuoteRequest = m.handleQuoteRequest
|
||||
fixApp.onQuoteAck = m.handleQuoteAck
|
||||
fixApp.onQuoteResponse = m.handleQuoteResponse
|
||||
fixApp.onExecutionReport = m.handleExecutionReport
|
||||
fixApp.onExecutionAck = m.handleExecutionAck
|
||||
m.app = fixApp
|
||||
|
||||
f, err := os.Open(m.cfg.SettingsFile)
|
||||
@ -117,72 +141,6 @@ func (m *Manager) onLogout(sessionID quickfix.SessionID) {
|
||||
m.sessionsMu.Unlock()
|
||||
}
|
||||
|
||||
// SendQuote implements domain.FIXSender.
|
||||
func (m *Manager) SendQuote(
|
||||
clOrdID, quoteID, symbol string,
|
||||
secIDSource string,
|
||||
currency string,
|
||||
bidPx, offerPx decimal.Decimal,
|
||||
) error {
|
||||
m.sessionsMu.RLock()
|
||||
var sessionID quickfix.SessionID
|
||||
var ok bool
|
||||
for _, sid := range m.sessions {
|
||||
sessionID = sid
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
m.sessionsMu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
err := tracerr.Errorf("error sending quote: no active FIX session")
|
||||
log.Error().Msg(err.Error())
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
q := quote.New(
|
||||
field.NewQuoteID(uuid.NewString()),
|
||||
field.NewQuoteType(enum.QuoteType_INDICATIVE),
|
||||
field.NewTransactTime(time.Now()),
|
||||
)
|
||||
|
||||
sIDSource := enum.SecurityIDSource_ISIN_NUMBER
|
||||
if secIDSource == "1" {
|
||||
sIDSource = enum.SecurityIDSource_CUSIP
|
||||
}
|
||||
|
||||
q.SetSymbol("[N/A]")
|
||||
q.SetSecurityID(symbol)
|
||||
q.SetSecurityIDSource(sIDSource)
|
||||
q.SetQuoteReqID(quoteID)
|
||||
|
||||
if currency != "" {
|
||||
q.SetCurrency(currency)
|
||||
}
|
||||
|
||||
if !bidPx.IsZero() {
|
||||
q.SetBidPx(bidPx, 8)
|
||||
q.SetSide(enum.Side_SELL)
|
||||
} else {
|
||||
q.SetOfferPx(offerPx, 8)
|
||||
q.SetSide(enum.Side_BUY)
|
||||
}
|
||||
|
||||
q.SetPriceType(enum.PriceType_PERCENTAGE)
|
||||
|
||||
if err := quickfix.SendToTarget(q, sessionID); err != nil {
|
||||
err = tracerr.Errorf("error sending FIX quote: %s", err)
|
||||
log.Error().Msg(err.Error())
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info("Quote sent", "clOrdID", clOrdID, "quoteID", quoteID, "symbol", symbol)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendQuoteStatusReport sends a QuoteStatusReport (35=AI) to acknowledge the incoming QuoteRequest.
|
||||
func (m *Manager) sendQuoteStatusReport(quoteReqID, ownerTraderID string, sessionID quickfix.SessionID) error {
|
||||
qsr := quotestatusreport.New(
|
||||
@ -199,6 +157,33 @@ func (m *Manager) sendQuoteStatusReport(quoteReqID, ownerTraderID string, sessio
|
||||
return quickfix.SendToTarget(qsr, sessionID)
|
||||
}
|
||||
|
||||
// sendTradeRequestAck sends a QuoteStatusReport (35=AI) to acknowledge a trade request (TRDREQACK).
|
||||
func (m *Manager) sendTradeRequestAck(quoteReqID, quoteRespID string, sessionID quickfix.SessionID) error {
|
||||
qsr := quotestatusreport.New(
|
||||
field.NewTransactTime(time.Now()),
|
||||
field.NewQuoteStatus(enum.QuoteStatus_ACCEPTED),
|
||||
)
|
||||
qsr.SetQuoteReqID(quoteReqID)
|
||||
qsr.SetQuoteRespID(quoteRespID)
|
||||
qsr.SetSymbol("[N/A]")
|
||||
|
||||
return quickfix.SendToTarget(qsr, sessionID)
|
||||
}
|
||||
|
||||
// sendExecutionAck sends an ExecutionAck (35=BN) to acknowledge an incoming ExecutionReport.
|
||||
func (m *Manager) sendExecutionAck(orderID, clOrdID, execID string, sessionID quickfix.SessionID) error {
|
||||
bn := executionack.New(
|
||||
field.NewOrderID(orderID),
|
||||
field.NewExecID(execID),
|
||||
field.NewExecAckStatus(enum.ExecAckStatus_ACCEPTED),
|
||||
)
|
||||
bn.SetClOrdID(clOrdID)
|
||||
bn.SetSymbol("[N/A]")
|
||||
bn.SetTransactTime(time.Now())
|
||||
|
||||
return quickfix.SendToTarget(bn, sessionID)
|
||||
}
|
||||
|
||||
// handleQuoteRequest auto-responds to an incoming QuoteRequest with a QuoteStatusReport (acknowledge) followed by a Quote at price 99.6.
|
||||
func (m *Manager) handleQuoteRequest(msg quoterequest.QuoteRequest, sessionID quickfix.SessionID) {
|
||||
quoteReqID, err := msg.GetQuoteReqID()
|
||||
@ -207,11 +192,17 @@ func (m *Manager) handleQuoteRequest(msg quoterequest.QuoteRequest, sessionID qu
|
||||
return
|
||||
}
|
||||
|
||||
// Validate LST_ prefix for List Trading flow.
|
||||
if !strings.HasPrefix(quoteReqID, "LST_") {
|
||||
slog.Warn("handleQuoteRequest: QuoteReqID missing LST_ prefix, ignoring", "quoteReqID", quoteReqID)
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
symbol, currency, ownerTraderID, settlDate string
|
||||
side enum.Side
|
||||
secIDSource enum.SecurityIDSource
|
||||
orderQty decimal.Decimal
|
||||
symbol, currency, ownerTraderID, settlDate, listID, negotiationType string
|
||||
side enum.Side
|
||||
secIDSource enum.SecurityIDSource
|
||||
orderQty decimal.Decimal
|
||||
)
|
||||
|
||||
relatedSyms, relErr := msg.GetNoRelatedSym()
|
||||
@ -224,6 +215,18 @@ func (m *Manager) handleQuoteRequest(msg quoterequest.QuoteRequest, sessionID qu
|
||||
ownerTraderID, _ = sym.GetOwnerTraderID()
|
||||
orderQty, _ = sym.GetOrderQty()
|
||||
settlDate, _ = sym.GetSettlDate()
|
||||
listID, _ = sym.GetListID()
|
||||
negotiationType, _ = sym.GetNegotiationType()
|
||||
}
|
||||
|
||||
if listID == "" {
|
||||
slog.Warn("handleQuoteRequest: missing ListID", "quoteReqID", quoteReqID)
|
||||
return
|
||||
}
|
||||
|
||||
if negotiationType != "RFQ" {
|
||||
slog.Warn("handleQuoteRequest: unexpected NegotiationType", "quoteReqID", quoteReqID, "negotiationType", negotiationType)
|
||||
return
|
||||
}
|
||||
|
||||
// Step 1: Send QuoteStatusReport (35=AI) to acknowledge the inquiry.
|
||||
@ -241,7 +244,7 @@ func (m *Manager) handleQuoteRequest(msg quoterequest.QuoteRequest, sessionID qu
|
||||
sIDSource = enum.SecurityIDSource_CUSIP
|
||||
}
|
||||
|
||||
quoteID := fmt.Sprintf("Q-%d", time.Now().UnixMilli())
|
||||
quoteID := quoteReqID
|
||||
q := quote.New(
|
||||
field.NewQuoteID(quoteID),
|
||||
field.NewQuoteType(enum.QuoteType_SEND_QUOTE),
|
||||
@ -265,6 +268,8 @@ func (m *Manager) handleQuoteRequest(msg quoterequest.QuoteRequest, sessionID qu
|
||||
q.SetSettlDate(settlDate)
|
||||
}
|
||||
|
||||
q.SetPrice(price, 8)
|
||||
|
||||
if side == enum.Side_BUY {
|
||||
q.SetOfferPx(price, 8)
|
||||
q.SetSide(enum.Side_BUY)
|
||||
@ -285,4 +290,111 @@ func (m *Manager) handleQuoteRequest(msg quoterequest.QuoteRequest, sessionID qu
|
||||
}
|
||||
|
||||
slog.Info("Quote sent", "quoteReqID", quoteReqID, "quoteID", quoteID, "symbol", symbol)
|
||||
|
||||
// Store trade state for subsequent steps.
|
||||
m.tradesMu.Lock()
|
||||
m.trades[quoteReqID] = &listTrade{
|
||||
QuoteReqID: quoteReqID,
|
||||
ListID: listID,
|
||||
Symbol: symbol,
|
||||
SecurityIDSrc: sIDSource,
|
||||
Currency: currency,
|
||||
Side: side,
|
||||
OrderQty: orderQty,
|
||||
SettlDate: settlDate,
|
||||
Price: price,
|
||||
OwnerTraderID: ownerTraderID,
|
||||
SessionID: sessionID,
|
||||
}
|
||||
m.tradesMu.Unlock()
|
||||
}
|
||||
|
||||
// handleQuoteAck handles an incoming QuoteAck (35=CW).
|
||||
func (m *Manager) handleQuoteAck(msg quoteack.QuoteAck, sessionID quickfix.SessionID) {
|
||||
quoteReqID, _ := msg.GetQuoteReqID()
|
||||
status, _ := msg.GetQuoteAckStatus()
|
||||
|
||||
if status != enum.QuoteAckStatus_ACCEPTED {
|
||||
slog.Warn("handleQuoteAck: unexpected status", "quoteReqID", quoteReqID, "quoteAckStatus", status)
|
||||
}
|
||||
}
|
||||
|
||||
// handleQuoteResponse handles an incoming QuoteResponse (35=AJ).
|
||||
func (m *Manager) handleQuoteResponse(msg quoteresponse.QuoteResponse, sessionID quickfix.SessionID) {
|
||||
quoteReqID, _ := msg.GetQuoteReqID()
|
||||
quoteRespID, _ := msg.GetQuoteRespID()
|
||||
|
||||
slog.Info("handleQuoteResponse", "quoteReqID", quoteReqID, "quoteRespID", quoteRespID)
|
||||
|
||||
if !strings.HasSuffix(quoteRespID, "_TRDREQ") {
|
||||
slog.Info("handleQuoteResponse: QuoteRespID does not end with _TRDREQ, ignoring", "quoteRespID", quoteRespID)
|
||||
return
|
||||
}
|
||||
|
||||
m.tradesMu.RLock()
|
||||
_, ok := m.trades[quoteReqID]
|
||||
m.tradesMu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
slog.Warn("handleQuoteResponse: no trade found for QuoteReqID", "quoteReqID", quoteReqID)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: handle 694=2 (Counter) for flow 8.5 (Client Countering) in the future.
|
||||
|
||||
// Send QuoteStatusReport (35=AI) as TRDREQACK.
|
||||
if ackErr := m.sendTradeRequestAck(quoteReqID, quoteRespID, sessionID); ackErr != nil {
|
||||
slog.Error("handleQuoteResponse: failed to send TRDREQACK", "quoteReqID", quoteReqID, "error", ackErr.Error())
|
||||
return
|
||||
}
|
||||
slog.Info("TRDREQACK sent", "quoteReqID", quoteReqID, "quoteRespID", quoteRespID)
|
||||
}
|
||||
|
||||
// handleExecutionReport handles an incoming ExecutionReport (35=8).
|
||||
// In flow 8.4 (List Trading), the dealer NEVER sends an ExecutionReport — only TW does.
|
||||
func (m *Manager) handleExecutionReport(msg executionreport.ExecutionReport, sessionID quickfix.SessionID) {
|
||||
execID, _ := msg.GetExecID()
|
||||
orderID, _ := msg.GetOrderID()
|
||||
clOrdID, _ := msg.GetClOrdID()
|
||||
execType, _ := msg.GetExecType()
|
||||
ordStatus, _ := msg.GetOrdStatus()
|
||||
listID, _ := msg.GetListID()
|
||||
|
||||
slog.Info("handleExecutionReport received",
|
||||
"execID", execID, "orderID", orderID, "clOrdID", clOrdID,
|
||||
"execType", string(execType), "ordStatus", string(ordStatus), "listID", listID,
|
||||
)
|
||||
|
||||
// Send ExecutionAck (35=BN) for every incoming ExecutionReport from TW.
|
||||
if ackErr := m.sendExecutionAck(orderID, clOrdID, execID, sessionID); ackErr != nil {
|
||||
slog.Error("handleExecutionReport: failed to send ExecutionAck", "execID", execID, "error", ackErr.Error())
|
||||
} else {
|
||||
slog.Info("ExecutionAck sent", "execID", execID)
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.Contains(execID, "_LISTEND"):
|
||||
slog.Info("List ended (due-in closed), awaiting trade result from TW",
|
||||
"execID", execID, "clOrdID", clOrdID)
|
||||
|
||||
case strings.Contains(execID, "_TRDEND"):
|
||||
slog.Info("Trade ended, cleaning up", "execID", execID, "clOrdID", clOrdID)
|
||||
|
||||
m.tradesMu.Lock()
|
||||
delete(m.trades, clOrdID)
|
||||
m.tradesMu.Unlock()
|
||||
|
||||
case strings.Contains(execID, "_TRDSUMM"):
|
||||
slog.Info("Trade summary received from TW",
|
||||
"execID", execID, "clOrdID", clOrdID, "ordStatus", string(ordStatus), "listID", listID)
|
||||
|
||||
case execType == enum.ExecType_TRADE:
|
||||
slog.Info("Trade result received from TW",
|
||||
"execID", execID, "clOrdID", clOrdID, "ordStatus", string(ordStatus), "listID", listID)
|
||||
}
|
||||
}
|
||||
|
||||
// handleExecutionAck handles an incoming ExecutionAck (35=BN) from TW.
|
||||
func (m *Manager) handleExecutionAck(msg executionack.ExecutionAck, sessionID quickfix.SessionID) {
|
||||
// Logged in application.go, no further action needed.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user