Binance API C++ implementation
Binance API implemented in C++ for both synchronous and asynchronous way.
BTC: 3BJKvx6LyKB2J5KgRBqst415KKmwQE5eQX
This implementation has been developed as a consequence of the lack of suitable alternatives as part of my multiuser trading platform project.
api::ping()
api::time()
api::exchange_info()
api::account_info()
api::depth()
api::trades()
api::agg_trades()
api::_24hrs_ticker()
api::price()
api::new_order()
api::order_info()
api::new_test_order()
api::cancel_order()
api::open_orders()
api::all_orders()
api::my_trades()
api::start_user_data_stream()
api::ping_user_data_stream()
api::close_user_data_stream()
websockets::part_depth()
websockets::diff_depth()
websockets::klines()
websockets::trade()
websockets::agg_trade()
websockets::mini_ticker()
websockets::mini_tickers()
websockets::market()
websockets::markets()
websockets::book()
websockets::books()
websockets::userdata()
The project is written using C++14 and boost (at least version 1.70). boost.beast is used to interact with the network.
#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;
}
#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;
}
#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;
}