Merge pull request #1280 from c9s/feature/bitget

FEATURE: [bitget] integrate QueryMarkets, QueryTicker and QueryAccount api
This commit is contained in:
c9s 2023-10-20 13:36:07 +08:00 committed by GitHub
commit eb404a5f9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 149 additions and 5 deletions

View File

@ -308,6 +308,8 @@ var BacktestCmd = &cobra.Command{
var reportDir = outputDirectory
var sessionTradeStats = make(map[string]map[string]*types.TradeStats)
// for each exchange session, iterate the positions and
// allocate trade collector to calculate the tradeStats
var tradeCollectorList []*core.TradeCollector
for _, exSource := range exchangeSources {
sessionName := exSource.Session.Name
@ -335,6 +337,7 @@ var BacktestCmd = &cobra.Command{
}
sessionTradeStats[sessionName] = tradeStatsMap
}
kLineHandlers = append(kLineHandlers, func(k types.KLine, _ *backtest.ExchangeDataSource) {
if k.Interval == types.Interval1d && k.Closed {
for _, collector := range tradeCollectorList {

View File

@ -38,6 +38,13 @@ func TestClient(t *testing.T) {
t.Logf("tickers: %+v", tickers)
})
t.Run("GetSymbolsRequest", func(t *testing.T) {
req := client.NewGetSymbolsRequest()
symbols, err := req.Do(ctx)
assert.NoError(t, err)
t.Logf("symbols: %+v", symbols)
})
t.Run("GetTickerRequest", func(t *testing.T) {
req := client.NewGetTickerRequest()
req.Symbol("BTCUSDT_SPBL")

View File

@ -18,8 +18,8 @@ type Symbol struct {
MaxTradeAmount fixedpoint.Value `json:"maxTradeAmount"`
TakerFeeRate fixedpoint.Value `json:"takerFeeRate"`
MakerFeeRate fixedpoint.Value `json:"makerFeeRate"`
PriceScale fixedpoint.Value `json:"priceScale"`
QuantityScale fixedpoint.Value `json:"quantityScale"`
PriceScale int `json:"priceScale"`
QuantityScale int `json:"quantityScale"`
MinTradeUSDT fixedpoint.Value `json:"minTradeUSDT"`
Status string `json:"status"`
BuyLimitPriceRatio fixedpoint.Value `json:"buyLimitPriceRatio"`

View File

@ -0,0 +1,25 @@
package bitget
import (
"strings"
"github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func toGlobalSymbol(s string) string {
return strings.ToUpper(s)
}
func toGlobalBalance(asset bitgetapi.AccountAsset) types.Balance {
return types.Balance{
Currency: asset.CoinName,
Available: asset.Available,
Locked: asset.Lock.Add(asset.Frozen),
Borrowed: fixedpoint.Zero,
Interest: fixedpoint.Zero,
NetAsset: fixedpoint.Zero,
MaxWithdrawAmount: fixedpoint.Zero,
}
}

View File

@ -1,9 +1,13 @@
package bitget
import (
"context"
"math"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
@ -43,3 +47,108 @@ func (e *Exchange) Name() types.ExchangeName {
func (e *Exchange) PlatformFeeCurrency() string {
return PlatformToken
}
func (e *Exchange) NewStream() types.Stream {
// TODO implement me
panic("implement me")
}
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
// TODO implement me
req := e.client.NewGetSymbolsRequest()
symbols, err := req.Do(ctx)
if err != nil {
return nil, err
}
markets := types.MarketMap{}
for _, s := range symbols {
symbol := toGlobalSymbol(s.SymbolName)
markets[symbol] = types.Market{
Symbol: s.SymbolName,
LocalSymbol: s.Symbol,
PricePrecision: s.PriceScale,
VolumePrecision: s.QuantityScale,
QuoteCurrency: s.QuoteCoin,
BaseCurrency: s.BaseCoin,
MinNotional: s.MinTradeUSDT,
MinAmount: s.MinTradeUSDT,
MinQuantity: s.MinTradeAmount,
MaxQuantity: s.MaxTradeAmount,
StepSize: fixedpoint.NewFromFloat(math.Pow10(-s.QuantityScale)),
TickSize: fixedpoint.NewFromFloat(math.Pow10(-s.PriceScale)),
MinPrice: fixedpoint.Zero,
MaxPrice: fixedpoint.Zero,
}
}
return markets, nil
}
func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) {
req := e.client.NewGetTickerRequest()
req.Symbol(symbol)
ticker, err := req.Do(ctx)
if err != nil {
return nil, err
}
return &types.Ticker{
Time: ticker.Ts.Time(),
Volume: ticker.BaseVol,
Last: ticker.Close,
Open: ticker.OpenUtc0,
High: ticker.High24H,
Low: ticker.Low24H,
Buy: ticker.BuyOne,
Sell: ticker.SellOne,
}, nil
}
func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) {
// TODO implement me
panic("implement me")
}
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
// TODO implement me
panic("implement me")
}
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
req := e.client.NewGetAccountAssetsRequest()
resp, err := req.Do(ctx)
if err != nil {
return nil, err
}
bals := types.BalanceMap{}
for _, asset := range resp {
b := toGlobalBalance(asset)
bals[asset.CoinName] = b
}
account := types.NewAccount()
account.UpdateBalances(bals)
return account, nil
}
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
// TODO implement me
panic("implement me")
}
func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (createdOrder *types.Order, err error) {
// TODO implement me
panic("implement me")
}
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
// TODO implement me
panic("implement me")
}
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) error {
// TODO implement me
panic("implement me")
}

View File

@ -47,11 +47,11 @@ type Market struct {
// 1.0 / math.Pow10(m.BaseUnitPrecision)
StepSize fixedpoint.Value `json:"stepSize,omitempty"`
MinPrice fixedpoint.Value `json:"minPrice,omitempty"`
MaxPrice fixedpoint.Value `json:"maxPrice,omitempty"`
// TickSize is the step size of price
TickSize fixedpoint.Value `json:"tickSize,omitempty"`
MinPrice fixedpoint.Value `json:"minPrice,omitempty"`
MaxPrice fixedpoint.Value `json:"maxPrice,omitempty"`
}
func (m Market) IsDustQuantity(quantity, price fixedpoint.Value) bool {