mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
Merge pull request #1757 from c9s/edwin/bybit/add-v5-execution-request
FEATURE: [bybit] add v5 execution request
This commit is contained in:
commit
a639a5831b
67
pkg/exchange/bybit/bybitapi/get_execution_list_request.go
Normal file
67
pkg/exchange/bybit/bybitapi/get_execution_list_request.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package bybitapi
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/c9s/requestgen"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Result
|
||||
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Result
|
||||
|
||||
type TradesResponse struct {
|
||||
NextPageCursor string `json:"nextPageCursor"`
|
||||
Category Category `json:"category"`
|
||||
List []Trade `json:"list"`
|
||||
}
|
||||
|
||||
type Trade struct {
|
||||
Symbol string `json:"symbol"`
|
||||
OrderId string `json:"orderId"`
|
||||
Side Side `json:"side"`
|
||||
OrderType OrderType `json:"orderType"`
|
||||
// ExecFee is supported on restful API v5, but not on websocket API.
|
||||
ExecFee fixedpoint.Value `json:"execFee"`
|
||||
ExecId string `json:"execId"`
|
||||
ExecPrice fixedpoint.Value `json:"execPrice"`
|
||||
ExecQty fixedpoint.Value `json:"execQty"`
|
||||
ExecTime types.MillisecondTimestamp `json:"execTime"`
|
||||
IsMaker bool `json:"isMaker"`
|
||||
}
|
||||
|
||||
//go:generate GetRequest -url "/v5/execution/list" -type GetExecutionListRequest -responseDataType .TradesResponse -rateLimiter 5+15/1s
|
||||
type GetExecutionListRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
category Category `param:"category,query" validValues:"spot"`
|
||||
|
||||
symbol *string `param:"symbol,query"`
|
||||
orderId *string `param:"orderId,query"`
|
||||
|
||||
// startTime the start timestamp (ms)
|
||||
// startTime and endTime are not passed, return 7 days by default;
|
||||
// Only startTime is passed, return range between startTime and startTime+7 days
|
||||
// Only endTime is passed, return range between endTime-7 days and endTime
|
||||
// If both are passed, the rule is endTime - startTime <= 7 days
|
||||
startTime *time.Time `param:"startTime,query,milliseconds"`
|
||||
|
||||
// endTime the end timestamp (ms)
|
||||
endTime *time.Time `param:"endTime,query,milliseconds"`
|
||||
|
||||
// limit for data size per page. [1, 100]. Default: 50
|
||||
limit *uint64 `param:"limit,query"`
|
||||
// cursor. Use the nextPageCursor token from the response to retrieve the next page of the result set
|
||||
cursor *string `param:"cursor,query"`
|
||||
}
|
||||
|
||||
// NewGetExecutionListRequest query users' execution records, sorted by execTime in descending order. However,
|
||||
// for Classic spot, they are sorted by execId in descending order.
|
||||
func (c *RestClient) NewGetExecutionListRequest() *GetExecutionListRequest {
|
||||
return &GetExecutionListRequest{
|
||||
client: c,
|
||||
category: CategorySpot,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,284 @@
|
|||
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Result -url /v5/execution/list -type GetExecutionListRequest -responseDataType .TradesResponse -rateLimiter 5+15/1s"; DO NOT EDIT.
|
||||
|
||||
package bybitapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"golang.org/x/time/rate"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var GetExecutionListRequestLimiter = rate.NewLimiter(15.000000150000002, 5)
|
||||
|
||||
func (g *GetExecutionListRequest) Category(category Category) *GetExecutionListRequest {
|
||||
g.category = category
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) Symbol(symbol string) *GetExecutionListRequest {
|
||||
g.symbol = &symbol
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) OrderId(orderId string) *GetExecutionListRequest {
|
||||
g.orderId = &orderId
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) StartTime(startTime time.Time) *GetExecutionListRequest {
|
||||
g.startTime = &startTime
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) EndTime(endTime time.Time) *GetExecutionListRequest {
|
||||
g.endTime = &endTime
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) Limit(limit uint64) *GetExecutionListRequest {
|
||||
g.limit = &limit
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) Cursor(cursor string) *GetExecutionListRequest {
|
||||
g.cursor = &cursor
|
||||
return g
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (g *GetExecutionListRequest) GetQueryParameters() (url.Values, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check category field -> json key category
|
||||
category := g.category
|
||||
|
||||
// TEMPLATE check-valid-values
|
||||
switch category {
|
||||
case "spot":
|
||||
params["category"] = category
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("category value %v is invalid", category)
|
||||
|
||||
}
|
||||
// END TEMPLATE check-valid-values
|
||||
|
||||
// assign parameter of category
|
||||
params["category"] = category
|
||||
// check symbol field -> json key symbol
|
||||
if g.symbol != nil {
|
||||
symbol := *g.symbol
|
||||
|
||||
// assign parameter of symbol
|
||||
params["symbol"] = symbol
|
||||
} else {
|
||||
}
|
||||
// check orderId field -> json key orderId
|
||||
if g.orderId != nil {
|
||||
orderId := *g.orderId
|
||||
|
||||
// assign parameter of orderId
|
||||
params["orderId"] = orderId
|
||||
} else {
|
||||
}
|
||||
// check startTime field -> json key startTime
|
||||
if g.startTime != nil {
|
||||
startTime := *g.startTime
|
||||
|
||||
// assign parameter of startTime
|
||||
// convert time.Time to milliseconds time stamp
|
||||
params["startTime"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
|
||||
} else {
|
||||
}
|
||||
// check endTime field -> json key endTime
|
||||
if g.endTime != nil {
|
||||
endTime := *g.endTime
|
||||
|
||||
// assign parameter of endTime
|
||||
// convert time.Time to milliseconds time stamp
|
||||
params["endTime"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10)
|
||||
} else {
|
||||
}
|
||||
// check limit field -> json key limit
|
||||
if g.limit != nil {
|
||||
limit := *g.limit
|
||||
|
||||
// assign parameter of limit
|
||||
params["limit"] = limit
|
||||
} else {
|
||||
}
|
||||
// check cursor field -> json key cursor
|
||||
if g.cursor != nil {
|
||||
cursor := *g.cursor
|
||||
|
||||
// assign parameter of cursor
|
||||
params["cursor"] = cursor
|
||||
} else {
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
for _k, _v := range params {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParameters builds and checks the parameters and return the result in a map object
|
||||
func (g *GetExecutionListRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (g *GetExecutionListRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := g.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if g.isVarSlice(_v) {
|
||||
g.iterateSlice(_v, func(it interface{}) {
|
||||
query.Add(_k+"[]", fmt.Sprintf("%v", it))
|
||||
})
|
||||
} else {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParametersJSON converts the parameters from GetParameters into the JSON format
|
||||
func (g *GetExecutionListRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := g.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json.Marshal(params)
|
||||
}
|
||||
|
||||
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
|
||||
func (g *GetExecutionListRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) applySlugsToUrl(url string, slugs map[string]string) string {
|
||||
for _k, _v := range slugs {
|
||||
needleRE := regexp.MustCompile(":" + _k + "\\b")
|
||||
url = needleRE.ReplaceAllString(url, _v)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
|
||||
sliceValue := reflect.ValueOf(slice)
|
||||
for _i := 0; _i < sliceValue.Len(); _i++ {
|
||||
it := sliceValue.Index(_i).Interface()
|
||||
_f(it)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := g.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
slugs[_k] = fmt.Sprintf("%v", _v)
|
||||
}
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
// GetPath returns the request path of the API
|
||||
func (g *GetExecutionListRequest) GetPath() string {
|
||||
return "/v5/execution/list"
|
||||
}
|
||||
|
||||
// Do generates the request object and send the request object to the API endpoint
|
||||
func (g *GetExecutionListRequest) Do(ctx context.Context) (*TradesResponse, error) {
|
||||
if err := GetExecutionListRequestLimiter.Wait(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// no body params
|
||||
var params interface{}
|
||||
query, err := g.GetQueryParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiURL string
|
||||
|
||||
apiURL = g.GetPath()
|
||||
|
||||
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := g.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse APIResponse
|
||||
|
||||
type responseUnmarshaler interface {
|
||||
Unmarshal(data []byte) error
|
||||
}
|
||||
|
||||
if unmarshaler, ok := interface{}(&apiResponse).(responseUnmarshaler); ok {
|
||||
if err := unmarshaler.Unmarshal(response.Body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// The line below checks the content type, however, some API server might not send the correct content type header,
|
||||
// Hence, this is commented for backward compatibility
|
||||
// response.IsJSON()
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
type responseValidator interface {
|
||||
Validate() error
|
||||
}
|
||||
|
||||
if validator, ok := interface{}(&apiResponse).(responseValidator); ok {
|
||||
if err := validator.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var data TradesResponse
|
||||
if err := json.Unmarshal(apiResponse.Result, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &data, nil
|
||||
}
|
|
@ -287,33 +287,18 @@ func (k *KLine) toGlobalKLine(symbol string) (types.KLine, error) {
|
|||
}
|
||||
|
||||
type TradeEvent struct {
|
||||
bybitapi.Trade
|
||||
// linear and inverse order id format: 42f4f364-82e1-49d3-ad1d-cd8cf9aa308d (UUID format)
|
||||
// spot: 1468264727470772736 (only numbers)
|
||||
// we only use spot trading.
|
||||
OrderId string `json:"orderId"`
|
||||
OrderLinkId string `json:"orderLinkId"`
|
||||
Category bybitapi.Category `json:"category"`
|
||||
Symbol string `json:"symbol"`
|
||||
ExecId string `json:"execId"`
|
||||
ExecPrice fixedpoint.Value `json:"execPrice"`
|
||||
ExecQty fixedpoint.Value `json:"execQty"`
|
||||
|
||||
// Is maker order. true: maker, false: taker
|
||||
IsMaker bool `json:"isMaker"`
|
||||
// Paradigm block trade ID
|
||||
BlockTradeId string `json:"blockTradeId"`
|
||||
// Order type. Market,Limit
|
||||
OrderType bybitapi.OrderType `json:"orderType"`
|
||||
// Side. Buy,Sell
|
||||
Side bybitapi.Side `json:"side"`
|
||||
// Executed timestamp(ms)
|
||||
ExecTime types.MillisecondTimestamp `json:"execTime"`
|
||||
// Closed position size
|
||||
ClosedSize fixedpoint.Value `json:"closedSize"`
|
||||
|
||||
/* The following parameters do not support SPOT trading. */
|
||||
// Executed trading fee. You can get spot fee currency instruction here. Normal spot is not supported
|
||||
ExecFee fixedpoint.Value `json:"execFee"`
|
||||
// Executed type. Normal spot is not supported
|
||||
ExecType string `json:"execType"`
|
||||
// Executed order value. Normal spot is not supported
|
||||
|
@ -377,7 +362,7 @@ func (t *TradeEvent) toGlobalTrade(symbolFee symbolFeeDetail) (*types.Trade, err
|
|||
Fee: fixedpoint.Zero,
|
||||
FeeCurrency: "",
|
||||
}
|
||||
trade.FeeCurrency, trade.Fee = calculateFee(*t, symbolFee)
|
||||
trade.FeeCurrency, trade.Fee = calculateFee(t.Trade, symbolFee)
|
||||
return trade, nil
|
||||
}
|
||||
|
||||
|
@ -400,7 +385,7 @@ func (t *TradeEvent) toGlobalTrade(symbolFee symbolFeeDetail) (*types.Trade, err
|
|||
// IsMakerOrder = FALSE
|
||||
// -> Side = Buy -> base currency (BTC)
|
||||
// -> Side = Sell -> quote currency (USDT)
|
||||
func calculateFee(t TradeEvent, feeDetail symbolFeeDetail) (string, fixedpoint.Value) {
|
||||
func calculateFee(t bybitapi.Trade, feeDetail symbolFeeDetail) (string, fixedpoint.Value) {
|
||||
if feeDetail.MakerFeeRate.Sign() > 0 || !t.IsMaker {
|
||||
if t.Side == bybitapi.SideBuy {
|
||||
return feeDetail.BaseCoin, baseCoinAsFee(t, feeDetail)
|
||||
|
@ -414,14 +399,14 @@ func calculateFee(t TradeEvent, feeDetail symbolFeeDetail) (string, fixedpoint.V
|
|||
return feeDetail.BaseCoin, baseCoinAsFee(t, feeDetail)
|
||||
}
|
||||
|
||||
func baseCoinAsFee(t TradeEvent, feeDetail symbolFeeDetail) fixedpoint.Value {
|
||||
func baseCoinAsFee(t bybitapi.Trade, feeDetail symbolFeeDetail) fixedpoint.Value {
|
||||
if t.IsMaker {
|
||||
return feeDetail.MakerFeeRate.Mul(t.ExecQty)
|
||||
}
|
||||
return feeDetail.TakerFeeRate.Mul(t.ExecQty)
|
||||
}
|
||||
|
||||
func quoteCoinAsFee(t TradeEvent, feeDetail symbolFeeDetail) fixedpoint.Value {
|
||||
func quoteCoinAsFee(t bybitapi.Trade, feeDetail symbolFeeDetail) fixedpoint.Value {
|
||||
baseFee := t.ExecPrice.Mul(t.ExecQty)
|
||||
if t.IsMaker {
|
||||
return feeDetail.MakerFeeRate.Mul(baseFee)
|
||||
|
|
|
@ -556,20 +556,22 @@ func TestTradeEvent_toGlobalTrade(t *testing.T) {
|
|||
FeeCurrency: "BTC",
|
||||
}
|
||||
tradeEvent := TradeEvent{
|
||||
OrderId: fmt.Sprintf("%d", expTrade.OrderID),
|
||||
Trade: bybitapi.Trade{
|
||||
OrderId: fmt.Sprintf("%d", expTrade.OrderID),
|
||||
Symbol: expTrade.Symbol,
|
||||
ExecId: fmt.Sprintf("%d", expTrade.ID),
|
||||
ExecPrice: expTrade.Price,
|
||||
ExecQty: expTrade.Quantity,
|
||||
IsMaker: false,
|
||||
OrderType: "",
|
||||
Side: bybitapi.SideBuy,
|
||||
ExecTime: types.MillisecondTimestamp(timeNow),
|
||||
ExecFee: fixedpoint.NewFromInt(0),
|
||||
},
|
||||
OrderLinkId: "1691419101980",
|
||||
Category: "spot",
|
||||
Symbol: expTrade.Symbol,
|
||||
ExecId: fmt.Sprintf("%d", expTrade.ID),
|
||||
ExecPrice: expTrade.Price,
|
||||
ExecQty: expTrade.Quantity,
|
||||
IsMaker: false,
|
||||
BlockTradeId: "",
|
||||
OrderType: "",
|
||||
Side: bybitapi.SideBuy,
|
||||
ExecTime: types.MillisecondTimestamp(timeNow),
|
||||
ClosedSize: fixedpoint.NewFromInt(0),
|
||||
ExecFee: fixedpoint.NewFromInt(0),
|
||||
ExecType: "",
|
||||
ExecValue: fixedpoint.NewFromInt(0),
|
||||
FeeRate: fixedpoint.NewFromInt(0),
|
||||
|
@ -602,8 +604,10 @@ func TestTradeEvent_toGlobalTrade(t *testing.T) {
|
|||
|
||||
t.Run("unexpected side", func(t *testing.T) {
|
||||
tradeEvent := TradeEvent{
|
||||
Trade: bybitapi.Trade{
|
||||
Side: bybitapi.Side("BOTH"),
|
||||
},
|
||||
Category: "spot",
|
||||
Side: bybitapi.Side("BOTH"),
|
||||
}
|
||||
|
||||
actualTrade, err := tradeEvent.toGlobalTrade(symbolFeeDetail{})
|
||||
|
@ -613,9 +617,11 @@ func TestTradeEvent_toGlobalTrade(t *testing.T) {
|
|||
|
||||
t.Run("unexpected order id", func(t *testing.T) {
|
||||
tradeEvent := TradeEvent{
|
||||
Trade: bybitapi.Trade{
|
||||
Side: bybitapi.SideBuy,
|
||||
OrderId: "ABCD3123",
|
||||
},
|
||||
Category: "spot",
|
||||
Side: bybitapi.SideBuy,
|
||||
OrderId: "ABCD3123",
|
||||
}
|
||||
|
||||
_, nerr := strconv.ParseUint(tradeEvent.OrderId, 10, 64)
|
||||
|
@ -626,10 +632,12 @@ func TestTradeEvent_toGlobalTrade(t *testing.T) {
|
|||
|
||||
t.Run("unexpected exec id", func(t *testing.T) {
|
||||
tradeEvent := TradeEvent{
|
||||
Trade: bybitapi.Trade{
|
||||
Side: bybitapi.SideBuy,
|
||||
OrderId: "3123",
|
||||
ExecId: "ABC3123",
|
||||
},
|
||||
Category: "spot",
|
||||
Side: bybitapi.SideBuy,
|
||||
OrderId: "3123",
|
||||
ExecId: "ABC3123",
|
||||
}
|
||||
|
||||
_, nerr := strconv.ParseUint(tradeEvent.ExecId, 10, 64)
|
||||
|
@ -654,13 +662,15 @@ func TestTradeEvent_CalculateFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.010000)
|
||||
price := fixedpoint.NewFromFloat(28830.8100)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: true,
|
||||
Side: bybitapi.SideBuy,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: true,
|
||||
Side: bybitapi.SideBuy,
|
||||
},
|
||||
}
|
||||
|
||||
feeCurrency, fee := calculateFee(*trade, symbolFee)
|
||||
feeCurrency, fee := calculateFee(trade.Trade, symbolFee)
|
||||
assert.Equal(t, feeCurrency, "BTC")
|
||||
assert.Equal(t, fee, qty.Mul(symbolFee.FeeRate.MakerFeeRate))
|
||||
})
|
||||
|
@ -679,13 +689,15 @@ func TestTradeEvent_CalculateFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.010000)
|
||||
price := fixedpoint.NewFromFloat(28830.8099)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: true,
|
||||
Side: bybitapi.SideSell,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: true,
|
||||
Side: bybitapi.SideSell,
|
||||
},
|
||||
}
|
||||
|
||||
feeCurrency, fee := calculateFee(*trade, symbolFee)
|
||||
feeCurrency, fee := calculateFee(trade.Trade, symbolFee)
|
||||
assert.Equal(t, feeCurrency, "USDT")
|
||||
assert.Equal(t, fee, qty.Mul(price).Mul(symbolFee.FeeRate.MakerFeeRate))
|
||||
})
|
||||
|
@ -704,13 +716,15 @@ func TestTradeEvent_CalculateFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.010000)
|
||||
price := fixedpoint.NewFromFloat(28830.8100)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Side: bybitapi.SideBuy,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Side: bybitapi.SideBuy,
|
||||
},
|
||||
}
|
||||
|
||||
feeCurrency, fee := calculateFee(*trade, symbolFee)
|
||||
feeCurrency, fee := calculateFee(trade.Trade, symbolFee)
|
||||
assert.Equal(t, feeCurrency, "BTC")
|
||||
assert.Equal(t, fee, qty.Mul(symbolFee.FeeRate.TakerFeeRate))
|
||||
})
|
||||
|
@ -729,13 +743,15 @@ func TestTradeEvent_CalculateFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.010000)
|
||||
price := fixedpoint.NewFromFloat(28830.8099)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Side: bybitapi.SideSell,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Side: bybitapi.SideSell,
|
||||
},
|
||||
}
|
||||
|
||||
feeCurrency, fee := calculateFee(*trade, symbolFee)
|
||||
feeCurrency, fee := calculateFee(trade.Trade, symbolFee)
|
||||
assert.Equal(t, feeCurrency, "USDT")
|
||||
assert.Equal(t, fee, qty.Mul(price).Mul(symbolFee.FeeRate.TakerFeeRate))
|
||||
})
|
||||
|
@ -754,13 +770,15 @@ func TestTradeEvent_CalculateFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.002289)
|
||||
price := fixedpoint.NewFromFloat(28829.7600)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: true,
|
||||
Side: bybitapi.SideBuy,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: true,
|
||||
Side: bybitapi.SideBuy,
|
||||
},
|
||||
}
|
||||
|
||||
feeCurrency, fee := calculateFee(*trade, symbolFee)
|
||||
feeCurrency, fee := calculateFee(trade.Trade, symbolFee)
|
||||
assert.Equal(t, feeCurrency, "USDT")
|
||||
assert.Equal(t, fee, qty.Mul(price).Mul(symbolFee.FeeRate.MakerFeeRate))
|
||||
})
|
||||
|
@ -779,13 +797,15 @@ func TestTradeEvent_CalculateFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.002289)
|
||||
price := fixedpoint.NewFromFloat(28829.7600)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: true,
|
||||
Side: bybitapi.SideSell,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: true,
|
||||
Side: bybitapi.SideSell,
|
||||
},
|
||||
}
|
||||
|
||||
feeCurrency, fee := calculateFee(*trade, symbolFee)
|
||||
feeCurrency, fee := calculateFee(trade.Trade, symbolFee)
|
||||
assert.Equal(t, feeCurrency, "BTC")
|
||||
assert.Equal(t, fee, qty.Mul(symbolFee.FeeRate.MakerFeeRate))
|
||||
})
|
||||
|
@ -804,13 +824,15 @@ func TestTradeEvent_CalculateFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.002289)
|
||||
price := fixedpoint.NewFromFloat(28829.7600)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Side: bybitapi.SideBuy,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Side: bybitapi.SideBuy,
|
||||
},
|
||||
}
|
||||
|
||||
feeCurrency, fee := calculateFee(*trade, symbolFee)
|
||||
feeCurrency, fee := calculateFee(trade.Trade, symbolFee)
|
||||
assert.Equal(t, feeCurrency, "BTC")
|
||||
assert.Equal(t, fee, qty.Mul(symbolFee.FeeRate.TakerFeeRate))
|
||||
})
|
||||
|
@ -829,13 +851,15 @@ func TestTradeEvent_CalculateFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.002289)
|
||||
price := fixedpoint.NewFromFloat(28829.7600)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Side: bybitapi.SideSell,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Side: bybitapi.SideSell,
|
||||
},
|
||||
}
|
||||
|
||||
feeCurrency, fee := calculateFee(*trade, symbolFee)
|
||||
feeCurrency, fee := calculateFee(trade.Trade, symbolFee)
|
||||
assert.Equal(t, feeCurrency, "USDT")
|
||||
assert.Equal(t, fee, qty.Mul(price).Mul(symbolFee.FeeRate.TakerFeeRate))
|
||||
})
|
||||
|
@ -855,14 +879,16 @@ func TestTradeEvent_baseCoinAsFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.002289)
|
||||
price := fixedpoint.NewFromFloat(28829.7600)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, symbolFee.FeeRate.TakerFeeRate.Mul(qty), baseCoinAsFee(*trade, symbolFee))
|
||||
assert.Equal(t, symbolFee.FeeRate.TakerFeeRate.Mul(qty), baseCoinAsFee(trade.Trade, symbolFee))
|
||||
|
||||
trade.IsMaker = true
|
||||
assert.Equal(t, symbolFee.FeeRate.MakerFeeRate.Mul(qty), baseCoinAsFee(*trade, symbolFee))
|
||||
assert.Equal(t, symbolFee.FeeRate.MakerFeeRate.Mul(qty), baseCoinAsFee(trade.Trade, symbolFee))
|
||||
}
|
||||
|
||||
func TestTradeEvent_quoteCoinAsFee(t *testing.T) {
|
||||
|
@ -878,12 +904,14 @@ func TestTradeEvent_quoteCoinAsFee(t *testing.T) {
|
|||
qty := fixedpoint.NewFromFloat(0.002289)
|
||||
price := fixedpoint.NewFromFloat(28829.7600)
|
||||
trade := &TradeEvent{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
Trade: bybitapi.Trade{
|
||||
ExecPrice: price,
|
||||
ExecQty: qty,
|
||||
IsMaker: false,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, symbolFee.FeeRate.TakerFeeRate.Mul(qty.Mul(price)), quoteCoinAsFee(*trade, symbolFee))
|
||||
assert.Equal(t, symbolFee.FeeRate.TakerFeeRate.Mul(qty.Mul(price)), quoteCoinAsFee(trade.Trade, symbolFee))
|
||||
|
||||
trade.IsMaker = true
|
||||
assert.Equal(t, symbolFee.FeeRate.MakerFeeRate.Mul(qty.Mul(price)), quoteCoinAsFee(*trade, symbolFee))
|
||||
assert.Equal(t, symbolFee.FeeRate.MakerFeeRate.Mul(qty.Mul(price)), quoteCoinAsFee(trade.Trade, symbolFee))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user