/
gdax.go
306 lines (259 loc) · 8.38 KB
/
gdax.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package vespyr
import (
"context"
"time"
"strings"
coinbase "github.com/DavidHuie/go-coinbase-exchange"
"github.com/gorilla/websocket"
"github.com/jonboulle/clockwork"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
)
const (
gdaxOrderMarket = "market"
gdaxRetries = 20
)
// GDAXClient is the interface for an underlying GDAX client.
type GDAXClient interface {
GetHistoricRates(product string, p ...coinbase.GetHistoricRatesParams) ([]coinbase.HistoricRate, error)
CreateOrder(*coinbase.Order) (coinbase.Order, error)
GetOrder(string) (coinbase.Order, error)
}
// GDAXExchange is a client to the GDAX cryptocurrency exchange.
type GDAXExchange struct {
client GDAXClient
clock clockwork.Clock
}
// NewGDAXExchange returns a new instance of GDAXExchange.
func NewGDAXExchange(client GDAXClient, clock clockwork.Clock) *GDAXExchange {
return &GDAXExchange{
client: client,
clock: clock,
}
}
func (k *GDAXExchange) EmitsFullCandlesticks() bool {
return false
}
// GetMessageChan returns a channel that emits exchange messages.
func (g *GDAXExchange) GetMessageChan(ctx context.Context, product Product) (<-chan *ExchangeMessage, error) {
var wsDialer websocket.Dialer
wsConn, _, err := wsDialer.Dial("wss://ws-feed.gdax.com", nil)
if err != nil {
return nil, errors.Wrapf(err, "error opening websocket connection to GDAX")
}
subscribe := map[string]string{
"type": "subscribe",
"product_id": string(product),
}
if err := wsConn.WriteJSON(subscribe); err != nil {
return nil, errors.Wrapf(err, "error writing to websocket")
}
c := make(chan *ExchangeMessage)
go func() {
for {
select {
case <-ctx.Done():
wsConn.Close()
return
default:
}
var message coinbase.Message
if err := wsConn.ReadJSON(&message); err != nil {
logrus.WithError(err).Errorf("error reading from websocket")
close(c)
return
}
exchangeMessage := &ExchangeMessage{
Price: message.Price,
ProductType: message.ProductId,
Size: message.Size,
Type: message.Type,
Time: message.Time.Time(),
}
c <- exchangeMessage
}
}()
return c, nil
}
func (g *GDAXExchange) StreamCandlesticks(ctx context.Context, product Product) (<-chan *CandlestickModel, error) {
panic("not implemented")
}
// GetCandlesticks returns candlesticks for a specified period and
// granularity from GDAX.
func (g *GDAXExchange) GetCandlesticks(product Product, start, end time.Time, granularitySeconds int) ([]*CandlestickModel, error) {
intervals := start.Sub(end).Seconds() / float64(granularitySeconds)
if intervals >= 200 {
return nil, errors.New("error: too many candlesticks requested of GDAX")
}
historicRates, err := g.client.GetHistoricRates(string(product), coinbase.GetHistoricRatesParams{
Start: start,
End: end,
Granularity: granularitySeconds,
})
if err != nil {
return nil, errors.Wrapf(err, "error fetching historic rates from GDAX")
}
var candles []*CandlestickModel
for _, h := range historicRates {
direction := CandlestickDirectionUp
if h.Close < h.Open {
direction = CandlestickDirectionDown
}
candles = append(candles, &CandlestickModel{
StartTime: h.Time,
EndTime: h.Time.Add(time.Second * time.Duration(granularitySeconds)),
Low: h.Low,
High: h.High,
Open: h.Open,
Close: h.Close,
Volume: h.Volume,
Direction: direction,
Product: product,
})
}
return candles, nil
}
// CreateMarketOrder creates a market order on GDAX.
func (g *GDAXExchange) CreateMarketOrder(args *MarketOrder) (*CreateMarketOrderResponse, error) {
order := &coinbase.Order{
Type: gdaxOrderMarket,
Side: args.Side,
ProductId: string(args.Product),
}
response := &CreateMarketOrderResponse{}
if args.Side == OrderBuy {
switch args.Product {
case ProductBTCUSD:
order.Funds = args.Cost
response.FilledSizeCurrency = CurrencyBTC
response.FeesCurrency = CurrencyUSD
case ProductETHUSD:
order.Funds = args.Cost
response.FilledSizeCurrency = CurrencyETH
response.FeesCurrency = CurrencyUSD
case ProductLTCUSD:
order.Funds = args.Cost
response.FilledSizeCurrency = CurrencyLTC
response.FeesCurrency = CurrencyUSD
default:
return nil, errors.Errorf("error: unrecognized product type: %s", args.Product)
}
logrus.Debugf("creating GDAX %s market order of size %f for %s", order.Side,
args.Cost, args.Product)
} else if args.Side == OrderSell {
switch args.Product {
case ProductBTCUSD:
order.Size = args.Cost
response.FilledSizeCurrency = CurrencyUSD
response.FeesCurrency = CurrencyUSD
case ProductETHUSD:
order.Size = args.Cost
response.FilledSizeCurrency = CurrencyUSD
response.FeesCurrency = CurrencyUSD
case ProductLTCUSD:
order.Size = args.Cost
response.FilledSizeCurrency = CurrencyUSD
response.FeesCurrency = CurrencyUSD
default:
return nil, errors.Errorf("error: unrecognized product type: %s", args.Product)
}
logrus.Debugf("creating GDAX %s market order of size %f for %s", order.Side,
args.Cost, args.Product)
}
logrus.Debugf("GDAX create market order args: %#v", order)
gdaxResponse, err := g.client.CreateOrder(order)
if err != nil {
return nil, errors.Wrapf(err, "error creating GDAX order")
}
logrus.Debugf("GDAX create market order response: %#v", gdaxResponse)
finalizedOrder := false
orderID := gdaxResponse.Id
RetryLoop:
for i := 0; i < gdaxRetries; i++ {
gdaxResponse, err = g.client.GetOrder(orderID)
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "rate limit") {
logrus.Warnf("error while getting GDAX order status, retrying: %s", err)
g.clock.Sleep(time.Second << uint(i))
continue
}
return nil, errors.Wrapf(err, "error fetching GDAX order")
}
logrus.Debugf("GDAX get market order response: %#v", gdaxResponse)
switch gdaxResponse.Status {
case "open", "pending", "active":
logrus.Debugf("GDAX market order not filled yet, status: %s", gdaxResponse.Status)
g.clock.Sleep(time.Second << uint(i))
continue
case "done":
if gdaxResponse.Settled {
finalizedOrder = true
break RetryLoop
}
logrus.Debugf("GDAX market order done but not settled, retrying")
g.clock.Sleep(time.Second << uint(i))
continue
default:
return nil, errors.Errorf("error: unknown GDAX order status: %s", gdaxResponse.Status)
}
}
if !finalizedOrder {
return nil, errors.Errorf("error: could not get market order status after retrying")
}
response.ExchangeID = gdaxResponse.Id
response.Fees = gdaxResponse.FillFees
if args.Side == OrderBuy {
response.FilledSize = gdaxResponse.FilledSize
} else {
response.FilledSize = gdaxResponse.ExecutedValue - gdaxResponse.FillFees
}
return response, nil
}
// FakeGDAXExchange is a GDAX implementation that places mock trades.
type FakeGDAXExchange struct {
GDAXExchange
}
// NewFakeGDAXExchange returns a new instance of FakeGDAXExchange.
func NewFakeGDAXExchange(clock clockwork.Clock) *FakeGDAXExchange {
return &FakeGDAXExchange{
GDAXExchange: *NewGDAXExchange(nil, clock),
}
}
// CreateMarketOrder mocks a market order at the last market price.
func (f *FakeGDAXExchange) CreateMarketOrder(args *MarketOrder) (*CreateMarketOrderResponse, error) {
ctx := context.Background()
messageChan, err := f.GetMessageChan(ctx, args.Product)
if err != nil {
return nil, errors.Wrapf(err, "error getting GDAX message channel")
}
var message *ExchangeMessage
for {
message = <-messageChan
if message.Type == string(MessageMatch) && string(args.Product) == message.ProductType {
break
}
}
price := message.Price
if args.Side == OrderBuy {
total := (1 - .0025) * args.Cost / price
fees := .0025 * args.Cost
return &CreateMarketOrderResponse{
ExchangeID: uuid.NewV4().String(),
FilledSize: total,
FilledSizeCurrency: ProductToMetadata[Product(args.Product)].MarketOrderSellCurrency,
Fees: fees,
FeesCurrency: ProductToMetadata[Product(args.Product)].MarketOrderFeesCurrency,
}, nil
}
// Sell
total := args.Cost * price * (1 - .0025)
fees := .0025 * args.Cost * price
return &CreateMarketOrderResponse{
ExchangeID: uuid.NewV4().String(),
FilledSize: total,
FilledSizeCurrency: ProductToMetadata[Product(args.Product)].MarketOrderBuyCurrency,
Fees: fees,
FeesCurrency: ProductToMetadata[Product(args.Product)].MarketOrderFeesCurrency,
}, nil
}