changing to initiator
This commit is contained in:
@ -3,7 +3,6 @@ package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
@ -38,17 +37,17 @@ type EnvironmentType int //nolint:recvcheck // The methods of this are autogener
|
||||
var environment EnvironmentType //nolint:gochecknoglobals // Just keept this global to avoid having to create an instance
|
||||
|
||||
func init() {
|
||||
aux := os.Getenv(quantexEnvironment)
|
||||
if aux == "" {
|
||||
panic("QUANTEX_ENVIRONMENT is not set")
|
||||
}
|
||||
// aux := os.Getenv(EnvironmentTypeDev)
|
||||
// if aux == "" {
|
||||
// panic("QUANTEX_ENVIRONMENT is not set")
|
||||
// }
|
||||
|
||||
env, err := ParseEnvironmentType(aux)
|
||||
if err != nil {
|
||||
panic("Invalid QUANTEX_ENVIRONMENT value: " + aux + " " + err.Error())
|
||||
}
|
||||
// env, err := ParseEnvironmentType(aux)
|
||||
// if err != nil {
|
||||
// panic("Invalid QUANTEX_ENVIRONMENT value: " + aux + " " + err.Error())
|
||||
// }
|
||||
|
||||
environment = env
|
||||
environment = EnvironmentTypeDev
|
||||
}
|
||||
|
||||
// Base returns the version base name
|
||||
|
||||
@ -1,29 +1,26 @@
|
||||
// Package fix implements the QuickFIX acceptor application.
|
||||
// Package fix implements the QuickFIX initiator application.
|
||||
package fix
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"quantex.com/qfixdpl/quickfix"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/newordersingle"
|
||||
"quantex.com/qfixdpl/src/domain"
|
||||
"quantex.com/qfixdpl/quickfix/gen/fix50sp2/quote"
|
||||
)
|
||||
|
||||
type application struct {
|
||||
router *quickfix.MessageRouter
|
||||
orderStore domain.OrderStore
|
||||
onLogon func(quickfix.SessionID)
|
||||
onLogout func(quickfix.SessionID)
|
||||
router *quickfix.MessageRouter
|
||||
onLogon func(quickfix.SessionID)
|
||||
onLogout func(quickfix.SessionID)
|
||||
onQuote func(quote.Quote, quickfix.SessionID)
|
||||
}
|
||||
|
||||
func newApplication(orderStore domain.OrderStore) *application {
|
||||
func newApplication() *application {
|
||||
app := &application{
|
||||
router: quickfix.NewMessageRouter(),
|
||||
orderStore: orderStore,
|
||||
router: quickfix.NewMessageRouter(),
|
||||
}
|
||||
|
||||
app.router.AddRoute(newordersingle.Route(app.handleNewOrderSingle))
|
||||
app.router.AddRoute(quote.Route(app.handleQuote))
|
||||
|
||||
return app
|
||||
}
|
||||
@ -33,14 +30,14 @@ func (a *application) OnCreate(sessionID quickfix.SessionID) {
|
||||
}
|
||||
|
||||
func (a *application) OnLogon(sessionID quickfix.SessionID) {
|
||||
slog.Info("FIX client logged on", "session", sessionID.String())
|
||||
slog.Info("FIX session logged on", "session", sessionID.String())
|
||||
if a.onLogon != nil {
|
||||
a.onLogon(sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *application) OnLogout(sessionID quickfix.SessionID) {
|
||||
slog.Info("FIX client logged out", "session", sessionID.String())
|
||||
slog.Info("FIX session logged out", "session", sessionID.String())
|
||||
if a.onLogout != nil {
|
||||
a.onLogout(sessionID)
|
||||
}
|
||||
@ -58,48 +55,19 @@ func (a *application) FromApp(msg *quickfix.Message, sessionID quickfix.SessionI
|
||||
return a.router.Route(msg, sessionID)
|
||||
}
|
||||
|
||||
func (a *application) handleNewOrderSingle(msg newordersingle.NewOrderSingle, sessionID quickfix.SessionID) quickfix.MessageRejectError {
|
||||
clOrdID, err := msg.GetClOrdID()
|
||||
func (a *application) handleQuote(msg quote.Quote, sessionID quickfix.SessionID) quickfix.MessageRejectError {
|
||||
quoteID, err := msg.GetQuoteID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
symbol, err := msg.GetSymbol()
|
||||
if err != nil {
|
||||
return err
|
||||
symbol, _ := msg.GetSymbol()
|
||||
|
||||
slog.Info("Quote received", "quoteID", quoteID, "symbol", symbol, "session", sessionID.String())
|
||||
|
||||
if a.onQuote != nil {
|
||||
a.onQuote(msg, sessionID)
|
||||
}
|
||||
|
||||
side, err := msg.GetSide()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ordType, err := msg.GetOrdType()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
orderQty, err := msg.GetOrderQty()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
price, _ := msg.GetPrice() // Price is optional for some OrdTypes
|
||||
|
||||
order := domain.Order{
|
||||
ClOrdID: clOrdID,
|
||||
Symbol: symbol,
|
||||
Side: string(side),
|
||||
OrdType: string(ordType),
|
||||
OrderQty: orderQty,
|
||||
Price: price,
|
||||
SessionID: sessionID.String(),
|
||||
ReceivedAt: time.Now(),
|
||||
}
|
||||
|
||||
a.orderStore.SaveOrder(order)
|
||||
|
||||
slog.Info("NewOrderSingle received", "clOrdID", clOrdID, "symbol", symbol, "side", order.Side)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -17,9 +17,9 @@ import (
|
||||
"quantex.com/qfixdpl/src/domain"
|
||||
)
|
||||
|
||||
// Manager wraps the QuickFIX acceptor and implements domain.FIXSender.
|
||||
// Manager wraps the QuickFIX initiator and implements domain.FIXSender.
|
||||
type Manager struct {
|
||||
acceptor *quickfix.Acceptor
|
||||
initiator *quickfix.Initiator
|
||||
app *application
|
||||
sessionsMu sync.RWMutex
|
||||
sessions map[string]quickfix.SessionID
|
||||
@ -38,7 +38,7 @@ func NewManager(cfg app.FIXConfig, orderStore domain.OrderStore, notify domain.N
|
||||
}
|
||||
|
||||
func (m *Manager) Start() error {
|
||||
fixApp := newApplication(m.orderStore)
|
||||
fixApp := newApplication()
|
||||
fixApp.onLogon = m.onLogon
|
||||
fixApp.onLogout = m.onLogout
|
||||
m.app = fixApp
|
||||
@ -57,26 +57,26 @@ func (m *Manager) Start() error {
|
||||
storeFactory := quickfix.NewMemoryStoreFactory()
|
||||
logFactory := quickfix.NewNullLogFactory()
|
||||
|
||||
acceptor, err := quickfix.NewAcceptor(fixApp, storeFactory, settings, logFactory)
|
||||
initiator, err := quickfix.NewInitiator(fixApp, storeFactory, settings, logFactory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating FIX acceptor: %w", err)
|
||||
return fmt.Errorf("creating FIX initiator: %w", err)
|
||||
}
|
||||
|
||||
m.acceptor = acceptor
|
||||
m.initiator = initiator
|
||||
|
||||
if err = m.acceptor.Start(); err != nil {
|
||||
return fmt.Errorf("starting FIX acceptor: %w", err)
|
||||
if err = m.initiator.Start(); err != nil {
|
||||
return fmt.Errorf("starting FIX initiator: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("FIX acceptor started", "settings", m.cfg.SettingsFile)
|
||||
slog.Info("FIX initiator started", "settings", m.cfg.SettingsFile)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) Stop() {
|
||||
if m.acceptor != nil {
|
||||
m.acceptor.Stop()
|
||||
slog.Info("FIX acceptor stopped")
|
||||
if m.initiator != nil {
|
||||
m.initiator.Stop()
|
||||
slog.Info("FIX initiator stopped")
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,17 +94,18 @@ func (m *Manager) onLogout(sessionID quickfix.SessionID) {
|
||||
|
||||
// SendQuote implements domain.FIXSender.
|
||||
func (m *Manager) SendQuote(clOrdID, quoteID, symbol, currency string, bidPx, offerPx, bidSize, offerSize decimal.Decimal) error {
|
||||
order, ok := m.orderStore.GetOrderByClOrdID(clOrdID)
|
||||
if !ok {
|
||||
return fmt.Errorf("order not found: %s", clOrdID)
|
||||
}
|
||||
|
||||
m.sessionsMu.RLock()
|
||||
sessionID, ok := m.sessions[order.SessionID]
|
||||
var sessionID quickfix.SessionID
|
||||
var ok bool
|
||||
for _, sid := range m.sessions {
|
||||
sessionID = sid
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
m.sessionsMu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("session not active for order %s (session %s)", clOrdID, order.SessionID)
|
||||
return fmt.Errorf("no active FIX session")
|
||||
}
|
||||
|
||||
q := quote.New(
|
||||
|
||||
Reference in New Issue
Block a user