Exchange Positions can now be obtained for multiple sub accounts

This commit is contained in:
taha afzal committed 2022-07-09 01:40:28 -04:00
1 parent 4a1c1d023a
commit d98fb399a8
10 files changed
+228 -133

No files matched your search

@@ -529,7 +529,7 @@ bts::api::orderman::RoutingRecord AdminServiceUtils::get_routing_record(
rv.set_account(account_ptr->name());
rv.set_operator_id(user_ptr->name());
AdminServiceUtils::ExchangeSubAccountResolver resolver;
auto sub_acc_ptr = resolver.find_entry(&sec, account_ptr->name());
auto sub_acc_ptr = resolver.find_entry(&sec, account_ptr->name(), true);
if (!sub_acc_ptr) {
out_result.set_is_success(false);
out_result.set_error_text("Could not resolve sub account");
@@ -26,7 +26,13 @@ class AdminServiceUtils {
utils::pb::to_string(exch_sub_accounts));
}
bts::config::toggles::ExchangeSubAccountUnit const *find_entry(
api::Security const *sec, const std::string &bts_account) {
api::Security const *sec,
const std::string &account,
bool is_int_account) {
auto get_account = [&](const auto &entry) {
return is_int_account ? entry.bts_account_name()
: entry.exchange_subaccount();
};
if (sec == nullptr)
return nullptr;
api::SecurityTuple s_st = {};
@@ -37,7 +43,7 @@ class AdminServiceUtils {
const auto &u_st = u.sec_tup();
if (u_st.type() == s_st.type() && u_st.venue() == s_st.venue() &&
u_st.symbol() == s_st.symbol()) {
if (u.bts_account_name() == bts_account)
if (get_account(u) == account)
return &u;
}
}
+38 -2
View File
@@ -110,7 +110,7 @@ value{
venue: VENUE_DERIBIT
symbol: "BTC"
}
bts_account_name: "ACC_BTC_SMALL"
bts_account_name: "ACC_BTC_S"
exchange_subaccount: "v1iSzZrY"
}
accounts{
@@ -119,10 +119,46 @@ value{
venue: VENUE_DERIBIT
symbol: "BTC"
}
bts_account_name: "ACC_BTC_SMALL"
bts_account_name: "ACC_BTC_S"
exchange_subaccount: "v1iSzZrY"
}
accounts{
sec_tup {
type: SECURITYTYPE_FUTURE
venue: VENUE_DERIBIT
symbol: "ETH"
}
bts_account_name: "ACC_ETH"
exchange_subaccount: "ZmIKL6fb"
}
accounts{
sec_tup {
type: SECURITYTYPE_OPTION
venue: VENUE_DERIBIT
symbol: "ETH"
}
bts_account_name: "ACC_ETH"
exchange_subaccount: "ZmIKL6fb"
}
accounts{
sec_tup {
type: SECURITYTYPE_FUTURE
venue: VENUE_DERIBIT
symbol: "ETH"
}
bts_account_name: "ACC_ETH_S"
exchange_subaccount: "v1iSzZrY"
}
accounts{
sec_tup {
type: SECURITYTYPE_OPTION
venue: VENUE_DERIBIT
symbol: "ETH"
}
bts_account_name: "ACC_ETH_S"
exchange_subaccount: "v1iSzZrY"
}
}
}
+1
View File
@@ -99,6 +99,7 @@ message DefaultAccount{
repeated DefaultAccountUnit default_accounts = 1;
}
// this ultimately needs to go to risk admin
message ExchangeSubAccountUnit{
optional bts.api.SecurityTuple sec_tup = 1;
optional string bts_account_name = 10;
@@ -53,31 +53,6 @@ namespace orderman {
using EMOrderKey = std::int64_t;
// This is used to resolve internal accounts for ExchangePositions
struct StaticAccountResolver {
const bts::config::toggles::DefaultAccount def_acc;
StaticAccountResolver()
: def_acc(config::get<config::toggles::DefaultAccount>()) {
LOG(OrderMan, Info, "StaticAccountResolver accounts: \n{} ",
utils::pb::to_string(def_acc));
}
std::string get_default_account(api::Security const *sec) {
if (sec == nullptr)
return "";
api::SecurityTuple s_st = {};
s_st.set_type(bts::securities::security_type(*sec));
s_st.set_venue(sec->exchange());
s_st.set_symbol(sec->symbol());
for (const auto &u : def_acc.default_accounts()) {
const auto &u_st = u.sec_tup();
if (u_st.type() == s_st.type() && u_st.venue() == s_st.venue() &&
u_st.symbol() == s_st.symbol()) {
return u.account_name();
}
}
return "";
}
};
// Order callbacks from order_mux are copied here and placed in the tbb queue
struct EMCallbackMsg {
// didnt make it const since tbb::queue needs a copy cons
@@ -88,14 +63,15 @@ struct EMCallbackMsg {
// Position callbacks from order_mux are copied here and placed in the tbb queue
struct EMExchangePositionCallbackMsg {
std::vector<emumba::order_entry::position> positions;
std::vector<EMPosition> positions;
};
struct EMExecutionGateway::PImpl {
PImpl(md::SecurityOracle o) : sec_oracle(o) {
LOG(OrderMan, Info, "EMExecutionGateway::PImpl constructor");
}
StaticAccountResolver account_resolver = {};
admin::AdminServiceUtils::ExchangeSubAccountResolver subaccount_resolver =
{};
// callback cashed data
api::admin::AdminData admin_data = {};
md::SecurityOracle sec_oracle;
@@ -322,9 +298,7 @@ bool EMExecutionGateway::_init() {
"positions: {}",
exch_pos.size());
// lots of copying going on, but this is periodic
EMExchangePositionCallbackMsg pos_cb = {};
for (const auto &exh_pos : exch_pos)
pos_cb.positions.push_back(exh_pos);
EMExchangePositionCallbackMsg pos_cb = {exch_pos};
me->exch_pos_queue.push(pos_cb);
};
me->order_mux
@@ -339,8 +313,9 @@ bool EMExecutionGateway::_init() {
}
// converts the CreateOrderMsg to emumba::order_entry::new_order and
// sends it to EM gateway
// sends it to ExchangePosition
void EMExecutionGateway::_send_create(const CreateOrderMsg &c) {
// TODO: check if sub account exists
LOG(OrderMan, Info,
"EMExecutionGateway::_send_create key: {} p: {} q: {} account: {} "
"operator_id: {} sub_account: {}",
@@ -368,11 +343,18 @@ void EMExecutionGateway::_send_create(const CreateOrderMsg &c) {
new_order.order_def.exchange_order = deribit_def;
new_order.order_def.account = c.routing_record->account_code();
// place order now
emumba::order_entry::order_key new_order_key =
me->order_mux->launch_new_order(new_order);
EMOrderKey em_key = new_order_key.value();
me->order_map.insert({em_key, c});
me->key_map.insert({c.key, em_key});
auto op_res = me->order_mux->launch_new_order(new_order);
if (op_res.is_success) {
auto new_order_key = op_res.ok;
EMOrderKey em_key = new_order_key.value();
me->order_map.insert({em_key, c});
me->key_map.insert({c.key, em_key});
} else {
LOG(OrderMan, Error,
"EMExecutionGateway::_send_create launch_new_order rejected, "
"error_msg: {}",
op_res.err_msg);
}
}
void EMExecutionGateway::_send_cxl_replace(
@@ -429,6 +411,7 @@ void EMExecutionGateway::_process_cb_and_fire_er(const EMCallbackMsg &em_cb) {
// cant reate one on the stack
ExecutionReport *er_p = me->exe_report_pool.construct();
ExecutionReport &er = *er_p;
// length of these have to be <= 12
to_array(cr_msg.routing_record->account(), &er.account_name.value());
to_array(cr_msg.routing_record->operator_id(), &er.operator_id.value());
er.order_key = cr_msg.key;
@@ -481,10 +464,23 @@ void EMExecutionGateway::_process_cb_and_fire_er(const EMCallbackMsg &em_cb) {
me->on_exe_report(er, {});
}
// converts the EMPosition received in the callback from IEMOrderMuxer, to
// ExchangePosition (resolved security and account)
void EMExecutionGateway::_process_pos_cb_and_fire(
const EMExchangePositionCallbackMsg &pos_cb_msg) {
auto get_account = [this](api::Security const *sec) {
auto account_name = me->account_resolver.get_default_account(sec);
// resoves the account from sec, sub_account
auto get_account = [this](
api::Security const *sec,
const std::string &exch_account) {
auto *entry =
me->subaccount_resolver.find_entry(sec, exch_account, false);
if (entry == nullptr) {
LOG(OrderMan, Error,
"Could not resolve internal account for sec: {}, sub_account: {}",
utils::pb::to_string(*sec), exch_account);
return std::tuple<std::string, api::admin::BTSAccount *>("", nullptr);
}
auto account_name = entry->bts_account_name();
auto bts_account_ptr =
admin::AdminServiceUtils::find_account(me->admin_data, account_name);
return std::tuple<std::string, api::admin::BTSAccount *>(
@@ -505,17 +501,21 @@ void EMExecutionGateway::_process_pos_cb_and_fire(
me->exchange_position.exchange = api::VENUE_DERIBIT;
for (const auto &pos : pos_cb_msg.positions) {
const std::string &exch_sec_desc =
std::get<std::string>(pos.sec_id.value());
std::get<std::string>(pos.position.sec_id.value());
auto sec = get_sec(exch_sec_desc);
double pos_qty = std::get<double>(pos.qty.value());
auto [account_name, bts_account_ptr] = get_account(sec);
double pos_qty = std::get<double>(pos.position.qty.value());
auto [account_name, bts_account_ptr] = get_account(sec, pos.sub_account);
std::optional<uint32_t> account_id =
bts_account_ptr == nullptr
? std::nullopt
: std::optional<uint32_t>(bts_account_ptr->id());
LOG(OrderMan, Info,
"EMExecutionGateway::_process_pos_cb_and_fire pos: {} bts_account: "
"{}",
pos, account_name);
me->exchange_position.total_exchange_positions.push_back(
{exch_sec_desc, sec, account_name, account_id, pos_qty,
Price(pos.average_price.to_double())});
Price(pos.position.average_price.to_double())});
}
LOG(OrderMan, Info, "EMExecutionGateway::_process_pos_cb_and_fire n_pos: {}",
me->exchange_position.total_exchange_positions.size());
@@ -1,13 +1,11 @@
#include "EmumbaMain.h"
#pragma once
#include "lib/config/config.h"
#include "lib/logging/logging.h"
#include "server/sparkd/cmds/crypto/DeribitDemoConfiguration.h"
#include <emumba-deribit-connector/deribit_handler.h>
#include <emumba-deribit-connector/deribit_listener.h>
#include <emumba-deribit-connector/trading_session_interface.h>
namespace bts::md {
namespace bts::orderman {
struct EMTradingSession {
emumba::deribit_connector::configuration::credential_config credential;
@@ -15,4 +13,4 @@ struct EMTradingSession {
trading_interface;
};
} // namespace bts::md
} // namespace bts::orderman
@@ -7,6 +7,7 @@
#include "lib/md/PriceSize.h"
#include "lib/md/Shm/FixedSecurity.h"
#include "lib/md/protos/co2.pb.h"
#include "lib/os/os.h"
#include "lib/persistence/IDGenerator.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/TimeStamp.h"
@@ -15,6 +16,7 @@
#include "lib/utils/datetime.h"
#include "lib/utils/overloaded.h"
#include "lib/utils/stats.h"
#include "server/sparkd/md/platforms/emumba/EMTradingSession.h"
#include "server/sparkd/md/platforms/emumba/EmumbaSecurityIndex.h"
#include "server/sparkd/md/platforms/emumba/IEMOrderMuxer.h"
#include <bits/stdint-intn.h>
@@ -36,6 +38,7 @@
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <string>
@@ -47,6 +50,7 @@
#include <vector>
using namespace emumba::deribit_connector;
using namespace bts::orderman;
namespace bts::md {
@@ -232,19 +236,33 @@ struct SecurityAuxData {
: locked_key(locked_key), symbol(symbol) {}
};
struct PoitionRequestHelper {
boost::container::small_vector<std::string, 2> currencies = {"BTC", "ETH"};
// Encapsulates the code that queries the trading session for position
struct PositionRequestHelper {
// TODO, get it from else where
const boost::container::small_vector<std::string, 2> currencies = {
"BTC", "ETH"};
const std::vector<EMTradingSession> *sessions;
size_t n_pending_reqs = 0;
size_t n_reqs_while_waiting = 0;
std::vector<emumba::order_entry::position> total_pos = {};
std::vector<EMPosition> total_pos = {};
void reset_new_requests() {
n_pending_reqs = currencies.size();
n_reqs_while_waiting = 0;
total_pos.clear();
PositionRequestHelper(std::vector<EMTradingSession> *sessions)
: sessions(sessions) {}
// gets the account from the index
std::string get_subaccount(size_t idx) const {
for (const auto &s : *sessions) {
if (s.credential.index == idx) {
return s.credential.client_id;
}
}
bts_assert(false);
return "";
}
// if there are any pending reqs
bool pending() const { return n_pending_reqs > 0; }
// update from the server for a single session, currency
void on_new_update(
const std::vector<emumba::order_entry::position const *> &positions) {
if (!pending()) {
@@ -254,10 +272,31 @@ struct PoitionRequestHelper {
"message");
return;
}
for (const auto *p : positions)
total_pos.push_back(*p);
LOG(OrderMan, Info, "PoitionRequestHelper::on_new_update count: {}",
positions.size());
for (const auto *p : positions) {
total_pos.push_back({*p, get_subaccount(p->subaccount_index)});
auto &new_it = total_pos.back();
LOG(OrderMan, Info, "PoitionRequestHelper::on_new_update {}", new_it);
}
n_pending_reqs--;
}
// sends requests for all sessions, currencies
bool send_all_position_requests() {
if (pending()) {
LOG(OrderMan, Error,
"PositionRequestHelper::send_all_position_requests already "
"pending requests");
return false;
}
total_pos.clear();
n_pending_reqs = sessions->size() * currencies.size();
for (auto &s : *sessions)
for (const auto &c : currencies)
s.trading_interface->get_all_positions(c);
return true;
}
};
struct EmMDListener::PImpl {
@@ -265,7 +304,7 @@ struct EmMDListener::PImpl {
uint32_t n_updates = 0;
bts::persistence::IDGenerator id_gen = {};
emumba::deribit_connector::deribit_handler *handler = nullptr;
emumba::deribit_connector::trading_session_interface *order_gtwy;
std::vector<EMTradingSession> trading_sessions;
EmumbaSecurityIndex &index;
MDProducer &md_producer;
std::unordered_map<std::string, SecurityAuxData> sec_map = {};
@@ -274,9 +313,10 @@ struct EmMDListener::PImpl {
orderman::OnEMOrderUpdate order_update_cb;
orderman::OnEMExchangePositionUpdate exchange_position_update_cb;
orderman::OnEMExchangeOpenOrderUpdate exchange_open_orders_update_cb;
std::map<int64_t, EMTradingSession *> order_acc_map;
std::unordered_map<int64_t, bts::orderman::EMOrderState> order_state_map =
{};
PoitionRequestHelper pos_helper = {};
PositionRequestHelper pos_helper = {&trading_sessions};
explicit PImpl(EmumbaSecurityIndex &index_a, MDProducer &md_producer_a)
: index(index_a), md_producer(md_producer_a) {}
};
@@ -295,28 +335,45 @@ EmMDListener::EmMDListener(EmumbaSecurityIndex &index, MDProducer &md_producer)
void run(__attribute__((unused)) const std::string &config_path) {}
emumba::order_entry::order_key EmMDListener::launch_new_order(
bts::orderman::OpResult EmMDListener::launch_new_order(
const emumba::order_entry::new_order &order) {
EMTradingSession *trading_session =
_find_trading_sessions(order.order_def.account);
if (trading_session == nullptr) {
return {
false, emumba::order_entry::order_key{0},
"Could not find session for subaccount"};
}
const std::string &def =
std::get<std::string>(order.order_def.sec_id.value());
auto qty = std::get<double>(order.qty.value());
auto key = me->order_gtwy->launch_new_order(order);
auto key = trading_session->trading_interface->launch_new_order(order);
// we add the key here, so we on update and cxl we can resolve the session
me->order_acc_map.insert({key.value(), trading_session});
LOG(OrderMan, Info,
"IEMOrderMuxer::launch_new_order sec: {} p: {} q: {} account: {} "
"IEMOrderMuxer::launch_new_order tid: {} sec: {} p: {} q: {} "
"account: "
"{} "
"returned_key: {}",
def, order.px->to_double(), qty, order.order_def.account, key.value());
return key;
os::get_thread_id(), def, order.px->to_double(), qty,
order.order_def.account, key.value());
return {true, key, ""};
}
bool EmMDListener::update_order(const emumba::order_entry::order_update &u) {
double q = std::get<double>(u.qty.value());
LOG(OrderMan, Info, "IEMOrderMuxer::update_order key: {} p: {} q: {}",
u.ord_key.value(), u.px.value().to_double(), q);
return me->order_gtwy->update_order(u);
auto it = me->order_acc_map.find(u.ord_key.value());
bts_assert(it != me->order_acc_map.end());
return it->second->trading_interface->update_order(u);
}
bool EmMDListener::cancel_order(const emumba::order_entry::order_key &key) {
LOG(OrderMan, Info, "IEMOrderMuxer::cancel_order key: {}", key.value());
return me->order_gtwy->cancel_order(key);
auto it = me->order_acc_map.find(key.value());
bts_assert(it != me->order_acc_map.end());
return it->second->trading_interface->cancel_order(key);
}
orderman::OnEMOrderUpdate &EmMDListener::order_update_cb() {
@@ -324,23 +381,8 @@ orderman::OnEMOrderUpdate &EmMDListener::order_update_cb() {
}
void EmMDListener::get_positions() {
if (me->pos_helper.pending()) {
me->pos_helper.n_reqs_while_waiting++;
LOG(OrderMan, Error,
"EmMDListener::get_positions already pending requests");
if (me->pos_helper.n_reqs_while_waiting <= 5)
return;
// we noticed that if we disconnect after making a request, we never get
// the on_get_all_position callback. Therefore if we wait long enough
// without getting a callback start anew
LOG(OrderMan, Error,
"EmMDListener::get_positions failed (because of pending requests) {} "
"times. Reseting and requesting again",
me->pos_helper.n_reqs_while_waiting);
}
me->pos_helper.reset_new_requests();
for (const auto &c : me->pos_helper.currencies)
me->order_gtwy->get_all_positions(c);
// redirected to pos_helper where all the async requests happen
me->pos_helper.send_all_position_requests();
}
orderman::OnEMExchangePositionUpdate &
@@ -349,8 +391,10 @@ EmMDListener::exchange_position_update_cb() {
}
void EmMDListener::get_open_orders() {
me->order_gtwy->get_all_open_orders("BTC");
me->order_gtwy->get_all_open_orders("ETH");
for (auto &s : me->trading_sessions) {
s.trading_interface->get_all_open_orders("BTC");
s.trading_interface->get_all_open_orders("ETH");
}
}
orderman::OnEMExchangeOpenOrderUpdate &
@@ -378,10 +422,11 @@ void EmMDListener::on_order_update(
order_state.exchange_order_state);
// these are all logs
LOG(OrderMan, Info,
"IEMOrderMuxer::on_order_update ec.value: {} ec.message: {} key: {} "
"IEMOrderMuxer::on_order_update tid: {} ec.value: {} ec.message: {} "
"key: {} "
"exchange_key: {} status: {} pr: {} qty: {} filled_pr: {} fills: {} "
"n_trades: {}",
ec.value(), ec.message(), order_key.value(),
os::get_thread_id(), ec.value(), ec.message(), order_key.value(),
exchange_order_state.exchange_order_id,
static_cast<int>(order_state.order_status), order_state.px.to_double(),
extract_qty(order_state.qty), order_state.avg_price.to_double(),
@@ -400,32 +445,12 @@ void EmMDListener::on_order_update(
me->order_update_cb(ec, order_key, ex_ord_state);
}
void log_position_discrepency(const emumba::order_entry::position &p) {
double pos = extract_qty(p.qty);
if ((p.position_side == emumba::market_data::side::buy && pos < 0) ||
(p.position_side == emumba::market_data::side::sell && pos > 0))
LOG(OrderMan, Error,
"EmMDListener::on_get_all_positions incompatible position sec: {} "
"side: {} qty: {}",
extract_security_desc(p.sec_id), static_cast<int>(p.position_side),
pos);
}
void EmMDListener::on_get_all_positions(
const emumba::deribit_connector::data_types::error &ec,
const std::vector<emumba::order_entry::position const *> &positions) {
LOG(OrderMan, Info,
"IEMOrderMuxer::on_get_all_positions no of positions: {} ec: {}",
positions.size(), ec.message());
for (auto p : positions) {
LOG(OrderMan, Info,
"IEMOrderMuxer::on_get_all_positions sec_id: {} side: {} qty: {} "
"avg_px: "
"{}",
extract_security_desc(p->sec_id), static_cast<int>(p->position_side),
std::get<double>(p->qty.value()), p->average_price.to_double());
log_position_discrepency(*p);
}
me->pos_helper.on_new_update(positions);
if (!me->pos_helper.pending()) {
me->exchange_position_update_cb(me->pos_helper.total_pos);
@@ -703,7 +728,8 @@ void EmMDListener::on_trades(
void EmMDListener::on_error(
const emumba::market_data::market_data_error &error) {
LOG(md, Error,
"EmMDListener::on_error error_type: {} error: {} resetting all books, "
"EmMDListener::on_error error_type: {} error: {} resetting all "
"books, "
"n_secs: {}",
static_cast<int>(error.err_type), error.err_text, me->index.n_secs());
if (error.err_type ==
@@ -719,10 +745,12 @@ void EmMDListener::on_error(
void EmMDListener::set_handler(
emumba::deribit_connector::deribit_handler *handler,
emumba::deribit_connector::trading_session_interface *order_gtway) {
const std::vector<EMTradingSession> &trading_sessions) {
LOG(OrderMan, Info, "EmMDListener::set_handler tid: {} n_sessions: {}",
os::get_thread_id(), trading_sessions.size());
_order_mux = this;
me->handler = handler;
me->order_gtwy = order_gtway;
me->trading_sessions = trading_sessions;
_subsribe();
}
@@ -912,6 +940,16 @@ void EmMDListener::_subsribe() {
}
}
EMTradingSession *EmMDListener::_find_trading_sessions(
const std::string &account) {
for (auto &t : me->trading_sessions) {
if (t.credential.client_id == account) {
return &t;
}
}
return nullptr;
}
/* #endregion */
} // namespace bts::md
@@ -1,4 +1,5 @@
#pragma once
#include "EMTradingSession.h"
#include "EmumbaSecurityIndex.h"
#include "IEMOrderMuxer.h"
#include "lib/domain_model/SecurityKey.h"
@@ -25,7 +26,7 @@ class EmMDListener : public emumba::deribit_connector::deribit_listener,
// Run the connector
void run(const std::string &config_path);
// IEMOrderMuxer interface
emumba::order_entry::order_key launch_new_order(
bts::orderman::OpResult launch_new_order(
const emumba::order_entry::new_order &order) override;
bool update_order(const emumba::order_entry::order_update &update) override;
bool cancel_order(const emumba::order_entry::order_key &key) override;
@@ -144,6 +145,8 @@ class EmMDListener : public emumba::deribit_connector::deribit_listener,
const emumba::market_data::security_id &id);
// subscribes to all securities here
void _subsribe();
bts::orderman::EMTradingSession *_find_trading_sessions(
const std::string &account);
public:
// the handler and the listener both need each other, however the handler
@@ -151,7 +154,7 @@ class EmMDListener : public emumba::deribit_connector::deribit_listener,
// listener, the handler is passed using the following function
void set_handler(
emumba::deribit_connector::deribit_handler *handler,
emumba::deribit_connector::trading_session_interface *order_gtway);
const std::vector<bts::orderman::EMTradingSession> &trading_sessions);
static IEMOrderMuxer *order_mux() { return _order_mux; }
private:
@@ -25,6 +25,7 @@
using namespace std;
using namespace bts::co2;
using namespace bts::orderman;
namespace bts::md {
@@ -73,9 +74,7 @@ struct EmumbaMain::PImpl {
trading_session_vec.push_back({sess, ts_ptr});
}
bts_assert(!trading_session_vec.empty());
listener.set_handler(
real_handler.get(),
trading_session_vec.at(0).trading_interface.get());
listener.set_handler(real_handler.get(), trading_session_vec);
}
};
@@ -11,8 +11,14 @@
namespace bts::orderman {
struct EMPosition {
emumba::order_entry::position position;
std::string sub_account;
};
// This is the order data passed by the EmMDListener via the order_update_cb
struct EMOrderState {
size_t n_cbs = 0;
double order_px;
double order_qty;
double total_fills = 0;
@@ -34,6 +40,7 @@ struct EMOrderState {
const emumba::order_entry::order_state &o_state) {
using namespace emumba::market_data;
using namespace emumba::order_entry;
n_cbs++;
status = o_state.order_status;
last_filled_qty = 0;
last_filled_price = 0;
@@ -73,20 +80,15 @@ struct EMOrderState {
rv.px = price(order_px);
rv.avg_price = price(avg_filled_price);
return rv;
// return {
// o_state.subaccount_index,
// o_state.error,
// o_state.order_update_type,
// o_state.order_status,
// quantity(order_qty),
// quantity(total_fills),
// price(order_px),
// price(avg_filled_price),
// o_state.trades,
// o_state.exchange_order_state};
}
};
struct OpResult {
bool is_success;
emumba::order_entry::order_key ok;
std::string err_msg;
};
inline std::ostream &operator<<(std::ostream &out, EMOrderState const &s) {
out << "status: " << static_cast<int>(s.status) << " pr: " << s.order_px
<< " q: " << s.order_qty << " fills: " << s.total_fills
@@ -97,13 +99,24 @@ inline std::ostream &operator<<(std::ostream &out, EMOrderState const &s) {
return out;
}
inline std::ostream &operator<<(std::ostream &out, EMPosition const &p) {
const auto &pos = p.position;
out << "subaccount_index: " << pos.subaccount_index
<< " subaccount: " << p.sub_account
<< " sec_id: " << std::get<std::string>(pos.sec_id.value())
<< " qty: " << std::get<double>(pos.qty.value())
<< " price: " << pos.average_price.to_double()
<< " side: " << (int)pos.position_side;
return out;
}
using OnEMOrderUpdate = std::function<void(
const emumba::deribit_connector::data_types::error &,
const emumba::order_entry::order_key &,
const EMOrderState &)>;
using OnEMExchangePositionUpdate =
std::function<void(const std::vector<emumba::order_entry::position> &)>;
std::function<void(const std::vector<EMPosition> &)>;
using OnEMExchangeOpenOrderUpdate = std::function<void(
const std::vector<emumba::order_entry::open_order const *> &)>;
@@ -117,7 +130,8 @@ class IEMOrderMuxer {
////////////////////////////////////////////////////////////////////////////
// These calls enter, update and cancel a single order respectively.
// Their results are corresponded in the order_update_cb callback
virtual emumba::order_entry::order_key launch_new_order(
// TODO, return OpResult
virtual OpResult launch_new_order(
const emumba::order_entry::new_order &order) = 0;
virtual bool update_order(
const emumba::order_entry::order_update &update) = 0;