Skip to content

niXman/binapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

WebSocket API

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

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •