Files
spark/src/server/sparkd/spreader/SingleLegQuoter.cpp
T
2017-11-29 12:15:03 -05:00

190 lines
9.1 KiB
C++

#include <memory>
#include "SingleLegQuoter.h"
#include "ISpreaderManager.h"
#include "SpreaderUtils.h"
#include "LegPropertyData.h"
#include "SpreaderOrderUpdateMessage.h"
#include "server/sparkd/orderman/utils/orderman_utils.h"
namespace bts {
namespace orderman {
SingleLegQuoter::SingleLegQuoter(ISpreaderManager& spreader,
LegPropertyData this_leg_property,
size_t n_spreader_legs,
bts::Price spread_order_price,
bts::Quantity spread_order_quantity,
bts::orderman::SpreaderOrderBase::ChildOrder child_order)
:
_two_legged (n_spreader_legs == 2),
_spreader(spreader),
_this_leg_data (this_leg_property),
_n_spreader_legs (n_spreader_legs),
_order (child_order),
_price_quantity_state(QuoteLegPriceQuantityState(spread_order_price, Quantity(SpreaderUtils::get_leg_quantity_from_spreader_quantity (this_leg_property, spread_order_quantity)))),
_is_permenantly_cancelled (false),
_worker(nullptr)
//_time_stamp (0) // TODO
{
for (size_t q_idx = 0; q_idx < _n_spreader_legs; q_idx++)
_aux_comp_data.push_back(PQPair());
_transform_to_START ();
}
/*********************************************************************************/
/*********************************spreader calls**********************************/
/*********************************************************************************/
void SingleLegQuoter::init()
{
LOG(OrderMan, Diag, "SingleLegQuoter::init key: {}", _order.order_key().value());
_worker->init();
}
void SingleLegQuoter::update_order_status(const SpreaderOrderUpdateMessage& u)
{
LOG(OrderMan, Diag, "SingleLegQuoter::update_order_status key: {} state: {} order status: {} fills: {}",
_order.order_key().value(), _worker->state_text(), static_cast<int>(u.status()), u.last_update_filled_quantity().value());
_worker->on_order_update (u);
}
void SingleLegQuoter::on_book(orderman::TickToTradeCorID cid, size_t leg_index, const bts::orderman::Book& book)
{
_worker->on_book (cid, leg_index, book);
}
void SingleLegQuoter::update_other_leg_fills(size_t leg_index, Quantity filled_quantity)
{
LOG(OrderMan, Diag, "SingleLegQuoter::update_other_leg_fills key: {} state: {} leg_index: {} filled_quantity: {}",
_order.order_key().value(), _worker->state_text(), leg_index, filled_quantity.value());
_worker->other_leg_quoter_fill (leg_index, filled_quantity);
}
void SingleLegQuoter::update_order_price_quantity(Price price, Quantity quantity)
{
Quantity this_leg_quantity = Quantity (SpreaderUtils::get_leg_quantity_from_spreader_quantity (_this_leg_data, quantity));
LOG(OrderMan, Diag, "SingleLegQuoter::update_order_price_quantity key: {} state: {} price: {} spread quantity: {} leg quantity: {}",
_order.order_key().value(), _worker->state_text(), price.value(), quantity.value(), this_leg_quantity.value());
_worker->update_spreader_order (price, this_leg_quantity);
}
void SingleLegQuoter::cancel_order()
{
_is_permenantly_cancelled = true;
LOG(OrderMan, Diag, "SingleLegQuoter::cancel_order key: {}, state: {}", _order.order_key().value(), _worker->state_text());
_worker->cancel ();
}
void SingleLegQuoter::terminate ()
{
_is_permenantly_cancelled = true;
LOG(OrderMan, Diag, "SingleLegQuoter::terminate key: {}, state: {}", _order.order_key().value(), _worker->state_text());
_worker->terminate ();
}
void SingleLegQuoter::on_time (const bts::TimeStamp& ts)
{
_time_stamp = ts;
_worker->on_time (ts);
}
bool SingleLegQuoter::is_active_order() const
{
QuoterState state = _worker->state();
return state == QuoterState::WAITING_FOR_FIRST_ACK || state == QuoterState::WAITING_FOR_FIRST_ACK_CANCELLED ||
state == QuoterState::WORKING || state == QuoterState::WAITING_FOR_CANCEL_ACK;
}
bool SingleLegQuoter::is_terminal_state () const {return _worker->state() == QuoterState::END;}
bool SingleLegQuoter::accept_modifications() const { return _worker->state() == QuoterState::WAITING_FOR_SIZE || _worker->state() == QuoterState::WORKING; }
void SingleLegQuoter::_incorporate_other_leg_fill (size_t leg_index, Quantity filled_quantity)
{
double this_leg_reduction_d = SpreaderUtils::get_equivelant_size (_spreader.get_leg_data(leg_index), filled_quantity, _this_leg_data);
_price_quantity_state.add_hedged_reduction (Quantity (this_leg_reduction_d));
}
void SingleLegQuoter::_compute_price_quantity(Price& price, Quantity& quantity)
{
Quantity remaining_quantity = _price_quantity_state.remaining_to_fill();
if (_two_legged)
{
size_t other_leg_index = _this_leg_data.leg_index() == 0 ? 1 : 0;
SpreaderUtils::compute_price_quantity_2_legs (_price_quantity_state.target_spread_price(), remaining_quantity,
_this_leg_data, _spreader.get_leg_data(other_leg_index), _spreader.get_book(other_leg_index),
price, quantity);
bts_assert (quantity.value() <= remaining_quantity.value());
}
else _compute_price_quantity_n_legs (price, quantity);
}
void SingleLegQuoter::_compute_price_quantity_n_legs(Price& price, Quantity& quantity)
{
size_t min_quoter_leg_quantity_index = 0;
for (size_t idx = 0; idx < _n_spreader_legs; idx++)
{
if (idx != _this_leg_data.leg_index())
{
const orderman::Book& other_leg_book = _spreader.get_book(idx);
const LegPropertyData& other_leg_data = _spreader.get_leg_data(idx);
Price other_leg_calc_price; Quantity other_leg_calc_quantity, this_leg_calc_quantity;
Quantity this_leg_target_quantity = _price_quantity_state.remaining_to_fill();
double other_leg_total_lean_ratio = other_leg_data.leg_params().quote_params().lean_quantity_ratio() * other_leg_data.size_multiplier() / _this_leg_data.size_multiplier();
double other_leg_target_quantity = this_leg_target_quantity.value() * other_leg_total_lean_ratio;
SpreaderUtils::traverse_depth_for_quantity(other_leg_book, SpreaderUtils::invert_side (other_leg_data.trading_side()), Quantity(other_leg_target_quantity), other_leg_calc_price, other_leg_calc_quantity);
this_leg_calc_quantity = Quantity(static_cast<uint32_t>(1.0 /*TODOit->lean_ratio*/ * other_leg_calc_quantity.value()));
PQPair& leg_price_quantity = _aux_comp_data.at(idx);
leg_price_quantity.set(other_leg_calc_price, other_leg_calc_quantity, this_leg_calc_quantity);
if (_aux_comp_data.at(min_quoter_leg_quantity_index).quoter_leg_quantity.value() <= this_leg_calc_quantity.value())
min_quoter_leg_quantity_index = idx;
}
}
quantity = _aux_comp_data.at(min_quoter_leg_quantity_index).quoter_leg_quantity;
price = _compute_leg_price_n_legs(_aux_comp_data);
}
Price SingleLegQuoter::_compute_leg_price_n_legs(const std::vector<PQPair>& leg_values)
{
double sum = 0;
for (size_t idx = 0; idx < _n_spreader_legs; idx++)
{
if (idx != _this_leg_data.leg_index())
{
const PQPair& leg_pq_pair = leg_values[idx];
const LegPropertyData& leg_data = _spreader.get_leg_data (idx);
sum += leg_pq_pair.leg_price.value() * leg_data.price_multiplier();
}
}
double rv = _price_quantity_state.target_spread_price().value() - sum; /// TODO, needs a divisor
return Price(rv);
}
/*********************************************************************************/
/*******************************state transformations*****************************/
/*********************************************************************************/
void SingleLegQuoter::_transform_to_START ()
{
bts_assert (_worker == nullptr);
_switch_state (&_start_state_worker);
}
void SingleLegQuoter::_transform_to_WAITING_FOR_SIZE ()
{
bts_assert (_worker->state() == QuoterState::START || _worker->state() == QuoterState::END);
_switch_state (&_waiting_for_size_state_worker);
}
void SingleLegQuoter::_transform_to_WAITING_FOR_FIRST_ACK ()
{
bts_assert (_worker->state() == QuoterState::WAITING_FOR_SIZE);
_switch_state (&_waiting_for_first_ack_state_worker);
}
void SingleLegQuoter::_transform_to_WAITING_FOR_FIRST_ACK_CANCELLED()
{
bts_assert (_worker->state() == QuoterState::WAITING_FOR_FIRST_ACK);
_switch_state (&_waiting_for_first_ack_cancelled_state_worker);
}
void SingleLegQuoter::_transform_to_WORKING()
{
bts_assert (_worker->state() == QuoterState::WAITING_FOR_FIRST_ACK);
_switch_state (&_working_state_worker);
}
void SingleLegQuoter::_transform_to_WAITING_FOR_CANCEL_ACK()
{
bts_assert (_worker->state() == QuoterState::WORKING || _worker->state() == QuoterState::WAITING_FOR_FIRST_ACK_CANCELLED);
_switch_state (&_waiting_for_cancel_ack_state_worker);
}
void SingleLegQuoter::_transform_to_END()
{
bts_assert (_worker->state() != QuoterState::END);
_switch_state (&_end_state_worker);
}
void SingleLegQuoter::_switch_state (IQuoterStateWorker* new_state)
{
LOG(OrderMan, Diag, "SingleLegQuoter::_switch_state key: {}, {} -> {} ", _order.order_key().value(), (_worker == nullptr ? "NULL" : _worker->state_text()), new_state->state_text());
_worker = new_state;
_worker->build_once (this);
}
}}