hirokisan / bybit

Bybit client library for Go
https://pkg.go.dev/github.com/hirokisan/bybit/v2
MIT License
98 stars 64 forks source link

Fix WebSocket data parsing to address omission of category indicators #170

Closed kazz187 closed 6 months ago

kazz187 commented 6 months ago

Description

This pull request addresses the need for adjustments due to changes in the structure of data updates from bybit's WebSocket service. Previously, certain columns such as Bid1Price and Gamma were included in updates, serving as indicators for categorizing data as linear, inverse, option, or spot. The omission of these columns in recent updates necessitated a new approach to ensure continued accurate categorization and processing.

Key Changes

hirokisan commented 6 months ago

📝 I'm a little busy, so it will take me a while to review

hirokisan commented 6 months ago

The omission of these columns in recent updates

It does not seem to be specifically mentioned in the documentation below. https://bybit-exchange.github.io/docs/v5/websocket/public/ticker

I will run the following to check.

$ git branch --show-current
fix-v5-ws-public-ticker
package main

import (
    "context"
    "log"

    "github.com/hirokisan/bybit/v2"
)

func main() {
    if err := run(); err != nil {
        log.Fatal(err)
    }
}

func run() error {
    wsClient := bybit.NewWebsocketClient()
    svc, err := wsClient.V5().Public(bybit.CategoryV5Linear)
    if err != nil {
        return err
    }
    if _, err := svc.SubscribeTicker(
        bybit.V5WebsocketPublicTickerParamKey{
            Symbol: bybit.SymbolV5("ADAUSDT"),
        },
        func(response bybit.V5WebsocketPublicTickerResponse) error {
            log.Printf("%+v\n", response.Data.LinearInverse)
            return nil
        },
    ); err != nil {
        return err
    }

    if err := svc.Start(context.Background(), func(isWebsocketClosed bool, err error) {
        if isWebsocketClosed {
            log.Println("closed: ", err)
        }
    }); err != nil {
        return err
    }
    return nil
}

Here are the results.

2024/03/27 08:45:42 &{Symbol:ADAUSDT TickDirection:MinusTick Price24hPercent:0.014945 LastPrice:0.6655 PrevPrice24h: HighPrice24h: LowPrice24h: PrevPrice1h: MarkPrice: IndexPrice: OpenInterest: OpenInterestValue: Turnover24h:96135777.2116 Volume24h:144049355.0000 NextFundingTime: FundingRate: Bid1Price:0.6654 Bid1Size:700 Ask1Price:0.6655 Ask1Size:320 DeliveryTime: BasisRate: DeliveryFeeRate: PredictedDeliveryPrice:}
2024/03/27 08:45:42 &{Symbol:ADAUSDT TickDirection:ZeroMinusTick Price24hPercent: LastPrice: PrevPrice24h: HighPrice24h: LowPrice24h: PrevPrice1h: MarkPrice: IndexPrice: OpenInterest: OpenInterestValue: Turnover24h: Volume24h: NextFundingTime: FundingRate: Bid1Price:0.6653 Bid1Size:16875 Ask1Price:0.6654 Ask1Size:8006 DeliveryTime: BasisRate: DeliveryFeeRate: PredictedDeliveryPrice:}
2024/03/27 08:45:43 &{Symbol:ADAUSDT TickDirection: Price24hPercent: LastPrice: PrevPrice24h: HighPrice24h: LowPrice24h: PrevPrice1h: MarkPrice: IndexPrice: OpenInterest: OpenInterestValue: Turnover24h: Volume24h: NextFundingTime: FundingRate: Bid1Price:0.6653 Bid1Size:14617 Ask1Price:0.6654 Ask1Size:9159 DeliveryTime: BasisRate: DeliveryFeeRate: PredictedDeliveryPrice:}
2024/03/27 08:45:43 &{Symbol:ADAUSDT TickDirection: Price24hPercent: LastPrice: PrevPrice24h: HighPrice24h: LowPrice24h: PrevPrice1h: MarkPrice: IndexPrice: OpenInterest: OpenInterestValue: Turnover24h: Volume24h: NextFundingTime: FundingRate: Bid1Price: Bid1Size: Ask1Price:0.6654 Ask1Size:11876 DeliveryTime: BasisRate: DeliveryFeeRate: PredictedDeliveryPrice:}

So I thought the change seemed reasonable 👍