Apifiny Algo Trading System
Apifiny Algo Trading System is a trading library that supports mulitple cryptocurrency exchanges. The core value of AATS is light weight and low latency since the low-level system is optimized and encapsulated by C++, such as exchange connectivity, order book management and order placement. It extremely decreases the learning curve for users. With AATS, users can seamlessly deploy your strategy and ready to live trade without any pain. Below is the introduction of AATS python API.
Package structure
At python module level, AATS utilizes the event driven framework and consists of the below components:
- Base Strategy: it is a base strategy class and you can develop your strategy based on it
- Trade Service: it is the core trading module that is used to load configuration, start trade server and add strategy and start running for live trading
- Market Service: it is used to start market server and subscribe symbols and exchanges you want to listen
- Control Service: it is related to server management, such as show running market/trade servers, kill servers and etc
Base Strategy
Once your strategy inherits from base strategy, you can overwrite the below functions/callbacks and add your own logic:
Functions/Callbacks |
Description |
send_order |
function to send order |
send_cancel_order |
function to send cancel order |
close |
function to define how to close strategy |
onQuote |
callbacks once orderbook data update |
onTick |
callbacks once trade data update |
onNotify |
combination of onQuote and onTick |
onOrderCreated |
callbacks once new order created |
onOrderAcked |
callbacks once new order is acknowledged from exchange |
onOrderRejected |
callbacks once new order is rejected from exchange |
onOrderCancelCreated |
callbacks once cancel order created |
onOrderCancelAcked |
callbacks once cancel order is acknowledged from exchange |
onOrderCancelRejected |
callbacks once cancel order is rejected from exchange |
onOrderExec |
callbacks once order is executed (include partially executed) from exchange |
onOrderCanceled |
callbacks once order is canceled from exchange |
onOrderClosed |
callbacks once order is closed from exchange |
send_order
send_order needs to construct an order object with the following field:
Field |
Description |
Mandatory |
exchange |
exchange you want to trade |
Yes |
symbol |
symbol you want to trade |
Yes |
price |
trade price |
Yes |
size |
trade size |
Yes |
side |
trade direction |
Yes |
tif |
order type, currently support IOC and POS |
Yes |
send_cancel_order
Field |
Description |
Mandatory |
order_id |
internal order id |
Yes |
close
This is an abstract function, users need to overwrite it and define how to close your strategy properly.
onQuote
callbacks once orderbook data update, message contains the below fields:
{
'type': 'onQuote',
'cid': 1001,
'length': 5, # order book depth level
'bid_0_qty': 0.27713,
'bid_0_px': 19327.67,
'bid_1_qty': 0.04621,
'bid_1_px': 19327.08,
'bid_2_qty': 0.12618,
'bid_2_px': 19326.8,
'bid_3_qty': 0.05,
'bid_3_px': 19326.68,
'bid_4_qty': 0.28508,
'bid_4_px': 19326.66,
'ask_0_qty': 0.13624,
'ask_0_px': 19327.87,
'ask_1_qty': 0.0063,
'ask_1_px': 19327.92,
'ask_2_qty': 0.02602,
'ask_2_px': 19328.0,
'ask_3_qty': 0.32749,
'ask_3_px': 19328.07,
'ask_4_qty': 0.0066,
'ask_4_px': 19328.16
}
OnTick
callbacks once trade data update, message contains the below fields:
{
'type': 'onTick',
'cid': 1002,
'tradeType': 1, # aggresive trade side, 1 for buy, -1 for sell
'qty': 0.0249,
'px': 1638.93
}
onNotify
Callbacks once onTrade or onQuote is fired, parameter "NoType" represents notify type, 1 for onQuote, 0 for onTick
onOrder**
onOrder callbacks with different order status, message contains the below fields:
{
"order":{
"exchange":"BINANCE",
"fill_notional":"0",
"fill_price":"19318.88",
"fill_size":"0.001",
"margin":false,
"margin_source":"spot",
"order_id":1, # internal order id
"price":"19318.88",
"remote_id":"", # exchange order id
"side":"buy",
"size":"0.001",
"symbol":"BTCUSDT",
"msg":"" # the order be rejected reson
},
"type":"onOrderCreated" # order status
}
Trade Service
Trade service provides below APIs
Functions/Callbacks |
Description |
set_ts_connection |
setup connection configuration with control server |
set_md_connection |
setup connection configuration with market server |
config_exchange |
setup exchange configuration |
set_apikey |
setup apikeys |
set_fee |
setup commission fee (optional) |
add_md_symbol |
add symbol you want to listen |
add_trade_symbol |
add symbol you want to trade |
add_strategy |
add user defined strategy |
run |
start live trading |
set_ts_connection
Field |
Description |
Mandatory |
ip |
control server ip |
Yes |
port |
control server port |
Yes |
set_md_connection
Field |
Description |
Mandatory |
ip |
market server ip |
Yes |
port |
market server port |
Yes |
send_type |
send type |
Yes |
config_exchange
Field |
Description |
Mandatory |
exchange |
exchange name |
Yes |
trade_type |
trade mode, "direct" for live trade, "sandbox" for test trade, currently "sandbox" only supports Binance |
Yes |
set_apikey
Field |
Description |
Mandatory |
exchange |
exchange name |
Yes |
key |
api key |
Yes |
secret |
api secret |
Yes |
password |
api password |
No |
subaccount |
api subaccount |
No |
set_fee
Field |
Description |
Mandatory |
exchange |
exchange name |
Yes |
taker_fee |
taker commission fee |
Yes |
maker_fee |
maker commission fee |
Yes |
add_md_symbol
Field |
Description |
Mandatory |
symbol |
symbol name |
Yes |
exchange |
exchange name |
Yes |
level_num |
maximum number of book level to listen |
No |
add_trade_symbol
Field |
Description |
Mandatory |
symbol |
symbol name |
Yes |
exchange |
exchange name |
Yes |
level_num |
maximum number of book level to listen |
No |
add_strategy
Field |
Description |
Mandatory |
strategy |
user defined strategy instance |
Yes |
Market Service
For market service, we provide three modes, P2P is for cloud user, BROADCAST is for docker user, MULTIBROAD is for native user. You need to setup network configuration correspondingly for different user cases.
Market service provides below APIs
Functions/Callbacks |
Description |
set_control_server |
setup connection configuration with control server |
set_network_cfg |
setup network configuration for market server |
open_l2_data |
If you want to receive l2 data call this function |
add_listen_symbol |
add listen symbols |
run |
start to launch market server |
set_control_server
Field |
Description |
Mandatory |
ip |
control server ip |
Yes |
port |
control server port |
Yes |
set_network_cfg
Field |
Description |
Mandatory |
send_type |
send message type. default is P2P |
No |
ip |
UPD/TCP ip address |
Yes |
port |
UPD/TCP port |
Yes |
netcard_name |
netcard name. only be setted when send type is MULTIBROAD |
No |
Remark:
There are three types of send_type,
1. P2P - TCP point-to-point data transfer
2. BROADCAST - UDP broadcast data transmission
3. MULTIBROAD - UDP MULTIBROAD data transmission
When the transmission type is P2P, the parameters ip and port correspond to the ip address and port that the market data server listens to.
When the transmission type is BROADCAST, the parameters ip (255.255.255.255) and port correspond to the broadcast ip and the port of the receiver
When the transmission type is MULTIBROAD, the parameters ip (224.0.0.0 to 239.255.255.255) and port correspond to the MULTIBROAD ip and the port of the receiver
add_listen_symbol
Field |
Description |
Mandatory |
symbol |
symbol name |
Yes |
exchange |
exchange name |
Yes |
level_num |
maximum number of book level to listen, default is 5 |
No |
Control Service
Control service has below functionalities:
Functions/Callbacks |
Description |
set_control_server |
setup connection configuration of control server |
show_all_trade_servers |
list all running trade servers |
show_all_market_servers |
list all running market servers |
kill_server |
kill server with given pid |