加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
get_crypto_price.py 1000 Bytes
一键复制 编辑 原始数据 按行查看 历史
Jérôme Krell 提交于 2019-10-10 14:22 . Reformat Code by PyCharm-Community
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"))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化