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:
trade: "$symbol"
trade: "$silent"
order: "$silent"
submitOrder: "$silent"
pnL: "bbgo-pnl"
reportPnL:
- averageCostBySymbols:
- "BTCUSDT"
- "BNBUSDT"
of: binance
when:
- "@daily"
- "@hourly"
persistence:
json:
directory: var/data
@ -60,4 +51,3 @@ crossExchangeStrategies:
MAX: 100
persistence:
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 {
// if sessions are not defined, we detect the sessions automatically
if len(userConfig.Sessions) == 0 {
return environ.AddExchangesByViperKeys()
}

View File

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

View File

@ -56,9 +56,10 @@ type Strategy struct {
*bbgo.Notifiability
*bbgo.Persistence
Symbol string `json:"symbol"`
SourceExchange string `json:"sourceExchange"`
TradingExchange string `json:"tradingExchange"`
Symbol string `json:"symbol"`
SourceExchange string `json:"sourceExchange"`
TradingExchange string `json:"tradingExchange"`
Quantity fixedpoint.Value `json:"quantity"`
DailyFeeBudgets map[string]fixedpoint.Value `json:"dailyFeeBudgets,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())
var quantity = s.tradingMarket.MinQuantity
if s.Quantity > 0 {
quantity = s.Quantity.Float64()
quantity = math.Min(quantity, s.tradingMarket.MinQuantity)
}
var quoteAmount = price * quantity
if quoteAmount <= s.tradingMarket.MinNotional {
quantity = math.Max(
@ -293,7 +300,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
Quantity: quantity,
Price: price,
Market: s.tradingMarket,
TimeInForce: "GTC",
// TimeInForce: "GTC",
GroupID: s.groupID,
}, types.SubmitOrder{
Symbol: s.Symbol,
@ -302,7 +309,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
Quantity: quantity,
Price: price,
Market: s.tradingMarket,
TimeInForce: "GTC",
// TimeInForce: "GTC",
GroupID: s.groupID,
})
if err != nil {