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
|
package bbgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
@ -21,11 +21,11 @@ func NewLocalActiveOrderBook() *LocalActiveOrderBook {
|
||||||
|
|
||||||
func (b *LocalActiveOrderBook) Print() {
|
func (b *LocalActiveOrderBook) Print() {
|
||||||
for _, o := range b.Bids.Orders() {
|
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() {
|
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 {
|
for currency, value := range m {
|
||||||
balances[currency] = types.Balance{
|
balances[currency] = types.Balance{
|
||||||
Currency: currency,
|
Currency: currency,
|
||||||
Available: value.Float64(),
|
Available: value,
|
||||||
Locked: 0.0,
|
Locked: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return balances
|
return balances
|
||||||
|
|
|
@ -125,6 +125,9 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
balances.Print()
|
||||||
|
|
||||||
|
|
||||||
session.Account.UpdateBalances(balances)
|
session.Account.UpdateBalances(balances)
|
||||||
session.Account.BindStream(session.Stream)
|
session.Account.BindStream(session.Stream)
|
||||||
|
|
||||||
|
@ -161,7 +164,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(kLines) == 0 {
|
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
|
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)
|
// SyncTradesFrom overrides the default trade scan time (-7 days)
|
||||||
func (environ *Environment) SyncTradesFrom(t time.Time) *Environment {
|
func (environ *Environment) SyncTradesFrom(t time.Time) *Environment {
|
||||||
environ.tradeScanTime = t
|
environ.tradeScanTime = t
|
||||||
|
|
|
@ -128,9 +128,9 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
|
||||||
continue
|
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",
|
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()))
|
types.USD.FormatMoneyFloat64(c.MinQuoteBalance.Float64()), order.String()))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
|
||||||
quantity = adjustQuantityByMaxAmount(quantity, price, c.MaxOrderAmount.Float64())
|
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 {
|
if quoteAssetQuota < market.MinAmount {
|
||||||
addError(
|
addError(
|
||||||
errors.Wrapf(
|
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 MaxBaseAssetBalance is enabled, we should check the current base asset balance
|
||||||
if baseBalance, hasBaseAsset := balances[market.BaseCurrency]; hasBaseAsset && c.MaxBaseAssetBalance > 0 {
|
if baseBalance, hasBaseAsset := balances[market.BaseCurrency]; hasBaseAsset && c.MaxBaseAssetBalance > 0 {
|
||||||
if baseBalance.Available > c.MaxBaseAssetBalance.Float64() {
|
if baseBalance.Available > c.MaxBaseAssetBalance {
|
||||||
addError(
|
addError(
|
||||||
errors.Wrapf(
|
errors.Wrapf(
|
||||||
ErrAssetBalanceLevelTooHigh,
|
ErrAssetBalanceLevelTooHigh,
|
||||||
"should not place buy order, asset balance level is too high: %f > %f, order: %s",
|
"should not place buy order, asset balance level is too high: %f > %f, order: %s",
|
||||||
baseBalance.Available,
|
baseBalance.Available.Float64(),
|
||||||
c.MaxBaseAssetBalance.Float64(),
|
c.MaxBaseAssetBalance.Float64(),
|
||||||
order.String()))
|
order.String()))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
baseAssetQuota := math.Max(0, c.MaxBaseAssetBalance.Float64()-baseBalance.Available)
|
baseAssetQuota := math.Max(0.0, c.MaxBaseAssetBalance.Float64()-baseBalance.Available.Float64())
|
||||||
if quantity > baseAssetQuota {
|
if quantity > baseAssetQuota {
|
||||||
quantity = baseAssetQuota
|
quantity = baseAssetQuota
|
||||||
}
|
}
|
||||||
|
@ -204,24 +204,24 @@ func (c *BasicRiskController) ProcessOrders(session *ExchangeSession, orders ...
|
||||||
quantity = adjustQuantityByMinAmount(quantity, price, market.MinNotional*1.01)
|
quantity = adjustQuantityByMinAmount(quantity, price, market.MinNotional*1.01)
|
||||||
|
|
||||||
// we should not SELL too much
|
// we should not SELL too much
|
||||||
quantity = math.Min(quantity, baseAssetBalance.Available)
|
quantity = math.Min(quantity, baseAssetBalance.Available.Float64())
|
||||||
|
|
||||||
if c.MinBaseAssetBalance > 0 {
|
if c.MinBaseAssetBalance > 0 {
|
||||||
if baseAssetBalance.Available < c.MinBaseAssetBalance.Float64() {
|
if baseAssetBalance.Available < c.MinBaseAssetBalance {
|
||||||
addError(
|
addError(
|
||||||
errors.Wrapf(
|
errors.Wrapf(
|
||||||
ErrAssetBalanceLevelTooLow,
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
quantity = math.Min(quantity, baseAssetBalance.Available-c.MinBaseAssetBalance.Float64())
|
quantity = math.Min(quantity, baseAssetBalance.Available.Float64()-c.MinBaseAssetBalance.Float64())
|
||||||
if quantity < market.MinQuantity {
|
if quantity < market.MinQuantity {
|
||||||
addError(
|
addError(
|
||||||
errors.Wrapf(
|
errors.Wrapf(
|
||||||
ErrInsufficientAssetBalance,
|
ErrInsufficientAssetBalance,
|
||||||
"insufficient asset balance: %f > minimal quantity %f",
|
"insufficient asset balance: %f > minimal quantity %f",
|
||||||
baseAssetBalance.Available,
|
baseAssetBalance.Available.Float64(),
|
||||||
market.MinQuantity))
|
market.MinQuantity))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -296,3 +296,10 @@ func formatOrders(session *ExchangeSession, orders []types.SubmitOrder) (formatt
|
||||||
|
|
||||||
return formattedOrders, err
|
return formattedOrders, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func max(a, b int64) int64 {
|
||||||
|
if a > b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package bbgo
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,10 +24,12 @@ func (e *RiskControlOrderExecutor) SubmitOrders(ctx context.Context, orders ...t
|
||||||
for symbol, orders := range symbolOrders {
|
for symbol, orders := range symbolOrders {
|
||||||
if controller, ok := e.BySymbol[symbol]; ok && controller != nil {
|
if controller, ok := e.BySymbol[symbol]; ok && controller != nil {
|
||||||
var riskErrs []error
|
var riskErrs []error
|
||||||
|
|
||||||
orders, riskErrs = controller.BasicRiskController.ProcessOrders(e.session, orders...)
|
orders, riskErrs = controller.BasicRiskController.ProcessOrders(e.session, orders...)
|
||||||
for _, riskErr := range riskErrs {
|
for _, riskErr := range riskErrs {
|
||||||
// use logger from ExchangeOrderExecutor
|
// use logger from ExchangeOrderExecutor
|
||||||
e.logger.Warnf(riskErr.Error())
|
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
|
return retOrders, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, fo := range formattedOrders {
|
||||||
|
logrus.Infof("submit order: %s", fo.String())
|
||||||
|
}
|
||||||
|
|
||||||
retOrders2, err := e.ExchangeOrderExecutor.SubmitOrders(ctx, formattedOrders...)
|
retOrders2, err := e.ExchangeOrderExecutor.SubmitOrders(ctx, formattedOrders...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return retOrders, err
|
return retOrders, err
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
"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 {
|
for _, b := range account.Balances {
|
||||||
balances[b.Asset] = types.Balance{
|
balances[b.Asset] = types.Balance{
|
||||||
Currency: b.Asset,
|
Currency: b.Asset,
|
||||||
Available: util.MustParseFloat(b.Free),
|
Available: fixedpoint.Must(fixedpoint.NewFromString(b.Free)),
|
||||||
Locked: util.MustParseFloat(b.Locked),
|
Locked: fixedpoint.Must(fixedpoint.NewFromString(b.Locked)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,11 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
|
||||||
|
|
||||||
"github.com/adshao/go-binance"
|
"github.com/adshao/go-binance"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -105,8 +105,8 @@ func NewStream(client *binance.Client) *Stream {
|
||||||
stream.OnOutboundAccountInfoEvent(func(e *OutboundAccountInfoEvent) {
|
stream.OnOutboundAccountInfoEvent(func(e *OutboundAccountInfoEvent) {
|
||||||
snapshot := types.BalanceMap{}
|
snapshot := types.BalanceMap{}
|
||||||
for _, balance := range e.Balances {
|
for _, balance := range e.Balances {
|
||||||
available := util.MustParseFloat(balance.Free)
|
available := fixedpoint.Must(fixedpoint.NewFromString(balance.Free))
|
||||||
locked := util.MustParseFloat(balance.Locked)
|
locked := fixedpoint.Must(fixedpoint.NewFromString(balance.Locked))
|
||||||
snapshot[balance.Asset] = types.Balance{
|
snapshot[balance.Asset] = types.Balance{
|
||||||
Currency: balance.Asset,
|
Currency: balance.Asset,
|
||||||
Available: available,
|
Available: available,
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
maxapi "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
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/types"
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
"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 {
|
for _, a := range userInfo.Accounts {
|
||||||
balances[toGlobalCurrency(a.Currency)] = types.Balance{
|
balances[toGlobalCurrency(a.Currency)] = types.Balance{
|
||||||
Currency: toGlobalCurrency(a.Currency),
|
Currency: toGlobalCurrency(a.Currency),
|
||||||
Available: util.MustParseFloat(a.Balance),
|
Available: fixedpoint.Must(fixedpoint.NewFromString(a.Balance)),
|
||||||
Locked: util.MustParseFloat(a.Locked),
|
Locked: fixedpoint.Must(fixedpoint.NewFromString(a.Locked)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,8 +361,8 @@ func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap,
|
||||||
for _, a := range accounts {
|
for _, a := range accounts {
|
||||||
balances[toGlobalCurrency(a.Currency)] = types.Balance{
|
balances[toGlobalCurrency(a.Currency)] = types.Balance{
|
||||||
Currency: toGlobalCurrency(a.Currency),
|
Currency: toGlobalCurrency(a.Currency),
|
||||||
Available: util.MustParseFloat(a.Balance),
|
Available: fixedpoint.Must(fixedpoint.NewFromString(a.Balance)),
|
||||||
Locked: util.MustParseFloat(a.Locked),
|
Locked: fixedpoint.Must(fixedpoint.NewFromString(a.Locked)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/valyala/fastjson"
|
"github.com/valyala/fastjson"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseEvent struct {
|
type BaseEvent struct {
|
||||||
|
@ -14,18 +14,18 @@ type BaseEvent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type OrderUpdate struct {
|
type OrderUpdate struct {
|
||||||
Event string `json:"e"`
|
Event string `json:"e"`
|
||||||
ID uint64 `json:"i"`
|
ID uint64 `json:"i"`
|
||||||
Side string `json:"sd"`
|
Side string `json:"sd"`
|
||||||
OrderType OrderType `json:"ot"`
|
OrderType OrderType `json:"ot"`
|
||||||
|
|
||||||
Price string `json:"p"`
|
Price string `json:"p"`
|
||||||
StopPrice string `json:"sp"`
|
StopPrice string `json:"sp"`
|
||||||
|
|
||||||
Volume string `json:"v"`
|
Volume string `json:"v"`
|
||||||
AveragePrice string `json:"ap"`
|
AveragePrice string `json:"ap"`
|
||||||
State OrderState `json:"S"`
|
State OrderState `json:"S"`
|
||||||
Market string `json:"M"`
|
Market string `json:"M"`
|
||||||
|
|
||||||
RemainingVolume string `json:"rv"`
|
RemainingVolume string `json:"rv"`
|
||||||
ExecutedVolume string `json:"ev"`
|
ExecutedVolume string `json:"ev"`
|
||||||
|
@ -37,7 +37,6 @@ type OrderUpdate struct {
|
||||||
CreatedAtMs int64 `json:"T"`
|
CreatedAtMs int64 `json:"T"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type OrderUpdateEvent struct {
|
type OrderUpdateEvent struct {
|
||||||
BaseEvent
|
BaseEvent
|
||||||
|
|
||||||
|
@ -173,12 +172,12 @@ type BalanceMessage struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *BalanceMessage) Balance() (*types.Balance, error) {
|
func (m *BalanceMessage) Balance() (*types.Balance, error) {
|
||||||
available, err := util.ParseFloat(m.Available)
|
available, err := fixedpoint.NewFromString(m.Available)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
locked, err := util.ParseFloat(m.Locked)
|
locked, err := fixedpoint.NewFromString(m.Locked)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user