Skip to content

Commit

Permalink
Add bybit
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mk committed Jan 6, 2024
1 parent 46de9eb commit d7f06f5
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pri

CLI tool for simply getting cryptocurrency prices from 8 big exchanges. No API key needed.
CLI tool for simply getting cryptocurrency prices from biggest exchanges. No API key needed.

Supported exchanges are:
- Binance
Expand All @@ -11,6 +11,7 @@ Supported exchanges are:
- BitStamp
- BitFinex
- Huobi
- Bybit

## Install

Expand Down Expand Up @@ -95,7 +96,7 @@ Getting prices...


## TODO
- add bybit
- add okx
- automate asset list compilation
- filter out delisted asset pairs, it's sometimes indicated in the api asset listsing
- make arbitrage groups, like
Expand Down
34 changes: 34 additions & 0 deletions getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@ func GeminiGetter(ticker string) (float64, error) {
return (af + bf) / 2, nil
}

type BybitTicker struct {
Reusult struct {
List []struct {
Ask string `json:"ask1Price"`
Bid string `json:"bid1Price"`
} `json:"list"`
} `json:"result"`
}

func BybitGetter(ticker string) (float64, error) {
url := "https://api-testnet.bybit.com/v5/market/tickers?category=linear&symbol=" + ticker
body, err := getHTTPResponseBodyFromUrl(url)
if err != nil {
return 0, err
}
var tickerData BybitTicker
err = json.Unmarshal(body, &tickerData)
if err != nil {
return 0, err
}
a := tickerData.Reusult.List[0].Ask
b := tickerData.Reusult.List[0].Bid

af, err := strconv.ParseFloat(a, 64)
if err != nil {
return 0, err
}
bf, err := strconv.ParseFloat(b, 64)
if err != nil {
return 0, err
}
return (af + bf) / 2, nil
}

type BitstampTicker struct {
Ask string `json:"ask"`
Bid string `json:"bid"`
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ExTickPri struct {

func (etp ExTickPri) String() string {
et := etp.ExTick
return fmt.Sprintf("[%25s]\t%s", et, formatFloat(etp.Price))
return fmt.Sprintf("[%15s]\t%s", et, formatFloat(etp.Price))
}

func (et ExTick) String() string {
Expand All @@ -41,6 +41,7 @@ const (
Kucoin Exchange = "kucoin"
Gateio Exchange = "gateio"
Bitfinex Exchange = "bitfinex"
Bybit Exchange = "bybit"
)

var exchangeGetters = map[Exchange]TickerGetter{
Expand All @@ -52,6 +53,7 @@ var exchangeGetters = map[Exchange]TickerGetter{
Kucoin: KUCoinGetter,
Gateio: GateIOGetter,
Bitfinex: BitfinexGetter,
Bybit: BybitGetter,
}

func getExchangeTickerPrice(et ExTick) (*ExTickPri, error) {
Expand Down Expand Up @@ -87,6 +89,7 @@ var exchangeSymbols = map[Exchange][]string{
Kucoin: symbols.Kucoin,
Gateio: symbols.Gateio,
Bitfinex: symbols.Bitfinex,
Bybit: symbols.Bybit,
}

func findExTick(symbol string) (*ExTick, error) {
Expand Down Expand Up @@ -116,7 +119,12 @@ func findExTick(symbol string) (*ExTick, error) {
return nil, fmt.Errorf("symbol not found: %s", symbol)
}
if len(foundSymbols) > 1 {
return nil, fmt.Errorf("symbol \"%s\" found in multiple exchanges: %v ", symbol, foundExchanges)
lst := []string{}
for i, v := range foundSymbols {
lst = append(lst, fmt.Sprintf("%s-%s", foundExchanges[i], v))
}
lines := strings.Join(lst, "\n")
return nil, fmt.Errorf("symbol %s found in multiple exchanges:\n%s", symbol, lines)
}
return &ExTick{foundExchanges[0], foundSymbols[0]}, nil
}
Expand Down
Loading

0 comments on commit d7f06f5

Please sign in to comment.