mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
use fixedpoint for balances
This commit is contained in:
parent
cdf7959029
commit
23c19c5968
|
@ -1,7 +1,7 @@
|
|||
package bbgo
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
@ -21,11 +21,11 @@ func NewLocalActiveOrderBook() *LocalActiveOrderBook {
|
|||
|
||||
func (b *LocalActiveOrderBook) Print() {
|
||||
for _, o := range b.Bids.Orders() {
|
||||
logrus.Infof("bid order: %d -> %s", o.OrderID, o.Status)
|
||||
log.Infof("bid order: %d -> %s", o.OrderID, o.Status)
|
||||
}
|
||||
|
||||
for _, o := range b.Asks.Orders() {
|
||||
logrus.Infof("ask order: %d -> %s", o.OrderID, o.Status)
|
||||
log.Infof("ask order: %d -> %s", o.OrderID, o.Status)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -84,8 +84,8 @@ func (m BacktestAccountBalanceMap) BalanceMap() types.BalanceMap {
|
|||
for currency, value := range m {
|
||||
balances[currency] = types.Balance{
|
||||
Currency: currency,
|
||||
Available: value.Float64(),
|
||||
Locked: 0.0,
|
||||
Available: value,
|
||||
Locked: 0,
|
||||
}
|
||||
}
|
||||
return balances
|
||||
|
|
|
@ -125,6 +125,9 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
balances.Print()
|
||||
|
||||
|
||||
session.Account.UpdateBalances(balances)
|
||||
session.Account.BindStream(session.Stream)
|
||||
|
||||
|
@ -161,7 +164,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
|||
}
|
||||
|
||||
if len(kLines) == 0 {
|
||||
log.Warnf("no kline data for interval %s", interval)
|
||||
log.Warnf("no kline data for interval %s (end time <= %s)", interval, environ.startTime)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -337,6 +340,11 @@ func (environ *Environment) ConfigureNotification(conf *NotificationConfig) {
|
|||
}
|
||||
}
|
||||
|
||||
func (environ *Environment) SetStartTime(t time.Time) *Environment {
|
||||
environ.startTime = t
|
||||
return environ
|
||||
}
|
||||
|
||||
// SyncTradesFrom overrides the default trade scan time (-7 days)
|
||||
func (environ *Environment) SyncTradesFrom(t time.Time) *Environment {
|
||||
environ.tradeScanTime = t
|
||||
|
|
|
@ -128,9 +128,9 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
|
|||
continue
|
||||
}
|
||||
|
||||
if quoteBalance.Available < c.MinQuoteBalance.Float64() {
|
||||
if quoteBalance.Available < c.MinQuoteBalance {
|
||||
addError(errors.Wrapf(ErrQuoteBalanceLevelTooLow, "can not place buy order, quote balance level is too low: %s < %s, order: %s",
|
||||
types.USD.FormatMoneyFloat64(quoteBalance.Available),
|
||||
types.USD.FormatMoneyFloat64(quoteBalance.Available.Float64()),
|
||||
types.USD.FormatMoneyFloat64(c.MinQuoteBalance.Float64()), order.String()))
|
||||
continue
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
|
|||
quantity = adjustQuantityByMaxAmount(quantity, price, c.MaxOrderAmount.Float64())
|
||||
}
|
||||
|
||||
quoteAssetQuota := math.Max(0.0, quoteBalance.Available-c.MinQuoteBalance.Float64())
|
||||
quoteAssetQuota := math.Max(0.0, quoteBalance.Available.Float64()-c.MinQuoteBalance.Float64())
|
||||
if quoteAssetQuota < market.MinAmount {
|
||||
addError(
|
||||
errors.Wrapf(
|
||||
|
@ -157,18 +157,18 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
|
|||
|
||||
// if MaxBaseAssetBalance is enabled, we should check the current base asset balance
|
||||
if baseBalance, hasBaseAsset := balances[market.BaseCurrency]; hasBaseAsset && c.MaxBaseAssetBalance > 0 {
|
||||
if baseBalance.Available > c.MaxBaseAssetBalance.Float64() {
|
||||
if baseBalance.Available > c.MaxBaseAssetBalance {
|
||||
addError(
|
||||
errors.Wrapf(
|
||||
ErrAssetBalanceLevelTooHigh,
|
||||
"should not place buy order, asset balance level is too high: %f > %f, order: %s",
|
||||
baseBalance.Available,
|
||||
baseBalance.Available.Float64(),
|
||||
c.MaxBaseAssetBalance.Float64(),
|
||||
order.String()))
|
||||
continue
|
||||
}
|
||||
|
||||
baseAssetQuota := math.Max(0, c.MaxBaseAssetBalance.Float64()-baseBalance.Available)
|
||||
baseAssetQuota := math.Max(0.0, c.MaxBaseAssetBalance.Float64()-baseBalance.Available.Float64())
|
||||
if quantity > baseAssetQuota {
|
||||
quantity = baseAssetQuota
|
||||
}
|
||||
|
@ -204,24 +204,24 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
|
|||
quantity = adjustQuantityByMinAmount(quantity, price, market.MinNotional*1.01)
|
||||
|
||||
// we should not SELL too much
|
||||
quantity = math.Min(quantity, baseAssetBalance.Available)
|
||||
quantity = math.Min(quantity, baseAssetBalance.Available.Float64())
|
||||
|
||||
if c.MinBaseAssetBalance > 0 {
|
||||
if baseAssetBalance.Available < c.MinBaseAssetBalance.Float64() {
|
||||
if baseAssetBalance.Available < c.MinBaseAssetBalance {
|
||||
addError(
|
||||
errors.Wrapf(
|
||||
ErrAssetBalanceLevelTooLow,
|
||||
"asset balance level is too low: %f > %f", baseAssetBalance.Available, c.MinBaseAssetBalance.Float64()))
|
||||
"asset balance level is too low: %f > %f", baseAssetBalance.Available.Float64(), c.MinBaseAssetBalance.Float64()))
|
||||
continue
|
||||
}
|
||||
|
||||
quantity = math.Min(quantity, baseAssetBalance.Available-c.MinBaseAssetBalance.Float64())
|
||||
quantity = math.Min(quantity, baseAssetBalance.Available.Float64()-c.MinBaseAssetBalance.Float64())
|
||||
if quantity < market.MinQuantity {
|
||||
addError(
|
||||
errors.Wrapf(
|
||||
ErrInsufficientAssetBalance,
|
||||
"insufficient asset balance: %f > minimal quantity %f",
|
||||
baseAssetBalance.Available,
|
||||
baseAssetBalance.Available.Float64(),
|
||||
market.MinQuantity))
|
||||
continue
|
||||
}
|
||||
|
@ -296,3 +296,10 @@ func formatOrders(session *ExchangeSession, orders []types.SubmitOrder) (formatt
|
|||
|
||||
return formattedOrders, err
|
||||
}
|
||||
|
||||
func max(a, b int64) int64 {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package bbgo
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
|
@ -22,10 +24,12 @@ func (e *RiskControlOrderExecutor) SubmitOrders(ctx context.Context, orders ...t
|
|||
for symbol, orders := range symbolOrders {
|
||||
if controller, ok := e.BySymbol[symbol]; ok && controller != nil {
|
||||
var riskErrs []error
|
||||
|
||||
orders, riskErrs = controller.BasicRiskController.ProcessOrders(e.session, orders...)
|
||||
for _, riskErr := range riskErrs {
|
||||
// use logger from ExchangeOrderExecutor
|
||||
e.logger.Warnf(riskErr.Error())
|
||||
logrus.Warnf("RISK ERROR: %s", riskErr.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,6 +38,10 @@ func (e *RiskControlOrderExecutor) SubmitOrders(ctx context.Context, orders ...t
|
|||
return retOrders, err
|
||||
}
|
||||
|
||||
for _, fo := range formattedOrders {
|
||||
logrus.Infof("submit order: %s", fo.String())
|
||||
}
|
||||
|
||||
retOrders2, err := e.ExchangeOrderExecutor.SubmitOrders(ctx, formattedOrders...)
|
||||
if err != nil {
|
||||
return retOrders, err
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
)
|
||||
|
@ -256,8 +257,8 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
|||
for _, b := range account.Balances {
|
||||
balances[b.Asset] = types.Balance{
|
||||
Currency: b.Asset,
|
||||
Available: util.MustParseFloat(b.Free),
|
||||
Locked: util.MustParseFloat(b.Locked),
|
||||
Available: fixedpoint.Must(fixedpoint.NewFromString(b.Free)),
|
||||
Locked: fixedpoint.Must(fixedpoint.NewFromString(b.Locked)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,11 +7,11 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
|
||||
"github.com/adshao/go-binance"
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
|
@ -105,8 +105,8 @@ func NewStream(client *binance.Client) *Stream {
|
|||
stream.OnOutboundAccountInfoEvent(func(e *OutboundAccountInfoEvent) {
|
||||
snapshot := types.BalanceMap{}
|
||||
for _, balance := range e.Balances {
|
||||
available := util.MustParseFloat(balance.Free)
|
||||
locked := util.MustParseFloat(balance.Locked)
|
||||
available := fixedpoint.Must(fixedpoint.NewFromString(balance.Free))
|
||||
locked := fixedpoint.Must(fixedpoint.NewFromString(balance.Locked))
|
||||
snapshot[balance.Asset] = types.Balance{
|
||||
Currency: balance.Asset,
|
||||
Available: available,
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
|
||||
maxapi "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
)
|
||||
|
@ -218,8 +219,8 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
|||
for _, a := range userInfo.Accounts {
|
||||
balances[toGlobalCurrency(a.Currency)] = types.Balance{
|
||||
Currency: toGlobalCurrency(a.Currency),
|
||||
Available: util.MustParseFloat(a.Balance),
|
||||
Locked: util.MustParseFloat(a.Locked),
|
||||
Available: fixedpoint.Must(fixedpoint.NewFromString(a.Balance)),
|
||||
Locked: fixedpoint.Must(fixedpoint.NewFromString(a.Locked)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -360,8 +361,8 @@ func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap,
|
|||
for _, a := range accounts {
|
||||
balances[toGlobalCurrency(a.Currency)] = types.Balance{
|
||||
Currency: toGlobalCurrency(a.Currency),
|
||||
Available: util.MustParseFloat(a.Balance),
|
||||
Locked: util.MustParseFloat(a.Locked),
|
||||
Available: fixedpoint.Must(fixedpoint.NewFromString(a.Balance)),
|
||||
Locked: fixedpoint.Must(fixedpoint.NewFromString(a.Locked)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
"github.com/valyala/fastjson"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
)
|
||||
|
||||
type BaseEvent struct {
|
||||
|
@ -37,7 +37,6 @@ type OrderUpdate struct {
|
|||
CreatedAtMs int64 `json:"T"`
|
||||
}
|
||||
|
||||
|
||||
type OrderUpdateEvent struct {
|
||||
BaseEvent
|
||||
|
||||
|
@ -173,12 +172,12 @@ type BalanceMessage struct {
|
|||
}
|
||||
|
||||
func (m *BalanceMessage) Balance() (*types.Balance, error) {
|
||||
available, err := util.ParseFloat(m.Available)
|
||||
available, err := fixedpoint.NewFromString(m.Available)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
locked, err := util.ParseFloat(m.Locked)
|
||||
locked, err := fixedpoint.NewFromString(m.Locked)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user