Deribit's option position request hooked up

This commit is contained in:
Taha Afzal committed 2023-06-06 18:30:38 +00:00
1 parent 4769f66eea
commit 6d95a645e3
8 files changed
+295 -23

No files matched your search

+159 -16
View File
@@ -3,14 +3,17 @@
#include "CCAPISingleOrderManager.h"
#include "ccapi_cpp/ccapi_session.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/core/TimeStamp.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/logging/logging.h"
#include "lib/md/SecurityOracle.h"
#include "lib/md/binance/BinanceSpot.h"
#include "lib/orderman/CancelOrderMsg.h"
#include "lib/orderman/CancelReplaceOrderMsg.h"
#include "lib/orderman/CreateOrderMsg.h"
#include "lib/orderman/RFQMsg.h"
#include "lib/orderman/ccapi/CCAPIOrderCallbackMsgs.h"
#include "lib/orderman/ccapi/ICCAPIOrderMux.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/bts_assert.h"
@@ -19,44 +22,52 @@
#include <ccapi_cpp/ccapi_event.h>
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_message.h>
#include <ccapi_cpp/ccapi_request.h>
#include <ccapi_cpp/ccapi_subscription.h>
#include <ccapi_cpp/ccapi_util_private.h>
#include <chrono>
#include <google/protobuf/timestamp.pb.h>
#include <google/protobuf/util/time_util.h>
#include <memory>
#include <nlohmann/json_fwd.hpp>
#include <ostream>
#include <random>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
namespace bts::orderman {
// Wrapper that implements ccapi's EventHandler to receive events. Events are written
// to a lockfree queue which is then dequeued in dispatch_incoming_messages
class OrderEventHandler : public ccapi::EventHandler {
boost::lockfree::spsc_queue<ccapi::Event> &event_queue;
std::set<ccapi::Event::Type> event_types;
const std::set<ccapi::Event::Type> interested_event_types;
public:
OrderEventHandler(
boost::lockfree::spsc_queue<ccapi::Event> &event_queue,
const std::vector<ccapi::Event::Type> &event_types)
: event_queue(event_queue),
event_types(event_types.begin(), event_types.end()) {}
interested_event_types(event_types.begin(), event_types.end()) {}
bool processEvent(
const ccapi::Event &event, ccapi::Session *session) override {
bts_assert(session != nullptr);
bool processing = event_types.find(event.getType()) != event_types.end();
bool interested = interested_event_types.find(event.getType()) !=
interested_event_types.end();
LOG(OrderMan, Info,
"CCAPIDeribitOrderMux::OrderEventHandler::processEvent tid: {} "
"processing: {} "
"interested: {} "
"event: {}",
os::get_thread_id(), processing, event.toStringPretty(2, 2));
if (processing)
os::get_thread_id(), interested, event.toStringPretty(2, 2));
if (interested)
event_queue.push(event);
return true;
}
};
// Since we subscribe to orders per security, this keeps track of it
struct SubscriptionStatus {
const uint32_t sec_id;
const std::string order_sub_corr_id;
@@ -77,7 +88,7 @@ struct SubscriptionStatus {
};
struct CCAPIDeribitOrderMux::PImpl {
md::SecurityOracle md_oracle;
md::SecurityOracle sec_oracle;
ccapi::SessionOptions session_options = {};
ccapi::SessionConfigs session_configs;
ccapi::Session *session;
@@ -87,7 +98,7 @@ struct CCAPIDeribitOrderMux::PImpl {
size_t corr_id = 0;
std::map<OrderKey, CCAPISingleOrderManager> order_map = {};
// std::set<std::string> subscribed_sec_map = {};
boost::lockfree::spsc_queue<ccapi::Event> event_queue;
boost::lockfree::spsc_queue<ccapi::Event> incoming_event_queue;
OrderEventHandler event_handler;
google::protobuf::Timestamp last_sec_check_time =
google::protobuf::util::TimeUtil::GetCurrentTime();
@@ -98,10 +109,15 @@ struct CCAPIDeribitOrderMux::PImpl {
// (order updates and trades), we buffer the create message and send it once
// we get subcriptions confirmation
std::vector<CreateOrderMsg> buffered_creates = {};
PImpl(const md::SecurityOracle &md_oracle)
: md_oracle(md_oracle), event_queue(100),
const size_t position_req_period_msecs = 5000;
core::TimeStamp last_position_req_time = {};
bool query_pos = true;
std::map<std::string, api::Security const *> sec_map = {};
std::vector<DeribitOpenPosition> pos_vec;
PImpl(const md::SecurityOracle &sec_oracle)
: sec_oracle(sec_oracle), incoming_event_queue(100),
event_handler(
event_queue,
incoming_event_queue,
{ccapi::Event::Type::RESPONSE,
ccapi::Event::Type::SUBSCRIPTION_DATA,
ccapi::Event::Type::SUBSCRIPTION_STATUS}) {
@@ -146,10 +162,21 @@ CCAPIDeribitOrderMux::~CCAPIDeribitOrderMux() {}
void CCAPIDeribitOrderMux::on_securities(
const bts::mercury::Update<NewSecuritiesEvent> &update) {
if (!update.events)
return;
me->sec_map.clear();
for (const auto &p : *update.events) {
api::Security const *sec_p = p.security;
if (sec_p->has_deribit()) {
me->sec_map.insert({sec_p->deribit().security_desc(), sec_p});
}
}
LOG(OrderMan, Info,
"CCAPIDeribitOrderMux::on_securities t_id: {} t_name: {} n_secs: {}",
"CCAPIDeribitOrderMux::on_securities t_id: {} t_name: {} n_secs: {} "
"sec_map sz: {}",
os::get_thread_id(), os::get_thread_name(),
update.events == nullptr ? 0 : update.events->size());
update.events == nullptr ? 0 : update.events->size(),
me->sec_map.size());
}
CCAPIOpResult CCAPIDeribitOrderMux::launch_new_order(
@@ -281,14 +308,14 @@ OnCCAPIOrderUpdate &CCAPIDeribitOrderMux::order_update_cb() {
// this gets called in the main thread, the events have already been queue'd
// from the CCAPI callback thread
void CCAPIDeribitOrderMux::dispatch_incoming_messages() {
auto n_items = me->event_queue.read_available();
auto n_items = me->incoming_event_queue.read_available();
if (n_items > 0) {
for (auto &kv : me->order_map)
kv.second.dispatch_start();
LOG(OrderMan, Info,
"CCAPIDeribitOrderMux::dispatch_incoming_messages n_items: {}",
n_items);
me->event_queue.consume_all(
me->incoming_event_queue.consume_all(
[this](const ccapi::Event &ev) { _handle_event(ev); });
for (auto &kv : me->order_map) {
auto ord_ch = kv.second.dispatch_end();
@@ -298,6 +325,7 @@ void CCAPIDeribitOrderMux::dispatch_incoming_messages() {
}
// TODO, remove orders in end state
}
_query_positions();
// _check_subs();
}
@@ -323,6 +351,8 @@ bool CCAPIDeribitOrderMux::_check_and_add_subscription(
const api::Security &sec) {
auto it = me->sub_map.find(sec.id());
if (it == me->sub_map.end()) {
// no subscription entry, will generate new corrids and sub to
// orders and trades for this security
auto order_status_sub_corrid = me->get_new_corrid();
auto trade_sub_corrid = me->get_new_corrid();
SubscriptionStatus new_status{
@@ -381,7 +411,33 @@ void CCAPIDeribitOrderMux::_handle_element(
_handle_subscription_status(sub_status_msg);
break;
}
case ccapi::Message::Type::GENERIC_PRIVATE_REQUEST: {
LOG(OrderMan, Info,
"CCAPIDeribitOrderMux::_handle_element msg_type: {} corr_id: {} map: "
"{}",
ccapi::Message::typeToString(msg_type), corr_id, map.size());
// the hierarchy is map (Elemenyt.nameValueMap) has a key called
// CCAPI_HTTP_BODY, and the body is json. Inside the json the
// "result" has an array of types - in this case we are interested in
// DeribitOpenPosition
auto it = map.find(CCAPI_HTTP_BODY);
if (it != map.end()) {
const auto &http_data = it->second;
nlohmann::json json_obj = nlohmann::json::parse(http_data);
_handle_generic_pvt_request(json_obj);
// bts::io::JsonReader jr{&json_obj};
// me->pos_vec.clear();
// jr.read("result", me->pos_vec);
// _fire_position_cb();
}
break;
}
default:
LOG(OrderMan, Info,
"CCAPIDeribitOrderMux::_handle_element unhandled messsage type "
"msg_type: {} corr_id: {} map: "
"{}",
ccapi::Message::typeToString(msg_type), corr_id, map.size());
break;
}
}
@@ -409,7 +465,8 @@ void CCAPIDeribitOrderMux::_handle_event(const ccapi::Event &event) {
void CCAPIDeribitOrderMux::_handle_subscription_status(
const CCAPISubscriptionStatusMsg &ss_msg) {
LOG(OrderMan, Info,
"CCAPIDeribitOrderMux::_handle_subscription_status status: {} corr_id: "
"CCAPIDeribitOrderMux::_handle_subscription_status status: {} "
"corr_id: "
"{}",
ss_msg.is_success, ss_msg.corr_id);
if (ss_msg.is_success) {
@@ -487,4 +544,90 @@ void CCAPIDeribitOrderMux::_send_buffered_orders(uint32_t sec_id) {
me->buffered_creates.size());
}
void CCAPIDeribitOrderMux::_query_positions() {
bts::core::TimeStamp now = bts::core::TimeStamp::now();
if (now.millis() - me->last_position_req_time.millis() >=
me->position_req_period_msecs) {
me->last_position_req_time = core::TimeStamp::now();
LOG(OrderMan, Info, "CCAPIDeribitOrderMux::_query_positions");
// TODO: BTC is hard coded here
std::vector<ccapi::Request> reqs = {};
// std::string pos_query = "currency=BTC&kind=future";
if (me->query_pos) {
// straggling the 2 queries for now, dont know why thats required
me->query_pos = false;
std::string pos_query = "currency=BTC";
// curl -X GET "https://test.deribit.com/api/v2/private/get_positions?currency=BTC&kind=future"
ccapi::Request pos_request(
ccapi::Request::Operation::GENERIC_PRIVATE_REQUEST,
CCAPI_EXCHANGE_NAME_DERIBIT);
pos_request.appendParam(
{{CCAPI_HTTP_METHOD, "GET"},
{CCAPI_HTTP_PATH, "/api/v2/private/get_positions"},
{CCAPI_HTTP_QUERY_STRING, pos_query},
{CCAPI_HTTP_HEADERS, "Content-Type: application/json"}});
//me->session->sendRequest(pos_request);
reqs.push_back(pos_request);
} else {
me->query_pos = true;
std::string ord_query = "currency=BTC&kind=future";
// curl -X GET "https://test.deribit.com/api/v2/private/get_open_orders_by_currency?currency=BTC"
ccapi::Request open_ord_request(
ccapi::Request::Operation::GENERIC_PRIVATE_REQUEST,
CCAPI_EXCHANGE_NAME_DERIBIT);
open_ord_request.appendParam(
{{CCAPI_HTTP_METHOD, "GET"},
{CCAPI_HTTP_PATH, "/api/v2/private/get_open_orders_by_currency"},
{CCAPI_HTTP_QUERY_STRING, ord_query},
{CCAPI_HTTP_HEADERS, "Content-Type: application/json"}});
reqs.push_back(open_ord_request);
me->session->sendRequest(open_ord_request);
}
me->session->sendRequest(reqs);
}
}
void CCAPIDeribitOrderMux::_fire_position_cb() {
std::vector<CCAPIPosition> cb_pos = {};
for (const auto &dop : me->pos_vec) {
LOG(OrderMan, Info, "CCAPIDeribitOrderMux::_fire_position_cb dop: {}",
dop);
cb_pos.push_back(CCAPIPosition{
api::VENUE_DERIBIT, dop.instrument_name, dop.size, dop.average_price,
""});
}
me->exchange_position_update_cb(cb_pos);
}
void CCAPIDeribitOrderMux::_handle_generic_pvt_request(
const nlohmann::json &json_obj) {
if (!json_obj.contains("result"))
return;
const auto &res_obj = json_obj.at("result");
LOG(OrderMan, Info,
"CCAPIDeribitOrderMux::_handle_generic_pvt_request res_type: {}",
std::string(res_obj.type_name()));
LOG(OrderMan, Info,
"CCAPIDeribitOrderMux::_handle_generic_pvt_request res: {}",
res_obj.dump());
if (res_obj.is_array()) {
me->pos_vec.clear();
for (const auto &res_entry : res_obj) {
DeribitOpenPosition dop = {};
bts::io::JsonReader jr{&res_entry};
if (jr.read(dop)) {
me->pos_vec.push_back(dop);
}
}
_fire_position_cb();
}
// bts::io::JsonReader jr{&res_obj};
// if (json_obj.is_array()) {
// bts::io::JsonReader jr{&json_obj};
// me->pos_vec.clear();
// jr.read("result", me->pos_vec);
// _fire_position_cb();
// }
}
} // namespace bts::orderman
@@ -58,6 +58,9 @@ class CCAPIDeribitOrderMux : public ICCAPIOrderMux {
// void _check_subs();
void _send_order(const CreateOrderMsg &);
void _send_buffered_orders(uint32_t sec_id);
void _query_positions();
void _fire_position_cb();
void _handle_generic_pvt_request(const nlohmann::json &json_obj);
};
} // namespace bts::orderman
@@ -1,4 +1,5 @@
#include "CCAPIOrderCallbackMsgs.h"
#include "lib/io/JsonReader.h"
#include "lib/utils/bts_assert.h"
namespace bts::orderman {
@@ -18,4 +19,42 @@ bts::api::orderman::OrderStatus convert_ccapi_status(
return bts::api::orderman::OrderStatus::ORDER_STATUS_COMPLETED;
}
void DeribitOpenPosition::parse(
const nlohmann::json &j, DeribitOpenPosition &op) {
bts::io::JsonReader jr{&j};
jr.read("total_profit_loss", op.total_profit_loss);
jr.read("size_currency", op.size_currency);
jr.read("size", op.size);
jr.read("settlement_price", op.settlement_price);
jr.read("realized_profit_loss", op.realized_profit_loss);
jr.read("instrument_name", op.instrument_name);
jr.read("kind", op.kind);
jr.read("direction", op.direction);
jr.read("average_price", op.average_price);
}
void from_json(const nlohmann::json &j, DeribitOpenPosition &dop) {
bts::io::JsonReader jr{&j};
jr.read("instrument_name", dop.instrument_name);
jr.read("direction", dop.direction);
jr.read("size", dop.size);
jr.read("average_price", dop.average_price);
}
void to_json(nlohmann::json &j, const DeribitOpenPosition &dop) {
j = nlohmann::json{
{"instrument_name", dop.instrument_name},
{"direction", dop.direction},
{"size", dop.size},
{"average_price", dop.average_price}};
}
std::ostream &operator<<(
std::ostream &out, const DeribitOpenPosition &dop) noexcept {
std::string sep = " ";
out << "instrument_name: " << dop.instrument_name << sep
<< "direction: " << dop.direction << sep << "size: " << dop.size << sep
<< "average_price: " << dop.average_price;
return out;
}
} // namespace bts::orderman
@@ -3,6 +3,7 @@
#include "ccapi_cpp/ccapi_session.h"
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/io/JsonReader.h"
#include "lib/logging/logging.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/utils/PBUtils.h"
@@ -14,6 +15,7 @@
// #include <chrono>
// #include <memory>
// #include <ostream>
#include <nlohmann/json.hpp>
#include <string>
using namespace std;
@@ -183,4 +185,36 @@ struct CCAPISubscriptionStatusMsg {
// maybe add info message here
};
class DeribitOpenPosition {
public:
double total_profit_loss;
double size_currency;
double size;
double settlement_price;
double realized_profit_loss;
double realized_funding;
double open_orders_margin;
double mark_price;
double maintenance_margin;
double leverage;
std::string kind;
double interest_value;
std::string instrument_name;
double initial_margin;
double index_price;
double floating_profit_loss;
double estimated_liquidation_price;
std::string direction;
double delta;
double average_price;
static void parse(const nlohmann::json &j, DeribitOpenPosition &dop);
};
void from_json(const nlohmann::json &j, DeribitOpenPosition &dop);
void to_json(nlohmann::json &j, const DeribitOpenPosition &dop);
std::ostream &operator<<(
std::ostream &out, const DeribitOpenPosition &dob) noexcept;
} // namespace bts::orderman
+5 -2
View File
@@ -13,6 +13,7 @@
//#include <bits/stdint-uintn.h>
//#include <boost/container/small_vector.hpp>
#include "CCAPIOrderCallbackMsgs.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/md/SecurityOracle.h"
#include "lib/mercury/events/Update.h"
#include "lib/orderman/events/NewSecuritiesEvent.h"
@@ -32,8 +33,10 @@ struct CCAPIOpResult {
};
struct CCAPIPosition {
uint32_t bts_sec_key; // bts sec key
double position; // +ve for long
api::Venue venue;
std::string exchange_sec_desc;
double position; // +ve for long
double average_price;
std::string sub_account;
};
@@ -81,8 +81,7 @@ api::orderman::NativeOrderStatus native_order_status(const CCAPIOrderState &s) {
}
struct CCAPIExecutionGateway::PImpl {
PImpl(md::SecurityOracle o)
: sec_oracle(o), order_mux(new CCAPIDeribitOrderMux(o)) {
PImpl(md::SecurityOracle o) : sec_oracle(o) {
LOG(OrderMan, Info, "CCAPIExecutionGateway::PImpl constructor");
}
admin::AdminServiceUtils::ExchangeSubAccountResolver subaccount_resolver =
@@ -97,7 +96,7 @@ struct CCAPIExecutionGateway::PImpl {
OnExchangePositionState on_exchange_position_state;
OnExchangeOpenOrdersState on_exchange_open_orders;
// orders are sent out and callbacks received via IEMOrderMuxer
ICCAPIOrderMux *order_mux;
ICCAPIOrderMux *order_mux = nullptr;
size_t msg_seq_num = 0;
boost::object_pool<ExecutionReport> exe_report_pool;
std::unordered_map<OrderKey, CreateOrderMsg> order_map = {};
@@ -108,6 +107,7 @@ struct CCAPIExecutionGateway::PImpl {
bts::datetime::PTime last_pos_fire_time = bts::datetime::now();
uint32_t pos_fire_period_ms = 5000;
size_t dispatch_idx = 0;
bool inited() const { return order_mux != nullptr; }
};
CCAPIExecutionGateway::CCAPIExecutionGateway(md::SecurityOracle o)
@@ -130,6 +130,8 @@ void CCAPIExecutionGateway::start() {
// this is a periodic timer
void CCAPIExecutionGateway::dispatch_incoming_fix_messages() {
if (!me->inited())
return;
me->order_mux->dispatch_incoming_messages();
}
@@ -139,6 +141,8 @@ void CCAPIExecutionGateway::on_securities(
"CCAPIExecutionGateway::on_securities t_id: {} t_name: {} n_secs: {}",
os::get_thread_id(), os::get_thread_name(),
update.events == nullptr ? 0 : update.events->size());
if (!me->inited())
return;
me->order_mux->on_securities(update);
}
@@ -159,6 +163,8 @@ void CCAPIExecutionGateway::send_fix(const jetfix::OutFIXPtr &, trace::CorID) {
void CCAPIExecutionGateway::send_msgs(
std::vector<OrderMsg> const &msg_vec, trace::CorID) {
if (!me->inited())
return;
LOG(OrderMan, Info, "CCAPIExecutionGateway::send_msgs count: {}",
msg_vec.size());
for (const auto &m : msg_vec) {
@@ -215,10 +221,37 @@ OnExchangeOpenOrdersState &CCAPIExecutionGateway::on_exchange_open_orders() {
}
void CCAPIExecutionGateway::_init() {
if (me->order_mux != nullptr)
return;
LOG(OrderMan, Info, "CCAPIExecutionGateway::_init");
me->order_mux = new CCAPIDeribitOrderMux(me->sec_oracle);
me->order_mux->order_update_cb() =
[this](const CCAPIOrderState &order_state) {
_on_order_cb(order_state);
};
me->order_mux
->exchange_position_update_cb() = [this](const std::vector<CCAPIPosition>
&pos_vec) {
LOG(OrderMan, Info, "CCAPIExecutionGateway position cb sz: {}",
pos_vec.size());
ExchangePosition out_pos = {api::Venue::VENUE_DERIBIT, {}};
for (const CCAPIPosition &pos : pos_vec) {
auto sec_ptr = _find_sec(pos.venue, pos.exchange_sec_desc);
if (sec_ptr != nullptr) {
out_pos.total_exchange_positions.push_back(
ExchangePosition::UnitPosition{
pos.exchange_sec_desc, sec_ptr, "MAIN", 9, pos.position,
Price{pos.average_price}});
} else {
LOG(OrderMan, Error,
"CCAPIExecutionGateway could not resolve exchange: {} desc: {}",
api::Venue_Name(pos.venue), pos.exchange_sec_desc);
}
}
LOG(OrderMan, Info, "CCAPIExecutionGateway firing positions sz: {}",
out_pos.total_exchange_positions.size());
me->on_exchange_position_state(out_pos);
};
}
void CCAPIExecutionGateway::_on_order_cb(const CCAPIOrderState &s) {
@@ -287,4 +320,18 @@ void CCAPIExecutionGateway::_on_order_cb(const CCAPIOrderState &s) {
// _fire_leg_er(em_cb, er);
}
api::Security const *CCAPIExecutionGateway::_find_sec(
// TODO
api::Venue venue,
const std::string &exchange_security_desc) {
for (auto p : me->sec_oracle.get_securities()) {
if (p->exchange() == venue) {
bts_assert(venue == api::VENUE_DERIBIT);
if (p->deribit().security_desc() == exchange_security_desc)
return p;
}
}
return nullptr;
}
} // namespace bts::orderman
@@ -2,6 +2,7 @@
#include "IExecutionGateway.h"
#include "lib/api/services/AdminService.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/orderman/CancelOrderMsg.h"
#include "lib/orderman/CancelReplaceOrderMsg.h"
#include "lib/orderman/CreateOrderMsg.h"
@@ -52,6 +53,8 @@ class CCAPIExecutionGateway : public IExecutionGateway {
private:
void _init();
void _on_order_cb(const CCAPIOrderState &);
api::Security const *_find_sec(
api::Venue venue, const std::string &exchange_desc);
};
} // namespace orderman
@@ -35,8 +35,8 @@ std::unique_ptr<IExecutionGateway> ExecutionGatewayFactory::create_gateway(
conn, pl, o);
return std::make_unique<GLBXExecutionGateway>(conn, pl, o);
} else if (conn.venue() == bts::api::VENUE_DERIBIT)
return std::make_unique<EMExecutionGateway>(o);
//return std::make_unique<CCAPIExecutionGateway>(o);
// return std::make_unique<EMExecutionGateway>(o);
return std::make_unique<CCAPIExecutionGateway>(o);
return std::make_unique<FakeExecutionGateway>(o);
}