From 67c29fa46e47eb4309232c836569d7718a9ad99f Mon Sep 17 00:00:00 2001 From: Lan Phan Date: Tue, 26 Mar 2024 10:55:28 +0700 Subject: [PATCH] support Binance paper trading for sync sub-command --- pkg/bbgo/environment.go | 5 ----- pkg/exchange/binance/exchange.go | 21 ++++----------------- pkg/exchange/binance/stream.go | 2 +- pkg/service/sync.go | 15 +++++++++++++++ pkg/util/paper_trade.go | 5 +++++ 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index a7873ef6bf..ab86153132 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -520,11 +520,6 @@ func (environ *Environment) Sync(ctx context.Context, userConfig ...*Config) err return nil } - // for paper trade mode, skip sync - if util.IsPaperTrade() { - return nil - } - environ.syncMutex.Lock() defer environ.syncMutex.Unlock() diff --git a/pkg/exchange/binance/exchange.go b/pkg/exchange/binance/exchange.go index b369506272..50fa6b1dba 100644 --- a/pkg/exchange/binance/exchange.go +++ b/pkg/exchange/binance/exchange.go @@ -3,7 +3,6 @@ package binance import ( "context" "fmt" - "os" "strconv" "strings" "sync" @@ -66,16 +65,6 @@ func init() { } } -func isBinanceUs() bool { - v, err := strconv.ParseBool(os.Getenv("BINANCE_US")) - return err == nil && v -} - -func paperTrade() bool { - v, ok := util.GetEnvVarBool("PAPER_TRADE") - return ok && v -} - type Exchange struct { types.MarginSettings types.FuturesSettings @@ -97,6 +86,9 @@ type Exchange struct { var timeSetterOnce sync.Once func New(key, secret string) *Exchange { + if util.IsPaperTrade() { + binance.UseTestnet = true + } var client = binance.NewClient(key, secret) client.HTTPClient = binanceapi.DefaultHttpClient client.Debug = viper.GetBool("debug-binance-client") @@ -105,15 +97,10 @@ func New(key, secret string) *Exchange { futuresClient.HTTPClient = binanceapi.DefaultHttpClient futuresClient.Debug = viper.GetBool("debug-binance-futures-client") - if isBinanceUs() { + if util.IsBinanceUs() { client.BaseURL = BinanceUSBaseURL } - if paperTrade() { - client.BaseURL = BinanceTestBaseURL - futuresClient.BaseURL = FutureTestBaseURL - } - client2 := binanceapi.NewClient(client.BaseURL) futuresClient2 := binanceapi.NewFuturesRestClient(futuresClient.BaseURL) diff --git a/pkg/exchange/binance/stream.go b/pkg/exchange/binance/stream.go index bcbc7d71b4..2a3630f2c4 100644 --- a/pkg/exchange/binance/stream.go +++ b/pkg/exchange/binance/stream.go @@ -304,7 +304,7 @@ func (s *Stream) getEndpointUrl(listenKey string) string { if s.IsFutures { url = FuturesWebSocketURL + "/ws" - } else if isBinanceUs() { + } else if util.IsBinanceUs() { url = BinanceUSWebSocketURL + "/ws" } else { url = WebSocketURL + "/ws" diff --git a/pkg/service/sync.go b/pkg/service/sync.go index ae9f6adb73..8a00de2c5f 100644 --- a/pkg/service/sync.go +++ b/pkg/service/sync.go @@ -6,6 +6,7 @@ import ( "time" "github.com/c9s/bbgo/pkg/cache" + "github.com/c9s/bbgo/pkg/util" log "github.com/sirupsen/logrus" @@ -88,6 +89,10 @@ func (s *SyncService) SyncRewardHistory(ctx context.Context, exchange types.Exch } log.Infof("syncing %s reward records...", exchange.Name()) + if util.IsPaperTrade() { + log.Info("Reward is not supported in paper trading") + return nil + } if err := s.RewardService.Sync(ctx, exchange, startTime); err != nil { return err } @@ -97,6 +102,11 @@ func (s *SyncService) SyncRewardHistory(ctx context.Context, exchange types.Exch func (s *SyncService) SyncDepositHistory(ctx context.Context, exchange types.Exchange, startTime time.Time) error { log.Infof("syncing %s deposit records...", exchange.Name()) + if util.IsPaperTrade() { + log.Info("Deposit is not supported in paper trading") + return nil + } + if err := s.DepositService.Sync(ctx, exchange, startTime); err != nil { if err != ErrNotImplemented { log.Warnf("%s deposit service is not supported", exchange.Name()) @@ -109,6 +119,11 @@ func (s *SyncService) SyncDepositHistory(ctx context.Context, exchange types.Exc func (s *SyncService) SyncWithdrawHistory(ctx context.Context, exchange types.Exchange, startTime time.Time) error { log.Infof("syncing %s withdraw records...", exchange.Name()) + if util.IsPaperTrade() { + log.Info("Withdraw is not supported in paper trading") + return nil + } + if err := s.WithdrawService.Sync(ctx, exchange, startTime); err != nil { if err != ErrNotImplemented { log.Warnf("%s withdraw service is not supported", exchange.Name()) diff --git a/pkg/util/paper_trade.go b/pkg/util/paper_trade.go index b3a09d68b5..376232c3dc 100644 --- a/pkg/util/paper_trade.go +++ b/pkg/util/paper_trade.go @@ -4,3 +4,8 @@ func IsPaperTrade() bool { v, ok := GetEnvVarBool("PAPER_TRADE") return ok && v } + +func IsBinanceUs() bool { + v, ok := GetEnvVarBool("BINANCE_US") + return ok && v +}