- Install Anaconda and PyCharm. Anaconda is a Python environment and PyCharm is a Python editor.
https://anaconda.org/anaconda/conda
https://www.jetbrains.com/pycharm/ - Open the terminal and check which environments are available.
conda env list
3. Delete the environment "py1".
conda env remove -n py1
- Open PyCharm and create a new project. Choose Python version 3.8.
- Install the package "ccxt". ccxt encapsulates the APIs of most exchanges worldwide.
pip install ccxt
- Create a new .py file and get all exchanges.
import ccxt
print(ccxt.exchanges)
- Instantiate a Binance exchange.
binance_exchange = ccxt.binance({
})
print('Exchange ID:', binance_exchange.id)
print('Exchange Name:', binance_exchange.name)
print('Supports Public API:', binance_exchange.has['publicAPI'])
print('Supports Private API:', binance_exchange.has['privateAPI'])
print('Supported Timeframes:', binance_exchange.timeframes)
print('Maximum Wait Time (s):', binance_exchange.timeout/1000)
print('Request Rate Limit (s):', binance_exchange.rateLimit/1000)
print('Current Time:', binance_exchange.iso8601(ccxt.binance.milliseconds()))
If using the OKEX exchange, it would be ccxt.okex().
The apiKey and secret are the API key we applied for on the exchange.
For OKEX, a password is also required, but not for other exchanges.
binance_exchange = ccxt.binance({
'apiKey': '',
'secret': '',
'timeout': 15000,
'enableRateLimit': True,
})
- Get market information.
binance_markets = binance_exchange.load_markets()
print(binance_exchange.id, binance_markets)
- Get order book information.
symbol = 'BTC/USDT'
orderbook = exchange.fetch_order_book(symbol)
print('Order Book:', orderbook)
print('Bids:', orderbook['bids'])
print('Asks:', orderbook['asks'])
- Get ticker information.
if (exchange.has['fetchTicker']):
print(exchange.fetch_ticker(symbol))
- Get candlestick data.
kline_data = exchange.fetch_ohlcv(symbol, '1d')
print('Candlestick Data:', kline_data)
- Get public trades.
public_trade = exchange.fetch_trades(symbol)
print('Public Trades:', public_trade)
- Get balance.
balance = exchange.fetch_balance()
print('Balance:', balance)
print('USDT Balance:', balance['USDT'])
print('Free USDT Balance:', balance['USDT']['free'])
- Get trades.
all_orders = exchange.fetch_orders(symbol=symbol)
print('All Orders:', all_orders)
open_orders = exchange.fetch_open_orders(symbol=symbol)
print('Open Orders:', open_orders)
- Get specific order information.
order_info = exchange.fetch_order('545646', 'BTC/USDT')
print('Order Info:', order_info)
- Place an order. The second parameter is the quantity and the third parameter is the price.
exchange.create_limit_buy_order('BTC/USDT', 1, 2)
exchange.cancel_order('54646554')