max: log adratio

This commit is contained in:
c9s 2022-05-25 20:34:25 +08:00
parent 459d839c1a
commit 4d8ea7d979
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
6 changed files with 86 additions and 49 deletions

View File

@ -0,0 +1,15 @@
---
sessions:
binance_margin_linkusdt:
exchange: binance
margin: true
isolatedMargin: true
isolatedMarginSymbol: LINKUSDT
exchangeStrategies:
- on: binance_margin_linkusdt
dummy:
symbol: LINKUSDT
interval: 1m

13
config/max-margin.yaml Normal file
View File

@ -0,0 +1,13 @@
---
sessions:
max_margin:
exchange: max
margin: true
exchangeStrategies:
- on: max_margin
pricealert:
symbol: LINKUSDT
interval: 1m

View File

@ -3,7 +3,6 @@ package max
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"
@ -21,12 +20,12 @@ type PublicService struct {
}
type Market struct {
ID string `json:"id"`
Name string `json:"name"`
BaseUnit string `json:"base_unit"`
BaseUnitPrecision int `json:"base_unit_precision"`
QuoteUnit string `json:"quote_unit"`
QuoteUnitPrecision int `json:"quote_unit_precision"`
ID string `json:"id"`
Name string `json:"name"`
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"`
}
@ -256,23 +255,12 @@ func (s *PublicService) KLines(symbol string, resolution string, start time.Time
return nil, fmt.Errorf("request build error: %s", err.Error())
}
resp, err := s.client.Do(req)
resp, err := s.client.SendRequest(req)
if err != nil {
return nil, fmt.Errorf("request failed: %s", err.Error())
}
defer func() {
if err := resp.Body.Close(); err != nil {
logger.WithError(err).Error("failed to close resp body")
}
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return parseKLines(body, symbol, resolution, interval)
return parseKLines(resp.Body, symbol, resolution, interval)
}
func parseKLines(payload []byte, symbol, resolution string, interval Interval) (klines []KLine, err error) {

View File

@ -2,6 +2,7 @@ package max
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
@ -213,17 +214,19 @@ type ADRatio struct {
TU types.MillisecondTimestamp `json:"TU"`
}
func parseADRatio(v *fastjson.Value) (*ADRatio, error) {
o, err := v.StringBytes()
if err != nil {
return nil, err
}
func (r *ADRatio) String() string {
return fmt.Sprintf("ADRatio: %v Asset: %v USDT, Debt: %v USDT (Mark Prices: %+v)", r.ADRatio, r.AssetInUSDT, r.DebtInUSDT, r.IndexPrices)
}
adRatio := struct {
ADRatio ADRatio `json:"ad"`
}{}
err = json.Unmarshal(o, &adRatio)
return &adRatio.ADRatio, err
type ADRatioEvent struct {
ADRatio ADRatio `json:"ad"`
}
func parseADRatioEvent(v *fastjson.Value) (*ADRatioEvent, error) {
o := v.String()
e := ADRatioEvent{}
err := json.Unmarshal([]byte(o), &e)
return &e, err
}
type Debt struct {
@ -233,17 +236,15 @@ type Debt struct {
TU types.MillisecondTimestamp `json:"TU"`
}
func parseDebts(v *fastjson.Value) ([]Debt, error) {
o, err := v.StringBytes()
if err != nil {
return nil, err
}
type DebtEvent struct {
Debts []Debt `json:"db"`
}
m := struct {
Debts []Debt `json:"db"`
}{}
err = json.Unmarshal(o, &m)
return m.Debts, err
func parseDebts(v *fastjson.Value) (*DebtEvent, error) {
o := v.String()
e := DebtEvent{}
err := json.Unmarshal([]byte(o), &e)
return &e, err
}
func ParseUserEvent(v *fastjson.Value) (interface{}, error) {
@ -262,19 +263,15 @@ func ParseUserEvent(v *fastjson.Value) (interface{}, error) {
return parseTradeUpdateEvent(v), nil
case "ad_ratio_snapshot", "ad_ratio_update":
return parseADRatio(v)
return parseADRatioEvent(v)
case "borrowing_snapshot", "borrowing_update":
return parseDebts(v)
case "account_snapshot", "account_update", "mwallet_account_snapshot", "mwallet_account_update":
var e AccountUpdateEvent
o, err := v.StringBytes()
if err != nil {
return nil, err
}
err = json.Unmarshal(o, &e)
o := v.String()
err := json.Unmarshal([]byte(o), &e)
return &e, err
case "error":

View File

@ -33,6 +33,8 @@ type Stream struct {
tradeSnapshotEventCallbacks []func(e max.TradeSnapshotEvent)
orderUpdateEventCallbacks []func(e max.OrderUpdateEvent)
orderSnapshotEventCallbacks []func(e max.OrderSnapshotEvent)
adRatioEventCallbacks []func(e max.ADRatioEvent)
debtEventCallbacks []func(e max.DebtEvent)
accountSnapshotEventCallbacks []func(e max.AccountSnapshotEvent)
accountUpdateEventCallbacks []func(e max.AccountUpdateEvent)
@ -47,7 +49,6 @@ func NewStream(key, secret string) *Stream {
stream.SetEndpointCreator(stream.getEndpoint)
stream.SetParser(max.ParseMessage)
stream.SetDispatcher(stream.dispatchEvent)
stream.OnConnect(stream.handleConnect)
stream.OnKLineEvent(stream.handleKLineEvent)
stream.OnOrderSnapshotEvent(stream.handleOrderSnapshotEvent)
@ -257,8 +258,11 @@ func (s *Stream) dispatchEvent(e interface{}) {
case *max.OrderUpdateEvent:
s.EmitOrderUpdateEvent(*e)
case *max.ADRatioEvent:
log.Info(e.ADRatio.String())
default:
log.Errorf("unsupported %T event: %+v", e, e)
log.Warnf("unhandled %T event: %+v", e, e)
}
}

View File

@ -106,6 +106,26 @@ func (s *Stream) EmitOrderSnapshotEvent(e max.OrderSnapshotEvent) {
}
}
func (s *Stream) OnAdRatioEvent(cb func(e max.ADRatioEvent)) {
s.adRatioEventCallbacks = append(s.adRatioEventCallbacks, cb)
}
func (s *Stream) EmitAdRatioEvent(e max.ADRatioEvent) {
for _, cb := range s.adRatioEventCallbacks {
cb(e)
}
}
func (s *Stream) OnDebtEvent(cb func(e max.DebtEvent)) {
s.debtEventCallbacks = append(s.debtEventCallbacks, cb)
}
func (s *Stream) EmitDebtEvent(e max.DebtEvent) {
for _, cb := range s.debtEventCallbacks {
cb(e)
}
}
func (s *Stream) OnAccountSnapshotEvent(cb func(e max.AccountSnapshotEvent)) {
s.accountSnapshotEventCallbacks = append(s.accountSnapshotEventCallbacks, cb)
}