-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
246 lines (214 loc) · 5.09 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"time"
"github.com/google/uuid"
"github.com/young-steveo/godax/gdax"
"github.com/young-steveo/godax/market"
"github.com/buger/jsonparser"
"github.com/joho/godotenv"
)
func main() {
var leftTicker string
var rightTicker string
flag.StringVar(&leftTicker, `left`, `LTC`, `Left side of pair, e.g. LTC`)
flag.StringVar(&rightTicker, `right`, `BTC`, `Right side of pair, e.g. BTC`)
flag.Parse()
productID := market.GetProductID(leftTicker, rightTicker)
log.SetOutput(os.Stdout)
log.Println(`¡Hola! Let's make some trades`)
log.Println(`===`)
log.Println(`Bootstrapping environment`)
err := godotenv.Load()
if err != nil {
log.Fatal(`Error loading .env file`, err.Error())
}
/**
* Signals
*/
sigs := make(chan os.Signal)
exit := make(chan int)
stopFill := make(chan int)
signal.Notify(sigs, os.Interrupt)
/**
* Message Channels
*/
done := make(chan []byte)
received := make(chan []byte)
/**
* Connection
*/
err = gdax.Connect()
if err != nil {
log.Fatal("Could not connect: ", err)
return
}
defer gdax.Close() // close the websocket when the main function exits.
/**
* Consume all websocket messages and route them to the proper channel.
*/
go func() {
defer close(exit) // if this goroutine returns, close the exit channel
for {
_, message, err := gdax.ReadMessage()
if err != nil {
log.Println("Could not read from socket: ", err)
gdax.Unsubscribe()
return
}
typ, _ := jsonparser.GetUnsafeString(message, `type`)
switch typ {
case `received`:
received <- message
case `done`:
done <- message
}
}
}()
/**
* Received messages from GDAX
*/
go func() {
orders := make(chan *market.Order)
defer close(orders)
for {
if message, more := <-received; more {
// need to match client order id with server order id and update the order
coid, err := jsonparser.GetUnsafeString(message, `client_oid`)
if err != nil {
log.Printf(`Could not read client_oid`)
continue
}
clientID, err := uuid.Parse(coid)
if err != nil {
log.Printf(`Could not parse client_oid`)
continue
}
oid, err := jsonparser.GetUnsafeString(message, `order_id`)
if err != nil {
log.Printf(`Could not read order_id`)
continue
}
orderID, err := uuid.Parse(oid)
if err != nil {
log.Printf(`Could not parse order_id`)
continue
}
gdax.SyncOrderID(clientID, orderID)
} else {
break
}
}
}()
/**
* Done messages from GDAX
*/
go func() {
for {
if msg, more := <-done; more {
// need to clean out local order and place new orders
log.Println(string(msg))
reason, err := jsonparser.GetString(msg, `reason`)
if err != nil {
log.Println(`Error reading reason from done message. Skipping.`)
continue
}
if reason == `canceled` {
log.Println(`Order canceled, skipping done handler.`)
continue
}
oid, err := jsonparser.GetString(msg, `order_id`)
if err != nil {
log.Println(`Error reading order_id from done message. Skipping.`)
continue
}
orderID, err := uuid.Parse(oid)
if err != nil {
log.Println(`Error parsing order_id from done message. Skipping.`)
continue
}
gdax.PlaceSpreadFrom(orderID)
gdax.RemoveOrder(orderID)
} else {
break
}
}
}()
/**
* Fill gaps
*/
go func() {
defer close(stopFill)
for {
select {
case <-stopFill:
break
default:
gdax.FillGaps(productID)
time.Sleep(5 * time.Second)
}
}
}()
/**
* Tracking
*/
var start float64
var end float64
/**
* Graceful shutdown. Unsubscribes to the websocket
*/
go func() {
defer close(exit) // close the exit channel when we are finished cleaning up.
<-sigs // wait for a signal.
log.Printf("Shutting down")
stopFill <- 0
gdax.Unsubscribe()
end, _, _ = gdax.GetBalance(productID)
log.Printf(`Delta in BTC: %f`, end-start)
}()
/**
* Subscribe message to GDAX initiates the websocket messages.
*/
gdax.Subscribe(productID)
accts, err := gdax.GetAccounts()
if err != nil {
log.Fatal(`error: ` + err.Error())
}
acct := accts.GetByCurrency(productID[0])
log.Println(`Canceling all pending orders`)
_, err = gdax.Request(`DELETE`, `/orders`, nil)
if err != nil {
log.Fatal(`Cancel Order request failed: ` + err.Error())
}
// wait for rebalance.
var orderStatus int
orderStatus, start, err = gdax.Rebalance(productID)
if err != nil {
log.Fatal(`Rebalance Failed: ` + err.Error())
}
switch orderStatus {
case -1:
log.Fatal(`error placing rebalance order`)
case 0:
log.Println(`No rebalance was needed.`)
_, price, err := gdax.GetBalance(productID)
if err != nil {
log.Fatal(`GetBalance Failed: ` + err.Error())
}
gdax.PlaceSpread(productID, price)
case 1:
log.Println(`Rebalance order placed. Once the done signal sends, we can place our spread.`)
default:
log.Fatal(`Unknown order satatus code. You done f'ed up your code.`)
}
/**
* Block until an exit message is received
*/
exitCode := <-exit
fmt.Println("Bye!")
os.Exit(exitCode)
}