-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
account.go
255 lines (209 loc) · 7.84 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package types
import (
"fmt"
"sync"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
var debugBalance = false
func init() {
debugBalance = viper.GetBool("debug-balance")
}
type PositionMap map[string]Position
type IsolatedMarginAssetMap map[string]IsolatedMarginAsset
type MarginAssetMap map[string]MarginUserAsset
type FuturesAssetMap map[string]FuturesUserAsset
type FuturesPositionMap map[string]FuturesPosition
type AccountType string
const (
AccountTypeFutures = AccountType("futures")
AccountTypeMargin = AccountType("margin")
AccountTypeIsolatedMargin = AccountType("isolated_margin")
AccountTypeSpot = AccountType("spot")
)
type Account struct {
sync.Mutex `json:"-"`
AccountType AccountType `json:"accountType,omitempty"`
FuturesInfo *FuturesAccountInfo
MarginInfo *MarginAccountInfo
IsolatedMarginInfo *IsolatedMarginAccountInfo
// Margin related common field
// From binance:
// Margin Level = Total Asset Value / (Total Borrowed + Total Accrued Interest)
// If your margin level drops to 1.3, you will receive a Margin Call, which is a reminder that you should either increase your collateral (by depositing more funds) or reduce your loan (by repaying what you’ve borrowed).
// If your margin level drops to 1.1, your assets will be automatically liquidated, meaning that Binance will sell your funds at market price to repay the loan.
MarginLevel fixedpoint.Value `json:"marginLevel,omitempty"`
MarginTolerance fixedpoint.Value `json:"marginTolerance,omitempty"`
BorrowEnabled bool `json:"borrowEnabled,omitempty"`
TransferEnabled bool `json:"transferEnabled,omitempty"`
// isolated margin related fields
// LiquidationPrice is only used when account is in the isolated margin mode
MarginRatio fixedpoint.Value `json:"marginRatio,omitempty"`
LiquidationPrice fixedpoint.Value `json:"liquidationPrice,omitempty"`
LiquidationRate fixedpoint.Value `json:"liquidationRate,omitempty"`
MakerFeeRate fixedpoint.Value `json:"makerFeeRate,omitempty"`
TakerFeeRate fixedpoint.Value `json:"takerFeeRate,omitempty"`
TotalAccountValue fixedpoint.Value `json:"totalAccountValue,omitempty"`
CanDeposit bool `json:"canDeposit"`
CanTrade bool `json:"canTrade"`
CanWithdraw bool `json:"canWithdraw"`
balances BalanceMap
}
type FuturesAccountInfo struct {
// Futures fields
Assets FuturesAssetMap `json:"assets"`
Positions FuturesPositionMap `json:"positions"`
TotalInitialMargin fixedpoint.Value `json:"totalInitialMargin"`
TotalMaintMargin fixedpoint.Value `json:"totalMaintMargin"`
TotalMarginBalance fixedpoint.Value `json:"totalMarginBalance"`
TotalOpenOrderInitialMargin fixedpoint.Value `json:"totalOpenOrderInitialMargin"`
TotalPositionInitialMargin fixedpoint.Value `json:"totalPositionInitialMargin"`
TotalUnrealizedProfit fixedpoint.Value `json:"totalUnrealizedProfit"`
TotalWalletBalance fixedpoint.Value `json:"totalWalletBalance"`
UpdateTime int64 `json:"updateTime"`
}
type MarginAccountInfo struct {
// Margin fields
BorrowEnabled bool `json:"borrowEnabled"`
MarginLevel fixedpoint.Value `json:"marginLevel"`
TotalAssetOfBTC fixedpoint.Value `json:"totalAssetOfBtc"`
TotalLiabilityOfBTC fixedpoint.Value `json:"totalLiabilityOfBtc"`
TotalNetAssetOfBTC fixedpoint.Value `json:"totalNetAssetOfBtc"`
TradeEnabled bool `json:"tradeEnabled"`
TransferEnabled bool `json:"transferEnabled"`
Assets MarginAssetMap `json:"userAssets"`
}
type IsolatedMarginAccountInfo struct {
TotalAssetOfBTC fixedpoint.Value `json:"totalAssetOfBtc"`
TotalLiabilityOfBTC fixedpoint.Value `json:"totalLiabilityOfBtc"`
TotalNetAssetOfBTC fixedpoint.Value `json:"totalNetAssetOfBtc"`
Assets IsolatedMarginAssetMap `json:"userAssets"`
}
func NewAccount() *Account {
return &Account{
AccountType: "spot",
FuturesInfo: nil,
MarginInfo: nil,
IsolatedMarginInfo: nil,
MarginLevel: fixedpoint.Zero,
MarginTolerance: fixedpoint.Zero,
BorrowEnabled: false,
TransferEnabled: false,
MarginRatio: fixedpoint.Zero,
LiquidationPrice: fixedpoint.Zero,
LiquidationRate: fixedpoint.Zero,
MakerFeeRate: fixedpoint.Zero,
TakerFeeRate: fixedpoint.Zero,
TotalAccountValue: fixedpoint.Zero,
CanDeposit: false,
CanTrade: false,
CanWithdraw: false,
balances: make(BalanceMap),
}
}
// Balances lock the balances and returned the copied balances
func (a *Account) Balances() (d BalanceMap) {
a.Lock()
d = a.balances.Copy()
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) {
a.Lock()
defer a.Unlock()
balance, ok := a.balances[currency]
if ok {
balance.Available = balance.Available.Add(fund)
a.balances[currency] = balance
return
}
a.balances[currency] = Balance{
Currency: currency,
Available: fund,
Locked: fixedpoint.Zero,
}
}
func (a *Account) UseLockedBalance(currency string, fund fixedpoint.Value) error {
a.Lock()
defer a.Unlock()
balance, ok := a.balances[currency]
if !ok {
return fmt.Errorf("account balance %s does not exist", currency)
}
// simple case, using fund less than locked
if balance.Locked.Compare(fund) >= 0 {
balance.Locked = balance.Locked.Sub(fund)
a.balances[currency] = balance
return nil
}
return fmt.Errorf("trying to use more than locked: locked %v < want to use %v diff %v", balance.Locked, fund, balance.Locked.Sub(fund))
}
var QuantityDelta = fixedpoint.MustNewFromString("0.00000000001")
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)
}
// Instead of showing error in UnlockBalance,
// since this function is only called when cancel orders,
// there might be inequivalence in the last order quantity
if unlocked.Compare(balance.Locked) > 0 {
// check if diff is within delta
if unlocked.Sub(balance.Locked).Compare(QuantityDelta) <= 0 {
balance.Available = balance.Available.Add(balance.Locked)
balance.Locked = fixedpoint.Zero
a.balances[currency] = balance
return nil
}
return fmt.Errorf("trying to unlocked more than locked %s: locked %v < want to unlock %v", currency, balance.Locked, unlocked)
}
balance.Locked = balance.Locked.Sub(unlocked)
balance.Available = balance.Available.Add(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.Compare(locked) >= 0 {
balance.Locked = balance.Locked.Add(locked)
balance.Available = balance.Available.Sub(locked)
a.balances[currency] = balance
return nil
}
return fmt.Errorf("insufficient available balance %s for lock: want to lock %v, available %v", currency, locked, balance.Available)
}
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) Print() {
a.Lock()
defer a.Unlock()
if a.AccountType != "" {
logrus.Infof("account type: %s", a.AccountType)
}
if a.MakerFeeRate.Sign() > 0 {
logrus.Infof("maker fee rate: %v", a.MakerFeeRate)
}
if a.TakerFeeRate.Sign() > 0 {
logrus.Infof("taker fee rate: %v", a.TakerFeeRate)
}
a.balances.Print()
}