f5e2e178e1
Split market pair logic into spot and futures files and rename price files to plural data-dimension names without changing behavior. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
package market
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (ms *MarketData) GetFuturePrice(symbol string) (float64, float64, int64, bool) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
premiums, err := ms.futuresClient.NewPremiumIndexService().Symbol(symbol).Do(ctx)
|
|
if err != nil {
|
|
log.Error().Err(err).Str("symbol", symbol).Msg("Failed to fetch futures premium index")
|
|
return 0, 0, 0, false
|
|
}
|
|
|
|
if len(premiums) == 0 {
|
|
return 0, 0, 0, false
|
|
}
|
|
|
|
p := premiums[0]
|
|
markPrice, err := strconv.ParseFloat(p.MarkPrice, 64)
|
|
if err != nil {
|
|
return 0, 0, 0, false
|
|
}
|
|
|
|
fundingRate, err := strconv.ParseFloat(p.LastFundingRate, 64)
|
|
if err != nil {
|
|
fundingRate = 0
|
|
}
|
|
|
|
return markPrice, fundingRate, p.NextFundingTime, true
|
|
}
|
|
|
|
type PremiumIndex struct {
|
|
MarkPrice float64
|
|
FundingRate float64
|
|
NextFundingTime int64
|
|
}
|
|
|
|
func (ms *MarketData) GetAllPremiumIndex() (map[string]PremiumIndex, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
premiums, err := ms.futuresClient.NewPremiumIndexService().Do(ctx)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("Failed to fetch all futures premium index")
|
|
return nil, err
|
|
}
|
|
|
|
result := make(map[string]PremiumIndex, len(premiums))
|
|
for _, p := range premiums {
|
|
markPrice, err := strconv.ParseFloat(p.MarkPrice, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
rate, _ := strconv.ParseFloat(p.LastFundingRate, 64)
|
|
result[p.Symbol] = PremiumIndex{
|
|
MarkPrice: markPrice,
|
|
FundingRate: rate,
|
|
NextFundingTime: p.NextFundingTime,
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (ms *MarketData) GetAllFundRate() (map[string]float64, map[string]int64) {
|
|
all, err := ms.GetAllPremiumIndex()
|
|
if err != nil {
|
|
return make(map[string]float64), make(map[string]int64)
|
|
}
|
|
|
|
rates := make(map[string]float64, len(all))
|
|
times := make(map[string]int64, len(all))
|
|
for sym, p := range all {
|
|
rates[sym] = p.FundingRate
|
|
times[sym] = p.NextFundingTime
|
|
}
|
|
return rates, times
|
|
}
|