541 lines
19 KiB
C++
541 lines
19 KiB
C++
#include "SpreaderOrder.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
#include "server/sparkd/spreader/SpreaderOrderUpdateMessage.h"
|
|
#include "server/sparkd/orderman/spreader_orders/SpreaderOrderState.h"
|
|
#include "server/sparkd/securities/SecurityDB.h"
|
|
#include "server/sparkd/spreader/SpreaderUtils.h"
|
|
#include "server/sparkd/spreader/LegPropertyData.h"
|
|
#include "server/sparkd/spreader/SpreaderLegData.h"
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
struct SpreaderOrder::PImpl
|
|
{
|
|
PImpl(bool resurrected_arg,
|
|
const api::orderman::CreateOrderRequest& create_order_request_arg,
|
|
securities::ISecurityDB& db_arg,
|
|
OrderKey orderkey_arg,
|
|
api::orderman::OrderStatus status,
|
|
Price price,
|
|
Quantity quantity,
|
|
std::string const &reject_msg)
|
|
:
|
|
resurrected (resurrected_arg),
|
|
create_order_request (create_order_request_arg),
|
|
db (db_arg),
|
|
order_key (orderkey_arg),
|
|
status(status),
|
|
price(price),
|
|
quantity(quantity),
|
|
reject_msg(reject_msg)
|
|
{}
|
|
const bool resurrected;
|
|
const api::orderman::CreateOrderRequest create_order_request;
|
|
securities::ISecurityDB& db;
|
|
const OrderKey order_key;
|
|
api::orderman::OrderStatus status;
|
|
Price price;
|
|
Quantity quantity;
|
|
std::string reject_msg;
|
|
};
|
|
|
|
SpreaderOrder::SpreaderOrder(securities::ISecurityDB &db,
|
|
NativeOrderFactory &f,
|
|
api::orderman::OrderState const&os)
|
|
:
|
|
SpreaderOrderBase(db, f, os),
|
|
me(new PImpl(true, api::orderman::CreateOrderRequest(), db, OrderKey (0), os.order_status(),
|
|
Price(os.spreader().price()), Quantity(os.spreader().quantity()), os.reject_msg())),
|
|
_state (State::START)
|
|
{
|
|
LOG(OrderMan, Diag, "recovered SpreaderOrder from state. state: {}", utils::pb::to_string(os));
|
|
}
|
|
|
|
SpreaderOrder::SpreaderOrder(securities::ISecurityDB& db,
|
|
NativeOrderFactory& f,
|
|
OrderKey k,
|
|
api::orderman::CreateOrderRequest const& cor)
|
|
:
|
|
SpreaderOrderBase(db, f, k, cor),
|
|
me(new PImpl(false, cor, db, k, api::orderman::OS_UNACKNOWLEDGED, Price(cor.spreader().price()),
|
|
Quantity(cor.spreader().quantity()), std::string("")))
|
|
{
|
|
LOG(OrderMan, Diag, "creating new SpreaderOrder. request: {}", utils::pb::to_string(cor));
|
|
bts_assert(cor.has_spreader());
|
|
bts_assert(cor.spreader().security().has_synthetic());
|
|
_transform_to_START ();
|
|
}
|
|
|
|
SpreaderOrder::~SpreaderOrder () {}
|
|
|
|
void SpreaderOrder::init()
|
|
{
|
|
if (me->resurrected) return;
|
|
LOG(OrderMan, Diag, "SpreaderOrder.Init");
|
|
if (_state == State::START)
|
|
{
|
|
_init_state();
|
|
_transform_to_WAITING_FOR_FIRST_CONNECTION();
|
|
}
|
|
else if (_state == State::WAITING_FOR_FIRST_CONNECT)
|
|
bts_assert(false);
|
|
else if (_state == State::WORKING)
|
|
bts_assert(false);
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
bts_assert(false);
|
|
else if (_state == State::END)
|
|
bts_assert(false);
|
|
}
|
|
|
|
void SpreaderOrder::on_connect()
|
|
{
|
|
if (me->resurrected) return;
|
|
LOG(OrderMan, Diag, "Spreaderorder.on_connect");
|
|
if (_state == State::START)
|
|
bts_assert(false);
|
|
else if (_state == State::WAITING_FOR_FIRST_CONNECT)
|
|
_transform_to_WORKING();
|
|
else if (_state == State::WORKING)
|
|
bts_assert(false);
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
bts_assert(false);
|
|
else if (_state == State::END)
|
|
{
|
|
// this is ok since once we have disconnected, we
|
|
// end up in END, and ignore subsequent re-connections
|
|
}
|
|
}
|
|
|
|
void SpreaderOrder::on_disconnect()
|
|
{
|
|
if (me->resurrected) return;
|
|
LOG(OrderMan,Diag,"on_disconnect");
|
|
if (_state == State::START)
|
|
// getting a disconnect before the first connect is not allowed
|
|
bts_assert(false);
|
|
else if (_state == WAITING_FOR_FIRST_CONNECT)
|
|
bts_assert(false);
|
|
if (_state == State::WORKING)
|
|
_transform_to_END();
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
_transform_to_END();
|
|
else if (_state == State::END)
|
|
{
|
|
// it means a connection was established in the end
|
|
// state which is dosconnected now
|
|
}
|
|
}
|
|
|
|
void SpreaderOrder::on_update(const api::orderman::UpdateSpreaderOrder& u)
|
|
{
|
|
LOG(OrderMan,Diag,"on_update SpreaderOrder. order: {}",utils::pb::to_string(u));
|
|
if (me->resurrected) return;
|
|
bool success = false;
|
|
if (_state == State::START)
|
|
bts_assert(false);
|
|
else if (_state == State::WAITING_FOR_FIRST_CONNECT)
|
|
{ }
|
|
else if (_state == State::WORKING)
|
|
{
|
|
// check if all quoters can accept the change (a price, qty
|
|
// update does not effect the hedgers)
|
|
bool accept_changes = true;
|
|
for (size_t q_idx = 0; q_idx < _bookkeeping_data->count(); q_idx++)
|
|
{
|
|
SpreaderLegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
if (leg_data.leg_property_data.is_active_quoting())
|
|
{
|
|
accept_changes = accept_changes && leg_data.quoter().accept_modifications();
|
|
if (!accept_changes)
|
|
break;
|
|
}
|
|
}
|
|
if (accept_changes)
|
|
{
|
|
me->price = Price(u.price());
|
|
me->quantity = Quantity(u.quantity ());
|
|
for (size_t q_idx = 0; q_idx < _bookkeeping_data->count(); q_idx++)
|
|
{
|
|
SpreaderLegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
if (leg_data.leg_property_data.is_active_quoting())
|
|
leg_data.quoter().update_order_price_quantity (Price(u.price()), Quantity(u.quantity()));
|
|
}
|
|
success = true;
|
|
}
|
|
}
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
{ }
|
|
else if (_state == State::END)
|
|
{ }
|
|
if (!success)
|
|
me->status = api::orderman::OrderStatus::OS_REJECTED_BY_SPARK;
|
|
report_state ();
|
|
}
|
|
|
|
void SpreaderOrder::on_cancel(const api::orderman::CancelSpreaderOrder& c)
|
|
{
|
|
LOG(OrderMan,Diag,"on_cancel SpreaderOrder. order: {}",utils::pb::to_string(c));
|
|
if (me->resurrected) return;
|
|
if (_state == State::START)
|
|
_transform_to_END();
|
|
else if (_state == State::WAITING_FOR_FIRST_CONNECT)
|
|
_transform_to_END();
|
|
else if (_state == State::WORKING)
|
|
_transform_to_WAITING_FOR_CANCEL_ALL();
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
{
|
|
// already cancelled
|
|
// TODO send a cancel rejection message out, saying its already canceleld
|
|
}
|
|
else if (_state == State::END)
|
|
{
|
|
}
|
|
}
|
|
|
|
void SpreaderOrder::on_book(TickToTradeCorID cid, Book const& book)
|
|
{
|
|
if (me->resurrected) return;
|
|
LOG(OrderMan, Diag, "on_book");
|
|
LOG(Latency, Info, "on_book {} {}", cid, utils::TimeStamp::now().to_uint64());
|
|
if (_state == State::WORKING)
|
|
{
|
|
// this is a key->index lookup
|
|
SpreaderLegData& md_leg = _bookkeeping_data->get_leg_data(book.security_key());
|
|
// updating md state (this is the only place where its done)
|
|
md_leg.set_book(book);
|
|
// sending md to all quoters
|
|
for (size_t q_idx = 0; q_idx < _bookkeeping_data->count(); q_idx++)
|
|
{
|
|
SpreaderLegData& d = _bookkeeping_data->get_leg_data(q_idx);
|
|
if (d.leg_property_data.is_active_quoting())
|
|
d.quoter().update_md(md_leg.leg_property_data.leg_index);
|
|
}
|
|
// send MD to hedgers hedging this security only
|
|
for (size_t h_idx = 0; h_idx < md_leg.n_hedgers(); h_idx++)
|
|
{
|
|
SpreaderHedgerData& h = md_leg.get_hedger(h_idx);
|
|
h.hedger().update_md();
|
|
}
|
|
}
|
|
else if (_state == State::START)
|
|
bts_assert(false);
|
|
else if (_state == State::WAITING_FOR_FIRST_CONNECT)
|
|
// no md updates until connection is established
|
|
bts_assert(false);
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
{
|
|
// no need to send md, quoters and hedgers
|
|
// done need md when cxl'ing
|
|
}
|
|
else if (_state == State::END)
|
|
{
|
|
}
|
|
}
|
|
|
|
void SpreaderOrder::on_time(TimeStamp const&)
|
|
{
|
|
// TODO, add time functions to quoter and hedger
|
|
// and call them
|
|
}
|
|
|
|
void SpreaderOrder::on_child_update(OrderKey order_key,
|
|
api::orderman::OrderStatus status,
|
|
Quantity filled_quantity,
|
|
Price average_price,
|
|
std::string const *error_msg)
|
|
{
|
|
if (me->resurrected) return;
|
|
using namespace bts::spreader;
|
|
static std::string empty;
|
|
LOG(OrderMan,Diag, "SpreaderOrder.on_child_update "
|
|
"k: {} status: {} filled_qty: {} avg_price: {} reject_msg: {}",
|
|
order_key.value(), status, filled_quantity.value(), average_price.value(), error_msg == nullptr ? empty : *error_msg);
|
|
if (_state == State::START)
|
|
bts_assert(false);
|
|
else if (_state == State::WAITING_FOR_FIRST_CONNECT)
|
|
bts_assert(false);
|
|
else if (_state == State::WORKING)
|
|
{
|
|
SpreaderLegData* leg_data_ptr = _bookkeeping_data->find_leg_from_quoter_orderkey(order_key);
|
|
if (leg_data_ptr != nullptr)
|
|
{
|
|
SpreaderLegData& leg_data = *(leg_data_ptr);
|
|
leg_data.order_update_message().update(status, filled_quantity, average_price, error_msg);
|
|
if (leg_data.order_update_message().has_error())
|
|
_handle_order_error();
|
|
else
|
|
_handle_quoter_order_update(leg_data);
|
|
}
|
|
else
|
|
{
|
|
// should be a hedge order then
|
|
SpreaderHedgerData* hedger_data = _bookkeeping_data->find_hedger_from_orderkey(order_key);
|
|
if (hedger_data != nullptr)
|
|
{
|
|
/// TODO
|
|
bts_assert (false);
|
|
// hedger_data->update_fill(filled_quantity);
|
|
// hedger_data->hedger.update_order_state(update_message);
|
|
}
|
|
}
|
|
if (_all_quoter_orders_in_terminal_state() && _all_hedger_orders_in_terminal_state())
|
|
_transform_to_END();
|
|
}
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
{
|
|
// all it has to do
|
|
SpreaderLegData* q_leg_data_ptr = _bookkeeping_data->find_leg_from_quoter_orderkey(order_key);
|
|
if (q_leg_data_ptr != nullptr)
|
|
{
|
|
SpreaderLegData& q_leg_data = *(q_leg_data_ptr);
|
|
q_leg_data.order_update_message().update (status, filled_quantity, average_price, error_msg);
|
|
q_leg_data.quoter().update_order_status(q_leg_data.order_update_message());
|
|
}
|
|
else
|
|
{
|
|
SpreaderHedgerData* hedger_data = _bookkeeping_data->find_hedger_from_orderkey(order_key);
|
|
if (hedger_data != nullptr)
|
|
{
|
|
// SpreaderOrderUpdateMessage update_message(orderkey, status, filled_quantity, average_price, hedger_data->filled_quantity, hedger_data->avergage_filled_price);
|
|
// hedger_data->update_fill(filled_quantity);
|
|
// hedger_data->hedger.update_order_state(update_message);
|
|
}
|
|
}
|
|
if (_all_quoter_orders_in_terminal_state() && _all_hedger_orders_in_terminal_state())
|
|
_transform_to_END();
|
|
}
|
|
else if (_state == State::END)
|
|
{
|
|
// it could have reached this state through an
|
|
// error in which case, as far as the spreader
|
|
// is concerned, no updates will be sent
|
|
}
|
|
}
|
|
|
|
void SpreaderOrder::on_external_error(std::string const&reason)
|
|
{
|
|
if (me->resurrected) return;
|
|
LOG(OrderMan,Warn, "on_external_error SpreaderOrder. msg: {}", reason);
|
|
if (_state == State::START)
|
|
_transform_to_END();
|
|
else if (_state == State::WAITING_FOR_FIRST_CONNECT)
|
|
_transform_to_END();
|
|
else if (_state == State::WORKING)
|
|
_transform_to_WAITING_FOR_CANCEL_ALL();
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
{
|
|
// stay here and wait for cxl's
|
|
}
|
|
else if (_state == State::END)
|
|
{
|
|
// nothing to do
|
|
}
|
|
}
|
|
|
|
const orderman::Book& SpreaderOrder::get_book(size_t leg_index) const
|
|
{
|
|
return _bookkeeping_data->get_leg_data(leg_index).book();
|
|
}
|
|
const LegPropertyData& SpreaderOrder::get_leg_data(size_t leg_index) const
|
|
{
|
|
return _bookkeeping_data->get_leg_data(leg_index).leg_property_data;
|
|
}
|
|
|
|
void SpreaderOrder::_init_state()
|
|
{
|
|
std::vector<SpreaderLegData> leg_data_vector;
|
|
//creating the basic data
|
|
ISpreaderManager& me_ref = *this;
|
|
const bts::api::orderman::CreateSpreaderOrder& cor = me->create_order_request.spreader();
|
|
spark::ExecutionStrategy execution_strategy = cor.execution_strategy();
|
|
const bts::api::Security& sec = cor.security();
|
|
size_t n_spreader_legs = sec.synthetic().legs().size();
|
|
size_t leg_idx = 0;
|
|
for (auto it = sec.synthetic().legs().begin(); it != sec.synthetic().legs().end(); ++it)
|
|
{
|
|
uint32_t sec_key = it->security().id();
|
|
auto st_it = execution_strategy.leg_params_map().find(sec_key);
|
|
bts_assert(st_it != execution_strategy.leg_params_map().end());
|
|
const bts::api::ContractLeg& leg = *it;
|
|
api::Side leg_order_side = SpreaderUtils::get_leg_side(cor.side(), leg);
|
|
LegPropertyData leg_property_data(leg_idx, leg, st_it->second, leg_order_side);
|
|
// order
|
|
bts::SecurityKey leg_key = bts::SecurityKey (leg_property_data.security_key());
|
|
api::Security const * db_sec = me->db.resolve (leg_key);
|
|
bts::orderman::SpreaderOrderBase::ChildFactory child_factory = get_child_factory(*db_sec, leg_property_data.trading_side, api::orderman::ChildOrderKind::COK_SPREADER_QUOTING);
|
|
bts::orderman::SpreaderOrderBase::ChildOrder child_order = child_factory();
|
|
SingleLegQuoter leg_quoter(me_ref, leg_property_data, n_spreader_legs, Price(cor.price()), Quantity(cor.quantity()), child_order);
|
|
SpreaderLegData leg_data(leg_property_data, leg_quoter, child_order.order_key());
|
|
leg_data_vector.push_back(leg_data);
|
|
leg_idx++;
|
|
}
|
|
bts_assert(leg_data_vector.size() > 0);
|
|
_bookkeeping_data = std::make_unique<SpreaderBookkeepingData>(sec, leg_data_vector);
|
|
}
|
|
|
|
void SpreaderOrder::_transform_to_START()
|
|
{
|
|
_state = State::START;
|
|
me->status = api::orderman::OrderStatus::OS_PENDING_NEW;
|
|
report_state ();
|
|
}
|
|
|
|
void SpreaderOrder::_transform_to_WAITING_FOR_FIRST_CONNECTION()
|
|
{
|
|
bts_assert(_state == State::START);
|
|
_state = State::WAITING_FOR_FIRST_CONNECT;
|
|
me->status = api::orderman::OrderStatus::OS_PENDING_NEW;
|
|
report_state ();
|
|
}
|
|
void SpreaderOrder::_transform_to_WORKING()
|
|
{
|
|
bts_assert(_state == State::WAITING_FOR_FIRST_CONNECT);
|
|
_state = State::WORKING;
|
|
// subsribe to md
|
|
for (size_t index = 0; index < _bookkeeping_data->count(); index++)
|
|
{
|
|
SpreaderLegData& leg_data = _bookkeeping_data->get_leg_data(index);
|
|
bts::SecurityKey leg_key = bts::SecurityKey (leg_data.leg_property_data.security_key());
|
|
api::Security const * db_sec = me->db.resolve (leg_key);
|
|
subscribe_to_md (*db_sec);
|
|
}
|
|
for (size_t index = 0; index < _bookkeeping_data->count(); index++)
|
|
{
|
|
SpreaderLegData& leg_data = _bookkeeping_data->get_leg_data(index);
|
|
if (leg_data.leg_property_data.is_active_quoting())
|
|
leg_data.quoter().init();
|
|
}
|
|
me->status = api::orderman::OrderStatus::OS_NEW;
|
|
report_state();
|
|
}
|
|
void SpreaderOrder::_transform_to_WAITING_FOR_CANCEL_ALL()
|
|
{
|
|
bts_assert(_state == State::WORKING);
|
|
_state = State::WAITING_FOR_CANCEL_ALL;
|
|
_cancel_orders(true, true);
|
|
me->status = api::orderman::OrderStatus::OS_PENDING_CANCEL;
|
|
report_state();
|
|
}
|
|
void SpreaderOrder::_transform_to_END()
|
|
{
|
|
bts_assert(_state == State::WORKING || _state == State::WAITING_FOR_CANCEL_ALL);
|
|
_state = State::END;
|
|
me->status = api::orderman::OrderStatus::OS_CANCELED; // TODO, this should is not just cancelled
|
|
report_state();
|
|
}
|
|
void SpreaderOrder::_handle_order_error()
|
|
{
|
|
_transform_to_WAITING_FOR_CANCEL_ALL();
|
|
}
|
|
void SpreaderOrder::_handle_quoter_order_update(SpreaderLegData& leg_data)
|
|
{
|
|
const SpreaderOrderUpdateMessage& update_message = leg_data.order_update_message();
|
|
// quoter needs to update, if there is a fill, the quoter
|
|
// will update its order quantity
|
|
leg_data.quoter().update_order_status(update_message);
|
|
// if there are fill, we need to
|
|
// i. update leg_data's price and quantity
|
|
// ii. tell other quotes of the fill
|
|
// iii send hedges
|
|
if (update_message.has_fills())
|
|
{
|
|
for (size_t idx = 0; idx < _bookkeeping_data->count(); idx++)
|
|
if (idx != leg_data.leg_property_data.leg_index)
|
|
{
|
|
if (leg_data.leg_property_data.is_active_quoting())
|
|
leg_data.quoter().update_other_leg_fills(leg_data.leg_property_data.leg_index, update_message.last_update_filled_quantity());
|
|
//_send_hedge(leg_data.leg_property_data.leg_index, this_fill_price, this_fill_quantity, idx);// TODO
|
|
}
|
|
}
|
|
}
|
|
void SpreaderOrder::_cancel_orders(bool cancel_quotes, bool cancel_hedges)
|
|
{
|
|
for (size_t q_idx = 0; q_idx < _bookkeeping_data->count(); q_idx++)
|
|
{
|
|
SpreaderLegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
if (cancel_quotes)
|
|
{
|
|
if (leg_data.leg_property_data.is_active_quoting())
|
|
leg_data.quoter().cancel_order();
|
|
}
|
|
if (cancel_hedges)
|
|
for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++)
|
|
{
|
|
SpreaderHedgerData& hedger_data = leg_data.get_hedger(h_idx);
|
|
hedger_data.hedger().cancel_hedge();
|
|
}
|
|
}
|
|
}
|
|
bool SpreaderOrder::_all_quoter_orders_in_terminal_state()
|
|
{
|
|
for (size_t q_idx = 0; q_idx < _bookkeeping_data->count(); q_idx++)
|
|
{
|
|
SpreaderLegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
if (leg_data.leg_property_data.is_active_quoting())
|
|
if (!leg_data.quoter().is_terminal_state())
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
// same as _all_quoter_orders_in_terminal_state, for hedgers
|
|
bool SpreaderOrder::_all_hedger_orders_in_terminal_state()
|
|
{
|
|
for (size_t q_idx = 0; q_idx < _bookkeeping_data->count(); q_idx++)
|
|
{
|
|
SpreaderLegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++)
|
|
{
|
|
SpreaderHedgerData& hedger_data = leg_data.get_hedger(h_idx);
|
|
if (!hedger_data.hedger().is_terminal_state())
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
void SpreaderOrder::_send_hedge(size_t, Price, Quantity, size_t)
|
|
{
|
|
}
|
|
void SpreaderOrder::_compute_hedge_price_quantity(size_t,
|
|
Price, Quantity,
|
|
size_t,
|
|
Price&, Quantity&)
|
|
{
|
|
}
|
|
|
|
void SpreaderOrder::report_state()
|
|
{
|
|
// report_state_change(SpreaderOrderState{
|
|
// me->status,
|
|
// me->price,
|
|
// me->quantity,
|
|
// me->reject_msg.empty() ? nullptr : &me->reject_msg
|
|
// });
|
|
}
|
|
|
|
bool SpreaderOrder::can_resolve_securties ()
|
|
{
|
|
const bts::api::orderman::CreateSpreaderOrder& cor = me->create_order_request.spreader();
|
|
const bts::api::Security& sec = cor.security();
|
|
for (auto it = sec.synthetic().legs().begin(); it != sec.synthetic().legs().end(); ++it)
|
|
{
|
|
const bts::api::ContractLeg& leg = *it;
|
|
bts::SecurityKey leg_key = bts::SecurityKey (leg.security().id());
|
|
api::Security const * db_sec = me->db.resolve (leg_key);
|
|
if (db_sec == nullptr)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/*******************************************************************************/
|
|
/******************Functions to Help Testing************************************/
|
|
|
|
SpreaderLegData& SpreaderOrder::test_only_func_leg_data (size_t index)
|
|
{
|
|
SpreaderLegData& leg_data = _bookkeeping_data->get_leg_data (index);
|
|
return leg_data;
|
|
}
|
|
|
|
}} |