MCPcopy Create free account
hub / github.com/geekcomputers/Python / getprice

Function getprice

get_crypto_price.py:4–26  ·  view source on GitHub ↗
(symbol, exchange_id)

Source from the content-addressed store, hash-verified

2
3
4def getprice(symbol, exchange_id):
5 symbol = symbol.upper() # BTC/USDT, LTC/USDT, ETH/BTC, LTC/BTC
6 exchange_id = exchange_id.lower() # binance, #bitmex
7 symbol_1 = symbol.split("/")
8 exchange = getattr(ccxt, exchange_id)(
9 {
10 # https://github.com/ccxt/ccxt/wiki/Manual#rate-limit
11 "enableRateLimit": True
12 }
13 )
14 try:
15 v_price = exchange.fetch_ticker(symbol)
16 r_price = v_price["info"]["lastPrice"]
17 if symbol_1[1] == "USD" or symbol_1[1] == "USDT":
18 v_return = "{:.2f} {}".format(float(r_price), symbol_1[1])
19 return v_return
20 else:
21 v_return = "{:.8f} {}".format(float(r_price), symbol_1[1])
22 return v_return
23 except (ccxt.ExchangeError, ccxt.NetworkError) as error:
24 # add necessary handling or rethrow the exception
25 return "Got an error", type(error).__name__, error.args
26 raise
27
28
29print(getprice("btc/usdt", "BINANCE"))

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected