all: add subscribe depth options

This commit is contained in:
c9s 2022-01-12 22:27:42 +08:00
parent f9e72dc79f
commit ec72a922c8
3 changed files with 55 additions and 11 deletions

View File

@ -157,7 +157,7 @@ func toGlobalFuturesBalance(balances []*futures.Balance) types.BalanceMap {
func toGlobalFuturesPositions(futuresPositions []*futures.AccountPosition) types.FuturesPositionMap { func toGlobalFuturesPositions(futuresPositions []*futures.AccountPosition) types.FuturesPositionMap {
retFuturesPositions := make(types.FuturesPositionMap) retFuturesPositions := make(types.FuturesPositionMap)
for _, futuresPosition := range futuresPositions { for _, futuresPosition := range futuresPositions {
retFuturesPositions[futuresPosition.Symbol] = types.FuturesPosition{ //TODO: types.FuturesPosition retFuturesPositions[futuresPosition.Symbol] = types.FuturesPosition{ // TODO: types.FuturesPosition
Isolated: futuresPosition.Isolated, Isolated: futuresPosition.Isolated,
PositionRisk: &types.PositionRisk{ PositionRisk: &types.PositionRisk{
Leverage: fixedpoint.MustNewFromString(futuresPosition.Leverage), Leverage: fixedpoint.MustNewFromString(futuresPosition.Leverage),
@ -172,7 +172,7 @@ func toGlobalFuturesPositions(futuresPositions []*futures.AccountPosition) types
func toGlobalFuturesUserAssets(assets []*futures.AccountAsset) (retAssets map[types.Asset]types.FuturesUserAsset) { func toGlobalFuturesUserAssets(assets []*futures.AccountAsset) (retAssets map[types.Asset]types.FuturesUserAsset) {
for _, asset := range assets { for _, asset := range assets {
//TODO: or modify to type FuturesAssetMap map[string]FuturesAssetMap // TODO: or modify to type FuturesAssetMap map[string]FuturesAssetMap
retAssets[types.Asset{Currency: asset.Asset}] = types.FuturesUserAsset{ retAssets[types.Asset{Currency: asset.Asset}] = types.FuturesUserAsset{
Asset: asset.Asset, Asset: asset.Asset,
InitialMargin: fixedpoint.MustNewFromString(asset.InitialMargin), InitialMargin: fixedpoint.MustNewFromString(asset.InitialMargin),
@ -565,7 +565,28 @@ func convertSubscription(s types.Subscription) string {
// depth values: 5, 10, 20 // depth values: 5, 10, 20
// Stream Names: <symbol>@depth<levels> OR <symbol>@depth<levels>@100ms. // Stream Names: <symbol>@depth<levels> OR <symbol>@depth<levels>@100ms.
// Update speed: 1000ms or 100ms // Update speed: 1000ms or 100ms
return fmt.Sprintf("%s@depth10@100ms", strings.ToLower(s.Symbol)) n := strings.ToLower(s.Symbol) + "@depth"
switch s.Options.Depth {
case types.DepthLevel5:
n += "5"
case types.DepthLevelMedium:
n += "20"
case types.DepthLevelFull:
default:
}
switch s.Options.Speed {
case types.SpeedHigh:
n += "@100ms"
case types.SpeedLow:
n += "@1000ms"
}
return n
case types.BookTickerChannel: case types.BookTickerChannel:
return fmt.Sprintf("%s@bookTicker", strings.ToLower(s.Symbol)) return fmt.Sprintf("%s@bookTicker", strings.ToLower(s.Symbol))
} }

View File

@ -7,7 +7,6 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"os" "os"
"strconv"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -73,14 +72,19 @@ func (s *Stream) handleConnect() {
Action: "subscribe", Action: "subscribe",
} }
for _, sub := range s.Subscriptions { for _, sub := range s.Subscriptions {
var err error
var depth int var depth int
if len(sub.Options.Depth) > 0 { if len(sub.Options.Depth) > 0 {
depth, err = strconv.Atoi(sub.Options.Depth) switch sub.Options.Depth {
if err != nil { case types.DepthLevelFull:
log.WithError(err).Errorf("depth parse error, given %v", sub.Options.Depth) depth = 0
continue
case types.DepthLevelMedium:
depth = 20
case types.DepthLevel5:
depth = 5
} }
} }

View File

@ -387,10 +387,29 @@ func (s *StandardStream) Close() error {
return nil return nil
} }
type Depth string
const (
DepthLevelFull Depth = "FULL"
DepthLevelMedium Depth = "MEDIUM"
DepthLevel1 Depth = "1"
DepthLevel5 Depth = "5"
DepthLevel20 Depth = "20"
)
type Speed string
const (
SpeedHigh Speed = "HIGH"
SpeedMedium Speed = "MEDIUM"
SpeedLow Speed = "LOW"
)
// SubscribeOptions provides the standard stream options // SubscribeOptions provides the standard stream options
type SubscribeOptions struct { type SubscribeOptions struct {
Interval string `json:"interval,omitempty"` Interval string `json:"interval,omitempty"`
Depth string `json:"depth,omitempty"` Depth Depth `json:"depth,omitempty"`
Speed Speed `json:"speed,omitempty"`
} }
func (o SubscribeOptions) String() string { func (o SubscribeOptions) String() string {
@ -398,7 +417,7 @@ func (o SubscribeOptions) String() string {
return o.Interval return o.Interval
} }
return o.Depth return string(o.Depth)
} }
type Subscription struct { type Subscription struct {