-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
account.go
236 lines (185 loc) · 5.17 KB
/
account.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
package types
import (
"fmt"
"strings"
"sync"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
type Balance struct {
Currency string `json:"currency"`
Available fixedpoint.Value `json:"available"`
Locked fixedpoint.Value `json:"locked"`
}
func (b Balance) Total() fixedpoint.Value {
return b.Available + b.Locked
}
func (b Balance) String() string {
if b.Locked > 0 {
return fmt.Sprintf("%s: %f (locked %f)", b.Currency, b.Available.Float64(), b.Locked.Float64())
}
return fmt.Sprintf("%s: %f", b.Currency, b.Available.Float64())
}
type Asset struct {
Currency string `json:"currency"`
Total fixedpoint.Value `json:"total"`
InUSD fixedpoint.Value `json:"inUSD"`
InBTC fixedpoint.Value `json:"inBTC"`
}
type AssetMap map[string]Asset
type BalanceMap map[string]Balance
func (m BalanceMap) String() string {
var ss []string
for _, b := range m {
ss = append(ss, b.String())
}
return "BalanceMap[" + strings.Join(ss, ", ") + "]"
}
func (m BalanceMap) Assets(prices map[string]float64) AssetMap {
assets := make(AssetMap)
for currency, b := range m {
if b.Locked == 0 && b.Available == 0 {
continue
}
asset := Asset{
Currency: currency,
Total: b.Available + b.Locked,
}
btcusdt, hasBtcPrice := prices["BTCUSDT"]
usdMarkets := []string{currency + "USDT", currency + "USDC", currency + "USD", "USDT" + currency}
for _, market := range usdMarkets {
if val, ok := prices[market]; ok {
if strings.HasPrefix(market, "USD") {
asset.InUSD = fixedpoint.NewFromFloat(asset.Total.Float64() / val)
} else {
asset.InUSD = asset.Total.MulFloat64(val)
}
if hasBtcPrice {
asset.InBTC = fixedpoint.NewFromFloat(asset.InUSD.Float64() / btcusdt)
}
}
}
assets[currency] = asset
}
return assets
}
func (m BalanceMap) Print() {
for _, balance := range m {
if balance.Available == 0 && balance.Locked == 0 {
continue
}
if balance.Locked > 0 {
logrus.Infof(" %s: %f (locked %f)", balance.Currency, balance.Available.Float64(), balance.Locked.Float64())
} else {
logrus.Infof(" %s: %f", balance.Currency, balance.Available.Float64())
}
}
}
type Account struct {
sync.Mutex `json:"-"`
MakerCommission int `json:"makerCommission,omitempty"`
TakerCommission int `json:"takerCommission,omitempty"`
AccountType string `json:"accountType,omitempty"`
balances BalanceMap
}
func NewAccount() *Account {
return &Account{
balances: make(BalanceMap),
}
}
// Balances lock the balances and returned the copied balances
func (a *Account) Balances() BalanceMap {
d := make(BalanceMap)
a.Lock()
for c, b := range a.balances {
d[c] = b
}
a.Unlock()
return d
}
func (a *Account) Balance(currency string) (balance Balance, ok bool) {
a.Lock()
balance, ok = a.balances[currency]
a.Unlock()
return balance, ok
}
func (a *Account) AddBalance(currency string, fund fixedpoint.Value) error {
a.Lock()
defer a.Unlock()
balance, ok := a.balances[currency]
if ok {
balance.Available += fund
a.balances[currency] = balance
return nil
}
a.balances[currency] = Balance{
Currency: currency,
Available: fund,
Locked: 0,
}
return nil
}
func (a *Account) UseLockedBalance(currency string, fund fixedpoint.Value) error {
a.Lock()
defer a.Unlock()
balance, ok := a.balances[currency]
if ok && balance.Locked >= fund {
balance.Locked -= fund
a.balances[currency] = balance
return nil
}
return fmt.Errorf("trying to use more than locked: locked %f < want to use %f", balance.Locked.Float64(), fund.Float64())
}
func (a *Account) UnlockBalance(currency string, unlocked fixedpoint.Value) error {
a.Lock()
defer a.Unlock()
balance, ok := a.balances[currency]
if !ok {
return fmt.Errorf("trying to unlocked inexisted balance: %s", currency)
}
if unlocked > balance.Locked {
return fmt.Errorf("trying to unlocked more than locked %s: locked %f < want to unlock %f", currency, balance.Locked.Float64(), unlocked.Float64())
}
balance.Locked -= unlocked
balance.Available += unlocked
a.balances[currency] = balance
return nil
}
func (a *Account) LockBalance(currency string, locked fixedpoint.Value) error {
a.Lock()
defer a.Unlock()
balance, ok := a.balances[currency]
if ok && balance.Available >= locked {
balance.Locked += locked
balance.Available -= locked
a.balances[currency] = balance
return nil
}
return fmt.Errorf("insufficient available balance %s for lock: want to lock %f, available %f", currency, locked.Float64(), balance.Available.Float64())
}
func (a *Account) UpdateBalances(balances BalanceMap) {
a.Lock()
defer a.Unlock()
if a.balances == nil {
a.balances = make(BalanceMap)
}
for _, balance := range balances {
a.balances[balance.Currency] = balance
}
}
func (a *Account) BindStream(stream Stream) {
stream.OnBalanceUpdate(a.UpdateBalances)
stream.OnBalanceSnapshot(a.UpdateBalances)
stream.OnBalanceUpdate(func(balances BalanceMap) {
logrus.Infof("balance update: %+v", balances)
})
}
func (a *Account) Print() {
a.Lock()
defer a.Unlock()
for _, balance := range a.balances {
if balance.Available != 0 {
logrus.Infof("account balance %s %f", balance.Currency, balance.Available.Float64())
}
}
}