binapi
Binance API implemented in C++ for both synchronous and asynchronous way.
Donate
BTC: 3BJKvx6LyKB2J5KgRBqst415KKmwQE5eQX
Motivation
This implementation has been developed as a consequence of the lack of suitable alternatives as part of my multiuser trading platform project.
REST API
- Test connectivity ->
api::ping()
- Check server time ->
api::time()
- Exchange information ->
api::exchange_info()
- Account information ->
api::account_info()
- Order book ->
api::depth()
- Recent trades list ->
api::trades()
- Aggregate trades list ->
api::agg_trades()
- 24hr ticker price change statistics ->
api::_24hrs_ticker()
- Symbol price ticker ->
api::price()
- New order ->
api::new_order()
- Query order ->
api::order_info()
- Test new order ->
api::new_test_order()
- Cancel order ->
api::cancel_order()
- Current open orders ->
api::open_orders()
- All orders ->
api::all_orders()
- Account trade list ->
api::my_trades()
- Start user data stream ->
api::start_user_data_stream()
- Keepalive user data stream ->
api::ping_user_data_stream()
- Close user data stream ->
api::close_user_data_stream()
WebSocket API
- Partial Book Depth Streams ->
websockets::part_depth()
- Diff. Depth Stream ->
websockets::diff_depth()
- Kline/Candlestick Streams ->
websockets::klines()
- Trade Streams ->
websockets::trade()
- Aggregate Trade Streams ->
websockets::agg_trade()
- Individual Symbol Mini Ticker Stream ->
websockets::mini_ticker()
- All Market Mini Tickers Stream ->
websockets::mini_tickers()
- Individual Symbol Ticker Streams ->
websockets::market()
- All Market Tickers Stream ->
websockets::markets()
- Individual Symbol Book Ticker Streams ->
websockets::book()
- All Book Tickers Stream ->
websockets::books()
- User Data Streams ->
websockets::userdata()
Implementation details
The project is written using C++14 and boost (at least version 1.70). boost.beast is used to interact with the network.
Synchronous example
#include "binapi/api.hpp"
#include <boost/asio/io_context.hpp>
#include <iostream>
int main() {
const std::string pk = "...";
const std::string sk = "...";
boost::asio::io_context ioctx;
binapi::rest::api api(
ioctx
,"api.binance.com"
,"443"
,pk
,sk
,10000 // recvWindow
);
auto account = api.account_info();
if ( !account ) {
std::cerr << "account info error: " << account.errmsg << std::endl;
return EXIT_FAILURE;
}
std::cout << "account info: " << account.v << std::endl << std::endl;
return EXIT_SUCCESS;
}
Asynchronous example
#include "binapi/api.hpp"
#include <boost/asio/io_context.hpp>
#include <iostream>
int main() {
const std::string pk = "...";
const std::string sk = "...";
boost::asio::io_context ioctx;
binapi::rest::api api(
ioctx
,"api.binance.com"
,"443"
,pk
,sk
,10000 // recvWindow
);
api.account_info([](const char *fl, int ec, std::string errmsg, binapi::rest::account_info_t res) {
if ( ec ) {
std::cerr << "account info error: fl=" << fl << ", ec=" << ec << ", emsg=" << errmsg << std::endl;
return false;
}
std::cout << "account info: " << res << std::endl;
return true;
});
ioctx.run();
return EXIT_SUCCESS;
}
WebSocket example
#include <binapi/api.hpp>
#include <binapi/websocket.hpp>
#include <boost/asio/io_context.hpp>
#include <iostream>
int main() {
boost::asio::io_context ioctx;
binapi::ws::websockets ws{
ioctx
,"stream.binance.com"
,"9443"
};
ws.part_depth("BTCUSDT",
[](const char *fl, int ec, std::string emsg, auto depths) {
if ( ec ) {
std::cerr << "subscribe depth error: fl=" << fl << ", ec=" << ec << ", emsg=" << emsg << std::endl;
return false;
}
std::cout << "depths: " << depths << std::endl;
return true;
}
);
ws.trade("BTCUSDT",
[](const char *fl, int ec, std::string emsg, auto trades) {
if ( ec ) {
std::cerr << "subscribe trades error: fl=" << fl << ", ec=" << ec << ", emsg=" << emsg << std::endl;
return false;
}
std::cout << "trades: " << trades << std::endl;
return true;
}
);
ioctx.run();
return EXIT_SUCCESS;
}
Tools (will write later...)
- filters
- report generators