pull out global premium index type and funding rate type

This commit is contained in:
c9s 2021-12-09 00:10:18 +08:00
parent 71e043e4b2
commit ca85aa69e6
5 changed files with 38 additions and 24 deletions

View File

@ -342,7 +342,7 @@ func convertSubscription(s types.Subscription) string {
return fmt.Sprintf("%s@%s", strings.ToLower(s.Symbol), s.Channel)
}
func convertPremiumIndex(index *futures.PremiumIndex) (*PremiumIndex, error) {
func convertPremiumIndex(index *futures.PremiumIndex) (*types.PremiumIndex, error) {
markPrice, err := fixedpoint.NewFromString(index.MarkPrice)
if err != nil {
return nil, err
@ -356,7 +356,7 @@ func convertPremiumIndex(index *futures.PremiumIndex) (*PremiumIndex, error) {
nextFundingTime := time.Unix(0, index.NextFundingTime*int64(time.Millisecond))
t := time.Unix(0, index.Time*int64(time.Millisecond))
return &PremiumIndex{
return &types.PremiumIndex{
Symbol: index.Symbol,
MarkPrice: markPrice,
NextFundingTime: nextFundingTime,

View File

@ -885,22 +885,7 @@ func (e *Exchange) BatchQueryKLines(ctx context.Context, symbol string, interval
return allKLines, nil
}
type FundingRate struct {
FundingRate fixedpoint.Value
FundingTime time.Time
Time time.Time
}
type PremiumIndex struct {
Symbol string `json:"symbol"`
MarkPrice fixedpoint.Value `json:"markPrice"`
LastFundingRate fixedpoint.Value `json:"lastFundingRate"`
NextFundingTime time.Time `json:"nextFundingTime"`
Time time.Time `json:"time"`
}
func (e *Exchange) QueryPremiumIndex(ctx context.Context, symbol string) (*PremiumIndex, error) {
func (e *Exchange) QueryPremiumIndex(ctx context.Context, symbol string) (*types.PremiumIndex, error) {
futuresClient := binance.NewFuturesClient(e.key, e.secret)
// when symbol is set, only one index will be returned.
@ -912,7 +897,7 @@ func (e *Exchange) QueryPremiumIndex(ctx context.Context, symbol string) (*Premi
return convertPremiumIndex(indexes[0])
}
func (e *Exchange) QueryFundingRateHistory(ctx context.Context, symbol string) (*FundingRate, error) {
func (e *Exchange) QueryFundingRateHistory(ctx context.Context, symbol string) (*types.FundingRate, error) {
futuresClient := binance.NewFuturesClient(e.key, e.secret)
rates, err := futuresClient.NewFundingRateService().
Symbol(symbol).
@ -932,7 +917,7 @@ func (e *Exchange) QueryFundingRateHistory(ctx context.Context, symbol string) (
return nil, err
}
return &FundingRate{
return &types.FundingRate{
FundingRate: fundingRate,
FundingTime: time.Unix(0, rate.FundingTime*int64(time.Millisecond)),
Time: time.Unix(0, rate.Time*int64(time.Millisecond)),

View File

@ -4,13 +4,14 @@ import (
"context"
"errors"
"fmt"
"github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/sirupsen/logrus"
"math"
"strings"
"time"
"github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
)
@ -87,7 +88,7 @@ func (s *Strategy) Validate() error {
}
func (s *Strategy) listenToFundingRate(ctx context.Context, exchange *binance.Exchange) {
var previousIndex, fundingRate24HoursLowIndex *binance.PremiumIndex
var previousIndex, fundingRate24HoursLowIndex *types.PremiumIndex
fundingRateTicker := time.NewTicker(1 * time.Hour)
defer fundingRateTicker.Stop()

13
pkg/types/fundingrate.go Normal file
View File

@ -0,0 +1,13 @@
package types
import (
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
type FundingRate struct {
FundingRate fixedpoint.Value
FundingTime time.Time
Time time.Time
}

15
pkg/types/premiumindex.go Normal file
View File

@ -0,0 +1,15 @@
package types
import (
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
type PremiumIndex struct {
Symbol string `json:"symbol"`
MarkPrice fixedpoint.Value `json:"markPrice"`
LastFundingRate fixedpoint.Value `json:"lastFundingRate"`
NextFundingTime time.Time `json:"nextFundingTime"`
Time time.Time `json:"time"`
}