This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
74 lines (65 loc) · 1.94 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
package main
import (
"fmt"
"log"
"github.com/adampointer/go-deribit/client/operations"
flag "github.com/spf13/pflag"
"github.com/adampointer/go-deribit"
)
func main() {
key := flag.String("access-key", "", "Access key")
secret := flag.String("secret-key", "", "Secret access key")
flag.Parse()
errs := make(chan error)
stop := make(chan bool)
e, err := deribit.NewExchange(true, errs, stop)
if err != nil {
log.Fatalf("Error creating connection: %s", err)
}
if err := e.Connect(); err != nil {
log.Fatalf("Error connecting to exchange: %s", err)
}
go func() {
err := <-errs
stop <- true
log.Fatalf("RPC error: %s", err)
}()
client := e.Client()
// Hit the test RPC endpoint
res, err := client.GetPublicTest(&operations.GetPublicTestParams{})
if err != nil {
log.Fatalf("Error testing connection: %s", err)
}
log.Printf("Connected to Deribit API v%s", *res.Payload.Result.Version)
if err := e.Authenticate(*key, *secret); err != nil {
log.Fatalf("Error authenticating: %s", err)
}
// Account summary
summary, err := client.GetPrivateGetAccountSummary(&operations.GetPrivateGetAccountSummaryParams{Currency: "BTC"})
if err != nil {
log.Fatalf("Error getting account summary: %s", err)
}
fmt.Printf("Available funds: %f\n", *summary.Payload.Result.AvailableFunds)
// Buy
buyParams := &operations.GetPrivateBuyParams{
Amount: 10,
InstrumentName: "BTC-PERPETUAL",
Type: strPointer("market"),
}
buy, err := client.GetPrivateBuy(buyParams)
if err != nil {
log.Fatalf("Error submitting buy order: %s", err)
}
fmt.Printf("Brought at %f\n", buy.Payload.Result.Order.AveragePrice)
// Order book subscription
book, err := e.SubscribeBookGroup("BTC-PERPETUAL", "none", "1", "100ms")
if err != nil {
log.Fatalf("Error subscribing to the book: %s", err)
}
for b := range book {
fmt.Printf("Top bid: %f Top ask: %f\n", b.Bids[0][0], b.Asks[0][0])
}
}
func strPointer(str string) *string {
return &str
}