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

feature: add ExchangePublic #1575

Merged
merged 5 commits into from Mar 13, 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
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Expand Up @@ -20,6 +20,7 @@ jobs:
go-version:
- "1.20"
env:
GITHUB_CI: true
MYSQL_DATABASE: bbgo
MYSQL_USER: "root"
MYSQL_PASSWORD: "root" # pragma: allowlist secret
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Expand Up @@ -72,13 +72,15 @@ require (
github.com/bitly/go-simplejson v0.5.1 // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/c9s/callbackgen v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/denisenkom/go-mssqldb v0.12.3 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dmarkham/enumer v1.5.9 // indirect
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
Expand Down Expand Up @@ -117,6 +119,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pascaldekloe/name v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand All @@ -141,6 +144,7 @@ require (
go.opentelemetry.io/otel v0.19.0 // indirect
go.opentelemetry.io/otel/metric v0.19.0 // indirect
go.opentelemetry.io/otel/trace v0.19.0 // indirect
go.uber.org/mock v0.4.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Expand Up @@ -82,6 +82,8 @@ github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/c-bata/goptuna v0.8.1 h1:25+n1MLv0yvCsD56xv4nqIus3oLHL9GuPAZDLIqmX1U=
github.com/c-bata/goptuna v0.8.1/go.mod h1:knmS8+Iyq5PPy1YUeIEq0pMFR4Y6x7z/CySc9HlZTCY=
github.com/c9s/callbackgen v1.1.0 h1:Igf1oPY9yYFoZP8YBK6JQ8JqNe6pzczVDY6xzg5rk38=
github.com/c9s/callbackgen v1.1.0/go.mod h1:8NRnf1Y3KEaHEaZgznkthi9alQiYCVoliPKc4Xz7A1o=
github.com/c9s/requestgen v1.3.6 h1:ul7dZ2uwGYjNBjreooRfSY10WTXvQmQSjZsHebz6QfE=
github.com/c9s/requestgen v1.3.6/go.mod h1:QwkZudcv84kJ8g9+E0RDTj+13btFXbTvv2aI+zbuLbc=
github.com/c9s/rockhopper/v2 v2.0.3-0.20240124055428-2473c6221858 h1:VLvl82XQCtH12axHdroyrfprPYKtA3TueEWicx8yT4A=
Expand Down Expand Up @@ -137,6 +139,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dmarkham/enumer v1.5.9 h1:NM/1ma/AUNieHZg74w67GkHFBNB15muOt3sj486QVZk=
github.com/dmarkham/enumer v1.5.9/go.mod h1:e4VILe2b1nYK3JKJpRmNdl5xbDQvELc6tQ8b+GsGk6E=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -509,6 +513,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ=
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/name v1.0.0 h1:n7LKFgHixETzxpRv2R77YgPUFo85QHGZKrdaYm7eY5U=
github.com/pascaldekloe/name v1.0.0/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
Expand Down Expand Up @@ -690,6 +696,8 @@ go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bj
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
2 changes: 1 addition & 1 deletion pkg/bbgo/exit_trailing_stop_test.go
Expand Up @@ -3,8 +3,8 @@ package bbgo
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
Expand Down
28 changes: 16 additions & 12 deletions pkg/bbgo/mocks/mock_order_executor_extended.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/bbgo/risk_test.go
Expand Up @@ -5,8 +5,8 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
Expand Down
6 changes: 3 additions & 3 deletions pkg/cache/cache.go
Expand Up @@ -126,7 +126,7 @@ func WithCache(key string, obj interface{}, fetcher DataFetcher) error {
return nil
}

func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (markets types.MarketMap, err error) {
func LoadExchangeMarketsWithCache(ctx context.Context, ex types.ExchangePublic) (markets types.MarketMap, err error) {
inMem, ok := util.GetEnvVarBool("USE_MARKETS_CACHE_IN_MEMORY")
if ok && inMem {
return loadMarketsFromMem(ctx, ex)
Expand All @@ -137,7 +137,7 @@ func LoadExchangeMarketsWithCache(ctx context.Context, ex types.Exchange) (marke
}

// loadMarketsFromMem is useful for one process to run multiple bbgos in different go routines.
func loadMarketsFromMem(ctx context.Context, ex types.Exchange) (markets types.MarketMap, _ error) {
func loadMarketsFromMem(ctx context.Context, ex types.ExchangePublic) (markets types.MarketMap, _ error) {
exName := ex.Name().String()
if globalMarketMemCache.IsOutdated(exName) {
op := func() error {
Expand All @@ -162,7 +162,7 @@ func loadMarketsFromMem(ctx context.Context, ex types.Exchange) (markets types.M
return rst, nil
}

func loadMarketsFromFile(ctx context.Context, ex types.Exchange) (markets types.MarketMap, err error) {
func loadMarketsFromFile(ctx context.Context, ex types.ExchangePublic) (markets types.MarketMap, err error) {
key := fmt.Sprintf("%s-markets", ex.Name())
if futureExchange, implemented := ex.(types.FuturesExchange); implemented {
settings := futureExchange.GetFuturesSettings()
Expand Down
4 changes: 2 additions & 2 deletions pkg/cache/cache_test.go
Expand Up @@ -6,8 +6,8 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/mocks"
Expand Down Expand Up @@ -67,7 +67,7 @@ func Test_loadMarketsFromMem(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockEx := mocks.NewMockExchange(mockCtrl)
mockEx := mocks.NewMockExchangePublic(mockCtrl)
mockEx.EXPECT().Name().Return(types.ExchangeName("max")).AnyTimes()
mockEx.EXPECT().QueryMarkets(gomock.Any()).Return(nil, errors.New("faked")).Times(1)
mockEx.EXPECT().QueryMarkets(gomock.Any()).Return(types.MarketMap{
Expand Down
2 changes: 1 addition & 1 deletion pkg/exchange/batch/trade_test.go
Expand Up @@ -6,9 +6,9 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/mocks"
Expand Down
28 changes: 14 additions & 14 deletions pkg/exchange/binance/exchange.go
Expand Up @@ -129,24 +129,24 @@ func New(key, secret string) *Exchange {
if len(key) > 0 && len(secret) > 0 {
client2.Auth(key, secret)
futuresClient2.Auth(key, secret)
}

ctx := context.Background()
go timeSetterOnce.Do(func() {
ex.setServerTimeOffset(ctx)
ctx := context.Background()
go timeSetterOnce.Do(func() {
ex.setServerTimeOffset(ctx)

ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return

case <-ticker.C:
ex.setServerTimeOffset(ctx)
}
case <-ticker.C:
ex.setServerTimeOffset(ctx)
}
})
}
}
})

return ex
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/exchange/binance/exchange_test.go
@@ -1,6 +1,8 @@
package binance

import (
"context"
"os"
"strings"
"testing"

Expand All @@ -15,3 +17,18 @@ func Test_newClientOrderID(t *testing.T) {
cID = newSpotClientOrderID("myid1")
assert.Equal(t, cID, "x-"+spotBrokerID+"myid1")
}

func Test_new(t *testing.T) {
ex := New("", "")
assert.NotEmpty(t, ex)
ctx := context.Background()
ticker, err := ex.QueryTicker(ctx, "btcusdt")
if len(os.Getenv("GITHUB_CI")) > 0 {
// Github action runs in the US, and therefore binance api is not accessible
assert.Empty(t, ticker)
assert.Error(t, err)
} else {
assert.NotEmpty(t, ticker)
assert.NoError(t, err)
}
}
2 changes: 1 addition & 1 deletion pkg/exchange/bybit/market_info_poller_test.go
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/c9s/bbgo/pkg/exchange/bybit/bybitapi"
"github.com/c9s/bbgo/pkg/exchange/bybit/mocks"
Expand Down
2 changes: 1 addition & 1 deletion pkg/exchange/bybit/mocks/stream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/risk/riskcontrol/position_test.go
Expand Up @@ -3,8 +3,8 @@ package riskcontrol
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/bbgo/mocks"
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/common/sync_test.go
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/mocks"
"github.com/golang/mock/gomock"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestSyncActiveOrders(t *testing.T) {
Expand Down