forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_tickers_request.go
72 lines (60 loc) · 2.36 KB
/
get_tickers_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package bybitapi
import (
"context"
"encoding/json"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/requestgen"
)
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Result
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Result
type Tickers struct {
Category Category `json:"category"`
List []Ticker `json:"list"`
// ClosedTime is current timestamp (ms). This value is obtained from outside APIResponse.
ClosedTime types.MillisecondTimestamp
}
type Ticker struct {
Symbol string `json:"symbol"`
Bid1Price fixedpoint.Value `json:"bid1Price"`
Bid1Size fixedpoint.Value `json:"bid1Size"`
Ask1Price fixedpoint.Value `json:"ask1Price"`
Ask1Size fixedpoint.Value `json:"ask1Size"`
LastPrice fixedpoint.Value `json:"lastPrice"`
PrevPrice24H fixedpoint.Value `json:"prevPrice24h"`
Price24HPcnt fixedpoint.Value `json:"price24hPcnt"`
HighPrice24H fixedpoint.Value `json:"highPrice24h"`
LowPrice24H fixedpoint.Value `json:"lowPrice24h"`
Turnover24H fixedpoint.Value `json:"turnover24h"`
Volume24H fixedpoint.Value `json:"volume24h"`
UsdIndexPrice fixedpoint.Value `json:"usdIndexPrice"`
}
// GetTickersRequest without **-responseDataType .InstrumentsInfo** in generation command, because the caller
// needs the APIResponse.Time. We implemented the DoWithResponseTime to handle this.
//
//go:generate GetRequest -url "/v5/market/tickers" -type GetTickersRequest
type GetTickersRequest struct {
client requestgen.APIClient
category Category `param:"category,query" validValues:"spot"`
symbol *string `param:"symbol,query"`
}
func (c *RestClient) NewGetTickersRequest() *GetTickersRequest {
return &GetTickersRequest{
client: c,
category: CategorySpot,
}
}
func (g *GetTickersRequest) DoWithResponseTime(ctx context.Context) (*Tickers, error) {
resp, err := g.Do(ctx)
if err != nil {
return nil, err
}
var data Tickers
if err := json.Unmarshal(resp.Result, &data); err != nil {
return nil, err
}
// Our types.Ticker requires the closed time, but this API does not provide it. This API returns the Tickers of the
// past 24 hours, so in terms of closed time, it is the current time, so fill it in Tickers.ClosedTime.
data.ClosedTime = resp.Time
return &data, nil
}