-
Notifications
You must be signed in to change notification settings - Fork 2
/
extendedapi.go
174 lines (144 loc) · 3.22 KB
/
extendedapi.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
/*
* Copyright (C) 2019-2020 Gregor Pogačnik
*/
package TokensApi
import (
"errors"
"strings"
"time"
"github.com/fiksn/TokensApi/entities"
"github.com/golang/glog"
)
/**
* Cancel all outstanding orders.
*/
func CancelAllOrders() error {
orders, err := GetAllOrders()
if err != nil {
return err
}
for _, order := range orders.OpenOrders {
fl, err := order.RemainingAmount.Float64()
if err != nil || fl <= 0 {
glog.Warningf("Order %v has a strange remaining amount %v", order.Id, order.RemainingAmount)
}
glog.V(6).Infof("Canceling order %v", order.Id)
CancelOrder(order.Id)
}
return nil
}
/**
* Get all supported currency codes.
* See GetCurrencies()
*/
func GetAllCurrencies() ([]string, error) {
resp, err := GetTradingPairs()
if err != nil {
return nil, err
}
set := make(map[string]bool, len(resp))
for _, pair := range resp {
if !set[pair.BaseCurrency] {
set[pair.BaseCurrency] = true
}
if !set[pair.CounterCurrency] {
set[pair.CounterCurrency] = true
}
}
ret := make([]string, len(set))
idx := 0
for key := range set {
ret[idx] = key
idx++
}
return ret, nil
}
type Amount float64
type Price float64
/**
* Place an order in a type-safe manner to avoid (costly) mistakes.
*/
func PlaceOrderTyped(
pair *entities.TradingPair,
side entities.OrderType,
amount Amount,
price Price,
takeProfitPrice *Price,
expireDate *time.Time) (entities.PlaceOrderResp, error) {
var resp entities.PlaceOrderResp
if pair == nil {
return resp, errors.New("Pair must not be nil")
}
minAmount, err := pair.MinAmount.Float64()
if err != nil || float64(amount) < minAmount {
return resp, errors.New("150 Amount is too low")
}
if takeProfitPrice != nil {
return PlaceOrder(
pair.BaseCurrency+pair.CounterCurrency,
side,
float64(amount),
pair.AmountDecimals,
float64(price),
pair.PriceDecimals,
float64(*takeProfitPrice),
expireDate)
} else {
return PlaceOrder(
pair.BaseCurrency+pair.CounterCurrency,
side,
float64(amount),
pair.AmountDecimals,
float64(price),
pair.PriceDecimals,
-1,
expireDate)
}
}
/**
* Get balances.
*/
func GetBalances(hideZero bool) map[string]*entities.BalanceResp {
all, err := GetAllBalances()
resp := make(map[string]*entities.BalanceResp)
if err != nil {
return nil
}
for currency, balance := range all.Balances {
total, err := balance.Total.Float64()
if err != nil {
continue
}
if (hideZero && total > 0) || !hideZero {
resp[strings.ToLower(currency)] = &entities.BalanceResp{
Base: all.Base,
Currency: strings.ToLower(currency),
Balance: *balance,
}
}
}
return resp
}
/**
* Get transactions. WARNING might take a while.
*/
func GetAllTransactions() (entities.TransactionResp, error) {
// starts with 1 not 0
page := 1
resp := entities.TransactionResp{}
resp.TotalPages = page
resp.Transactions = make([]entities.Transaction, 0)
for page < resp.TotalPages {
temp, err := GetTransactions(page)
resp.CurrentPage = temp.CurrentPage
resp.TotalPages = temp.TotalPages
resp.Status = temp.Status
resp.Timestamp = temp.Timestamp
resp.Transactions = append(resp.Transactions, temp.Transactions...)
if err != nil {
return resp, err
}
page++
}
return resp, nil
}