2020-09-07 06:20:03 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2020-09-07 06:20:03 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
2020-10-12 14:46:06 +00:00
|
|
|
sync.Mutex
|
2020-09-07 06:20:03 +00:00
|
|
|
Balances map[string]types.Balance
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
func (a *Account) handleBalanceUpdates(balances map[string]types.Balance) {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
2020-09-07 06:20:03 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
for _, balance := range balances {
|
|
|
|
a.Balances[balance.Currency] = balance
|
|
|
|
}
|
|
|
|
}
|
2020-09-07 06:20:03 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
func (a *Account) BindStream(stream types.Stream) {
|
|
|
|
stream.OnBalanceUpdate(a.handleBalanceUpdates)
|
|
|
|
stream.OnBalanceSnapshot(a.handleBalanceUpdates)
|
2020-09-07 06:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) Print() {
|
2020-10-12 14:46:06 +00:00
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
for _, balance := range a.Balances {
|
|
|
|
if util.NotZero(balance.Available) {
|
|
|
|
log.Infof("[trader] balance %s %f", balance.Currency, balance.Available)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|