fix InitExchange for publicOnly session

This commit is contained in:
c9s 2021-12-26 15:29:42 +08:00
parent 30a7ca1ce1
commit 05a0745d08
4 changed files with 38 additions and 40 deletions

View File

@ -16,20 +16,11 @@ notifications:
# routing rules # routing rules
routing: routing:
trade: "$symbol" trade: "$silent"
order: "$silent" order: "$silent"
submitOrder: "$silent" submitOrder: "$silent"
pnL: "bbgo-pnl" pnL: "bbgo-pnl"
reportPnL:
- averageCostBySymbols:
- "BTCUSDT"
- "BNBUSDT"
of: binance
when:
- "@daily"
- "@hourly"
persistence: persistence:
json: json:
directory: var/data directory: var/data
@ -60,4 +51,3 @@ crossExchangeStrategies:
MAX: 100 MAX: 100
persistence: persistence:
type: redis type: redis

View File

@ -189,6 +189,7 @@ func (environ *Environment) AddExchange(name string, exchange types.Exchange) (s
} }
func (environ *Environment) ConfigureExchangeSessions(userConfig *Config) error { func (environ *Environment) ConfigureExchangeSessions(userConfig *Config) error {
// if sessions are not defined, we detect the sessions automatically
if len(userConfig.Sessions) == 0 { if len(userConfig.Sessions) == 0 {
return environ.AddExchangesByViperKeys() return environ.AddExchangesByViperKeys()
} }

View File

@ -6,13 +6,14 @@ import (
"strings" "strings"
"time" "time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/cmd/cmdutil" "github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/service" "github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
log "github.com/sirupsen/logrus"
) )
var ( var (
@ -305,6 +306,7 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
session.markets = markets session.markets = markets
// query and initialize the balances // query and initialize the balances
if !session.PublicOnly {
log.Infof("querying balances from session %s...", session.Name) log.Infof("querying balances from session %s...", session.Name)
balances, err := session.Exchange.QueryAccountBalances(ctx) balances, err := session.Exchange.QueryAccountBalances(ctx)
if err != nil { if err != nil {
@ -313,13 +315,13 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
log.Infof("%s account", session.Name) log.Infof("%s account", session.Name)
balances.Print() balances.Print()
session.Account.UpdateBalances(balances) session.Account.UpdateBalances(balances)
// forward trade updates and order updates to the order executor // forward trade updates and order updates to the order executor
session.UserDataStream.OnTradeUpdate(session.OrderExecutor.EmitTradeUpdate) session.UserDataStream.OnTradeUpdate(session.OrderExecutor.EmitTradeUpdate)
session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate) session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate)
session.Account.BindStream(session.UserDataStream) session.Account.BindStream(session.UserDataStream)
}
// TODO: move this logic to Environment struct // TODO: move this logic to Environment struct
// if back-test service is not set, meaning we are not back-testing // if back-test service is not set, meaning we are not back-testing
@ -690,18 +692,16 @@ func (session *ExchangeSession) InitExchange(name string, exchange types.Exchang
var err error var err error
var exchangeName = session.ExchangeName var exchangeName = session.ExchangeName
if exchange == nil { if exchange == nil {
if session.PublicOnly {
exchange, err = cmdutil.NewExchangePublic(exchangeName)
} else {
if session.Key != "" && session.Secret != "" { if session.Key != "" && session.Secret != "" {
if !session.PublicOnly {
if len(session.Key) == 0 || len(session.Secret) == 0 {
return fmt.Errorf("can not create exchange %s: empty key or secret", exchangeName)
}
}
exchange, err = cmdutil.NewExchangeStandard(exchangeName, session.Key, session.Secret, session.Passphrase, session.SubAccount) exchange, err = cmdutil.NewExchangeStandard(exchangeName, session.Key, session.Secret, session.Passphrase, session.SubAccount)
} else { } else {
exchange, err = cmdutil.NewExchangeWithEnvVarPrefix(exchangeName, session.EnvVarPrefix) exchange, err = cmdutil.NewExchangeWithEnvVarPrefix(exchangeName, session.EnvVarPrefix)
} }
} }
}
if err != nil { if err != nil {
return err return err

View File

@ -59,6 +59,7 @@ type Strategy struct {
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
SourceExchange string `json:"sourceExchange"` SourceExchange string `json:"sourceExchange"`
TradingExchange string `json:"tradingExchange"` TradingExchange string `json:"tradingExchange"`
Quantity fixedpoint.Value `json:"quantity"`
DailyFeeBudgets map[string]fixedpoint.Value `json:"dailyFeeBudgets,omitempty"` DailyFeeBudgets map[string]fixedpoint.Value `json:"dailyFeeBudgets,omitempty"`
DailyMaxVolume fixedpoint.Value `json:"dailyMaxVolume,omitempty"` DailyMaxVolume fixedpoint.Value `json:"dailyMaxVolume,omitempty"`
@ -279,6 +280,12 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
log.Infof("mid price %f", midPrice.Float64()) log.Infof("mid price %f", midPrice.Float64())
var quantity = s.tradingMarket.MinQuantity var quantity = s.tradingMarket.MinQuantity
if s.Quantity > 0 {
quantity = s.Quantity.Float64()
quantity = math.Min(quantity, s.tradingMarket.MinQuantity)
}
var quoteAmount = price * quantity var quoteAmount = price * quantity
if quoteAmount <= s.tradingMarket.MinNotional { if quoteAmount <= s.tradingMarket.MinNotional {
quantity = math.Max( quantity = math.Max(
@ -293,7 +300,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
Quantity: quantity, Quantity: quantity,
Price: price, Price: price,
Market: s.tradingMarket, Market: s.tradingMarket,
TimeInForce: "GTC", // TimeInForce: "GTC",
GroupID: s.groupID, GroupID: s.groupID,
}, types.SubmitOrder{ }, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
@ -302,7 +309,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
Quantity: quantity, Quantity: quantity,
Price: price, Price: price,
Market: s.tradingMarket, Market: s.tradingMarket,
TimeInForce: "GTC", // TimeInForce: "GTC",
GroupID: s.groupID, GroupID: s.groupID,
}) })
if err != nil { if err != nil {