483 lines
17 KiB
C++
483 lines
17 KiB
C++
#include "SpreaderOrder.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/utils/PBUtils.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/SpreaderOrderUpdateMessage.h"
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
|
|
struct SpreaderOrder::PImpl
|
|
{
|
|
PImpl(api::orderman::OrderStatus status,
|
|
Price price, Quantity quantity,
|
|
std::string const &reject_msg)
|
|
:status(status),
|
|
price(price),
|
|
quantity(quantity),
|
|
reject_msg(reject_msg)
|
|
{}
|
|
|
|
api::orderman::OrderStatus status;
|
|
Price price;
|
|
Quantity quantity;
|
|
std::string reject_msg;
|
|
};
|
|
|
|
SpreaderOrder::SpreaderOrder(securities::SecurityDB &db,
|
|
NativeOrderFactory &f,
|
|
api::orderman::OrderState const&os)
|
|
:
|
|
SpreaderOrderBase(db, f, os),
|
|
me(new PImpl(os.order_status(),
|
|
Price(os.spreader().price()),
|
|
Quantity(os.spreader().quantity()),
|
|
os.reject_msg()))
|
|
{
|
|
LOG(OrderMan, Diag, "recovered SpreaderOrder from state. state: {}",
|
|
utils::pb::to_string(os));
|
|
}
|
|
|
|
SpreaderOrder::SpreaderOrder(securities::SecurityDB& db,
|
|
NativeOrderFactory& f,
|
|
OrderKey k,
|
|
api::orderman::CreateOrderRequest const& cor)
|
|
:
|
|
SpreaderOrderBase(db, f, k, cor),
|
|
me(new PImpl(api::orderman::OS_UNACKNOWLEDGED,
|
|
Price(cor.spreader().price()),
|
|
Quantity(cor.spreader().quantity()),
|
|
std::string(""))),
|
|
_state(State::START)
|
|
{
|
|
LOG(OrderMan, Diag, "creating new SpreaderOrder. request: {}", utils::pb::to_string(cor));
|
|
bts_assert(cor.has_spreader());
|
|
// init is the first call, and is called only once
|
|
api::Security const * sec_ptr = db.resolve(SecurityKey(cor.spreader().security_key()));
|
|
bts_assert (sec_ptr != NULL);
|
|
const api::Security& sec = *sec_ptr;
|
|
bts_assert(sec.has_synthetic());
|
|
std::vector<SpreaderBookkeepingData::LegData> leg_data_vector;
|
|
size_t leg_idx = 0;
|
|
//creating the basic data
|
|
ISpreaderManager& me_ref = *this;
|
|
spark::ExecutionStrategy execution_strategy; // TODO, add it to the proto and get it from there
|
|
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.spreader().side(), leg);
|
|
LegPropertyData leg_property_data(leg_idx++, leg, st_it->second, leg_order_side);
|
|
SpreaderBookkeepingData::LegData leg_data(leg_property_data);
|
|
leg_data_vector.push_back(leg_data);
|
|
}
|
|
bts_assert(leg_data_vector.size() > 0);
|
|
_bookkeeping_data = std::make_unique<SpreaderBookkeepingData>(sec, leg_data_vector);
|
|
// now settings up the quoters
|
|
for (size_t index = 0; index < _bookkeeping_data->count(); index++)
|
|
{
|
|
OrderTemplate order_template;
|
|
SpreaderBookkeepingData::LegData& leg_data = _bookkeeping_data->get_leg_data(index);
|
|
bts::orderman::SpreaderOrderBase::ChildFactory child_factory = get_child_factory(leg_data.leg_property_data.security(), leg_data.leg_property_data.trading_side, api::orderman::ChildOrderKind::COK_SPREADER_QUOTING);
|
|
bts::orderman::SpreaderOrderBase::ChildOrder child_order = child_factory();
|
|
leg_data.order_key = child_order.order_key();
|
|
leg_data.quoter = new SingleLegQuoter(me_ref, order_template, index, Price(cor.spreader().price()), Quantity(cor.spreader().quantity()), child_order);
|
|
}
|
|
}
|
|
|
|
SpreaderOrder::~SpreaderOrder () {}
|
|
|
|
void SpreaderOrder::init()
|
|
{
|
|
LOG(OrderMan, Diag, "Init");
|
|
report_state();
|
|
if (_state == State::START)
|
|
_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()
|
|
{
|
|
LOG(OrderMan, Diag, "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()
|
|
{
|
|
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));
|
|
// TODO : send status updates out
|
|
if (_state == State::START)
|
|
{
|
|
bts_assert(false);
|
|
}
|
|
else if (_state == State::WAITING_FOR_FIRST_CONNECT)
|
|
{
|
|
// TODO send a rejection out
|
|
}
|
|
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++)
|
|
{
|
|
SpreaderBookkeepingData::LegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
accept_changes = accept_changes && leg_data.quoter->accept_modifications();
|
|
if (!accept_changes)
|
|
break;
|
|
}
|
|
if (accept_changes)
|
|
{
|
|
for (size_t q_idx = 0; q_idx < _bookkeeping_data->count(); q_idx++)
|
|
{
|
|
SpreaderBookkeepingData::LegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
leg_data.quoter->update_order_price_quantity(Price(u.price()), Quantity(u.quantity()));
|
|
}
|
|
}
|
|
}
|
|
else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
{
|
|
// TODO send a rejection out
|
|
}
|
|
else if (_state == State::END)
|
|
{
|
|
// TODO send a rejection out
|
|
}
|
|
}
|
|
|
|
void SpreaderOrder::on_cancel(const api::orderman::CancelSpreaderOrder& c)
|
|
{
|
|
LOG(OrderMan,Diag,"on_cancel SpreaderOrder. order: {}",utils::pb::to_string(c));
|
|
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)
|
|
{
|
|
LOG(OrderMan, Diag, "on_book");
|
|
LOG(Latency, Info, "on_book {} {}", cid, utils::TimeStamp::now().to_uint64());
|
|
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::WORKING)
|
|
{
|
|
// this is a key->index lookup
|
|
SpreaderBookkeepingData::LegData& 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 idx = 0; idx < _bookkeeping_data->count(); idx++)
|
|
{
|
|
SpreaderBookkeepingData::LegData& d = _bookkeeping_data->get_leg_data(idx);
|
|
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++)
|
|
{
|
|
SpreaderBookkeepingData::HedgerData h = md_leg.get_hedger(h_idx);
|
|
h.hedger.update_md();
|
|
}
|
|
}
|
|
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 orderkey,
|
|
api::orderman::OrderStatus status,
|
|
Quantity filled_quantity,
|
|
Price avg_price,
|
|
std::string const *error_msg)
|
|
{
|
|
using namespace bts::spreader;
|
|
static std::string empty;
|
|
LOG(OrderMan,Diag,
|
|
"on_child_update SpreaderOrder."
|
|
"k: {} status: {} filled_qty: {} avg_price: {} reject_msg: {}",
|
|
orderkey.value(), status, filled_quantity.value(), avg_price.value(), error_msg ? 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)
|
|
//{
|
|
// if (_order_update_has_error(error_msg))
|
|
// _handle_order_error();
|
|
// else
|
|
// {
|
|
// SpreaderBookkeepingData::LegData* q_leg_data_ptr = _bookkeeping_data->find_quoter_from_orderkey(orderkey);
|
|
// if (q_leg_data_ptr != NULL)
|
|
// {
|
|
// SpreaderBookkeepingData::LegData& q_leg_data = *(q_leg_data_ptr);
|
|
// SpreaderOrderUpdateMessage update_message(orderkey, status, filled_quantity, avg_price, q_leg_data.filled_quantity, q_leg_data.avergage_filled_price);
|
|
// _handle_quoter_order_update(q_leg_data, update_message);
|
|
// }
|
|
// else
|
|
// {
|
|
// // should be a hedge order then
|
|
// SpreaderBookkeepingData::HedgerData* hedger_data = _bookkeeping_data->find_hedger_from_orderkey(orderkey);
|
|
// if (hedger_data != NULL)
|
|
// {
|
|
// SpreaderOrderUpdateMessage update_message(orderkey, status, filled_quantity, avg_price, hedger_data->filled_quantity, hedger_data->avergage_filled_price);
|
|
// hedger_data->update_fill(filled_quantity);
|
|
// hedger_data->hedger.update_order_state(update_message);
|
|
// }
|
|
// }
|
|
// }
|
|
//}
|
|
//else if (_state == State::WAITING_FOR_CANCEL_ALL)
|
|
//{
|
|
// // all it has to do
|
|
// SpreaderBookkeepingData::LegData* q_leg_data_ptr = _bookkeeping_data->find_quoter_from_orderkey(orderkey);
|
|
// if (q_leg_data_ptr != NULL)
|
|
// {
|
|
// SpreaderBookkeepingData::LegData& q_leg_data = *(q_leg_data_ptr);
|
|
// SpreaderOrderUpdateMessage update_message(orderkey, status, filled_quantity, avg_price, q_leg_data.filled_quantity, q_leg_data.avergage_filled_price);
|
|
// q_leg_data_ptr->quoter->update_order_status(update_message);
|
|
// }
|
|
// else
|
|
// {
|
|
// SpreaderBookkeepingData::HedgerData* hedger_data = _bookkeeping_data->find_hedger_from_orderkey(orderkey);
|
|
// if (hedger_data != NULL)
|
|
// {
|
|
// SpreaderOrderUpdateMessage update_message(orderkey, status, filled_quantity, avg_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)
|
|
{
|
|
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
|
|
}
|
|
}
|
|
|
|
bts::orderman::OrderKey SpreaderOrder::send_limit_order(const OrderTemplate&, bts::Price, bts::Quantity)
|
|
{
|
|
bts::orderman::OrderKey k;
|
|
return k;
|
|
}
|
|
void SpreaderOrder::update_order(bts::orderman::OrderKey, bts::Price, bts::Quantity)
|
|
{
|
|
}
|
|
|
|
void SpreaderOrder::cancel_order(bts::orderman::OrderKey)
|
|
{
|
|
}
|
|
|
|
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;
|
|
}
|
|
size_t SpreaderOrder::n_legs() const
|
|
{
|
|
return _bookkeeping_data->count();
|
|
}
|
|
|
|
void SpreaderOrder::_transform_to_WAITING_FOR_FIRST_CONNECTION()
|
|
{
|
|
bts_assert(_state == State::START);
|
|
_state = State::WAITING_FOR_FIRST_CONNECT;
|
|
}
|
|
void SpreaderOrder::_transform_to_WORKING()
|
|
{
|
|
bts_assert(_state == State::WAITING_FOR_FIRST_CONNECT);
|
|
_state = State::WORKING;
|
|
for (size_t index = 0; index < _bookkeeping_data->count(); index++)
|
|
{
|
|
SpreaderBookkeepingData::LegData& leg_data = _bookkeeping_data->get_leg_data(index);
|
|
leg_data.quoter->init();
|
|
}
|
|
}
|
|
void SpreaderOrder::_transform_to_WAITING_FOR_CANCEL_ALL()
|
|
{
|
|
bts_assert(_state == State::WORKING);
|
|
_state = State::WAITING_FOR_CANCEL_ALL;
|
|
_cancel_orders(true, true);
|
|
}
|
|
void SpreaderOrder::_transform_to_END()
|
|
{
|
|
bts_assert(_state == State::WORKING || _state == State::WAITING_FOR_CANCEL_ALL);
|
|
_state = State::END;
|
|
}
|
|
void SpreaderOrder::_handle_order_error()
|
|
{
|
|
_transform_to_WAITING_FOR_CANCEL_ALL();
|
|
}
|
|
void SpreaderOrder::_handle_quoter_order_update(SpreaderBookkeepingData::LegData&, const SpreaderOrderUpdateMessage&)
|
|
{
|
|
//// 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
|
|
//Quantity this_fill_quantity; Price this_fill_price;
|
|
//if (update_message.update_fill_price_quantity(this_fill_quantity, this_fill_price))
|
|
//{
|
|
// leg_data.filled_quantity = update_message.filled_quantity;
|
|
// leg_data.avergage_filled_price = update_message.avg_price;
|
|
// for (size_t idx = 0; idx < _bookkeeping_data->count(); idx++)
|
|
// {
|
|
// if (idx != leg_data.leg_property_data.leg_index)
|
|
// {
|
|
// leg_data.quoter->update_other_leg_fills(leg_data.leg_property_data.leg_index, this_fill_quantity);
|
|
// _send_hedge(leg_data.leg_property_data.leg_index, this_fill_price, this_fill_quantity, idx);
|
|
// }
|
|
// }
|
|
//}
|
|
}
|
|
void SpreaderOrder::_cancel_orders(bool cancel_quotes, bool cancel_hedges)
|
|
{
|
|
for (size_t q_idx = 0; q_idx < _bookkeeping_data->count(); q_idx++)
|
|
{
|
|
SpreaderBookkeepingData::LegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
if (cancel_quotes)
|
|
leg_data.quoter->cancel_order();
|
|
if (cancel_hedges)
|
|
for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++)
|
|
{
|
|
SpreaderBookkeepingData::HedgerData& 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++)
|
|
{
|
|
SpreaderBookkeepingData::LegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
if (leg_data.quoter->is_active())
|
|
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++)
|
|
{
|
|
SpreaderBookkeepingData::LegData& leg_data = _bookkeeping_data->get_leg_data(q_idx);
|
|
for (size_t h_idx = 0; h_idx < leg_data.n_hedgers(); h_idx++)
|
|
{
|
|
SpreaderBookkeepingData::HedgerData hedger_data = leg_data.get_hedger(h_idx);
|
|
if (hedger_data.hedger.is_active())
|
|
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 });
|
|
}
|
|
|
|
}}
|