Merge pull request #1605 from lanphan/sync

support Binance paper trading for sync sub-command
This commit is contained in:
c9s 2024-03-28 14:47:25 +08:00 committed by GitHub
commit f300791e34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 17 deletions

View File

@ -520,11 +520,6 @@ func (environ *Environment) Sync(ctx context.Context, userConfig ...*Config) err
return nil return nil
} }
// for paper trade mode, skip sync
if util.IsPaperTrade() {
return nil
}
environ.syncMutex.Lock() environ.syncMutex.Lock()
defer environ.syncMutex.Unlock() defer environ.syncMutex.Unlock()

View File

@ -3,7 +3,6 @@ package binance
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -67,12 +66,7 @@ func init() {
} }
func isBinanceUs() bool { func isBinanceUs() bool {
v, err := strconv.ParseBool(os.Getenv("BINANCE_US")) v, ok := util.GetEnvVarBool("BINANCE_US")
return err == nil && v
}
func paperTrade() bool {
v, ok := util.GetEnvVarBool("PAPER_TRADE")
return ok && v return ok && v
} }
@ -97,6 +91,9 @@ type Exchange struct {
var timeSetterOnce sync.Once var timeSetterOnce sync.Once
func New(key, secret string) *Exchange { func New(key, secret string) *Exchange {
if util.IsPaperTrade() {
binance.UseTestnet = true
}
var client = binance.NewClient(key, secret) var client = binance.NewClient(key, secret)
client.HTTPClient = binanceapi.DefaultHttpClient client.HTTPClient = binanceapi.DefaultHttpClient
client.Debug = viper.GetBool("debug-binance-client") client.Debug = viper.GetBool("debug-binance-client")
@ -109,11 +106,6 @@ func New(key, secret string) *Exchange {
client.BaseURL = BinanceUSBaseURL client.BaseURL = BinanceUSBaseURL
} }
if paperTrade() {
client.BaseURL = BinanceTestBaseURL
futuresClient.BaseURL = FutureTestBaseURL
}
client2 := binanceapi.NewClient(client.BaseURL) client2 := binanceapi.NewClient(client.BaseURL)
futuresClient2 := binanceapi.NewFuturesRestClient(futuresClient.BaseURL) futuresClient2 := binanceapi.NewFuturesRestClient(futuresClient.BaseURL)

View File

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/c9s/bbgo/pkg/cache" "github.com/c9s/bbgo/pkg/cache"
"github.com/c9s/bbgo/pkg/util"
log "github.com/sirupsen/logrus" 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()) 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 { if err := s.RewardService.Sync(ctx, exchange, startTime); err != nil {
return err 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 { func (s *SyncService) SyncDepositHistory(ctx context.Context, exchange types.Exchange, startTime time.Time) error {
log.Infof("syncing %s deposit records...", exchange.Name()) 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 := s.DepositService.Sync(ctx, exchange, startTime); err != nil {
if err != ErrNotImplemented { if err != ErrNotImplemented {
log.Warnf("%s deposit service is not supported", exchange.Name()) 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 { func (s *SyncService) SyncWithdrawHistory(ctx context.Context, exchange types.Exchange, startTime time.Time) error {
log.Infof("syncing %s withdraw records...", exchange.Name()) 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 := s.WithdrawService.Sync(ctx, exchange, startTime); err != nil {
if err != ErrNotImplemented { if err != ErrNotImplemented {
log.Warnf("%s withdraw service is not supported", exchange.Name()) log.Warnf("%s withdraw service is not supported", exchange.Name())