forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_crypto_price.py
30 lines (26 loc) · 1 KB
/
get_crypto_price.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import ccxt
def getprice(symbol, exchange_id):
symbol = symbol.upper() # BTC/USDT, LTC/USDT, ETH/BTC, LTC/BTC
exchange_id = exchange_id.lower() # binance, #bitmex
symbol_1 = symbol.split("/")
exchange = getattr(ccxt, exchange_id)(
{
# https://github.com/ccxt/ccxt/wiki/Manual#rate-limit
"enableRateLimit": True
}
)
try:
v_price = exchange.fetch_ticker(symbol)
r_price = v_price["info"]["lastPrice"]
if symbol_1[1] == "USD" or symbol_1[1] == "USDT":
v_return = "{:.2f} {}".format(float(r_price), symbol_1[1])
return v_return
else:
v_return = "{:.8f} {}".format(float(r_price), symbol_1[1])
return v_return
except (ccxt.ExchangeError, ccxt.NetworkError) as error:
# add necessary handling or rethrow the exception
return "Got an error", type(error).__name__, error.args
raise
print(getprice("btc/usdt", "BINANCE"))
print(getprice("btc/usd", "BITMEX"))