-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (162 loc) · 3.7 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
package main
import (
"bufio"
"context"
"encoding/csv"
"fmt"
"os"
"os/user"
"strconv"
"time"
"github.com/adshao/go-binance/v2"
"github.com/zalando/go-keyring"
)
var buildStamp = "unreleased"
// This application exports all binance USD trades into CSV file.
// Assume interractive mode and require application to read user input prior to closing
func main() {
err := process()
argsWithoutProg := os.Args[1:]
rv := 0
if err != nil {
fmt.Println(err.Error())
rv = 1
}
// any argument would indicate non-interactive mode
// -b is for batch or background mode (non-interactive)
interactive := len(argsWithoutProg) == 0
if interactive {
fmt.Print("Press Enter to quit application:")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
}
os.Exit(rv)
}
func process() error {
var (
apiKey string
secretKey string
err error
)
fmt.Println("binance-trades version: ", buildStamp)
{
var u *user.User
u, err = user.Current()
if err != nil {
return err
}
fmt.Println("using username: ", u.Username)
apiKey, err = getSecret("binance.api.key", u.Username)
if err != nil {
return err
}
secretKey, err = getSecret("binance.api.secret", u.Username)
if err != nil {
return err
}
}
client := binance.NewClient(apiKey, secretKey)
client.BaseURL = "https://api.binance.us"
recvWinddow := binance.WithRecvWindow(10000)
var account *binance.Account
{
svc := client.NewGetAccountService()
account, err = svc.Do(context.Background(), recvWinddow)
if err != nil {
return err
}
}
{
svc := client.NewListTradesService()
file, err := os.Create("result.csv")
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
writer.Write([]string{
"Commission",
"CommissionAsset",
"Price",
"Quantity",
"QuoteQuantity",
"Symbol",
"ID",
"IsBestMatch",
"IsBuyer",
"IsIsolated",
"IsMaker",
"OrderID",
"Time",
})
for _, balance := range account.Balances {
if balance.Asset == "USD" {
continue
}
var free float64
free, err = strconv.ParseFloat(balance.Free, 64)
if err != nil {
return err
}
if free == 0.0 {
continue
}
fmt.Println("Asset: ", balance.Asset, ", free: ", balance.Free, ", locked: ", balance.Locked)
svc.Symbol(balance.Asset + "USD")
trades, err := svc.Do(context.Background(), recvWinddow)
if err != nil {
return err
}
fmt.Println(" ", balance.Asset, " trades")
for _, trade := range trades {
line := []string{
trade.Commission,
trade.CommissionAsset,
trade.Price,
trade.Quantity,
trade.QuoteQuantity,
trade.Symbol,
fmt.Sprintf("%v", trade.ID),
fmt.Sprintf("%v", trade.IsBestMatch),
fmt.Sprintf("%v", trade.IsBuyer),
fmt.Sprintf("%v", trade.IsIsolated),
fmt.Sprintf("%v", trade.IsMaker),
fmt.Sprintf("%v", trade.OrderID),
fmt.Sprintf("%v", timeFromMillisec(trade.Time)),
}
writer.Write(line)
}
}
}
return nil
}
func timeFromMillisec(timeMillisec int64) time.Time {
d := timeMillisec * int64(time.Millisecond)
return time.Unix(0, d)
}
func getSecret(service, user string) (string, error) {
var secret string
var err error
// get secret
secret, err = keyring.Get(service, user)
if err != nil {
// assume password is not set
secret = readValue(fmt.Sprintf("New '%s' for %s", service, user))
// set password
err = keyring.Set(service, user, secret)
if err != nil {
return "", fmt.Errorf("failed to set keyring value: %v", err)
}
}
return secret, nil
}
func readValue(message string) string {
scanner := bufio.NewScanner(os.Stdin)
fmt.Print(message)
fmt.Print(": ")
for scanner.Scan() {
return scanner.Text()
}
return ""
}