/
public.go
184 lines (150 loc) · 4.29 KB
/
public.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package max
import (
"context"
"fmt"
"strings"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type PublicService struct {
client *RestClient
}
type Market struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"market_status"` // active
BaseUnit string `json:"base_unit"`
BaseUnitPrecision int `json:"base_unit_precision"`
QuoteUnit string `json:"quote_unit"`
QuoteUnitPrecision int `json:"quote_unit_precision"`
MinBaseAmount fixedpoint.Value `json:"min_base_amount"`
MinQuoteAmount fixedpoint.Value `json:"min_quote_amount"`
SupportMargin bool `json:"m_wallet_supported"`
}
type Ticker struct {
Time time.Time
At int64 `json:"at"`
Buy fixedpoint.Value `json:"buy"`
Sell fixedpoint.Value `json:"sell"`
Open fixedpoint.Value `json:"open"`
High fixedpoint.Value `json:"high"`
Low fixedpoint.Value `json:"low"`
Last fixedpoint.Value `json:"last"`
Volume fixedpoint.Value `json:"vol"`
VolumeInBTC fixedpoint.Value `json:"vol_in_btc"`
}
func (s *PublicService) Timestamp(ctx context.Context) (int64, error) {
req := s.client.NewGetTimestampRequest()
ts, err := req.Do(ctx)
if err != nil || ts == nil {
return 0, nil
}
return int64(*ts), nil
}
func (s *PublicService) Markets(ctx context.Context) ([]Market, error) {
req := s.client.NewGetMarketsRequest()
return req.Do(ctx)
}
func (s *PublicService) Tickers(ctx context.Context) (TickerMap, error) {
req := s.client.NewGetTickersRequest()
return req.Do(ctx)
}
func (s *PublicService) Ticker(market string) (*Ticker, error) {
req := s.client.NewGetTickerRequest()
req.Market(market)
return req.Do(context.Background())
}
type Interval int64
func ParseInterval(a string) (Interval, error) {
switch strings.ToLower(a) {
case "1m":
return 1, nil
case "5m":
return 5, nil
case "15m":
return 15, nil
case "30m":
return 30, nil
case "1h":
return 60, nil
case "2h":
return 60 * 2, nil
case "3h":
return 60 * 3, nil
case "4h":
return 60 * 4, nil
case "6h":
return 60 * 6, nil
case "8h":
return 60 * 8, nil
case "12h":
return 60 * 12, nil
case "1d":
return 60 * 24, nil
case "3d":
return 60 * 24 * 3, nil
case "1w":
return 60 * 24 * 7, nil
}
return 0, fmt.Errorf("incorrect resolution: %q", a)
}
type KLine struct {
Symbol string
Interval string
StartTime, EndTime time.Time
Open, High, Low, Close fixedpoint.Value
Volume fixedpoint.Value
Closed bool
}
func (k KLine) KLine() types.KLine {
return types.KLine{
Exchange: types.ExchangeMax,
Symbol: strings.ToUpper(k.Symbol), // global symbol
Interval: types.Interval(k.Interval),
StartTime: types.Time(k.StartTime),
EndTime: types.Time(k.EndTime),
Open: k.Open,
Close: k.Close,
High: k.High,
Low: k.Low,
Volume: k.Volume,
// QuoteVolume: util.MustParseFloat(k.QuoteAssetVolume),
// LastTradeID: 0,
// NumberOfTrades: k.TradeNum,
Closed: k.Closed,
}
}
func (s *PublicService) KLines(symbol string, resolution string, start time.Time, limit int) ([]KLine, error) {
interval, err := ParseInterval(resolution)
if err != nil {
return nil, err
}
req := s.client.NewGetKLinesRequest()
req.Market(symbol).Period(int(interval)).Timestamp(start).Limit(limit)
data, err := req.Do(context.Background())
if err != nil {
return nil, err
}
var kLines []KLine
for _, slice := range data {
ts := int64(slice[0])
startTime := time.Unix(ts, 0)
endTime := startTime.Add(time.Duration(interval)*time.Minute - time.Millisecond)
isClosed := time.Now().Before(endTime)
kLines = append(kLines, KLine{
Symbol: symbol,
Interval: resolution,
StartTime: startTime,
EndTime: endTime,
Open: fixedpoint.NewFromFloat(slice[1]),
High: fixedpoint.NewFromFloat(slice[2]),
Low: fixedpoint.NewFromFloat(slice[3]),
Close: fixedpoint.NewFromFloat(slice[4]),
Volume: fixedpoint.NewFromFloat(slice[5]),
Closed: isClosed,
})
}
return kLines, nil
// return parseKLines(resp.Body, symbol, resolution, interval)
}