Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Binance paper trading for sync sub-command #1605

Merged
merged 1 commit into from Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
16 changes: 4 additions & 12 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 @@ -67,12 +66,7 @@ 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")
v, ok := util.GetEnvVarBool("BINANCE_US")
return ok && v
}

Expand All @@ -97,6 +91,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 @@ -109,11 +106,6 @@ func New(key, secret string) *Exchange {
client.BaseURL = BinanceUSBaseURL
}

if paperTrade() {
client.BaseURL = BinanceTestBaseURL
futuresClient.BaseURL = FutureTestBaseURL
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need

my new code is added before create new binance client as followings

        if util.IsPaperTrade() {
		binance.UseTestnet = true
	}
	var client = binance.NewClient(key, secret)

it'll help to set BaseURL of binance client to its test base url. I did run it and it works.


client2 := binanceapi.NewClient(client.BaseURL)
futuresClient2 := binanceapi.NewFuturesRestClient(futuresClient.BaseURL)

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() {
c9s marked this conversation as resolved.
Show resolved Hide resolved
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