Notional qty handling in spreader
This commit is contained in:
1 parent
f4930e8cd4
commit
9c3bb85674
4 files changed
+163
-38
No files matched your search
@@ -152,6 +152,12 @@ void SpreaderOrder::_on_time(const bts::mercury::TimerEvent &t_event) {
|
||||
}
|
||||
}
|
||||
void SpreaderOrder::_on_book_update(const BookUpdateEvent &book_event) {
|
||||
// TODO: rmeove
|
||||
LOG(OrderMan, Diag,
|
||||
"SpreaderOrder::_on_book_update skey: {} bid: {}/{} a: {}/{}",
|
||||
book_event.key(), book_event.book.bid().at(0).price(),
|
||||
book_event.book.bid().at(0).size(), book_event.book.ask().at(0).price(),
|
||||
book_event.book.ask().at(0).size());
|
||||
if (!_active)
|
||||
return;
|
||||
auto scoped = _order_services->log_scope(
|
||||
@@ -429,13 +435,18 @@ void SpreaderOrder::_compute_hedge_price_quantity(
|
||||
_bookkeeping_data().get_leg_data(filled_leg_index);
|
||||
SpreaderLegData &hedged_leg_data =
|
||||
_bookkeeping_data().get_leg_data(hedge_leg_index);
|
||||
if (_bookkeeping_data().n_legs() == 2)
|
||||
if (_bookkeeping_data().n_legs() == 2) {
|
||||
SpreaderUtils::compute_hedge_price_size_2_legs(
|
||||
_bookkeeping_data().effective_price(),
|
||||
filled_leg_data.leg_property_data(), fill_price, fill_quantity,
|
||||
hedged_leg_data.leg_property_data(), computed_hedge_price,
|
||||
hedge_quantity);
|
||||
else {
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderOrder::_compute_hedge_price_quantity effective_price: {} "
|
||||
"fill_price: {} fill_qty: hedge_price: {} hedge_qty: {}",
|
||||
_bookkeeping_data().effective_price(), fill_price, fill_quantity,
|
||||
computed_hedge_price, hedge_quantity);
|
||||
} else {
|
||||
NLegsHedgeRecompData &recomp_data =
|
||||
_bookkeeping_data().hedge_recomp_data();
|
||||
if (recomp_data.activated && recomp_data.last_call_succ()) {
|
||||
|
||||
@@ -9,9 +9,12 @@
|
||||
#include "lib/orderman/events/BookUpdateEvent.h"
|
||||
#include "lib/orderman/events/OrderStateUpdateEvent.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/SpreaderUtils.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include <bits/stdint-uintn.h>
|
||||
#include <cmath>
|
||||
#include <utility>
|
||||
|
||||
namespace bts::orderman {
|
||||
@@ -34,29 +37,70 @@ bool has_crypto_leg(const std::vector<LegPropertyData> &legs) {
|
||||
}
|
||||
|
||||
bool has_notional_leg(const std::vector<LegPropertyData> &legs) {
|
||||
for (const auto &l : legs) {
|
||||
return l.is_notional();
|
||||
return has_crypto_leg(legs);
|
||||
// TODO
|
||||
// for (const auto &l : legs) {
|
||||
// return l.is_notional();
|
||||
// }
|
||||
// return false;
|
||||
}
|
||||
|
||||
struct QtyTickInfo {
|
||||
public:
|
||||
const double tick_base;
|
||||
const double tick_unit;
|
||||
const double tick_sz;
|
||||
const double tick_mult;
|
||||
QtyTickInfo(double tick_base_, double tick_unit_)
|
||||
: tick_base(tick_base_), tick_unit(tick_unit_),
|
||||
tick_sz(tick_unit_ / tick_base_), tick_mult(tick_base_ / tick_unit_) {}
|
||||
};
|
||||
|
||||
const QtyTickInfo &get_qty_tick_info(
|
||||
const std::map<bts::SecurityKey, QtyTickInfo> &qty_info_map,
|
||||
SecurityKey skey,
|
||||
const QtyTickInfo &def) {
|
||||
auto it = qty_info_map.find(skey);
|
||||
if (it != qty_info_map.end()) {
|
||||
return def;
|
||||
}
|
||||
return false;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
struct SpreaderFractionalQtyHelper ::PImpl {
|
||||
Book op_book = {};
|
||||
OrderStateUpdateEvent op_ord = {};
|
||||
const bool passthrough = false;
|
||||
const double in_qty_mult;
|
||||
//const double in_qty_mult;
|
||||
const bool is_notional;
|
||||
const QtyTickInfo def_qty_tick_info = QtyTickInfo(10000.0, 1.0);
|
||||
std::map<bts::SecurityKey, QtyTickInfo> qty_info_map = {};
|
||||
|
||||
PImpl(const std::vector<LegPropertyData> &lpd_vec)
|
||||
: passthrough(has_crypto_leg(lpd_vec) ? false : true),
|
||||
in_qty_mult(
|
||||
has_crypto_leg(lpd_vec)
|
||||
? static_cast<double>(
|
||||
config::get<bts::config::Spreader>().qty_multiplier())
|
||||
: 1.0),
|
||||
// in_qty_mult(
|
||||
// has_crypto_leg(lpd_vec)
|
||||
// ? static_cast<double>(
|
||||
// config::get<bts::config::Spreader>().qty_multiplier())
|
||||
// : 1.0),
|
||||
is_notional(has_notional_leg(lpd_vec)) {
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderFractionalQtyHelper::cons, in_qty_mult: {} is_notional: {}",
|
||||
in_qty_mult, is_notional);
|
||||
// LOG(OrderMan, Info,
|
||||
// "SpreaderFractionalQtyHelper::cons, in_qty_mult: {} is_notional: {}",
|
||||
// in_qty_mult, is_notional);
|
||||
auto i = 0;
|
||||
for (const auto &l : lpd_vec) {
|
||||
const auto &qiv = l.security().contract_spec().quantity_infos();
|
||||
QtyTickInfo qi =
|
||||
qiv.empty()
|
||||
? QtyTickInfo(100.0, 1)
|
||||
: QtyTickInfo(qiv.at(0).tick_base(), qiv.at(0).tick_unit());
|
||||
bts::SecurityKey skey(l.security_key());
|
||||
qty_info_map.insert({skey, qi});
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderFractionalQtyHelper::cons leg: {} sec: {}", i,
|
||||
utils::pb::to_string(l.security()));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -65,8 +109,12 @@ std::vector<LegPropertyData> SpreaderFractionalQtyHelper::get_transformed_legs(
|
||||
std::vector<LegPropertyData> rv = {};
|
||||
for (const auto &l : legs) {
|
||||
bts::api::ContractLeg leg_adj = l.leg_contract();
|
||||
leg_adj.set_quantity(l.size_multiplier() * me->in_qty_mult);
|
||||
double qty_tick_size = 1.0 / me->in_qty_mult;
|
||||
const auto &q_tinfo = get_qty_tick_info(
|
||||
me->qty_info_map, bts::SecurityKey(l.leg_security_key()),
|
||||
me->def_qty_tick_info);
|
||||
double qm = q_tinfo.tick_mult;
|
||||
leg_adj.set_quantity(l.size_multiplier() * qm);
|
||||
double qty_tick_size = q_tinfo.tick_sz;
|
||||
LegPropertyData lpd_adj(
|
||||
l.leg_index(), leg_adj, l.leg_params(), l.trading_side(),
|
||||
bts::api::SyntheticContract::PriceCalc::
|
||||
@@ -83,13 +131,23 @@ SpreaderFractionalQtyHelper::SpreaderFractionalQtyHelper(
|
||||
|
||||
const Book &SpreaderFractionalQtyHelper::in_transform_book(
|
||||
const Book &bk) const {
|
||||
if (me->passthrough)
|
||||
if (me->passthrough) {
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderFractionalQtyHelper::in_transform_book passthrough");
|
||||
return bk;
|
||||
if (me->is_notional) {
|
||||
//convert to notional
|
||||
}
|
||||
me->op_book = bk;
|
||||
double qm = me->in_qty_mult;
|
||||
if (me->is_notional) {
|
||||
SpreaderUtils::compute_notional_book(bk, me->op_book);
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderFractionalQtyHelper::in_transform_book sec: {} bk: {} "
|
||||
"notional: {}",
|
||||
bk.security_key().value(), bk, me->op_book);
|
||||
} else {
|
||||
me->op_book = bk;
|
||||
}
|
||||
const auto &q_tinfo = get_qty_tick_info(
|
||||
me->qty_info_map, bk.security_key(), me->def_qty_tick_info);
|
||||
double qm = q_tinfo.tick_mult;
|
||||
for (auto &ps : me->op_book.bid()) {
|
||||
ps = bts::md::PriceSize(ps.int_price(), ps.size() * qm);
|
||||
}
|
||||
@@ -97,6 +155,9 @@ const Book &SpreaderFractionalQtyHelper::in_transform_book(
|
||||
ps = bts::md::PriceSize(ps.int_price(), ps.size() * qm);
|
||||
}
|
||||
// mult by leg_mult
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderFractionalQtyHelper::in_transform_book bk: {} output: {}", bk,
|
||||
me->op_book);
|
||||
return me->op_book;
|
||||
}
|
||||
|
||||
@@ -105,17 +166,29 @@ SpreaderFractionalQtyHelper::in_transform_order_event(
|
||||
SecurityKey sec_key, const OrderStateUpdateEvent &ev) const {
|
||||
auto log_ev = [](auto sec_key, const auto &ev, const auto &rv_ev) {
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderFractionalQtyHelper::in_transform_order_event sec_key: {} "
|
||||
"ev.qty: {} ev.filled_qty: {} rv.qty: {} rv.filled_qty: {}",
|
||||
sec_key.value(), ev.quantity, ev.filled_quantity, rv_ev.quantity,
|
||||
rv_ev.filled_quantity);
|
||||
"SpreaderFractionalQtyHelper::in_transform_order_event sec_key: "
|
||||
"{} "
|
||||
"ev.qty: {} ev.filled_qty: ev.avg_price: {} rv.qty: {} "
|
||||
"rv.filled_qty: {}",
|
||||
sec_key.value(), ev.quantity, ev.filled_quantity, ev.avg_price,
|
||||
rv_ev.quantity, rv_ev.filled_quantity);
|
||||
};
|
||||
if (me->passthrough) {
|
||||
log_ev(sec_key, ev, ev);
|
||||
return ev;
|
||||
}
|
||||
me->op_ord = ev;
|
||||
double qmult = me->in_qty_mult;
|
||||
// if notional, then order qtys needs to be converted to notional, since spreader sees books
|
||||
// in notionals and fire orders in notionals
|
||||
me->op_ord.quantity =
|
||||
SpreaderUtils::to_notional(Price(ev.price), ev.quantity);
|
||||
me->op_ord.filled_quantity =
|
||||
SpreaderUtils::to_notional(Price(ev.avg_price), ev.filled_quantity);
|
||||
me->op_ord.filled_at_attach_time_quantity = os::nan();
|
||||
// factor mult
|
||||
const auto &q_tinfo =
|
||||
get_qty_tick_info(me->qty_info_map, sec_key, me->def_qty_tick_info);
|
||||
double qmult = q_tinfo.tick_mult;
|
||||
me->op_ord.quantity *= qmult;
|
||||
me->op_ord.filled_quantity *= qmult;
|
||||
me->op_ord.filled_at_attach_time_quantity *= qmult;
|
||||
@@ -132,18 +205,33 @@ Quantity SpreaderFractionalQtyHelper::in_transform_spreader_qty(
|
||||
}
|
||||
|
||||
Quantity SpreaderFractionalQtyHelper::out_transform_leg_order_qty(
|
||||
SecurityKey, Price, Quantity qty) const {
|
||||
SecurityKey skey, Price price, Quantity qty) const {
|
||||
auto qty_init = qty;
|
||||
auto tr = [&]() {
|
||||
if (me->passthrough)
|
||||
return qty;
|
||||
double qm = me->in_qty_mult;
|
||||
return Quantity::create_new(qty.full_value() / qm);
|
||||
if (me->is_notional) {
|
||||
qty = SpreaderUtils::from_notional(price, qty);
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderFractionalQtyHelper::out_transform_leg_order_qty "
|
||||
"notional -> physical {} -> {}",
|
||||
qty_init, qty);
|
||||
}
|
||||
const auto &q_tinfo =
|
||||
get_qty_tick_info(me->qty_info_map, skey, me->def_qty_tick_info);
|
||||
double tick_sz = q_tinfo.tick_sz;
|
||||
double qm = q_tinfo.tick_mult;
|
||||
double qty_out = qty.full_value() / qm;
|
||||
// rouning it to tick size
|
||||
qty_out = std::floor(qty_out / tick_sz) * tick_sz;
|
||||
return Quantity::create_new(qty_out);
|
||||
};
|
||||
auto rv_qty = tr();
|
||||
LOG(OrderMan, Info,
|
||||
"SpreaderFractionalQtyHelper::out_transform_leg_order_qty qty: {} "
|
||||
"SpreaderFractionalQtyHelper::out_transform_leg_order_qty price: {} "
|
||||
"qty: {} "
|
||||
"rv_qty: {}",
|
||||
qty.full_value(), rv_qty.full_value());
|
||||
price, qty_init.full_value(), rv_qty.full_value());
|
||||
return rv_qty;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
//#include "SpreaderLegData.h"
|
||||
//#include "lib/domain_model/common.h"
|
||||
//#include "lib/orderman/orderman_utils/orderman_common.h"
|
||||
#include "lib/domain_model/SecurityKey.h"
|
||||
#include "lib/orderman/orders/spreader_orders/spreader/LegPropertyData.h"
|
||||
|
||||
namespace bts::api::orderman {
|
||||
@@ -33,6 +34,8 @@ class SpreaderFractionalQtyHelper {
|
||||
SecurityKey key, Price price, Quantity qty) const;
|
||||
// the state reported ot he rest of the system also has to be converted
|
||||
void out_transform_order_state(bts::api::orderman::OrderState &) const;
|
||||
// struct QtyTickInfo;
|
||||
// const QtyTickInfo &_get_tickinfo(SecurityKey skey);
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "lib/jetfix/codec/FieldDefinitions.h"
|
||||
#include "lib/md/IntPrice.h"
|
||||
#include "lib/md/PriceSize.h"
|
||||
#include "lib/md/Shm/MDStructure.h"
|
||||
#include "lib/orderman/OrderBuilder.h"
|
||||
#include "lib/orderman/orderman_utils/Book.h"
|
||||
#include "lib/orderman/orderman_utils/fix_order_codec.h"
|
||||
@@ -1145,25 +1146,47 @@ class SpreaderUtils {
|
||||
// notional ///////////////////////////////////////
|
||||
//////////////////////////////////////////////////
|
||||
// for crypto
|
||||
static md::Size to_notional(Price price, md::Size size) {
|
||||
return price.value() * size;
|
||||
static md::Size to_notional(Price price, md::Size physical_size) {
|
||||
// qn = pr * qp
|
||||
double rv = to_notional(price, static_cast<double>(physical_size));
|
||||
return static_cast<md::Size>(rv);
|
||||
}
|
||||
|
||||
static md::Size from_notional(Price price, md::Size size) {
|
||||
return price.value() / size;
|
||||
static double to_notional(Price price, double physical_size) {
|
||||
// qn = pr * qp
|
||||
double notional_size = price.value() * physical_size;
|
||||
return notional_size;
|
||||
}
|
||||
|
||||
static md::Size from_notional(Price price, md::Size notional_sz) {
|
||||
md::Size physical_size = notional_sz / price.value();
|
||||
return physical_size;
|
||||
}
|
||||
|
||||
static Quantity from_notional(Price price, Quantity notional_qty) {
|
||||
double physical_qty = notional_qty.full_value() / price.value();
|
||||
return Quantity::create_new(physical_qty);
|
||||
}
|
||||
|
||||
void static compute_notional_book(
|
||||
const BookSide &,
|
||||
const Book &source_book, Book &target_book) {
|
||||
target_book = source_book;
|
||||
// TODO, 5
|
||||
compute_notional_book(source_book.bid(), 0, 5, target_book.bid());
|
||||
compute_notional_book(source_book.ask(), 0, 5, target_book.ask());
|
||||
}
|
||||
|
||||
void static compute_notional_book(
|
||||
const BookSide &source_book_side,
|
||||
size_t start_index,
|
||||
size_t n_levels,
|
||||
BookSide &target_book_side) {
|
||||
empty_out_book(target_book_side, start_index, n_levels);
|
||||
for (size_t i = start_index; i < n_levels; i++) {
|
||||
md::PriceSize &ps_target = target_book_side.at(i);
|
||||
const md::PriceSize &ps_source = target_book_side.at(i);
|
||||
const md::PriceSize &ps_source = source_book_side.at(i);
|
||||
md::Size notional_sz =
|
||||
to_notional(Price(ps_source.price()), ps_source.size());
|
||||
md::PriceSize &ps_target = target_book_side.at(i);
|
||||
ps_target = md::PriceSize(ps_source.int_price(), notional_sz);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user