Skip to content

Commit 35e4dba

Browse files
author
robaho
committed
fix issue #5 - update for fixed API changes
1 parent 0185799 commit 35e4dba

File tree

6 files changed

+28
-23
lines changed

6 files changed

+28
-23
lines changed

cmd/algo/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"github.com/robaho/fixed"
67
"log"
78
"time"
89

910
. "github.com/robaho/go-trader/pkg/common"
1011
"github.com/robaho/go-trader/pkg/connector"
11-
"github.com/shopspring/decimal"
1212
)
1313

1414
type algoState int
@@ -27,9 +27,9 @@ var exchange ExchangeConnector
2727
type MyAlgo struct {
2828
symbol string
2929
instrument Instrument
30-
entryPrice decimal.Decimal
31-
offset decimal.Decimal
32-
totalProfit decimal.Decimal
30+
entryPrice fixed.Fixed
31+
offset fixed.Fixed
32+
totalProfit fixed.Fixed
3333
state algoState
3434
runs int
3535
nextEntry time.Time
@@ -86,7 +86,7 @@ func (a *MyAlgo) OnFill(fill *Fill) {
8686
if a.state == waitSell {
8787
profit := fill.Price.Sub(a.entryPrice)
8888
fmt.Println("exited market at ", fill.Price)
89-
if profit.GreaterThan(decimal.Zero) {
89+
if profit.GreaterThan(fixed.ZERO) {
9090
fmt.Println("!!!! winner ", profit)
9191
} else {
9292
fmt.Println("____ loser ", profit)
@@ -118,7 +118,7 @@ func main() {
118118
flag.Parse()
119119

120120
callback.symbol = *symbol
121-
callback.offset = decimal.NewFromFloat(*offset)
121+
callback.offset = fixed.NewF(*offset)
122122

123123
p, err := NewProperties(*props)
124124
if err != nil {
@@ -133,7 +133,7 @@ func main() {
133133
panic("exchange is not connected")
134134
}
135135

136-
err := exchange.DownloadInstruments()
136+
err = exchange.DownloadInstruments()
137137
if err != nil {
138138
panic(err)
139139
}
@@ -148,7 +148,7 @@ func main() {
148148
for {
149149
time.Sleep(time.Duration(10) * time.Second)
150150
tp := callback.totalProfit
151-
if tp.LessThan(decimal.Zero) {
151+
if tp.LessThan(fixed.ZERO) {
152152
fmt.Println("<<<<< total profit", tp)
153153
} else {
154154
fmt.Println(">>>>> total profit", tp)

cmd/client/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ func (MyCallback) OnFill(fill *Fill) {
133133
color = gocui.ColorRed
134134
}
135135
if fill.IsQuote {
136-
vlogcf("fills", color, "quote fill on %s, %s %s @ %s\n", fill.Instrument.Symbol(), fill.Side, fill.Quantity.String(), fill.Price.StringFixed(2))
136+
vlogcf("fills", color, "quote fill on %s, %s %s @ %s\n", fill.Instrument.Symbol(), fill.Side, fill.Quantity.String(), fill.Price.StringN(2))
137137
} else {
138-
vlogcf("fills", color, "order %d fill on %s, %s %s @ %s\n", fill.Order.Id, fill.Instrument.Symbol(), fill.Side, fill.Quantity.String(), fill.Price.StringFixed(2))
138+
vlogcf("fills", color, "order %d fill on %s, %s %s @ %s\n", fill.Order.Id, fill.Instrument.Symbol(), fill.Side, fill.Quantity.String(), fill.Price.StringN(2))
139139
}
140140
}
141141

cmd/marketmaker/main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"math/rand"
88
"os"
99
"runtime/pprof"
10-
"sync"
1110
"sync/atomic"
1211
"time"
1312

@@ -17,13 +16,13 @@ import (
1716
)
1817

1918
type MyCallback struct {
20-
wg sync.WaitGroup
19+
ch chan bool
2120
symbol string
2221
}
2322

2423
func (cb *MyCallback) OnBook(book *Book) {
2524
if book.Instrument.Symbol() == cb.symbol {
26-
cb.wg.Done()
25+
cb.ch <- true
2726
}
2827
}
2928

@@ -42,7 +41,7 @@ func (*MyCallback) OnTrade(trade *Trade) {
4241
}
4342

4443
func main() {
45-
var callback = MyCallback{}
44+
var callback = MyCallback{make(chan bool, 128), ""}
4645

4746
symbol := flag.String("symbol", "IBM", "set the symbol")
4847
fix := flag.String("fix", "configs/qf_mm1_settings", "set the fix session file")
@@ -139,12 +138,15 @@ func main() {
139138
if bidPrice.Equal(askPrice) {
140139
panic("bid price equals ask price")
141140
}
142-
callback.wg.Add(1)
143141
err := exchange.Quote(instrument, bidPrice, bidQty, askPrice, askQty)
144142
if err != nil {
145143
log.Fatal("unable to submit quote", err)
146144
}
147-
callback.wg.Wait()
145+
<-callback.ch
146+
// drain channel
147+
for len(callback.ch) > 0 {
148+
<-callback.ch
149+
}
148150
}
149151
h.Add(float64(time.Now().Sub(now).Nanoseconds()))
150152
if *delay != 0 {

pkg/common/exchange.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package common
33
import (
44
"errors"
55
. "github.com/robaho/fixed"
6-
"github.com/shopspring/decimal"
76
"time"
87
)
98

@@ -35,8 +34,8 @@ type Fill struct {
3534
// Order will be nil on quote trade, the order is unlocked
3635
Order *Order
3736
ExchangeID string
38-
Quantity decimal.Decimal
39-
Price decimal.Decimal
37+
Quantity Fixed
38+
Price Fixed
4039
Side Side
4140
IsLegTrade bool
4241
}

pkg/connector/grpc/connector.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
. "github.com/robaho/fixed"
66
. "github.com/robaho/go-trader/pkg/common"
77
"github.com/robaho/go-trader/pkg/protocol"
8-
"github.com/shopspring/decimal"
98
"google.golang.org/grpc"
109
"io"
1110
"log"
@@ -326,8 +325,8 @@ func (c *grpcConnector) handleExecutionReport(rpt *protocol.ExecutionReport) {
326325
}
327326

328327
if rpt.ReportType == protocol.ExecutionReport_Fill {
329-
lastPx := decimal.NewFromFloat(rpt.LastPrice)
330-
lastQty := decimal.NewFromFloat(rpt.LastQuantity)
328+
lastPx := NewF(rpt.LastPrice)
329+
lastQty := NewF(rpt.LastQuantity)
331330

332331
var side Side
333332
if rpt.Side == protocol.CreateOrderRequest_Buy {

pkg/connector/qfix/qfixapp.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package qfix
33
import (
44
"fmt"
55
"github.com/quickfixgo/fix44/securitydefinition"
6+
"github.com/robaho/fixed"
67
"strings"
78

89
"github.com/quickfixgo/enum"
@@ -168,10 +169,14 @@ func (app *myApplication) onExecutionReport(msg executionreport.ExecutionReport,
168169
return err
169170
}
170171
lastQty, err := msg.GetLastQty()
172+
173+
lastPxF, _ := lastPx.Float64()
174+
lastQtyF, _ := lastQty.Float64()
175+
171176
if err != nil {
172177
return err
173178
}
174-
fill := &Fill{instrument, id == 0, order, exchangeId, lastQty, lastPx, MapFromFixSide(side), false}
179+
fill := &Fill{instrument, id == 0, order, exchangeId, fixed.NewF(lastQtyF), fixed.NewF(lastPxF), MapFromFixSide(side), false}
175180
app.c.callback.OnFill(fill)
176181
}
177182

0 commit comments

Comments
 (0)