Rapberry Pi Python CCXT

Somsak Lima
2 min readApr 16, 2022

--

ติดตั้ง ccxt ใช้ user PI

pip install ccxt

เรียกใช้งาน

import ccxt
# Connect binance
binance = ccxt.binance()
btc_ticker = binance.fetch_ticker(‘BTC/USDT’)
btc_ticker

ผล

หรือสร้าง ex1.py

pi@raspberrypi:~ $ cat ex1.py
import ccxt
# Connect binance
binance = ccxt.binance()
print (btc_ticker)

run โดย python ex1.py

ex2.py https://blog.shrimpy.io/blog/ccxt-live-crypto-exchange-price-ticker

hitbtc = ccxt.hitbtc()

ticker_data = []

# fetch the BTC/USDT ticker for use in converting assets to price in USDT
bitcoin_ticker = hitbtc.fetch_ticker('BTC/USDT')

# calculate the ticker price of BTC in terms of USDT by taking the midpoint of the best bid and ask
bitcoinPriceUSDT = (float(bitcoin_ticker['info']['ask']) + float(bitcoin_ticker['info']['bid'])) / 2

# fetch the tickers for each asset on HitBTC
# this will take as long as 5 minutes
for trading_pair in hitbtc.load_markets():
base = trading_pair.split('/')[0]
quote = trading_pair.split('/')[1]
if quote == 'BTC':
pair_ticker = hitbtc.fetch_ticker(trading_pair)
pair_ticker['base'] = base
ticker_data.append(pair_ticker)

prices = []

# create the price tickers for each asset, removing unnecessary data
for ticker in ticker_data:
price = {}
price['symbol'] = ticker['base']
price['price'] = ((float(ticker['info']['ask']) + float(ticker['info']['bid'])) / 2) * bitcoinPriceUSDT
prices.append(price)

# additional processing is required for assets without BTC pairs
# additional processing is required to calculate the 24-hour price change

ผล เช่น

[
{
"symbol": "LTC",
"price": "60.683353731"
},
{
"symbol": "DASH",
"price": "69.435138045"
},
...
]

EX3

import ccxt
import pandas as pdftx = ccxt.ftx({
'api_key': '................', # API Keys
'secret': '.................'}) # API Secret
ftx.fetch_ticker('BNB/USD')['last']

EX4.

import ccxt
print(ccxt.exchanges) # print a list of all available exchange classes

EX5.

ftx.fetch_ticker(‘HNT/USD’)[‘last’]

Reference

https://blog.shrimpy.io/blog/ccxt-live-crypto-exchange-price-ticker

--

--

Somsak Lima
Somsak Lima

Written by Somsak Lima

สนับสนุนและส่งเสริมให้ผู้สนใจสามารถใช้งานเทคโนโลยี LoRa และ LoRaWAN ได้ โดยนำความรู้ที่ได้ไปต่อยอดเพื่อใช้งาน

No responses yet