Quantitative technology house team launched a series of quantitative investment courses in CSDN College
Students who are interested in systematically learning quantitative investment are welcome, click the link below to register:
Quantitative Investment Crash Camp (Introductory Course)
Python stock quantitative investment
Python Futures Quantitative Investment
Python digital currency quantitative investment
C++ Language CTP Futures Trading System Development
Digital currency JavaScript language quantitative trading system development
How to install CCXT, how to query help documents
Install CCXT
For the Python language, the way to install CCXT is the same as other Python three-part libraries. Use the statement pip install ccxt to install.
After the installation is complete, we can try to use import ccxt to import the CCXT library. If there is no error when running this statement, it means that CCXT has been successfully imported
CCXT Manual (help documentation)
CCXT provides detailed help documents, and the sidebar of the help documents provides classifications of different API interfaces, including MarketAPI, Implicit API, Unified API, Public API, Private API and other categories, which are convenient for users to search according to their needs.
For each API, CCXT not only gives the description of the calling function name, parameters, and examples, but also gives the structure data returned after calling. The legend is the return structure of the Currency Structure in the Market category.
CCXT initialization
After introducing the installation of CCXT and how to find help documents, then we start using CCXT. The initialization of CCXT can be divided into three steps, the third step is optional, and the first two steps must be done.
The first step is to load the ccxt module
The second step, initialization, calls ccxt
The third step is to obtain market information
# 加载ccxt模块
import ccxt
print(ccxt.exchanges)
# market API
exchange = ccxt.exchange()
markets = exchange.load_markets()
print(exchange.fetch_markets())
print(exchange.fetchCurrencies())
Then we can see through the total number of the markets variable, and the detailed transaction information is also fed back to us by CCXT through the dict data format.
How to call CCXT Quotes API
After initializing CCXT, next we try to call CCXT's spot market API to obtain various market data. CCXT helps us to package and use the available market data, which can be invoked with one click using standard methods.
We use a loop to call and query all data, including:
# quote API
# 设置间隔2秒
delay = 0.5
for symbol in exchange.markets:
print(symbol)
print(exchange.fetch_order_book(symbol, 10))
time.sleep(delay)
print(exchange.fetch_trades(symbol, limit=5))
time.sleep(delay)
print(exchange.fetch_ticker(symbol))
time.sleep(delay)
print(exchange.fetch_ohlcv(symbol, '1d'))
time.sleep(delay)
Let’s look at the data structure order book returned after acquisition (take the acquisition of 10-level handicap data as an example)
Public transaction data
K-line data (take the daily line as an example)
How to call CCXT trading API
After getting familiar with the market API, let's try to use CCTX's trading API. But before using the trading API, it must be noted that since the market API is a public API, and the trading API is a private API, we must call the CCXT method and pass the key into the exchange variable just created to realize the permission to call the private API , the incoming method is as follows
api_key = ""
secret_key = ""
exchange.apiKey = api_key
exchange.secret = secret_key
Then, we can start to perform a series of operations related to the private API, including:
a. Asset inquiry
b. Order inquiry
c. Transaction inquiry
d.
e. Cancellation
# trade API
# 资产查询
exchange.fetch_balance()
# 订单查询(依次是所有、未完成、已完成订单)
exchange.fetchOrders(symbol)
exchange.fetchOpenOrders(symbol)
exchange.fetchClosedOrders(symbol)
# 成交查询
exchange.fetchMyTrades(symbol)
# 报单(依次是市价、限价、买报单、卖报单)
exchange.create_order(symbol, 'market', 'buy', amount)
exchange.createLimitBuyOrder(symbol, amount, orderprice)
exchange.createLimitSellOrder(symbol, amount, orderprice)
exchange.createMarketBuyOrder(symbol, amount)
exchange.createMarketSellOrder(symbol, amount)
# 撤单
exchange.cancel_order(order_id)
For orders and cancellations, if the order cancellation is successful, the returned information is the order_id of the order or the order_id of the cancellation. If it is wrong, it will return the corresponding error code and error reason. I will not give examples here.
CCXT uses small chestnuts
Finally, by combining the CCXT initialization we just learned, calling the market API, and calling the transaction API, we can complete a simple process from initializing and loading Key, to obtaining the latest market price, and then placing an order according to the price of the market. Chestnut is small, but it has opened up the spot API of the entire CCXT.
# 加载ccxt模块
import ccxt
# 加载Key
exchange = ccxt.xxxx()
api_key = ""
secret_key = ""
exchange.apiKey = api_key
exchange.secret = secret_key
symbol = ''
# 获取最新行情
orderbook = exchange.fetch_order_book(symbol)
orderprice = orderbook['asks'][0][0] if len(orderbook['asks']) > 0 else None
# 按照该行情价格下买单
amount = 1
exchange.createLimitBuyOrder(symbol, amount, orderprice)
CCXT practical skills, have you got it yet?