Skip to content

Commit

Permalink
Merge pull request #366 from crypto-chassis/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
cryptochassis authored Jan 31, 2023
2 parents dd59edf + 9fd7df8 commit 33f38fe
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
* Code closely follows Bloomberg's API: https://www.bloomberg.com/professional/support/api-library/.
* It is ultra fast thanks to very careful optimizations: move semantics, regex optimization, locality of reference, lock contention minimization, etc.
* Supported exchanges:
* Market data: coinbase, gemini, kraken, kraken-futures, bitstamp, bitfinex, bitmex, binance-us, binance, binance-usds-futures, binance-coin-futures, huobi, huobi-usdt-swap, huobi-coin-swap, okx, erisx, kucoin, kucoin-futures, deribit, gateio, gateio-perpetual-futures, cryptocom, bybit, bybit-derivatives, ascendex, bitget, bitget-futures, bitmart, mexc, mexc-futures, whitebit.
* Execution Management: coinbase, gemini, kraken, kraken-futures, bitstamp, bitfinex, bitmex, binance-us, binance, binance-usds-futures, binance-coin-futures, huobi, huobi-usdt-swap, huobi-coin-swap, okx, erisx, kucoin, kucoin-futures, deribit, gateio, gateio-perpetual-futures, cryptocom, bybit, bybit-derivatives, ascendex, bitget, bitget-futures, bitmart, mexc.
* Market data: coinbase, gemini, kraken, kraken-futures, bitstamp, bitfinex, bitmex, binance-us, binance, binance-usds-futures, binance-coin-futures, huobi, huobi-usdt-swap, huobi-coin-swap, okx, erisx (Cboe Digital), kucoin, kucoin-futures, deribit, gateio, gateio-perpetual-futures, cryptocom, bybit, bybit-derivatives, ascendex, bitget, bitget-futures, bitmart, mexc, mexc-futures, whitebit.
* Execution Management: coinbase, gemini, kraken, kraken-futures, bitstamp, bitfinex, bitmex, binance-us, binance, binance-usds-futures, binance-coin-futures, huobi, huobi-usdt-swap, huobi-coin-swap, okx, erisx (Cboe Digital), kucoin, kucoin-futures, deribit, gateio, gateio-perpetual-futures, cryptocom, bybit, bybit-derivatives, ascendex, bitget, bitget-futures, bitmart, mexc.
* FIX: coinbase, gemini.
* A spot market making application is provided as an end-to-end solution for liquidity providers.
* A single order execution application is provided as an end-to-end solution for executing large orders.
Expand Down
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ add_subdirectory(src/utility_set_timer)
add_subdirectory(src/custom_service_class)
add_subdirectory(src/cross_exchange_arbitrage)
add_subdirectory(src/market_making)
add_subdirectory(src/override_exchange_url_at_runtime)
5 changes: 5 additions & 0 deletions example/src/override_exchange_url_at_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(NAME override_exchange_url_at_runtime)
project(${NAME})
add_compile_definitions(CCAPI_ENABLE_SERVICE_MARKET_DATA)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_OKX)
add_executable(${NAME} main.cpp)
41 changes: 41 additions & 0 deletions example/src/override_exchange_url_at_runtime/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "ccapi_cpp/ccapi_session.h"
namespace ccapi {
Logger* Logger::logger = nullptr; // This line is needed.
class MyEventHandler : public EventHandler {
public:
bool processEvent(const Event& event, Session* session) override {
if (event.getType() == Event::Type::SESSION_STATUS) {
for (const auto& message : event.getMessageList()) {
if (message.getType() == Message::Type::SESSION_CONNECTION_UP) {
for (const auto& element : message.getElementList()) {
const std::map<std::string, std::string>& elementNameValueMap = element.getNameValueMap();
std::cout << "Connected to " + toString(elementNameValueMap.at("CONNECTION_URL")) << std::endl;
}
}
}
}
return true;
}
};
} /* namespace ccapi */
using ::ccapi::MyEventHandler;
using ::ccapi::Session;
using ::ccapi::SessionConfigs;
using ::ccapi::SessionOptions;
using ::ccapi::Subscription;
using ::ccapi::toString;
int main(int argc, char** argv) {
SessionOptions sessionOptions;
SessionConfigs sessionConfigs;
std::map<std::string, std::string> urlWebsocketBase = sessionConfigs.getUrlWebsocketBase();
urlWebsocketBase["okx"] = "wss://wsaws.okx.com:8443";
sessionConfigs.setUrlWebsocketBase(urlWebsocketBase);
MyEventHandler eventHandler;
Session session(sessionOptions, sessionConfigs, &eventHandler);
Subscription subscription("okx", "BTC-USDT", "MARKET_DEPTH");
session.subscribe(subscription);
std::this_thread::sleep_for(std::chrono::seconds(10));
session.stop();
std::cout << "Bye" << std::endl;
return EXIT_SUCCESS;
}
3 changes: 3 additions & 0 deletions include/ccapi_cpp/ccapi_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@
#ifndef CCAPI_CONNECTION_ID
#define CCAPI_CONNECTION_ID "CONNECTION_ID"
#endif
#ifndef CCAPI_CONNECTION_URL
#define CCAPI_CONNECTION_URL "CONNECTION_URL"
#endif
#ifndef CCAPI_REASON
#define CCAPI_REASON "REASON"
#endif
Expand Down
11 changes: 11 additions & 0 deletions include/ccapi_cpp/ccapi_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,17 @@ class Session {
}
});
}
void purgeHttpConnectionPool(const std::string& serviceName = "", const std::string& exchangeName = "") {
for (const auto& x : this->serviceByServiceNameExchangeMap) {
if (serviceName.empty() || serviceName == x.first) {
for (const auto& y : x.second) {
if (exchangeName.empty() || exchangeName == y.first) {
y.second->purgeHttpConnectionPool();
}
}
}
}
}
#endif
#ifndef CCAPI_EXPOSE_INTERNAL

