Skip to content

Commit f538edd

Browse files
Merge pull request geekcomputers#585 from onionsh/onionsh
add get_crypto_price.py
2 parents 4a29659 + d50dea6 commit f538edd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

get_crypto_price.py

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

0 commit comments

Comments
 (0)