forked from bitfinexcom/bitfinex-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (63 loc) · 1.44 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
package main
import (
"fmt"
"log"
"os"
"github.com/laikasinjason/bitfinex-api-go/pkg/models/event"
"github.com/laikasinjason/bitfinex-api-go/pkg/models/order"
"github.com/laikasinjason/bitfinex-api-go/pkg/mux"
)
func main() {
m := mux.
New().
TransformRaw().
WithAPIKEY("YOUR_API_KEY").
WithAPISEC("YOUR_API_SECRET").
Start()
crash := make(chan error)
auth := make(chan bool)
go func() {
// if listener will fail, program will exit by passing error to crash channel
crash <- m.Listen(func(msg interface{}, err error) {
if err != nil {
log.Printf("error received: %s\n", err)
}
switch v := msg.(type) {
case event.Info:
if v.Event == "auth" && v.Status == "OK" {
// notify auth channel about successful login
auth <- true
}
case order.New:
// new order received, can update it now
log.Printf("%T: %+v\n", v, v)
m.Send(&order.UpdateRequest{
ID: v.ID,
Amount: 0.002,
})
case order.Update:
// order update performed, exiting
log.Printf("%T: %+v\n", v, v)
close(crash)
}
})
}()
for {
select {
case err := <-crash:
fmt.Printf("err: %s\n", err)
os.Exit(1)
case <-auth:
// authenticated, safe to submit orders etc
if err := m.Send(&order.NewRequest{
CID: 788,
Type: "EXCHANGE LIMIT",
Symbol: "tBTCUSD",
Price: 33,
Amount: 0.001,
}); err != nil {
fmt.Printf("err submitting new order: %s\n", err)
}
}
}
}