Skip to content

Commit

Permalink
pkg/exchange: add query trade api
Browse files Browse the repository at this point in the history
  • Loading branch information
bailantaotao committed Jul 28, 2023
1 parent 7eb6e40 commit d857721
Show file tree
Hide file tree
Showing 8 changed files with 579 additions and 11 deletions.
13 changes: 13 additions & 0 deletions pkg/exchange/bybit/bybitapi/v3/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v3

import (
"github.com/c9s/requestgen"

"github.com/c9s/bbgo/pkg/exchange/bybit/bybitapi"
)

type APIResponse = bybitapi.APIResponse

type Client struct {
Client requestgen.AuthenticatedAPIClient
}
44 changes: 44 additions & 0 deletions pkg/exchange/bybit/bybitapi/v3/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v3

import (
"context"
"os"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/c9s/bbgo/pkg/exchange/bybit/bybitapi"
"github.com/c9s/bbgo/pkg/testutil"
)

func getTestClientOrSkip(t *testing.T) *bybitapi.RestClient {
if b, _ := strconv.ParseBool(os.Getenv("CI")); b {
t.Skip("skip test for CI")
}

key, secret, ok := testutil.IntegrationTestConfigured(t, "BYBIT")
if !ok {
t.Skip("BYBIT_* env vars are not configured")
return nil
}

client, err := bybitapi.NewClient()
assert.NoError(t, err)
client.Auth(key, secret)
return client
}

func TestClient(t *testing.T) {
client := getTestClientOrSkip(t)
v3Client := Client{Client: client}
ctx := context.Background()

t.Run("GetTradeRequest", func(t *testing.T) {
startTime := time.Date(2023, 7, 27, 16, 13, 9, 0, time.UTC)
apiResp, err := v3Client.NewGetTradesRequest().Symbol("BTCUSDT").StartTime(startTime).Do(ctx)
assert.NoError(t, err)
t.Logf("apiResp: %+v", apiResp)
})
}
55 changes: 55 additions & 0 deletions pkg/exchange/bybit/bybitapi/v3/get_trades_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package v3

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 {
List []Trade `json:"list"`
}

type Trade struct {
Symbol string `json:"symbol"`
Id string `json:"id"`
OrderId string `json:"orderId"`
TradeId string `json:"tradeId"`
OrderPrice fixedpoint.Value `json:"orderPrice"`
OrderQty fixedpoint.Value `json:"orderQty"`
ExecFee fixedpoint.Value `json:"execFee"`
FeeTokenId string `json:"feeTokenId"`
CreatTime types.MillisecondTimestamp `json:"creatTime"`
IsBuyer Side `json:"isBuyer"`
IsMaker OrderType `json:"isMaker"`
MatchOrderId string `json:"matchOrderId"`
MakerRebate fixedpoint.Value `json:"makerRebate"`
ExecutionTime types.MillisecondTimestamp `json:"executionTime"`
BlockTradeId string `json:"blockTradeId"`
}

//go:generate GetRequest -url "/spot/v3/private/my-trades" -type GetTradesRequest -responseDataType .TradesResponse
type GetTradesRequest struct {
client requestgen.AuthenticatedAPIClient

symbol *string `param:"symbol,query"`
orderId *string `param:"orderId,query"`
// Limit default value is 50, max 50
limit *uint64 `param:"limit,query"`
startTime *time.Time `param:"startTime,query,milliseconds"`
endTime *time.Time `param:"endTime,query,milliseconds"`
fromTradeId *string `param:"fromTradeId,query"`
toTradeId *string `param:"toTradeId,query"`
}

func (c *Client) NewGetTradesRequest() *GetTradesRequest {
return &GetTradesRequest{
client: c.Client,
}
}
238 changes: 238 additions & 0 deletions pkg/exchange/bybit/bybitapi/v3/get_trades_request_requestgen.go

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

15 changes: 15 additions & 0 deletions pkg/exchange/bybit/bybitapi/v3/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v3

type Side string

const (
SideBuy Side = "0"
SideSell Side = "1"
)

type OrderType string

const (
OrderTypeMaker OrderType = "0"
OrderTypeTaker OrderType = "1"
)
Loading

0 comments on commit d857721

Please sign in to comment.