Expand Down
3 changes: 3 additions & 0 deletions include/ccapi_cpp/ccapi_session_configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class SessionConfigs CCAPI_FINAL {
const std::map<std::string, std::string>& getUrlFixBase() const { return urlFixBase; }
const std::map<std::string, int>& getInitialSequenceByExchangeMap() const { return initialSequenceByExchangeMap; }
const std::map<std::string, std::string>& getCredential() const { return credential; }
void setUrlWebsocketBase(const std::map<std::string, std::string>& urlWebsocketBase) { this->urlWebsocketBase = urlWebsocketBase; }
void setUrlRestBase(const std::map<std::string, std::string>& urlRestBase) { this->urlRestBase = urlRestBase; }
void setUrlFixBase(const std::map<std::string, std::string>& urlFixBase) { this->urlFixBase = urlFixBase; }
void setCredential(const std::map<std::string, std::string>& credential) { this->credential = credential; }
#ifndef CCAPI_EXPOSE_INTERNAL

Expand Down
23 changes: 11 additions & 12 deletions include/ccapi_cpp/service/ccapi_execution_management_service_okx.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,16 @@ class ExecutionManagementServiceOkx : public ExecutionManagementService {
rj::Value args(rj::kArrayType);
const auto& fieldSet = subscription.getFieldSet();
const auto& instrumentSet = subscription.getInstrumentSet();
for (const auto& field : fieldSet) {
std::string channel;
if (fieldSet.find(CCAPI_EM_ORDER_UPDATE) != fieldSet.end() || fieldSet.find(CCAPI_EM_PRIVATE_TRADE) != fieldSet.end()) {
channel = "orders";
}
for (const auto& instrument : instrumentSet) {
rj::Value arg(rj::kObjectType);
arg.AddMember("channel", rj::Value(channel.c_str(), allocator).Move(), allocator);
arg.AddMember("instId", rj::Value(instrument.c_str(), allocator).Move(), allocator);
arg.AddMember("instType", rj::Value("ANY").Move(), allocator);
args.PushBack(arg, allocator);
}
std::string channel;
if (fieldSet.find(CCAPI_EM_ORDER_UPDATE) != fieldSet.end() || fieldSet.find(CCAPI_EM_PRIVATE_TRADE) != fieldSet.end()) {
channel = "orders";
}
for (const auto& instrument : instrumentSet) {
rj::Value arg(rj::kObjectType);
arg.AddMember("channel", rj::Value(channel.c_str(), allocator).Move(), allocator);
arg.AddMember("instId", rj::Value(instrument.c_str(), allocator).Move(), allocator);
arg.AddMember("instType", rj::Value("ANY").Move(), allocator);
args.PushBack(arg, allocator);
}
document.AddMember("args", args, allocator);
rj::StringBuffer stringBufferSubscribe;
Expand Down Expand Up @@ -491,6 +489,7 @@ class ExecutionManagementServiceOkx : public ExecutionManagementService {
{CCAPI_EM_ORDER_LIMIT_PRICE, std::make_pair("px", JsonDataType::STRING)},
{CCAPI_EM_ORDER_QUANTITY, std::make_pair("sz", JsonDataType::STRING)},
{CCAPI_EM_ORDER_CUMULATIVE_FILLED_QUANTITY, std::make_pair("accFillSz", JsonDataType::STRING)},
{CCAPI_EM_ORDER_AVERAGE_FILLED_PRICE, std::make_pair("avgPx", JsonDataType::STRING)},
{CCAPI_EM_ORDER_STATUS, std::make_pair("state", JsonDataType::STRING)},
};
Element info;
Expand Down
4 changes: 3 additions & 1 deletion include/ccapi_cpp/service/ccapi_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Service : public std::enable_shared_from_this<Service> {
this->httpConnectionPoolPurgeTimer->cancel();
}
}
// void setEventHandler(const std::function<void(Event& event)>& eventHandler) { this->eventHandler = eventHandler; }
void purgeHttpConnectionPool() { this->httpConnectionPool.purge(); }
void stop() {
for (const auto& x : this->sendRequestDelayTimerByCorrelationIdMap) {
x.second->cancel();
Expand Down Expand Up @@ -836,6 +836,7 @@ class Service : public std::enable_shared_from_this<Service> {
message.setCorrelationIdList(correlationIdList);
Element element;
element.insert(CCAPI_CONNECTION_ID, wsConnection.id);
element.insert(CCAPI_CONNECTION_URL, wsConnection.url);
message.setElementList({element});
event.setMessageList({message});
this->eventHandler(event, nullptr);
Expand Down Expand Up @@ -925,6 +926,7 @@ class Service : public std::enable_shared_from_this<Service> {
message.setType(Message::Type::SESSION_CONNECTION_DOWN);
Element element;
element.insert(CCAPI_CONNECTION_ID, wsConnection.id);
element.insert(CCAPI_CONNECTION_URL, wsConnection.url);
element.insert(CCAPI_REASON, reason);
message.setElementList({element});
std::vector<std::string> correlationIdList;
Expand Down

0 comments on commit 33f38fe

Please sign in to comment.