Skip to content

Commit

Permalink
support Binance paper trading for sync sub-command
Browse files Browse the repository at this point in the history
  • Loading branch information
lanphan committed Mar 27, 2024
1 parent 693b641 commit 67c29fa
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
5 changes: 0 additions & 5 deletions pkg/bbgo/environment.go
Expand Up @@ -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()

Expand Down
21 changes: 4 additions & 17 deletions pkg/exchange/binance/exchange.go
Expand Up @@ -3,7 +3,6 @@ package binance
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pkg/exchange/binance/stream.go
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions pkg/service/sync.go
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/c9s/bbgo/pkg/cache"
"github.com/c9s/bbgo/pkg/util"

log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -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
}
Expand All @@ -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())
Expand All @@ -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())
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/paper_trade.go
Expand Up @@ -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
}

0 comments on commit 67c29fa

Please sign in to comment.