mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 08:15:15 +00:00
Merge pull request #1280 from c9s/feature/bitget
FEATURE: [bitget] integrate QueryMarkets, QueryTicker and QueryAccount api
This commit is contained in:
commit
eb404a5f9b
|
@ -308,6 +308,8 @@ var BacktestCmd = &cobra.Command{
|
||||||
var reportDir = outputDirectory
|
var reportDir = outputDirectory
|
||||||
var sessionTradeStats = make(map[string]map[string]*types.TradeStats)
|
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
|
var tradeCollectorList []*core.TradeCollector
|
||||||
for _, exSource := range exchangeSources {
|
for _, exSource := range exchangeSources {
|
||||||
sessionName := exSource.Session.Name
|
sessionName := exSource.Session.Name
|
||||||
|
@ -335,6 +337,7 @@ var BacktestCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
sessionTradeStats[sessionName] = tradeStatsMap
|
sessionTradeStats[sessionName] = tradeStatsMap
|
||||||
}
|
}
|
||||||
|
|
||||||
kLineHandlers = append(kLineHandlers, func(k types.KLine, _ *backtest.ExchangeDataSource) {
|
kLineHandlers = append(kLineHandlers, func(k types.KLine, _ *backtest.ExchangeDataSource) {
|
||||||
if k.Interval == types.Interval1d && k.Closed {
|
if k.Interval == types.Interval1d && k.Closed {
|
||||||
for _, collector := range tradeCollectorList {
|
for _, collector := range tradeCollectorList {
|
||||||
|
|
|
@ -38,6 +38,13 @@ func TestClient(t *testing.T) {
|
||||||
t.Logf("tickers: %+v", tickers)
|
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) {
|
t.Run("GetTickerRequest", func(t *testing.T) {
|
||||||
req := client.NewGetTickerRequest()
|
req := client.NewGetTickerRequest()
|
||||||
req.Symbol("BTCUSDT_SPBL")
|
req.Symbol("BTCUSDT_SPBL")
|
||||||
|
|
|
@ -18,8 +18,8 @@ type Symbol struct {
|
||||||
MaxTradeAmount fixedpoint.Value `json:"maxTradeAmount"`
|
MaxTradeAmount fixedpoint.Value `json:"maxTradeAmount"`
|
||||||
TakerFeeRate fixedpoint.Value `json:"takerFeeRate"`
|
TakerFeeRate fixedpoint.Value `json:"takerFeeRate"`
|
||||||
MakerFeeRate fixedpoint.Value `json:"makerFeeRate"`
|
MakerFeeRate fixedpoint.Value `json:"makerFeeRate"`
|
||||||
PriceScale fixedpoint.Value `json:"priceScale"`
|
PriceScale int `json:"priceScale"`
|
||||||
QuantityScale fixedpoint.Value `json:"quantityScale"`
|
QuantityScale int `json:"quantityScale"`
|
||||||
MinTradeUSDT fixedpoint.Value `json:"minTradeUSDT"`
|
MinTradeUSDT fixedpoint.Value `json:"minTradeUSDT"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
BuyLimitPriceRatio fixedpoint.Value `json:"buyLimitPriceRatio"`
|
BuyLimitPriceRatio fixedpoint.Value `json:"buyLimitPriceRatio"`
|
||||||
|
|
25
pkg/exchange/bitget/convert.go
Normal file
25
pkg/exchange/bitget/convert.go
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,13 @@
|
||||||
package bitget
|
package bitget
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi"
|
"github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi"
|
||||||
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,3 +47,108 @@ func (e *Exchange) Name() types.ExchangeName {
|
||||||
func (e *Exchange) PlatformFeeCurrency() string {
|
func (e *Exchange) PlatformFeeCurrency() string {
|
||||||
return PlatformToken
|
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")
|
||||||
|
}
|
||||||
|
|
|
@ -47,11 +47,11 @@ type Market struct {
|
||||||
// 1.0 / math.Pow10(m.BaseUnitPrecision)
|
// 1.0 / math.Pow10(m.BaseUnitPrecision)
|
||||||
StepSize fixedpoint.Value `json:"stepSize,omitempty"`
|
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 is the step size of price
|
||||||
TickSize fixedpoint.Value `json:"tickSize,omitempty"`
|
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 {
|
func (m Market) IsDustQuantity(quantity, price fixedpoint.Value) bool {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user