Skip to content

Commit

Permalink
Improves logging and docs/info
Browse files Browse the repository at this point in the history
  • Loading branch information
ackleymi committed Jan 25, 2023
1 parent 3d4cd55 commit 3d5e97e
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 83 deletions.
4 changes: 3 additions & 1 deletion cmd/executor/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Executor
Executor is a FIX acceptor service that fills every limit order it receives.
Executor is a FIX acceptor service that fills every limit order it receives.

(Note: it will reject any non-limit order type)

## Features
* Accept any canonical `NewOrderSingle` message for an instrument, with the instrument symbol consisting of an arbitrary string
Expand Down
56 changes: 25 additions & 31 deletions cmd/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
package executor

import (
"bufio"
"bytes"
"fmt"
"io"
"path"
"syscall"

"github.com/fatih/color"
"github.com/quickfixgo/enum"
"github.com/quickfixgo/examples/cmd/utils"
"github.com/quickfixgo/field"
"github.com/quickfixgo/tag"
"github.com/shopspring/decimal"
Expand Down Expand Up @@ -101,6 +100,7 @@ func (e *executor) OnFIX40NewOrderSingle(msg fix40nos.NewOrderSingle, sessionID
}

if ordType != enum.OrdType_LIMIT {
utils.PrintBad("incoming order was not a limit order and was rejected")
return quickfix.ValueIsIncorrect(tag.OrdType)
}

Expand Down Expand Up @@ -146,7 +146,7 @@ func (e *executor) OnFIX40NewOrderSingle(msg fix40nos.NewOrderSingle, sessionID

sendErr := quickfix.SendToTarget(execReport, sessionID)
if sendErr != nil {
fmt.Println(sendErr)
utils.PrintBad(sendErr.Error())
}

return nil
Expand All @@ -158,6 +158,7 @@ func (e *executor) OnFIX41NewOrderSingle(msg fix41nos.NewOrderSingle, sessionID
return
}
if ordType != enum.OrdType_LIMIT {
utils.PrintBad("incoming order was not a limit order and was rejected")
return quickfix.ValueIsIncorrect(tag.OrdType)
}

Expand Down Expand Up @@ -205,7 +206,7 @@ func (e *executor) OnFIX41NewOrderSingle(msg fix41nos.NewOrderSingle, sessionID

sendErr := quickfix.SendToTarget(execReport, sessionID)
if sendErr != nil {
fmt.Println(sendErr)
utils.PrintBad(sendErr.Error())
}
return
}
Expand All @@ -217,6 +218,7 @@ func (e *executor) OnFIX42NewOrderSingle(msg fix42nos.NewOrderSingle, sessionID
}

if ordType != enum.OrdType_LIMIT {
utils.PrintBad("incoming order was not a limit order and was rejected")
return quickfix.ValueIsIncorrect(tag.OrdType)
}

Expand Down Expand Up @@ -273,7 +275,7 @@ func (e *executor) OnFIX42NewOrderSingle(msg fix42nos.NewOrderSingle, sessionID

sendErr := quickfix.SendToTarget(execReport, sessionID)
if sendErr != nil {
fmt.Println(sendErr)
utils.PrintBad(sendErr.Error())
}

return
Expand All @@ -285,6 +287,7 @@ func (e *executor) OnFIX43NewOrderSingle(msg fix43nos.NewOrderSingle, sessionID
return err
}
if ordType != enum.OrdType_LIMIT {
utils.PrintBad("incoming order was not a limit order and was rejected")
return quickfix.ValueIsIncorrect(tag.OrdType)
}

Expand Down Expand Up @@ -340,7 +343,7 @@ func (e *executor) OnFIX43NewOrderSingle(msg fix43nos.NewOrderSingle, sessionID

sendErr := quickfix.SendToTarget(execReport, sessionID)
if sendErr != nil {
fmt.Println(sendErr)
utils.PrintBad(sendErr.Error())
}

return
Expand All @@ -353,6 +356,7 @@ func (e *executor) OnFIX44NewOrderSingle(msg fix44nos.NewOrderSingle, sessionID
}

if ordType != enum.OrdType_LIMIT {
utils.PrintBad("incoming order was not a limit order and was rejected")
return quickfix.ValueIsIncorrect(tag.OrdType)
}

Expand Down Expand Up @@ -408,7 +412,7 @@ func (e *executor) OnFIX44NewOrderSingle(msg fix44nos.NewOrderSingle, sessionID

sendErr := quickfix.SendToTarget(execReport, sessionID)
if sendErr != nil {
fmt.Println(sendErr)
utils.PrintBad(sendErr.Error())
}

return
Expand All @@ -421,6 +425,7 @@ func (e *executor) OnFIX50NewOrderSingle(msg fix50nos.NewOrderSingle, sessionID
}

if ordType != enum.OrdType_LIMIT {
utils.PrintBad("incoming order was not a limit order and was rejected")
return quickfix.ValueIsIncorrect(tag.OrdType)
}

Expand Down Expand Up @@ -476,26 +481,26 @@ func (e *executor) OnFIX50NewOrderSingle(msg fix50nos.NewOrderSingle, sessionID

sendErr := quickfix.SendToTarget(execReport, sessionID)
if sendErr != nil {
fmt.Println(sendErr)
utils.PrintBad(sendErr.Error())
}

return
}

const (
usage = "executor"
short = "Start an executor"
long = "Start an executor."
short = "Start an order execution (FIX acceptor) service"
long = "Start an order execution (FIX acceptor) service."
)

var (
// Cmd is the quote command.
// Cmd is the executor command.
Cmd = &cobra.Command{
Use: usage,
Short: short,
Long: long,
Aliases: []string{"x"},
Example: "qf ordermatch config/executor.cfg",
Example: "qf executor [YOUR_FIX_CONFIG_FILE_HERE.cfg] (default is ./config/executor.cfg)",
RunE: execute,
}
)
Expand All @@ -506,6 +511,8 @@ func execute(cmd *cobra.Command, args []string) error {
switch argLen {
case 0:
{
utils.PrintInfo("FIX config file not provided...")
utils.PrintInfo("attempting to use default location './config/executor.cfg' ...")
cfgFileName = path.Join("config", "executor.cfg")
}
case 1:
Expand All @@ -532,40 +539,27 @@ func execute(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error reading cfg: %s,", err)
}

logFactory := quickfix.NewScreenLogFactory()
logger := utils.NewFancyLog()
app := newExecutor()

printConfig(bytes.NewReader(stringData))
acceptor, err := quickfix.NewAcceptor(app, quickfix.NewMemoryStoreFactory(), appSettings, logFactory)
utils.PrintConfig("acceptor", bytes.NewReader(stringData))
acceptor, err := quickfix.NewAcceptor(app, quickfix.NewMemoryStoreFactory(), appSettings, logger)
if err != nil {
return fmt.Errorf("unable to create acceptor: %s", err)
}

err = acceptor.Start()
if err != nil {
return fmt.Errorf("unable to start acceptor: %s", err)
return fmt.Errorf("unable to start FIX acceptor: %s", err)
}

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
<-interrupt

utils.PrintInfo("stopping FIX acceptor service..")
acceptor.Stop()
utils.PrintInfo("stopped")

return nil
}

func printConfig(reader io.Reader) {
scanner := bufio.NewScanner(reader)
color.Set(color.Bold)
fmt.Println("Starting FIX acceptor with config:")
color.Unset()

color.Set(color.FgHiMagenta)
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
}

color.Unset()
}
2 changes: 1 addition & 1 deletion cmd/ordermatch/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Ordermatch
Ordermatch is a simple matching engine (A set of orderbooks) with a FIX acceptor serving as the point of ingress.
Ordermatch is a simple matching engine (A set of orderbooks) with a FIX acceptor service as the point of ingress.

## Features
* Accept any canonical `NewOrderSingle` message for an instrument, with the instrument symbol consisting of an arbitrary string
Expand Down
33 changes: 10 additions & 23 deletions cmd/ordermatch/ordermatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"strconv"
"syscall"

"github.com/fatih/color"
"github.com/quickfixgo/enum"
"github.com/quickfixgo/examples/cmd/ordermatch/internal"
"github.com/quickfixgo/examples/cmd/utils"
"github.com/quickfixgo/field"
"github.com/quickfixgo/fix42/executionreport"
"github.com/quickfixgo/fix42/marketdatarequest"
Expand Down Expand Up @@ -234,8 +234,8 @@ func (a *Application) updateOrder(order internal.Order, status enum.OrdStatus) {

const (
usage = "ordermatch"
short = "Start an ordermatcher"
long = "Start an ordermatcher."
short = "Start an order matching (FIX acceptor) service"
long = "Start an order matching (FIX acceptor) service."
)

var (
Expand All @@ -245,7 +245,7 @@ var (
Short: short,
Long: long,
Aliases: []string{"oms"},
Example: "qf ordermatch config/ordermatch.cfg",
Example: "qf ordermatch [YOUR_FIX_CONFIG_FILE_HERE.cfg] (default is ./config/ordermatch.cfg)",
RunE: execute,
}
)
Expand All @@ -256,6 +256,8 @@ func execute(cmd *cobra.Command, args []string) error {
switch argLen {
case 0:
{
utils.PrintInfo("FIX config file not provided...")
utils.PrintInfo("attempting to use default location './config/ordermatch.cfg' ...")
cfgFileName = path.Join("config", "ordermatch.cfg")
}
case 1:
Expand Down Expand Up @@ -283,18 +285,18 @@ func execute(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error reading cfg: %s,", err)
}

logFactory := quickfix.NewScreenLogFactory()
logger := utils.NewFancyLog()
app := newApplication()

printConfig(bytes.NewReader(stringData))
acceptor, err := quickfix.NewAcceptor(app, quickfix.NewMemoryStoreFactory(), appSettings, logFactory)
utils.PrintConfig("acceptor", bytes.NewReader(stringData))
acceptor, err := quickfix.NewAcceptor(app, quickfix.NewMemoryStoreFactory(), appSettings, logger)
if err != nil {
return fmt.Errorf("unable to create acceptor: %s", err)
}

err = acceptor.Start()
if err != nil {
return fmt.Errorf("unable to start acceptor: %s", err)
return fmt.Errorf("unable to start FIX acceptor: %s", err)
}

interrupt := make(chan os.Signal, 1)
Expand All @@ -317,18 +319,3 @@ func execute(cmd *cobra.Command, args []string) error {
}
}
}

func printConfig(reader io.Reader) {
scanner := bufio.NewScanner(reader)
color.Set(color.Bold)
fmt.Println("Starting FIX acceptor with config:")
color.Unset()

color.Set(color.FgHiMagenta)
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
}

color.Unset()
}
36 changes: 12 additions & 24 deletions cmd/tradeclient/tradeclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
package tradeclient

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path"

"github.com/fatih/color"
"github.com/quickfixgo/examples/cmd/tradeclient/internal"
"github.com/quickfixgo/examples/cmd/utils"
"github.com/spf13/cobra"

"github.com/quickfixgo/quickfix"
Expand Down Expand Up @@ -53,20 +52,20 @@ func (e TradeClient) ToAdmin(msg *quickfix.Message, sessionID quickfix.SessionID

// ToApp implemented as part of Application interface
func (e TradeClient) ToApp(msg *quickfix.Message, sessionID quickfix.SessionID) (err error) {
fmt.Printf("Sending %s\n", msg)
utils.PrintInfo(fmt.Sprintf("Sending: %s", msg.String()))
return
}

// FromApp implemented as part of Application interface. This is the callback for all Application level messages from the counter party.
func (e TradeClient) FromApp(msg *quickfix.Message, sessionID quickfix.SessionID) (reject quickfix.MessageRejectError) {
fmt.Printf("FromApp: %s\n", msg.String())
utils.PrintInfo(fmt.Sprintf("FromApp: %s", msg.String()))
return
}

const (
usage = "tradeclient"
short = "Start a tradeclient"
long = "Start a tradeclient."
short = "Start a tradeclient (FIX initiator) cli trading agent"
long = "Start a tradeclient (FIX initiator) cli trading agent."
)

var (
Expand All @@ -76,7 +75,7 @@ var (
Short: short,
Long: long,
Aliases: []string{"tc"},
Example: "qf tradeclient config/tradeclient.cfg",
Example: "qf tradeclient [YOUR_FIX_CONFIG_FILE_HERE.cfg] (default is ./config/tradeclient.cfg)",
RunE: execute,
}
)
Expand All @@ -87,6 +86,8 @@ func execute(cmd *cobra.Command, args []string) error {
switch argLen {
case 0:
{
utils.PrintInfo("FIX config file not provided...")
utils.PrintInfo("attempting to use default location './config/tradeclient.cfg' ...")
cfgFileName = path.Join("config", "tradeclient.cfg")
}
case 1:
Expand Down Expand Up @@ -132,7 +133,7 @@ func execute(cmd *cobra.Command, args []string) error {
return fmt.Errorf("unable to start initiator: %s", err)
}

printConfig(bytes.NewReader(stringData))
utils.PrintConfig("initiator", bytes.NewReader(stringData))

Loop:
for {
Expand Down Expand Up @@ -160,25 +161,12 @@ Loop:
}

if err != nil {
fmt.Printf("%v\n", err)
utils.PrintBad(err.Error())
}
}

utils.PrintInfo("stopping FIX initiator ..")
initiator.Stop()
utils.PrintInfo("stopped")
return nil
}

func printConfig(reader io.Reader) {
scanner := bufio.NewScanner(reader)
color.Set(color.Bold)
fmt.Println("Started FIX initiator with config:")
color.Unset()

color.Set(color.FgHiMagenta)
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
}

color.Unset()
}
Loading

0 comments on commit 3d5e97e

Please sign in to comment.