bbgo_origin/pkg/exchange/max/maxapi/public.go

185 lines
4.3 KiB
Go
Raw Permalink Normal View History

2020-10-02 02:10:59 +00:00
package max
import (
"context"
"fmt"
"strings"
2020-10-02 02:10:59 +00:00
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
2020-10-11 08:46:15 +00:00
"github.com/c9s/bbgo/pkg/types"
2020-10-02 02:10:59 +00:00
)
type PublicService struct {
client *RestClient
2020-10-02 02:10:59 +00:00
}
type Market struct {
2022-05-25 12:34:25 +00:00
ID string `json:"id"`
Name string `json:"name"`
2022-05-26 12:32:25 +00:00
Status string `json:"market_status"` // active
2022-05-25 12:34:25 +00:00
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"`
2022-05-26 12:32:25 +00:00
SupportMargin bool `json:"m_wallet_supported"`
2020-10-02 02:10:59 +00:00
}
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"`
2020-10-02 02:10:59 +00:00
}
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
2020-10-02 02:10:59 +00:00
}
return int64(*ts), nil
2020-10-02 02:10:59 +00:00
}
func (s *PublicService) Markets(ctx context.Context) ([]Market, error) {
req := s.client.NewGetMarketsRequest()
return req.Do(ctx)
2020-10-02 02:10:59 +00:00
}
func (s *PublicService) Tickers(ctx context.Context) (TickerMap, error) {
req := s.client.NewGetTickersRequest()
return req.Do(ctx)
2020-10-02 02:10:59 +00:00
}
func (s *PublicService) Ticker(market string) (*Ticker, error) {
req := s.client.NewGetTickerRequest()
req.Market(market)
return req.Do(context.Background())
2020-10-02 02:10:59 +00:00
}
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
}
2020-11-09 08:34:35 +00:00
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,
2020-11-05 07:04:56 +00:00
Symbol: strings.ToUpper(k.Symbol), // global symbol
2020-10-28 01:43:19 +00:00
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
}
2023-04-12 14:43:32 +00:00
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)
}