Merge branch 'master' of bitbucket.org:bluetradesys/spark
This commit is contained in:
commit
5ff7e5f72e
7 files changed
+977
-766
No files matched your search
@@ -9,46 +9,31 @@ QuoteLegPriceQuantityState::QuoteLegPriceQuantityState(Price target_spread_price
|
||||
_in_target_leg_quantity(target_leg_quantity),
|
||||
_aggregated_filled_quantity(0),
|
||||
_aggregated_hedged_reduction(0),
|
||||
_computed_quantity(0),
|
||||
_out_quantity(0)
|
||||
{}
|
||||
|
||||
void QuoteLegPriceQuantityState::update_target_spread_price(Price new_target_spread_price)
|
||||
{
|
||||
_in_target_spread_price = new_target_spread_price;
|
||||
}
|
||||
void QuoteLegPriceQuantityState::update_target_spread_price(Price new_target_spread_price) { _in_target_spread_price = new_target_spread_price; }
|
||||
|
||||
void QuoteLegPriceQuantityState::update_target_leg_quantity(Quantity new_target_leg_quantity)
|
||||
{
|
||||
_in_target_leg_quantity = new_target_leg_quantity;
|
||||
}
|
||||
void QuoteLegPriceQuantityState::update_target_leg_quantity(Quantity new_target_leg_quantity) { _in_target_leg_quantity = new_target_leg_quantity; }
|
||||
|
||||
void QuoteLegPriceQuantityState::add_filled_quantity(Quantity filled_qty)
|
||||
{
|
||||
_aggregated_filled_quantity = Quantity(_aggregated_filled_quantity.value() + filled_qty.value());
|
||||
}
|
||||
|
||||
void QuoteLegPriceQuantityState::update_filled_quantity(Quantity aggregated_filled_quantity) { _aggregated_filled_quantity = Quantity(aggregated_filled_quantity); }
|
||||
|
||||
void QuoteLegPriceQuantityState::add_hedged_reduction(Quantity normalized_hedge_reduction)
|
||||
{
|
||||
_aggregated_hedged_reduction = Quantity(_aggregated_hedged_reduction.value() + normalized_hedge_reduction.value());
|
||||
}
|
||||
|
||||
void QuoteLegPriceQuantityState::update_computed_price_quantity(Price new_computed_price, Quantity new_computed_quantity)
|
||||
void QuoteLegPriceQuantityState::update_out_price_quantity(Price new_out_price, Quantity new_out_quantity)
|
||||
{
|
||||
_computed_price = new_computed_price;
|
||||
_computed_quantity = new_computed_quantity;
|
||||
_out_price = new_out_price;
|
||||
_out_quantity = new_out_quantity;
|
||||
}
|
||||
|
||||
void QuoteLegPriceQuantityState::synchronize_out_price_quantity()
|
||||
{
|
||||
_out_price = _computed_price;
|
||||
_out_quantity = _computed_quantity;
|
||||
}
|
||||
|
||||
bool QuoteLegPriceQuantityState::out_quantity_synchronized() { return _out_quantity == _computed_quantity; }
|
||||
|
||||
bool QuoteLegPriceQuantityState::out_price_synchronized() { return _out_price == _computed_price; }
|
||||
|
||||
Quantity QuoteLegPriceQuantityState::remaining_to_fill()
|
||||
{
|
||||
if (_aggregated_hedged_reduction.value() >= _in_target_leg_quantity.value())
|
||||
|
||||
@@ -30,13 +30,6 @@ private:
|
||||
// for example with with a a*leg_1 - b*leg_2, spread an N lot sized
|
||||
// fill on leg_2 will will result in N*a/b added to _aggregated_hedged_reduction
|
||||
Quantity _aggregated_hedged_reduction;
|
||||
// theoretically computed price. this might be different from
|
||||
// _out_price because of the state of the quoter. eventually
|
||||
// they should converge
|
||||
Price _computed_price;
|
||||
// theoretically computed quantity. similiar behaviour to
|
||||
// _computed_price
|
||||
Quantity _computed_quantity;
|
||||
// price of the last order sent.
|
||||
Price _out_price;
|
||||
// quantity of the last order sent
|
||||
@@ -52,27 +45,19 @@ public:
|
||||
// called when a fill happens for this leg. this increases the filled counter.
|
||||
// this is needed to know the "remainder quantity" needed to price this leg
|
||||
void add_filled_quantity(Quantity filled_qty);
|
||||
// updates the filled quantity as a whole (vs adding increments)
|
||||
void update_filled_quantity(Quantity total_filled_quantity);
|
||||
// called when a fill happens in one of the other quoting legs.
|
||||
void add_hedged_reduction(Quantity normalized_hedge_qty);
|
||||
// updates the computed price and quantity
|
||||
void update_computed_price_quantity(Price new_computed_price, Quantity new_computed_quantity);
|
||||
// this sets the out price/quantity to the computed price/quantity
|
||||
void synchronize_out_price_quantity();
|
||||
// returns true when the out pice is the same as computed price
|
||||
bool out_quantity_synchronized();
|
||||
// Tell if the out quantity is the same as computed quantity
|
||||
bool out_price_synchronized();
|
||||
void add_hedged_reduction(Quantity normalized_hedge_qty);
|
||||
// updates the out price and quantity
|
||||
void update_out_price_quantity(Price new_computed_price, Quantity new_computed_quantity);
|
||||
// Accessor functions
|
||||
Price target_spread_price() { return _in_target_spread_price; }
|
||||
Quantity target_leg_quantity() { return _in_target_leg_quantity; }
|
||||
Quantity aggregated_filled_quantity() { return _aggregated_filled_quantity; }
|
||||
Quantity aggregated_hedged_reduction() { return _aggregated_hedged_reduction; }
|
||||
Price computed_price() { return _computed_price; }
|
||||
Quantity computed_quantity() { return _computed_quantity; }
|
||||
Price out_price() { return _out_price; }
|
||||
Quantity out_quantity() { return _out_quantity; }
|
||||
bool computed_price_is_higher() { return _computed_price.value() > _out_price.value(); }
|
||||
Price change_in_computed_price() { return Price(_computed_price.value() - _out_price.value()); }
|
||||
Quantity remaining_to_fill();
|
||||
};
|
||||
}}
|
||||
@@ -12,14 +12,13 @@ SingleFillHedger::SingleFillHedger(ISpreaderInternalOrderBridge* order_bridge,
|
||||
const bts::spark::ExecutionStrategy& execution_strategy,
|
||||
bts::Price hedge_price,
|
||||
bts::Quantity hedge_quantity,
|
||||
api::BuySell )
|
||||
api::BuySell hedge_side)
|
||||
:
|
||||
_tick_size(this_leg.security().contract_spec().low_tick()),
|
||||
_order_client_id(order_client_id),
|
||||
// _contract_leg(this_leg),
|
||||
_post_hedge_params(SpreaderUtils::get_leg_params(execution_strategy,this_leg).post_hedge_params()),
|
||||
// _hedge_side(hedge_side),
|
||||
_price_quantity_state(hedge_price, hedge_quantity),
|
||||
_price_quantity_state(hedge_price, hedge_quantity, hedge_side),
|
||||
_state(HedgerState::START),
|
||||
_order_bridge(order_bridge)
|
||||
{
|
||||
@@ -27,152 +26,153 @@ SingleFillHedger::SingleFillHedger(ISpreaderInternalOrderBridge* order_bridge,
|
||||
|
||||
bool SingleFillHedger::is_active() { return _state != HedgerState::START && _state != HedgerState::END; }
|
||||
|
||||
bool SingleFillHedger::accepting_price_change() { return _state == HedgerState::WORKING; }
|
||||
|
||||
Quantity SingleFillHedger::remaining_to_fill() { return _price_quantity_state.remaining_to_fill(); }
|
||||
|
||||
void SingleFillHedger::update_order_status(const OrderState& order_state)
|
||||
void SingleFillHedger::update_order_state(const OrderState& order_state)
|
||||
{
|
||||
bool filled, terminal_state, encountered_error;
|
||||
SpreaderUtils::parse_order(order_state, filled, terminal_state, encountered_error);
|
||||
ExternalHedgerEvents ex_ev = ExternalHedgerEvents::OrderStatusError;
|
||||
if (terminal_state) ex_ev = ExternalHedgerEvents::OrderStatusTerminal;
|
||||
else
|
||||
{
|
||||
if (encountered_error) ex_ev = ExternalHedgerEvents::OrderStatusError;
|
||||
else ex_ev = ExternalHedgerEvents::OrderStatusNormal;
|
||||
}
|
||||
_fire_event(ex_ev);
|
||||
SingleFillHedger::EventData event_data (&order_state);
|
||||
_fire_event(event_data);
|
||||
}
|
||||
|
||||
void SingleFillHedger::update_md(Book const&b)
|
||||
void SingleFillHedger::update_md(const Book &b)
|
||||
{
|
||||
_book = b;
|
||||
_fire_event(ExternalHedgerEvents::MDUpdate);
|
||||
SingleFillHedger::EventData event_data(&b);
|
||||
_fire_event(event_data);
|
||||
}
|
||||
|
||||
void SingleFillHedger::modify_hedge_price(Price new_hedge_price)
|
||||
{
|
||||
_price_quantity_state.udpate_target_price(new_hedge_price);
|
||||
// working state will check the out price vs target price
|
||||
// and modify the price to target price
|
||||
_fire_event(ExternalHedgerEvents::UserHedgePriceChange);
|
||||
SingleFillHedger::EventData event_data(new_hedge_price);
|
||||
_fire_event(event_data);
|
||||
}
|
||||
|
||||
void SingleFillHedger::cancel_hedge()
|
||||
{
|
||||
_fire_event(ExternalHedgerEvents::HedgeCancel);
|
||||
SingleFillHedger::EventData event_data(ExternalHedgerEvents::HedgeCancel);
|
||||
_fire_event(event_data);
|
||||
}
|
||||
|
||||
void SingleFillHedger::_fire_event(ExternalHedgerEvents ex_ev)
|
||||
{
|
||||
if (_state == HedgerState::WORKING)
|
||||
_handle_events_in_WORKING(ex_ev);
|
||||
if (_state == HedgerState::START)
|
||||
_handle_events_in_START(ex_ev);
|
||||
else if (_state == HedgerState::WAITING_FOR_FIRST_ACK)
|
||||
_handle_events_in_WAITING_FOR_FIRST_ACK(ex_ev);
|
||||
else if (_state == HedgerState::WAITING_FOR_CANCEL_ACK)
|
||||
_handle_events_in_WAITING_FOR_CANCEL_ACK(ex_ev);
|
||||
else if (_state == HedgerState::END)
|
||||
_handle_events_in_END(ex_ev);
|
||||
}
|
||||
|
||||
void SingleFillHedger::_handle_events_in_START(ExternalHedgerEvents)
|
||||
/***************************************************************************/
|
||||
/********************External Event Handling in START***********************/
|
||||
/***************************************************************************/
|
||||
void SingleFillHedger::_handle_events_in_START(const EventData&)
|
||||
{
|
||||
// start can get no events
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
void SingleFillHedger::_handle_events_in_WAITING_FOR_FIRST_ACK(ExternalHedgerEvents ex_ev)
|
||||
/***************************************************************************/
|
||||
/*********External Event Handling in WAITING_FOR_FIRST_ACK******************/
|
||||
/***************************************************************************/
|
||||
void SingleFillHedger::transform_to_WAITING_FOR_FIRST_ACK()
|
||||
{
|
||||
if (ex_ev == ExternalHedgerEvents::OrderStatusNormal)
|
||||
_change_state(HedgerState::WORKING);
|
||||
else if (ex_ev == ExternalHedgerEvents::OrderStatusError)
|
||||
_change_state(HedgerState::WAITING_FOR_CANCEL_ACK);
|
||||
else if (ex_ev == ExternalHedgerEvents::OrderStatusTerminal)
|
||||
_change_state(HedgerState::END);
|
||||
else if (ex_ev == ExternalHedgerEvents::MDUpdate)
|
||||
//for now we are only going to implement a time based post hedge rule, so
|
||||
//market data is not needed
|
||||
{
|
||||
}
|
||||
else if (ex_ev == ExternalHedgerEvents::UserHedgePriceChange)
|
||||
// when the hedger goes to WORKING, the out price is checked vs the computed price
|
||||
// and price adjustments are made if necessary
|
||||
{
|
||||
}
|
||||
else if (ex_ev == ExternalHedgerEvents::HedgeCancel)
|
||||
_change_state(HedgerState::WAITING_FOR_CANCEL_ACK);
|
||||
bts_assert(_state == HedgerState::START);
|
||||
_state = HedgerState::WAITING_FOR_FIRST_ACK;
|
||||
// The order is sent for the one and only time here
|
||||
// TODO check for market orders pre hedge rule
|
||||
_order_bridge->send_limit_order(_order_client_id, _price_quantity_state.target_price(), _price_quantity_state.target_quantity());
|
||||
}
|
||||
|
||||
void SingleFillHedger::_handle_events_in_WORKING(ExternalHedgerEvents ex_ev)
|
||||
void SingleFillHedger::_handle_events_in_WAITING_FOR_FIRST_ACK(const EventData& event_data)
|
||||
{
|
||||
if (ex_ev == ExternalHedgerEvents::OrderStatusNormal)
|
||||
if (event_data.external_event == ExternalHedgerEvents::OrderState)
|
||||
{
|
||||
// the order might be changing non terminal states (ex from 'waiting_for_ack' to
|
||||
// 'ack_received' but there is nothing to do as a response*/
|
||||
bool filled, terminal_status, encountered_error, new_fills;
|
||||
SpreaderUtils::parse_order(*event_data.order_state, _price_quantity_state.aggregated_filled_quantity(), filled, terminal_status, encountered_error, new_fills);
|
||||
if (terminal_status)
|
||||
transform_to_END();
|
||||
else if (encountered_error)
|
||||
transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
else
|
||||
_transform_to_WORKING();
|
||||
}
|
||||
else if (ex_ev == ExternalHedgerEvents::OrderStatusError)
|
||||
// any error means we cancel
|
||||
_change_state(HedgerState::WAITING_FOR_CANCEL_ACK);
|
||||
else if (ex_ev == ExternalHedgerEvents::OrderStatusTerminal)
|
||||
// either it was cancelled externally, filled, rejected etc
|
||||
_change_state(HedgerState::END);
|
||||
else if (ex_ev == ExternalHedgerEvents::MDUpdate)
|
||||
{
|
||||
}
|
||||
else if (ex_ev == ExternalHedgerEvents::UserHedgePriceChange)
|
||||
{
|
||||
if (!_price_quantity_state.prices_synced())
|
||||
{
|
||||
_price_quantity_state.sync_out_price();
|
||||
_order_bridge->update_order(_order_client_id, _price_quantity_state.target_price(), _price_quantity_state.target_quantity());
|
||||
}
|
||||
}
|
||||
else if (ex_ev == ExternalHedgerEvents::HedgeCancel)
|
||||
_change_state(HedgerState::WAITING_FOR_CANCEL_ACK);
|
||||
}
|
||||
|
||||
void SingleFillHedger::_handle_events_in_WAITING_FOR_CANCEL_ACK(ExternalHedgerEvents ex_ev)
|
||||
{
|
||||
// waiting for the cancel to take place (or, conincidentally a terminal state
|
||||
// through other means
|
||||
if (ex_ev == ExternalHedgerEvents::OrderStatusTerminal)
|
||||
_change_state(HedgerState::END);
|
||||
}
|
||||
|
||||
void SingleFillHedger::_handle_events_in_END(ExternalHedgerEvents ex_ev)
|
||||
{
|
||||
bts_assert(ex_ev != ExternalHedgerEvents::OrderStatusNormal);
|
||||
bts_assert(ex_ev != ExternalHedgerEvents::OrderStatusError);
|
||||
bts_assert(ex_ev != ExternalHedgerEvents::OrderStatusTerminal);
|
||||
}
|
||||
|
||||
void SingleFillHedger::_change_state(HedgerState new_state)
|
||||
{
|
||||
HedgerState curr_state = _state;
|
||||
_state = new_state;
|
||||
if (new_state == HedgerState::START)
|
||||
else if (event_data.external_event == ExternalHedgerEvents::MDUpdate) {}
|
||||
// md will have no impact when waiting for order ack
|
||||
else if (event_data.external_event == ExternalHedgerEvents::UserHedgePriceChange)
|
||||
// accept_changes should have returned false, forbiding price changes
|
||||
bts_assert(false);
|
||||
else if (new_state == HedgerState::WAITING_FOR_FIRST_ACK)
|
||||
else if (event_data.external_event == ExternalHedgerEvents::HedgeCancel)
|
||||
transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
}
|
||||
/***************************************************************************/
|
||||
/*******************External Event Handling in WORKING**********************/
|
||||
/***************************************************************************/
|
||||
void SingleFillHedger::_transform_to_WORKING()
|
||||
{
|
||||
bts_assert(_state == HedgerState::WAITING_FOR_FIRST_ACK);
|
||||
_state = HedgerState::WORKING;
|
||||
// nothing to do here, since any changes in the previous
|
||||
// state were rejected
|
||||
}
|
||||
void SingleFillHedger::_handle_events_in_WORKING(const EventData& event_data)
|
||||
{
|
||||
if (event_data.external_event == ExternalHedgerEvents::OrderState)
|
||||
{
|
||||
bts_assert(curr_state == HedgerState::START);
|
||||
// The order is sent for the only and only time here
|
||||
// TODO check for market orders pre hedge rule
|
||||
_price_quantity_state.sync_out_price();
|
||||
_order_bridge->send_limit_order(_order_client_id, _price_quantity_state.target_price(), _price_quantity_state.target_quantity());
|
||||
bool filled, terminal_status, encountered_error, new_fills;
|
||||
SpreaderUtils::parse_order(*event_data.order_state, _price_quantity_state.aggregated_filled_quantity(), filled, terminal_status, encountered_error, new_fills);
|
||||
if (terminal_status)
|
||||
transform_to_END();
|
||||
else if (encountered_error)
|
||||
transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
}
|
||||
else if (new_state == HedgerState::WORKING)
|
||||
else if (event_data.external_event == ExternalHedgerEvents::MDUpdate)
|
||||
{
|
||||
// If while in WAITING_FOR_FIRST_ACK, the price was changed
|
||||
// we adjust it here.
|
||||
// We could also have made the main spreader query this class
|
||||
// and reject the change if the Hedger's state != Working
|
||||
// (this will be much easier and maybe worth it)
|
||||
_handle_events_in_WORKING(ExternalHedgerEvents::UserHedgePriceChange);
|
||||
// TODO apply post hedge rule
|
||||
}
|
||||
else if (new_state == HedgerState::WAITING_FOR_CANCEL_ACK)
|
||||
else if (event_data.external_event == ExternalHedgerEvents::UserHedgePriceChange)
|
||||
{
|
||||
bts_assert(curr_state == HedgerState::WAITING_FOR_FIRST_ACK || curr_state == HedgerState::WORKING);
|
||||
_order_bridge->cancel_order(_order_client_id);
|
||||
_price_quantity_state.udpate_target_price(event_data.hedge_price);
|
||||
_order_bridge->update_order(_order_client_id, _price_quantity_state.target_price(), _price_quantity_state.target_quantity());
|
||||
}
|
||||
else if (event_data.external_event == ExternalHedgerEvents::HedgeCancel)
|
||||
transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
}
|
||||
/***************************************************************************/
|
||||
/*********External Event Handling in WAITING_FOR_CANCEL_ACK*****************/
|
||||
/***************************************************************************/
|
||||
void SingleFillHedger::transform_to_WAITING_FOR_CANCEL_ACK()
|
||||
{
|
||||
bts_assert(_state == HedgerState::WAITING_FOR_FIRST_ACK || _state == HedgerState::WORKING);
|
||||
_state = HedgerState::WAITING_FOR_CANCEL_ACK;
|
||||
_order_bridge->cancel_order(_order_client_id);
|
||||
}
|
||||
void SingleFillHedger::_handle_events_in_WAITING_FOR_CANCEL_ACK(const EventData& event_data)
|
||||
{
|
||||
if (event_data.external_event == ExternalHedgerEvents::OrderState)
|
||||
{
|
||||
bool filled, terminal_status, encountered_error, new_fills;
|
||||
SpreaderUtils::parse_order(*event_data.order_state, _price_quantity_state.aggregated_filled_quantity(), filled, terminal_status, encountered_error, new_fills);
|
||||
if (terminal_status)
|
||||
transform_to_END();
|
||||
}
|
||||
}
|
||||
/***************************************************************************/
|
||||
/*********************External Event Handling in END************************/
|
||||
/***************************************************************************/
|
||||
void SingleFillHedger::transform_to_END()
|
||||
{
|
||||
bts_assert(_state == HedgerState::WAITING_FOR_FIRST_ACK || _state == HedgerState::WORKING);
|
||||
_state = HedgerState::END;
|
||||
}
|
||||
void SingleFillHedger::_handle_events_in_END(const EventData&)
|
||||
{
|
||||
}
|
||||
|
||||
void SingleFillHedger::_fire_event(const EventData& event_data)
|
||||
{
|
||||
if (_state == HedgerState::START)
|
||||
_handle_events_in_START(event_data);
|
||||
else if (_state == HedgerState::WAITING_FOR_FIRST_ACK)
|
||||
_handle_events_in_WAITING_FOR_FIRST_ACK(event_data);
|
||||
else if (_state == HedgerState::WORKING)
|
||||
_handle_events_in_WORKING(event_data);
|
||||
else if (_state == HedgerState::WAITING_FOR_CANCEL_ACK)
|
||||
_handle_events_in_WAITING_FOR_CANCEL_ACK(event_data);
|
||||
else if (_state == HedgerState::END)
|
||||
_handle_events_in_END(event_data);
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -15,15 +15,8 @@ class SingleFillHedger
|
||||
{
|
||||
enum class ExternalHedgerEvents
|
||||
{
|
||||
// normal order status event from the exchange (this is a non terminal
|
||||
// order status)
|
||||
OrderStatusNormal = 0,
|
||||
// error report from the exchange (this could be a temporary error,
|
||||
// however the state machine will cancel the order)
|
||||
OrderStatusError,
|
||||
// terminal order status even from the exchange
|
||||
OrderStatusTerminal,
|
||||
// timer event from the spreader engine
|
||||
// order status update (or fills)
|
||||
OrderState,
|
||||
Timer,
|
||||
// market data update from the exchange
|
||||
MDUpdate,
|
||||
@@ -42,23 +35,68 @@ class SingleFillHedger
|
||||
END
|
||||
};
|
||||
|
||||
class EventData
|
||||
{
|
||||
public:
|
||||
const ExternalHedgerEvents external_event;
|
||||
const OrderState* order_state;
|
||||
const Book* book;
|
||||
const Price hedge_price;
|
||||
|
||||
EventData(const OrderState* order_state_arg)
|
||||
: external_event(ExternalHedgerEvents::OrderState),
|
||||
order_state(order_state_arg),
|
||||
book(NULL),
|
||||
hedge_price(0.0)
|
||||
{}
|
||||
EventData(const Book* book_arg)
|
||||
: external_event(ExternalHedgerEvents::MDUpdate),
|
||||
order_state(NULL),
|
||||
book(book_arg),
|
||||
hedge_price(0.0)
|
||||
{}
|
||||
EventData(Price hedge_price_arg)
|
||||
: external_event(ExternalHedgerEvents::UserHedgePriceChange),
|
||||
order_state(NULL),
|
||||
book(NULL),
|
||||
hedge_price(hedge_price_arg)
|
||||
{}
|
||||
EventData(ExternalHedgerEvents external_event_arg)
|
||||
: external_event(external_event_arg),
|
||||
order_state(NULL),
|
||||
book(NULL),
|
||||
hedge_price(0.0)
|
||||
{}
|
||||
private:
|
||||
EventData()
|
||||
: external_event(ExternalHedgerEvents::Timer),
|
||||
order_state(NULL),
|
||||
book(NULL),
|
||||
hedge_price(0.0)
|
||||
{}
|
||||
};
|
||||
|
||||
// price and quantity state for the hedger is kept here
|
||||
class HedgePriceQuantityState
|
||||
{
|
||||
private:
|
||||
// price at which to get the fill. this could
|
||||
// be modifie dby the user
|
||||
Price _target_price;
|
||||
// this can never be modified
|
||||
const Quantity _target_quantity;
|
||||
// the price of the "active" hedger order
|
||||
Price _out_price;
|
||||
const api::BuySell _side;
|
||||
// filled part of the order, updated as fills
|
||||
// come in
|
||||
Quantity _aggregated_filled_quantity;
|
||||
|
||||
// the price of the "active" hedger order
|
||||
Price _out_price;
|
||||
public:
|
||||
HedgePriceQuantityState(Price target_price, Quantity target_quantity)
|
||||
HedgePriceQuantityState(Price target_price, Quantity target_quantity, api::BuySell target_side)
|
||||
:
|
||||
_target_price(target_price),
|
||||
_target_quantity(target_quantity),
|
||||
_out_price(0.0),
|
||||
_side (target_side),
|
||||
_aggregated_filled_quantity(0)
|
||||
{
|
||||
}
|
||||
@@ -69,14 +107,11 @@ class SingleFillHedger
|
||||
{
|
||||
_aggregated_filled_quantity = Quantity(_aggregated_filled_quantity.value() + fill_quantity.value());
|
||||
}
|
||||
// sets the outprice to the target price
|
||||
void sync_out_price() { _out_price = _target_price; }
|
||||
Price target_price() { return _target_price; }
|
||||
const Quantity target_quantity() { return _target_quantity; }
|
||||
Price out_price() { return _out_price; }
|
||||
Quantity aggregated_filled_quantity() { return _aggregated_filled_quantity; }
|
||||
// tells if the out_price and target_price are in sync
|
||||
bool prices_synced() { return _out_price.value() == _target_price.value(); }
|
||||
api::BuySell side() { return _side; }
|
||||
// this is the order quantity net of the fills
|
||||
Quantity remaining_to_fill()
|
||||
{
|
||||
@@ -90,22 +125,27 @@ private:
|
||||
const int _order_client_id;
|
||||
// const bts::api::ContractLeg& _contract_leg;
|
||||
const bts::spark::PostHedgeParams& _post_hedge_params;
|
||||
// const api::BuySell _hedge_side;
|
||||
HedgePriceQuantityState _price_quantity_state;
|
||||
HedgerState _state;
|
||||
ISpreaderInternalOrderBridge* _order_bridge;
|
||||
Book _book;
|
||||
private:
|
||||
void _fire_event(ExternalHedgerEvents events);
|
||||
void _handle_events_in_START (ExternalHedgerEvents events);
|
||||
void _handle_events_in_WAITING_FOR_FIRST_ACK(ExternalHedgerEvents events);
|
||||
void _handle_events_in_WORKING(ExternalHedgerEvents events);
|
||||
void _handle_events_in_WAITING_FOR_CANCEL_ACK(ExternalHedgerEvents events);
|
||||
void _handle_events_in_END(ExternalHedgerEvents events);
|
||||
// all state machine state changes occur here. also all actions
|
||||
// performed at the transition edges also are called from within
|
||||
// this function
|
||||
void _change_state(HedgerState new_state);
|
||||
private:
|
||||
/********************External Event Handling in START***********************/
|
||||
void _handle_events_in_START(const EventData& event_data);
|
||||
/*********External Event Handling in WAITING_FOR_FIRST_ACK******************/
|
||||
void transform_to_WAITING_FOR_FIRST_ACK();
|
||||
void _handle_events_in_WAITING_FOR_FIRST_ACK(const EventData& event_data);
|
||||
/*******************External Event Handling in WORKING**********************/
|
||||
void _transform_to_WORKING();
|
||||
void _handle_events_in_WORKING(const EventData& event_data);
|
||||
/*********External Event Handling in WAITING_FOR_CANCEL_ACK******************/
|
||||
void transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
void _handle_events_in_WAITING_FOR_CANCEL_ACK(const EventData& event_data);
|
||||
/*********************External Event Handling in END*************************/
|
||||
void transform_to_END();
|
||||
void _handle_events_in_END(const EventData& event_data);
|
||||
|
||||
void _fire_event(const EventData& event_data);
|
||||
public:
|
||||
SingleFillHedger(ISpreaderInternalOrderBridge* order_bridge,
|
||||
int order_client_id,
|
||||
@@ -116,12 +156,14 @@ public:
|
||||
api::BuySell hedge_side);
|
||||
|
||||
// true if the hedger has an active working order
|
||||
bool is_active();
|
||||
bool is_active();
|
||||
// true if the hedger can accept a price change
|
||||
bool accepting_price_change();
|
||||
// this is the order quantity net of the fills (qty - filled_qty)
|
||||
Quantity remaining_to_fill();
|
||||
// called by the spreader when there is an order update
|
||||
// reported by the exchange
|
||||
void update_order_status(OrderState const& state);
|
||||
void update_order_state(OrderState const& state);
|
||||
// called when the market data associated
|
||||
// with this security is changed
|
||||
void update_md(Book const&b);
|
||||
|
||||
@@ -1,388 +1,454 @@
|
||||
#include <memory>
|
||||
#include "SingleLegQuoter.h"
|
||||
#include "ISpreaderInternalOrderBridge.h"
|
||||
#include "SpreaderUtils.h"
|
||||
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
|
||||
namespace bts
|
||||
SingleLegQuoter::SingleLegQuoter(ISpreaderInternalOrderBridge* order_bridge,
|
||||
api::Security const& spread_security,
|
||||
bts::api::ContractLeg const& this_leg,
|
||||
const bts::spark::ExecutionStrategy& execution_strategy,
|
||||
bts::Price spread_order_price,
|
||||
bts::Quantity spread_order_quantity,
|
||||
api::BuySell side)
|
||||
:
|
||||
_tick_size(this_leg.security().contract_spec().low_tick()),
|
||||
_size_ratio(this_leg.quantity()),
|
||||
_spread_security(spread_security),
|
||||
_contract_leg(this_leg),
|
||||
_order_bridge(order_bridge),
|
||||
_bridge_client_id(order_bridge->get_id(_contract_leg.security(), _contract_leg.side())),
|
||||
_side(side),
|
||||
_quote_side(SpreaderUtils::get_leg_side(_side, _contract_leg)),
|
||||
_this_leg_index(SpreaderUtils::get_leg_index(_spread_security, _contract_leg)),
|
||||
_quote_params(SpreaderUtils::get_leg_params(execution_strategy, _contract_leg).quote_params()),
|
||||
_state(QuoterState::START),
|
||||
_price_quantity_state(QuoteLegPriceQuantityState(spread_order_price, Quantity(static_cast<double>(spread_order_quantity.value() * _contract_leg.quantity()))))
|
||||
{
|
||||
namespace spreader
|
||||
_leg_data.reserve(spread_security.synthetic().legs_size());
|
||||
_aux_comp_data.reserve(spread_security.synthetic().legs_size());
|
||||
size_t n_legs = static_cast<size_t>(spread_security.synthetic().legs_size());
|
||||
for (size_t i = 0; i < n_legs; i++)
|
||||
{
|
||||
SingleLegQuoter::SingleLegQuoter(ISpreaderInternalOrderBridge* order_bridge,
|
||||
api::Security const& spread_security,
|
||||
bts::api::ContractLeg const& this_leg,
|
||||
const bts::spark::ExecutionStrategy& execution_strategy,
|
||||
bts::Price spread_order_price,
|
||||
bts::Quantity spread_order_quantity,
|
||||
api::BuySell side)
|
||||
:
|
||||
_tick_size(this_leg.security().contract_spec().low_tick()),
|
||||
_size_ratio(static_cast<double>(this_leg.quantity())),
|
||||
_spread_security(spread_security),
|
||||
_contract_leg(this_leg),
|
||||
_order_bridge(order_bridge),
|
||||
_bridge_client_id(order_bridge->get_id(_contract_leg.security(), _contract_leg.side())),
|
||||
_side (side),
|
||||
_quote_side (_get_quote_side (_side, _contract_leg)),
|
||||
_this_leg_index (_get_leg_index (_spread_security, _contract_leg)),
|
||||
_quote_params (_get_quote_params (execution_strategy, _contract_leg)),
|
||||
_state(QuoterState::START),
|
||||
_price_quantity_state (QuoteLegPriceQuantityState(spread_order_price, Quantity(static_cast<double>(spread_order_quantity.value() * _contract_leg.quantity()))))
|
||||
{
|
||||
_leg_data.reserve(spread_security.synthetic().legs_size());
|
||||
_aux_comp_data.reserve(spread_security.synthetic().legs_size());
|
||||
size_t n_legs = static_cast<size_t>(spread_security.synthetic().legs_size());
|
||||
for (size_t i = 0; i < n_legs; i++)
|
||||
{
|
||||
auto leg_contract = spread_security.synthetic().legs(i);
|
||||
auto leg_params_it = execution_strategy.leg_params_map().find(leg_contract.security().id());
|
||||
bts_assert(leg_params_it != execution_strategy.leg_params_map().end());
|
||||
bts::spark::LegSpreaderParams leg_params = leg_params_it->second;
|
||||
double leg_lean_ratio = leg_params.quote_params().lean_quantity_ratio() * (static_cast<double>(leg_contract.quantity()) / static_cast<double>(this_leg.quantity()));
|
||||
LegData leg_data = LegData(leg_contract, leg_params, *(new bts::spreader::Book()), leg_lean_ratio);
|
||||
_leg_data.push_back(leg_data);
|
||||
_key_index_map.insert(std::pair<uint32_t, size_t>(leg_contract.security().id(), i));
|
||||
}
|
||||
}
|
||||
auto leg_contract = spread_security.synthetic().legs(i);
|
||||
auto leg_params_it = execution_strategy.leg_params_map().find(leg_contract.security().id());
|
||||
bts_assert(leg_params_it != execution_strategy.leg_params_map().end());
|
||||
bts::spark::LegSpreaderParams leg_params = leg_params_it->second;
|
||||
double leg_lean_ratio = leg_params.quote_params().lean_quantity_ratio() * (static_cast<double>(leg_contract.quantity()) / static_cast<double>(this_leg.quantity()));
|
||||
LegData leg_data = LegData(leg_contract, leg_params, *(new bts::spreader::Book()), leg_lean_ratio);
|
||||
_leg_data.push_back(leg_data);
|
||||
_key_index_map.insert(std::pair<uint32_t, size_t>(leg_contract.security().id(), i));
|
||||
}
|
||||
}
|
||||
|
||||
bool SingleLegQuoter::is_active() { return true; }
|
||||
bool SingleLegQuoter::is_active() { return _state == QuoterState::WAITING_FOR_FIRST_ACK || _state == QuoterState::WORKING || _state == QuoterState::WAITING_FOR_CANCEL_ACK; }
|
||||
|
||||
void SingleLegQuoter::update_order_status(OrderState const& order_status, std::vector<ChildFill> const& fills)
|
||||
{
|
||||
uint32_t filled_quantity = 0;
|
||||
for (std::vector<ChildFill>::const_iterator it = fills.begin(); it != fills.end(); ++it)
|
||||
filled_quantity += it->quantity.value();
|
||||
if (filled_quantity > 0)
|
||||
{
|
||||
Quantity q(filled_quantity);
|
||||
_price_quantity_state.add_filled_quantity(q);
|
||||
}
|
||||
ExternalQuoterEvents ex_ev = _parse_order_status(order_status);
|
||||
_fire_event(ex_ev);
|
||||
}
|
||||
bool SingleLegQuoter::accept_modifications() { return _state == QuoterState::WAITING_FOR_MARKET || _state == QuoterState::WORKING; }
|
||||
|
||||
void SingleLegQuoter::update_md(api::Security const& s, Book const& b)
|
||||
{
|
||||
size_t index = 0;
|
||||
if (_get_index(s.id(), index))
|
||||
{
|
||||
_leg_data.at(index).book = b;
|
||||
_recompute_and_set_price_quantity();
|
||||
_fire_event(ExternalQuoterEvents::MDUpdate);
|
||||
}
|
||||
}
|
||||
void SingleLegQuoter::update_order_status(OrderState const& order_state)
|
||||
{
|
||||
_event_data.set_order_event(order_state);
|
||||
_fire_event(_event_data);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::update_other_leg_fills(api::Security const& security, Quantity filled_quantity, Price)
|
||||
{
|
||||
double this_leg_reduction = _get_equivelant_quantity(security, filled_quantity);
|
||||
_price_quantity_state.add_hedged_reduction(Quantity(this_leg_reduction));
|
||||
_recompute_and_set_price_quantity();
|
||||
_fire_event(ExternalQuoterEvents::OtherLegQuotingFill);
|
||||
}
|
||||
void SingleLegQuoter::update_md(api::Security const& s, Book const& b)
|
||||
{
|
||||
size_t index = 0;
|
||||
if (SpreaderUtils::find_index(_key_index_map, s.id(), index))
|
||||
{
|
||||
_event_data.set_md_event(index, b);
|
||||
_fire_event(_event_data);
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLegQuoter::modify_order_price(Price new_price)
|
||||
{
|
||||
_price_quantity_state.update_target_spread_price(new_price);
|
||||
_recompute_and_set_price_quantity();
|
||||
_fire_event(ExternalQuoterEvents::UserSpreadPriceChange);
|
||||
}
|
||||
void SingleLegQuoter::update_other_leg_fills(api::Security const& security, Quantity filled_quantity, Price)
|
||||
{
|
||||
size_t leg_index;
|
||||
if (SpreaderUtils::find_index(_key_index_map, security.id(), leg_index))
|
||||
{
|
||||
double equivelant_quantity = _get_equivelant_quantity(leg_index, filled_quantity);
|
||||
uint32_t this_leg_reduction = static_cast<uint32_t> (equivelant_quantity);
|
||||
_event_data.set_other_leg_quoting_fill(Quantity(this_leg_reduction));
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLegQuoter::modify_order_quantity(Quantity new_quantity)
|
||||
{
|
||||
double leg_quantity_d = _size_ratio * new_quantity.value();
|
||||
_price_quantity_state.update_target_leg_quantity(Quantity(leg_quantity_d));
|
||||
_recompute_and_set_price_quantity();
|
||||
_fire_event(ExternalQuoterEvents::UserSpreadQuantityChange);
|
||||
}
|
||||
void SingleLegQuoter::modify_order_price(Price new_price)
|
||||
{
|
||||
_event_data.set_new_spread_price(new_price);
|
||||
_fire_event(_event_data);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::cancel_order()
|
||||
{
|
||||
_fire_event(ExternalQuoterEvents::CancelOrder);
|
||||
}
|
||||
void SingleLegQuoter::modify_order_quantity(Quantity new_quantity)
|
||||
{
|
||||
double leg_quantity_d = _size_ratio * new_quantity.value();
|
||||
_event_data.set_new_leg_quantity(Quantity(leg_quantity_d));
|
||||
_fire_event(_event_data);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_change_state(QuoterState new_state)
|
||||
{
|
||||
QuoterState current_state = _state;
|
||||
_state = new_state;
|
||||
if (new_state == QuoterState::WAITING_FOR_MARKET)
|
||||
{
|
||||
bts_assert(current_state == QuoterState::START);
|
||||
// this needs to bootstrap by reactin to market
|
||||
_handle_events_in_WAITING_FOR_MARKET(ExternalQuoterEvents::MDUpdate);
|
||||
}
|
||||
else if (new_state == QuoterState::WAITING_FOR_FIRST_ACK)
|
||||
{
|
||||
/// change to this state means send the order
|
||||
bts_assert(current_state == QuoterState::WAITING_FOR_MARKET);
|
||||
bts_assert(_price_quantity_state.aggregated_filled_quantity().value() == 0);
|
||||
bts_assert(_price_quantity_state.out_quantity().value() == 0);
|
||||
bts_assert(_price_quantity_state.computed_quantity().value() > 0);
|
||||
// All changes to price (for spread, there is no active leg order till now) and qty
|
||||
// have been subsumed into the price_qty_state
|
||||
// Also anything that would have ended the order (mainly other leg fills
|
||||
// and subsequent hedges) would have been checked in waiting_for_md
|
||||
_price_quantity_state.synchronize_out_price_quantity();
|
||||
// This is the first and only time an order is sent from SingleLegQuoter
|
||||
_order_bridge->send_limit_order(_bridge_client_id, _price_quantity_state.out_price(), _price_quantity_state.out_quantity());
|
||||
}
|
||||
else if (new_state == QuoterState::WORKING)
|
||||
{
|
||||
bts_assert(current_state == QuoterState::WAITING_FOR_FIRST_ACK);
|
||||
/// check for pending changes immediatly
|
||||
_handle_events_in_WORKING(ExternalQuoterEvents::MDUpdate);
|
||||
}
|
||||
else if (new_state == QuoterState::WAITING_FOR_CANCEL_ACK)
|
||||
{
|
||||
bts_assert(current_state == QuoterState::WAITING_FOR_FIRST_ACK || current_state == QuoterState::WORKING);
|
||||
_order_bridge->cancel_order(_bridge_client_id);
|
||||
}
|
||||
}
|
||||
void SingleLegQuoter::cancel_order()
|
||||
{
|
||||
_event_data.set_cancel_order();
|
||||
_fire_event(_event_data);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_handle_events_in_WAITING_FOR_MARKET(ExternalQuoterEvents ex_ev)
|
||||
{
|
||||
if (ex_ev == ExternalQuoterEvents::OrderStatusNormal ||
|
||||
ex_ev == ExternalQuoterEvents::OrderStatusError ||
|
||||
ex_ev == ExternalQuoterEvents::OrderStatusTerminalFilled ||
|
||||
ex_ev == ExternalQuoterEvents::OrderStatusTerminalUnFilled ||
|
||||
ex_ev == ExternalQuoterEvents::OrderFills)
|
||||
{
|
||||
bts_assert(false);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::UserLegPriceChange ||
|
||||
ex_ev == ExternalQuoterEvents::UserLegCancel)
|
||||
{
|
||||
bts_assert(false);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::OtherLegQuotingFill ||
|
||||
ex_ev == ExternalQuoterEvents::UserSpreadPriceChange ||
|
||||
ex_ev == ExternalQuoterEvents::UserSpreadQuantityChange)
|
||||
{
|
||||
// Changes casued by these 3 events can result
|
||||
// in price/qty changes, which is handled when responding
|
||||
// to the MD event
|
||||
_handle_events_in_WAITING_FOR_MARKET(ExternalQuoterEvents::MDUpdate);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::CancelOrder)
|
||||
{
|
||||
// while the quoter was waiting for the right market conditions
|
||||
// to send an order, the order was cancelled by the above layer
|
||||
// (could be the user, could be the spreader engine itself)
|
||||
_change_state(QuoterState::END);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::MDUpdate)
|
||||
{
|
||||
// this is where it figures if it is ready to send an
|
||||
// order or not
|
||||
if (_price_quantity_state.computed_quantity().value() > 0)
|
||||
_change_state(QuoterState::WAITING_FOR_FIRST_ACK);
|
||||
else
|
||||
{
|
||||
if (_price_quantity_state.remaining_to_fill().value() == 0)
|
||||
{
|
||||
// In this state other fills couSpald keep coming , reducing this leg's quoting qty.
|
||||
// If the other sides have been fully filled, this means this side
|
||||
// will be all hedges, and no need to quote
|
||||
_change_state(QuoterState::END);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**********************************************************************/
|
||||
/************External Event Handling in START**************************/
|
||||
/**********************************************************************/
|
||||
|
||||
void SingleLegQuoter::_handle_events_in_WAITING_FOR_FIRST_ACK(ExternalQuoterEvents ex_ev)
|
||||
{
|
||||
if (ex_ev == ExternalQuoterEvents::OrderStatusNormal)
|
||||
{
|
||||
_change_state(QuoterState::WORKING);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::OrderStatusError || ex_ev == ExternalQuoterEvents::CancelOrder || ex_ev == ExternalQuoterEvents::UserLegCancel)
|
||||
{
|
||||
_change_state(QuoterState::WAITING_FOR_CANCEL_ACK);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::OrderStatusTerminalFilled || ex_ev == ExternalQuoterEvents::OrderStatusTerminalUnFilled)
|
||||
{
|
||||
_change_state(QuoterState::END);
|
||||
}
|
||||
}
|
||||
void SingleLegQuoter::_handle_events_in_START(const EventData&)
|
||||
{
|
||||
// as soon as the class initializes it moves out of the START
|
||||
// state. no events should be received in this state
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_handle_events_in_WAITING_FOR_CANCEL_ACK(ExternalQuoterEvents ex_ev)
|
||||
{
|
||||
if (ex_ev == ExternalQuoterEvents::OrderStatusTerminalFilled || ex_ev == ExternalQuoterEvents::OrderStatusTerminalUnFilled)
|
||||
_change_state(QuoterState::END);
|
||||
}
|
||||
/**********************************************************************/
|
||||
/************External Event Handling in WAITING FOR MARKET*************/
|
||||
/**********************************************************************/
|
||||
|
||||
void SingleLegQuoter::_handle_events_in_WORKING(ExternalQuoterEvents ex_ev)
|
||||
{
|
||||
// This is where bulk of the activity happens
|
||||
if (ex_ev == ExternalQuoterEvents::OrderStatusNormal)
|
||||
{
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::OrderStatusError)
|
||||
{
|
||||
_change_state(QuoterState::WAITING_FOR_CANCEL_ACK);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::OrderStatusTerminalUnFilled)
|
||||
{
|
||||
_change_state(QuoterState::END);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::OrderStatusTerminalFilled)
|
||||
{
|
||||
_change_state(QuoterState::END);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::OtherLegQuotingFill)
|
||||
{
|
||||
/// Since this shoots off a hedge, the
|
||||
_handle_events_in_WORKING(ExternalQuoterEvents::MDUpdate);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::UserSpreadPriceChange)
|
||||
{
|
||||
_handle_events_in_WORKING(ExternalQuoterEvents::MDUpdate);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::UserSpreadQuantityChange)
|
||||
{
|
||||
_handle_events_in_WORKING(ExternalQuoterEvents::MDUpdate);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::UserLegPriceChange)
|
||||
{
|
||||
_handle_events_in_WORKING(ExternalQuoterEvents::MDUpdate);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::UserLegCancel)
|
||||
{
|
||||
_change_state(QuoterState::WAITING_FOR_CANCEL_ACK);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::CancelOrder)
|
||||
{
|
||||
_change_state(QuoterState::WAITING_FOR_CANCEL_ACK);
|
||||
}
|
||||
else if (ex_ev == ExternalQuoterEvents::MDUpdate)
|
||||
{
|
||||
if (_price_quantity_state.computed_quantity().value() == 0)
|
||||
{
|
||||
_change_state(QuoterState::WAITING_FOR_CANCEL_ACK);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_price_quantity_state.out_quantity_synchronized())
|
||||
{
|
||||
_price_quantity_state.synchronize_out_price_quantity();
|
||||
// If there is a quantity change we dont need to check
|
||||
// on the price related quoting parameters, just update
|
||||
// with the latest price and quantity
|
||||
_order_bridge->update_order(_bridge_client_id, _price_quantity_state.out_price(), _price_quantity_state.out_quantity());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_price_quantity_state.out_price_synchronized())
|
||||
{
|
||||
if (_reprice_quote())
|
||||
_order_bridge->update_order(_bridge_client_id, _price_quantity_state.out_price(), _price_quantity_state.out_quantity());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void SingleLegQuoter::_tranform_to_WAITING_FOR_MARKET()
|
||||
{
|
||||
bts_assert(_state == QuoterState::START);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_fire_event(ExternalQuoterEvents ex_ev)
|
||||
{
|
||||
if (_state == QuoterState::WORKING)
|
||||
_handle_events_in_WORKING(ex_ev);
|
||||
else if (_state == QuoterState::WAITING_FOR_MARKET)
|
||||
_handle_events_in_WAITING_FOR_MARKET(ex_ev);
|
||||
else if (_state == QuoterState::WAITING_FOR_FIRST_ACK)
|
||||
_handle_events_in_WAITING_FOR_FIRST_ACK(ex_ev);
|
||||
else if (_state == QuoterState::WAITING_FOR_CANCEL_ACK)
|
||||
_handle_events_in_WAITING_FOR_CANCEL_ACK(ex_ev);
|
||||
}
|
||||
void SingleLegQuoter::_check_md_in_WAITING_FOR_MARKET()
|
||||
{
|
||||
Price price; Quantity quantity;
|
||||
_compute_price_quantity(price, quantity);
|
||||
if (quantity.value() > 0)
|
||||
_tranform_to_WAITING_FOR_FIRST_ACK();
|
||||
}
|
||||
|
||||
bool SingleLegQuoter::_reprice_quote()
|
||||
void SingleLegQuoter::_handle_events_in_WAITING_FOR_MARKET(const EventData& event_data)
|
||||
{
|
||||
if (event_data.external_event == ExternalEvents::OrderStateUpdate)
|
||||
bts_assert(false);
|
||||
else if (event_data.external_event == ExternalEvents::MDUpdate)
|
||||
{
|
||||
_leg_data.at(event_data.book_leg_index).book = event_data.book;
|
||||
_check_md_in_WAITING_FOR_MARKET();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::OtherLegQuotingFill)
|
||||
{
|
||||
_price_quantity_state.add_hedged_reduction(event_data.other_leg_filled_quanity);
|
||||
if (_price_quantity_state.remaining_to_fill().value() == 0)
|
||||
// if the size is changed such that hedge redution has made
|
||||
// the remainder 0, we dont need to quote this leg
|
||||
_transform_to_END();
|
||||
else
|
||||
// evaluating quantity again, because the reduced size requirements
|
||||
// may mean that we have enough size available in the other legs
|
||||
_check_md_in_WAITING_FOR_MARKET();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::SpreadPriceChange)
|
||||
{
|
||||
// a spread price change is not change the depth requirements
|
||||
// in the other legs, so the state is not going to change
|
||||
_price_quantity_state.update_target_spread_price(event_data.new_spread_price);
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::SpreadQuantityChange)
|
||||
{
|
||||
_price_quantity_state.update_target_leg_quantity(event_data.new_leg_quantity);
|
||||
if (_price_quantity_state.remaining_to_fill().value() == 0)
|
||||
// if the size is changed such that hedge redution has made
|
||||
// the remainder 0, we dont need to quote this leg
|
||||
_transform_to_END();
|
||||
else
|
||||
_check_md_in_WAITING_FOR_MARKET();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::CancelOrder)
|
||||
{
|
||||
// no order has been sent for this leg, but the spreader order
|
||||
// is cancelled.
|
||||
_transform_to_END();
|
||||
}
|
||||
}
|
||||
|
||||
/********************************************************************************/
|
||||
/*****************External Event Handling in WAITING_FOR_FIRST_ACK***************/
|
||||
/********************************************************************************/
|
||||
|
||||
void SingleLegQuoter::_tranform_to_WAITING_FOR_FIRST_ACK()
|
||||
{
|
||||
bts_assert(_state == QuoterState::WAITING_FOR_MARKET);
|
||||
_state = QuoterState::WAITING_FOR_FIRST_ACK;
|
||||
bts_assert(_price_quantity_state.aggregated_filled_quantity().value() == 0);
|
||||
bts_assert(_price_quantity_state.out_quantity().value() == 0);
|
||||
Price price; Quantity quantity;
|
||||
_compute_price_quantity(price, quantity);
|
||||
bts_assert(quantity.value() > 0);
|
||||
_price_quantity_state.update_out_price_quantity(price, quantity);
|
||||
// This is the first and only time a quoting order is sent
|
||||
_order_bridge->send_limit_order(_bridge_client_id, _price_quantity_state.out_price(), _price_quantity_state.out_quantity());
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_handle_events_in_WAITING_FOR_FIRST_ACK(const EventData& event_data)
|
||||
{
|
||||
if (event_data.external_event == ExternalEvents::OrderStateUpdate)
|
||||
{
|
||||
OrderState& order_state = event_data.order_state;
|
||||
bool filled, terminal_state, encountered_error, new_fills;
|
||||
SpreaderUtils::parse_order(order_state, _price_quantity_state.aggregated_filled_quantity(), filled, terminal_state, encountered_error, new_fills);
|
||||
_price_quantity_state.update_filled_quantity(order_state.filled_quantity);
|
||||
if (terminal_state)
|
||||
_transform_to_END();
|
||||
else if (encountered_error)
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
else
|
||||
{
|
||||
Price price_change = _price_quantity_state.change_in_computed_price();
|
||||
// inside_market_limit and outside_market_limit can be cached
|
||||
double inside_market_limit = _quote_params.quote_mute_zone().inside_ticks() * _tick_size,
|
||||
outside_market_limit = _quote_params.quote_mute_zone().outside_ticks() * _tick_size;
|
||||
/// TODO, when price is not ni multiples of ticks
|
||||
/// TODO, check for negative values
|
||||
if (price_change.value() != 0)
|
||||
{
|
||||
if (_quote_side == api::BuySell::BUY)
|
||||
{
|
||||
if (price_change.value() > 0)
|
||||
return price_change.value() > inside_market_limit;
|
||||
else
|
||||
return fabs(price_change.value()) > outside_market_limit;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (price_change.value() < 0)
|
||||
return fabs(price_change.value()) > inside_market_limit;
|
||||
else
|
||||
return price_change.value() > outside_market_limit;
|
||||
}
|
||||
}
|
||||
if (_price_quantity_state.remaining_to_fill().value() == 0)
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
else
|
||||
return false;
|
||||
// this is the working state
|
||||
_transform_to_WORKING();
|
||||
}
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::MDUpdate)
|
||||
{
|
||||
// nothing to do, we just wait for a working order status
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::OtherLegQuotingFill)
|
||||
{
|
||||
_price_quantity_state.add_hedged_reduction(event_data.other_leg_filled_quanity);
|
||||
if (_price_quantity_state.remaining_to_fill().value() == 0)
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::SpreadPriceChange)
|
||||
{
|
||||
// price changes are not allowed in this state, accept_modifications
|
||||
// shoudl have returned false and disallowed this call to happen
|
||||
bts_assert(false);
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::SpreadQuantityChange)
|
||||
{
|
||||
// accept_modifications() should reject all changes
|
||||
bts_assert(false);
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::CancelOrder)
|
||||
{
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_recompute_and_set_price_quantity()
|
||||
/********************************************************************************/
|
||||
/**********************External Event Handling in WORKING************************/
|
||||
/********************************************************************************/
|
||||
|
||||
void SingleLegQuoter::_transform_to_WORKING()
|
||||
{
|
||||
bts_assert(_state == QuoterState::WAITING_FOR_FIRST_ACK);
|
||||
_state = QuoterState::WORKING;
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_reprice_in_WORKING()
|
||||
{
|
||||
Price new_price; Quantity new_quantity;
|
||||
_compute_price_quantity(new_price, new_quantity);
|
||||
if (new_quantity.value() == 0)
|
||||
// the other legs dried up, so the quanity gets
|
||||
// computed to be 0
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
else
|
||||
{
|
||||
// adjusted for the fills, since _compute_price_quantity
|
||||
// is computed on the remainder
|
||||
new_quantity = Quantity(new_quantity.value() + _price_quantity_state.aggregated_filled_quantity().value());
|
||||
if (new_quantity.value() != _price_quantity_state.out_quantity().value())
|
||||
{
|
||||
size_t idx = 0, min_quoter_leg_quantity_index = 0;
|
||||
for (auto it = _leg_data.begin(); it != _leg_data.end(); it++)
|
||||
{
|
||||
if (idx != _this_leg_index)
|
||||
{
|
||||
const Book& leg_book = it->book;
|
||||
Price leg_price; Quantity leg_quantity, quoter_leg_quantity;
|
||||
Quantity leg_target_quantity = _price_quantity_state.remaining_to_fill();
|
||||
_traverse_depth_for_quantity(leg_book, api::BuySell::BUY, Quantity(static_cast<uint32_t>(leg_target_quantity.value() * it->lean_ratio)), leg_price, leg_quantity);
|
||||
quoter_leg_quantity = Quantity(static_cast<uint32_t>(it->lean_ratio * leg_quantity.value()));
|
||||
PQPair& leg_price_quantity = _aux_comp_data.at(idx);
|
||||
leg_price_quantity.set(leg_price, leg_quantity, quoter_leg_quantity);
|
||||
if (_aux_comp_data.at(min_quoter_leg_quantity_index).quoter_leg_quantity.value() <= quoter_leg_quantity.value())
|
||||
min_quoter_leg_quantity_index = idx;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
Quantity computed_quantity = _aux_comp_data.at(min_quoter_leg_quantity_index).quoter_leg_quantity;
|
||||
Price computed_price = _compute_leg_price(_aux_comp_data);
|
||||
_price_quantity_state.update_computed_price_quantity(computed_price, computed_quantity);
|
||||
_price_quantity_state.update_out_price_quantity(new_price, new_quantity);
|
||||
_order_bridge->update_order(_bridge_client_id, new_price, new_quantity);
|
||||
}
|
||||
|
||||
Price SingleLegQuoter::_compute_leg_price(const std::vector<PQPair>& leg_values)
|
||||
else
|
||||
{
|
||||
size_t idx = 0;
|
||||
double sum = 0;
|
||||
for (auto it = leg_values.begin(); it != leg_values.end(); it++)
|
||||
bool reprice = true;
|
||||
if (_quote_params.has_quote_mute_zone())
|
||||
reprice = SpreaderUtils::reprice_quote_mute_zone(_price_quantity_state.out_price().value(), new_price.value(), _quote_side, _tick_size, _quote_params.quote_mute_zone());
|
||||
if (reprice)
|
||||
{
|
||||
if (idx != _this_leg_index)
|
||||
{
|
||||
LegData& leg_data = _leg_data.at(idx);
|
||||
sum += it->leg_price.value() * leg_data.price_ratio();
|
||||
}
|
||||
_price_quantity_state.update_out_price_quantity(new_price, new_quantity);
|
||||
_order_bridge->update_order(_bridge_client_id, new_price, new_quantity);
|
||||
}
|
||||
double rv = _price_quantity_state.target_spread_price().value() - sum; /// TODO, needds a divisor
|
||||
return Price(rv);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_traverse_depth_for_quantity(const Book& leg_book, const api::BuySell buy_sell, Quantity target_quantity,
|
||||
Price& out_price, Quantity& out_quantity)
|
||||
{
|
||||
double out_price_d = 0.0;
|
||||
uint32_t out_quantity_u = 0;
|
||||
const size_t n_levels = leg_book.n_levels();
|
||||
for (size_t level = 0; level < n_levels; level++)
|
||||
{
|
||||
auto level_price_qty = leg_book.level(buy_sell, level);
|
||||
Quantity level_qty = std::get<1>(level_price_qty);
|
||||
out_price = std::get<0>(level_price_qty);
|
||||
out_quantity_u += level_qty.value();
|
||||
if (out_quantity_u >= target_quantity.value())
|
||||
{
|
||||
out_quantity_u = target_quantity.value();
|
||||
break;
|
||||
}
|
||||
if (level_qty.value() == 0)
|
||||
break;
|
||||
}
|
||||
out_price = Price(out_price_d);
|
||||
out_quantity = Quantity(out_quantity_u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_handle_events_in_WORKING(const EventData& event_data)
|
||||
{
|
||||
if (event_data.external_event == ExternalEvents::OrderStateUpdate)
|
||||
{
|
||||
const OrderState& order_state = event_data.order_state;
|
||||
bool filled, terminal_state, encountered_error, new_fills;
|
||||
SpreaderUtils::parse_order(order_state, _price_quantity_state.aggregated_filled_quantity(), filled, terminal_state, encountered_error, new_fills);
|
||||
_price_quantity_state.update_filled_quantity(order_state.filled_quantity);
|
||||
if (terminal_state)
|
||||
_transform_to_END();
|
||||
else if (encountered_error)
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
else
|
||||
{
|
||||
// normal state
|
||||
if (new_fills)
|
||||
{
|
||||
if (_price_quantity_state.remaining_to_fill().value() == 0)
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
else
|
||||
_reprice_in_WORKING();
|
||||
}
|
||||
else
|
||||
{
|
||||
// this is the normal state
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::MDUpdate)
|
||||
{
|
||||
_reprice_in_WORKING();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::OtherLegQuotingFill)
|
||||
{
|
||||
_price_quantity_state.add_hedged_reduction(event_data.other_leg_filled_quanity);
|
||||
if (_price_quantity_state.remaining_to_fill().value() == 0)
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
else
|
||||
_reprice_in_WORKING();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::SpreadPriceChange)
|
||||
{
|
||||
_price_quantity_state.update_target_spread_price(event_data.new_spread_price);
|
||||
_reprice_in_WORKING();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::SpreadQuantityChange)
|
||||
{
|
||||
_price_quantity_state.update_target_leg_quantity(event_data.new_leg_quantity);
|
||||
if (_price_quantity_state.remaining_to_fill().value() == 0)
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
else
|
||||
_reprice_in_WORKING();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::CancelOrder)
|
||||
{
|
||||
_transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
}
|
||||
}
|
||||
|
||||
/********************************************************************************/
|
||||
/*****************External Event Handling in WAITING_FOR_CANCEL_ACK**************/
|
||||
/********************************************************************************/
|
||||
|
||||
void SingleLegQuoter::_transform_to_WAITING_FOR_CANCEL_ACK()
|
||||
{
|
||||
bts_assert(_state == QuoterState::WAITING_FOR_FIRST_ACK || _state == QuoterState::WORKING);
|
||||
_state = QuoterState::WAITING_FOR_CANCEL_ACK;
|
||||
// this is only place where the order is cancelled
|
||||
_order_bridge->cancel_order(_bridge_client_id);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_handle_events_in_WAITING_FOR_CANCEL_ACK(const EventData& event_data)
|
||||
{
|
||||
if (event_data.external_event == ExternalEvents::OrderStateUpdate)
|
||||
{
|
||||
const OrderState& order_state = event_data.order_state;
|
||||
bool filled, terminal_state, encountered_error, new_fills;
|
||||
SpreaderUtils::parse_order(order_state, _price_quantity_state.aggregated_filled_quantity(), filled, terminal_state, encountered_error, new_fills);
|
||||
_price_quantity_state.update_filled_quantity(order_state.filled_quantity);
|
||||
if (terminal_state)
|
||||
_transform_to_END();
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::MDUpdate)
|
||||
{
|
||||
// nothing to do, we just wait for a cancel ack
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::OtherLegQuotingFill)
|
||||
{
|
||||
_price_quantity_state.add_hedged_reduction(event_data.other_leg_filled_quanity);
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::SpreadPriceChange)
|
||||
{
|
||||
// price changes are not allowed in this state, accept_modifications
|
||||
// shoudl have returned false and disallowed this call to happen
|
||||
bts_assert(false);
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::SpreadQuantityChange)
|
||||
{
|
||||
// accept_modifications() should reject all changes
|
||||
bts_assert(false);
|
||||
}
|
||||
else if (event_data.external_event == ExternalEvents::CancelOrder)
|
||||
{
|
||||
// already cancelled
|
||||
}
|
||||
}
|
||||
|
||||
/********************************************************************************/
|
||||
/************************External Event Handling in END**************************/
|
||||
/********************************************************************************/
|
||||
|
||||
void SingleLegQuoter::_transform_to_END()
|
||||
{
|
||||
bts_assert(_state == QuoterState::WAITING_FOR_FIRST_ACK || _state == QuoterState::WORKING ||
|
||||
_state == QuoterState::WAITING_FOR_CANCEL_ACK);
|
||||
_state = QuoterState::END;
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_handle_events_in_END(const EventData & event_data)
|
||||
{
|
||||
if (event_data.external_event == ExternalEvents::OrderStateUpdate)
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_fire_event(const EventData& event_data)
|
||||
{
|
||||
if (_state == QuoterState::WORKING)
|
||||
_handle_events_in_WORKING(event_data);
|
||||
else if (_state == QuoterState::START)
|
||||
_handle_events_in_START(event_data);
|
||||
else if (_state == QuoterState::WAITING_FOR_MARKET)
|
||||
_handle_events_in_WAITING_FOR_MARKET(event_data);
|
||||
else if (_state == QuoterState::WAITING_FOR_FIRST_ACK)
|
||||
_handle_events_in_WAITING_FOR_FIRST_ACK(event_data);
|
||||
else if (_state == QuoterState::WAITING_FOR_CANCEL_ACK)
|
||||
_handle_events_in_WAITING_FOR_CANCEL_ACK(event_data);
|
||||
}
|
||||
|
||||
void SingleLegQuoter::_compute_price_quantity(Price& price, Quantity& quantity)
|
||||
{
|
||||
size_t idx = 0, min_quoter_leg_quantity_index = 0;
|
||||
for (auto it = _leg_data.begin(); it != _leg_data.end(); it++)
|
||||
{
|
||||
if (idx != _this_leg_index)
|
||||
{
|
||||
const Book& leg_book = it->book;
|
||||
Price leg_price; Quantity leg_quantity, quoter_leg_quantity;
|
||||
Quantity leg_target_quantity = _price_quantity_state.remaining_to_fill();
|
||||
SpreaderUtils::traverse_depth_for_quantity(leg_book, api::BuySell::BUY, Quantity(static_cast<uint32_t>(leg_target_quantity.value() * it->lean_ratio)), leg_price, leg_quantity);
|
||||
quoter_leg_quantity = Quantity(static_cast<uint32_t>(it->lean_ratio * leg_quantity.value()));
|
||||
PQPair& leg_price_quantity = _aux_comp_data.at(idx);
|
||||
leg_price_quantity.set(leg_price, leg_quantity, quoter_leg_quantity);
|
||||
if (_aux_comp_data.at(min_quoter_leg_quantity_index).quoter_leg_quantity.value() <= quoter_leg_quantity.value())
|
||||
min_quoter_leg_quantity_index = idx;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
quantity = _aux_comp_data.at(min_quoter_leg_quantity_index).quoter_leg_quantity;
|
||||
price = _compute_leg_price(_aux_comp_data);
|
||||
}
|
||||
|
||||
Price SingleLegQuoter::_compute_leg_price(const std::vector<PQPair>& leg_values)
|
||||
{
|
||||
size_t idx = 0;
|
||||
double sum = 0;
|
||||
for (auto it = leg_values.begin(); it != leg_values.end(); it++)
|
||||
{
|
||||
if (idx != _this_leg_index)
|
||||
{
|
||||
LegData& leg_data = _leg_data.at(idx);
|
||||
sum += it->leg_price.value() * leg_data.price_ratio();
|
||||
}
|
||||
}
|
||||
double rv = _price_quantity_state.target_spread_price().value() - sum; /// TODO, needs a divisor
|
||||
return Price(rv);
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -5,237 +5,261 @@
|
||||
#include "QuoteLegPriceQuantityState.h"
|
||||
#include <boost/container/flat_map.hpp>
|
||||
|
||||
namespace bts
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
|
||||
class ISpreaderInternalOrderBridge;
|
||||
|
||||
|
||||
class SingleLegQuoter
|
||||
{
|
||||
namespace spreader
|
||||
{
|
||||
class ISpreaderInternalOrderBridge;
|
||||
// this represents all signals generated
|
||||
// esternally that the quoter is interested in
|
||||
enum class ExternalEvents
|
||||
{
|
||||
// order update could be status updates or fills
|
||||
OrderStateUpdate,
|
||||
// market data update from the exchange
|
||||
MDUpdate,
|
||||
// fill on another leg
|
||||
OtherLegQuotingFill,
|
||||
// spread price changes (manually from the user
|
||||
// but at some point it could be automatic too,
|
||||
// though the source is irrelevant)
|
||||
SpreadPriceChange,
|
||||
// quantity changes (from the user)
|
||||
SpreadQuantityChange,
|
||||
// cancel of this leg order (automatic or manual)
|
||||
CancelOrder,
|
||||
// leg price changes from the user
|
||||
LegPriceChange,
|
||||
};
|
||||
|
||||
enum class ExternalQuoterEvents
|
||||
// the states of the quoter state machine
|
||||
enum class QuoterState
|
||||
{
|
||||
// start state. there is a transient state, no
|
||||
// event processing happens in this state. as soon
|
||||
// as the quoter sees the init, it goes to
|
||||
// waiting_for_market
|
||||
START = 0,
|
||||
// start goes to waiting_for_market as soon as the
|
||||
// quoter is initialized. here it waits for the
|
||||
// market data in the other legs to be available
|
||||
// so that it can price itself (it's leg), and move
|
||||
// to waiting_for_first_ack
|
||||
WAITING_FOR_MARKET,
|
||||
// the transition to this state is accompanied by
|
||||
// submitting an order. it waits here till it gets
|
||||
// an order ack (of any kind) and moves to working
|
||||
WAITING_FOR_FIRST_ACK,
|
||||
// this is the main working state
|
||||
WORKING,
|
||||
// transition to this state is accompanied by cancelling
|
||||
// the order. it waits in this state till it gets an
|
||||
// order state signalling a terminal state
|
||||
WAITING_FOR_CANCEL_ACK,
|
||||
// there are no working orders in this state.
|
||||
END
|
||||
};
|
||||
|
||||
// the external events and data are packaged into
|
||||
// this class and sent over to the state machine.
|
||||
// depending on the value of the external_event,
|
||||
// different parts of this class are lightened up
|
||||
// (not very pretty, maybe std variant could be used)
|
||||
class EventData
|
||||
{
|
||||
public:
|
||||
ExternalEvents external_event;
|
||||
OrderState& order_state;
|
||||
size_t book_leg_index;
|
||||
Book& book;
|
||||
Price new_spread_price;
|
||||
Quantity new_leg_quantity;
|
||||
Quantity other_leg_filled_quanity;
|
||||
|
||||
EventData()
|
||||
: order_state (*(new OrderState())),
|
||||
book (*(new Book()))
|
||||
{
|
||||
/// Normal status event from the exchange
|
||||
OrderStatusNormal = 0,
|
||||
/// Error status event from the exchage (this could be a temporary error, however the state machine will cxl the order)
|
||||
OrderStatusError,
|
||||
/// When the order is completely filled (end event, should not expect anything else after that)
|
||||
OrderStatusTerminalFilled,
|
||||
/// When the order is cancelled (end event, should not expect anything else after that)
|
||||
OrderStatusTerminalUnFilled,
|
||||
/// Fills reported by the exchange
|
||||
OrderFills,
|
||||
/// Market data update from the exchange
|
||||
MDUpdate,
|
||||
/// One of the other quoting legs in the spread got filled (not a hedge fill)
|
||||
OtherLegQuotingFill,
|
||||
/// User changes the price of the spread order
|
||||
UserSpreadPriceChange,
|
||||
/// User changes the quantity of the spread order
|
||||
UserSpreadQuantityChange,
|
||||
/// User changes the price of this leg
|
||||
UserLegPriceChange,
|
||||
/// User cancels this leg
|
||||
UserLegCancel,
|
||||
/// Either the user or the spreader cancels the order
|
||||
CancelOrder
|
||||
};
|
||||
enum class QuoterState
|
||||
external_event = ExternalEvents::MDUpdate;
|
||||
}
|
||||
|
||||
void set_order_event(const OrderState& order_state_arg)
|
||||
{
|
||||
START = 0,
|
||||
WAITING_FOR_MARKET,
|
||||
WAITING_FOR_FIRST_ACK,
|
||||
WORKING,
|
||||
WAITING_FOR_CANCEL_ACK,
|
||||
END
|
||||
};
|
||||
external_event = ExternalEvents::OrderStateUpdate;
|
||||
order_state = order_state_arg;
|
||||
}
|
||||
|
||||
class SingleLegQuoter
|
||||
{
|
||||
struct PQPair
|
||||
{
|
||||
public:
|
||||
Price leg_price;
|
||||
Quantity leg_quantity;
|
||||
Quantity quoter_leg_quantity;
|
||||
PQPair()
|
||||
: leg_price(0.0),
|
||||
leg_quantity(0),
|
||||
quoter_leg_quantity (0)
|
||||
{}
|
||||
void set(Price new_price, Quantity new_quantity, Quantity new_quoter_leg_quantity)
|
||||
{
|
||||
leg_price = new_price;
|
||||
leg_quantity = new_quantity;
|
||||
quoter_leg_quantity = new_quoter_leg_quantity;
|
||||
}
|
||||
};
|
||||
struct LegData
|
||||
{
|
||||
public:
|
||||
const bts::api::ContractLeg& contract;
|
||||
const bts::spark::LegSpreaderParams& params;
|
||||
Book& book;
|
||||
// Lean Ratio of this leg (l2) wrt to the Quoter's Leg (l1)
|
||||
// In other words, to price 1 lot of l1, we have to look
|
||||
// into to l2's book with a target qty of lean ratio.
|
||||
const double lean_ratio;
|
||||
const double equivelant_quoter_leg;
|
||||
void set_md_event(size_t leg_index_arg, const Book& book_arg)
|
||||
{
|
||||
external_event = ExternalEvents::MDUpdate;
|
||||
book_leg_index = leg_index_arg;
|
||||
book = book_arg;
|
||||
}
|
||||
|
||||
LegData(bts::api::ContractLeg& leg_contract,
|
||||
bts::spark::LegSpreaderParams& leg_params,
|
||||
Book& leg_book,
|
||||
double leg_lean_ratio)
|
||||
: contract (leg_contract),
|
||||
params (leg_params),
|
||||
book (leg_book),
|
||||
lean_ratio (leg_lean_ratio),
|
||||
equivelant_quoter_leg (1.0/leg_lean_ratio)
|
||||
{}
|
||||
void set_other_leg_quoting_fill(Quantity other_leg_filled_quanity_arg)
|
||||
{
|
||||
external_event = ExternalEvents::OtherLegQuotingFill;
|
||||
other_leg_filled_quanity = other_leg_filled_quanity_arg;
|
||||
}
|
||||
|
||||
double price_ratio() { return contract.price_multiplier(); }
|
||||
};
|
||||
private:
|
||||
const double _tick_size;
|
||||
const double _size_ratio;
|
||||
const bts::api::Security& _spread_security;
|
||||
const bts::api::ContractLeg& _contract_leg;
|
||||
ISpreaderInternalOrderBridge* _order_bridge;
|
||||
const int _bridge_client_id;
|
||||
const api::BuySell _side;
|
||||
const api::BuySell _quote_side;
|
||||
const size_t _this_leg_index;
|
||||
const bts::spark::QuoteParams& _quote_params;
|
||||
boost::container::flat_map<uint32_t, size_t> _key_index_map;
|
||||
std::vector<LegData> _leg_data;
|
||||
std::vector<PQPair> _aux_comp_data;
|
||||
QuoterState _state;
|
||||
QuoteLegPriceQuantityState _price_quantity_state;
|
||||
public:
|
||||
SingleLegQuoter(ISpreaderInternalOrderBridge* order_bridge,
|
||||
api::Security const& spread_security,
|
||||
bts::api::ContractLeg const& this_leg,
|
||||
const bts::spark::ExecutionStrategy& execution_strategy,
|
||||
bts::Price spread_order_price,
|
||||
bts::Quantity spread_order_quantity,
|
||||
api::BuySell side);
|
||||
|
||||
~SingleLegQuoter()
|
||||
{
|
||||
//_price_quantity_state.release();
|
||||
}
|
||||
void set_new_spread_price(Price new_spread_price_arg)
|
||||
{
|
||||
external_event = ExternalEvents::SpreadPriceChange;
|
||||
new_spread_price = new_spread_price_arg;
|
||||
}
|
||||
|
||||
bool is_active();
|
||||
void set_new_leg_quantity(Quantity new_leg_quantity_arg)
|
||||
{
|
||||
external_event = ExternalEvents::SpreadQuantityChange;
|
||||
new_leg_quantity = new_leg_quantity_arg;
|
||||
}
|
||||
|
||||
void update_order_status(OrderState const& state, std::vector<ChildFill> const& fills);
|
||||
void set_cancel_order()
|
||||
{
|
||||
external_event = ExternalEvents::CancelOrder;
|
||||
}
|
||||
};
|
||||
|
||||
void update_md(api::Security const&s, Book const&b);
|
||||
|
||||
void update_other_leg_fills(api::Security const& other_leg_security, Quantity filled_quantity, Price filled_price);
|
||||
struct PQPair
|
||||
{
|
||||
public:
|
||||
Price leg_price;
|
||||
Quantity leg_quantity;
|
||||
Quantity quoter_leg_quantity;
|
||||
PQPair()
|
||||
: leg_price(0.0),
|
||||
leg_quantity(0),
|
||||
quoter_leg_quantity(0)
|
||||
{}
|
||||
void set(Price new_price, Quantity new_quantity, Quantity new_quoter_leg_quantity)
|
||||
{
|
||||
leg_price = new_price;
|
||||
leg_quantity = new_quantity;
|
||||
quoter_leg_quantity = new_quoter_leg_quantity;
|
||||
}
|
||||
};
|
||||
|
||||
void modify_order_price(Price new_spread_price);
|
||||
struct LegData
|
||||
{
|
||||
public:
|
||||
const bts::api::ContractLeg& contract;
|
||||
const bts::spark::LegSpreaderParams& params;
|
||||
Book& book;
|
||||
// lean ratio of this leg (l2) wrt to the quoter's leg (l1).
|
||||
// in other words, to price 1 lot of l1, we have to look
|
||||
// into to l2's book with a target qty of lean ratio.
|
||||
const double lean_ratio;
|
||||
const double equivelant_quoter_leg;
|
||||
|
||||
void modify_order_quantity(Quantity new_spread_quantity);
|
||||
LegData(bts::api::ContractLeg& leg_contract,
|
||||
bts::spark::LegSpreaderParams& leg_params,
|
||||
Book& leg_book,
|
||||
double leg_lean_ratio)
|
||||
: contract(leg_contract),
|
||||
params(leg_params),
|
||||
book(leg_book),
|
||||
lean_ratio(leg_lean_ratio),
|
||||
equivelant_quoter_leg(1.0 / leg_lean_ratio)
|
||||
{}
|
||||
|
||||
void cancel_order();
|
||||
private:
|
||||
void _change_state(QuoterState new_state);
|
||||
double price_ratio() { return contract.price_multiplier(); }
|
||||
};
|
||||
|
||||
void _handle_events_in_WAITING_FOR_MARKET(ExternalQuoterEvents external_event);
|
||||
void _handle_events_in_WAITING_FOR_FIRST_ACK(ExternalQuoterEvents external_event);
|
||||
void _handle_events_in_WAITING_FOR_CANCEL_ACK(ExternalQuoterEvents external_event);
|
||||
void _handle_events_in_WORKING(ExternalQuoterEvents external_event);
|
||||
private:
|
||||
const double _tick_size;
|
||||
const double _size_ratio;
|
||||
const bts::api::Security& _spread_security;
|
||||
const bts::api::ContractLeg& _contract_leg;
|
||||
ISpreaderInternalOrderBridge* _order_bridge;
|
||||
const int _bridge_client_id;
|
||||
const api::BuySell _side;
|
||||
const api::BuySell _quote_side;
|
||||
const size_t _this_leg_index;
|
||||
const bts::spark::QuoteParams& _quote_params;
|
||||
boost::container::flat_map<uint32_t, size_t> _key_index_map;
|
||||
std::vector<LegData> _leg_data;
|
||||
std::vector<PQPair> _aux_comp_data;
|
||||
EventData _event_data;
|
||||
QuoterState _state;
|
||||
QuoteLegPriceQuantityState _price_quantity_state;
|
||||
public:
|
||||
SingleLegQuoter(ISpreaderInternalOrderBridge* order_bridge,
|
||||
api::Security const& spread_security,
|
||||
bts::api::ContractLeg const& this_leg,
|
||||
const bts::spark::ExecutionStrategy& execution_strategy,
|
||||
bts::Price spread_order_price,
|
||||
bts::Quantity spread_order_quantity,
|
||||
api::BuySell side);
|
||||
|
||||
double _get_equivelant_quantity(const api::Security&, Quantity)
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
uint32_t _aggregate_fills(const std::vector<ChildFill>& fills)
|
||||
{
|
||||
uint32_t rv = 0;
|
||||
for (std::vector<ChildFill>::const_iterator it = fills.begin(); it != fills.end(); ++it)
|
||||
rv += it->quantity.value();
|
||||
return rv;
|
||||
}
|
||||
|
||||
// This function traverses the book for the security at the specified index
|
||||
// for a quantity specified in the target quantity. T
|
||||
void static _traverse_depth_for_quantity(const Book& leg_book, const api::BuySell buy_sell, Quantity target_quantity,
|
||||
Price& out_price, Quantity& effective_quantity);
|
||||
|
||||
void _fire_event(ExternalQuoterEvents ex_ev);
|
||||
|
||||
// This computes the values of price and quantity and sets
|
||||
// it to _price_quantity_state's computed
|
||||
void _recompute_and_set_price_quantity();
|
||||
|
||||
Price _compute_leg_price(const std::vector<PQPair>& leg_values);
|
||||
|
||||
// Applies the Quoting Parameters to see if quote
|
||||
// needs to be repriced
|
||||
bool _reprice_quote();
|
||||
|
||||
bool _get_index(uint32_t sec_key, size_t& out_index)
|
||||
{
|
||||
auto it =_key_index_map.find(sec_key);
|
||||
if (it == _key_index_map.end())
|
||||
return false;
|
||||
else
|
||||
{
|
||||
out_index = it->second;
|
||||
bts_assert(out_index < _leg_data.size());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
api::BuySell static _get_quote_side(api::BuySell spread_side, bts::api::ContractLeg const& quote_leg)
|
||||
{
|
||||
if (spread_side == api::BuySell::BUY)
|
||||
return quote_leg.side();
|
||||
else
|
||||
return _invert_side(quote_leg.side());
|
||||
}
|
||||
|
||||
api::BuySell static _invert_side(api::BuySell side)
|
||||
{
|
||||
return side == api::BuySell::BUY ? api::BuySell::SELL : api::BuySell::BUY;
|
||||
}
|
||||
|
||||
const static bts::spark::QuoteParams& _get_quote_params(const bts::spark::ExecutionStrategy& execution_strategy,
|
||||
const bts::api::ContractLeg& this_leg)
|
||||
{
|
||||
auto it = execution_strategy.leg_params_map().find(this_leg.security().id());
|
||||
bts_assert(it != execution_strategy.leg_params_map().end());
|
||||
const bts::spark::QuoteParams& rv = it->second.quote_params();
|
||||
return rv;
|
||||
}
|
||||
|
||||
size_t static _get_leg_index(api::Security const& spread_security, bts::api::ContractLeg const& this_leg)
|
||||
{
|
||||
size_t rv_idx = 0;
|
||||
for (auto it = spread_security.synthetic().legs().begin(); it != spread_security.synthetic().legs().end(); it++)
|
||||
{
|
||||
if (it->security().id() == this_leg.security().id())
|
||||
return rv_idx;
|
||||
rv_idx++;
|
||||
}
|
||||
bts_assert(false);
|
||||
}
|
||||
|
||||
ExternalQuoterEvents _parse_order_status(const OrderState& state)
|
||||
{
|
||||
ExternalQuoterEvents rv = ExternalQuoterEvents::OrderStatusNormal;
|
||||
// Terminal states
|
||||
if (state.status == api::orderman::OrderStatus::OS_CANCELED ||
|
||||
state.status == api::orderman::OrderStatus::OS_DONE ||
|
||||
state.status == api::orderman::OrderStatus::OS_EXPIRED)
|
||||
rv = ExternalQuoterEvents::OrderStatusTerminalUnFilled;
|
||||
else if (state.status == api::orderman::OrderStatus::OS_FILLED)
|
||||
rv = ExternalQuoterEvents::OrderStatusTerminalFilled;
|
||||
else if (!state.error_msg.empty())
|
||||
rv = ExternalQuoterEvents::OrderStatusError;
|
||||
return rv;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
~SingleLegQuoter()
|
||||
{
|
||||
}
|
||||
// true if the quoter has an active working order. the order can
|
||||
// be in an unknown state, but as long as an order has been sent
|
||||
// and it has not reached a terminal state, its considered "active"
|
||||
bool is_active();
|
||||
// the quoter returns true if it is in a state where orders
|
||||
// can be modified
|
||||
bool accept_modifications();
|
||||
// called by the spreader a change in order status (or fills) is
|
||||
// reported by the exchange
|
||||
void update_order_status(OrderState const& state);
|
||||
// called when the market data associated
|
||||
// with this security is changed
|
||||
void update_md(api::Security const&s, Book const&b);
|
||||
// this is called by the spreader when a quoting fill
|
||||
// (ie not hedging fill) happens on one of the quoting
|
||||
// legs
|
||||
void update_other_leg_fills(api::Security const& other_leg_security, Quantity filled_quantity, Price filled_price);
|
||||
// called when the user modified the spreader order price
|
||||
void modify_order_price(Price new_spread_price);
|
||||
// called when the user modifies the quantity
|
||||
void modify_order_quantity(Quantity new_spread_quantity);
|
||||
// called when the hedge order is cancelled, either
|
||||
// manually or automatically
|
||||
void cancel_order();
|
||||
private:
|
||||
/********************External Event Handling in START***********************/
|
||||
void _handle_events_in_START(const EventData& event_data);
|
||||
/*************External Event Handling in WAITING_FOR_MARKET*****************/
|
||||
void _tranform_to_WAITING_FOR_MARKET();
|
||||
// using the market of the other legs, computes the
|
||||
// quoting price and quantity, and if the quantity > 0
|
||||
// then it transforms to waiting_for_first_ack
|
||||
void _check_md_in_WAITING_FOR_MARKET();
|
||||
void _handle_events_in_WAITING_FOR_MARKET(const EventData& event_data);
|
||||
/*************External Event Handling in WAITING_FOR_FIRST_ACK*****************/
|
||||
void _tranform_to_WAITING_FOR_FIRST_ACK();
|
||||
void _handle_events_in_WAITING_FOR_FIRST_ACK(const EventData& event_data);
|
||||
/*******************External Event Handling in WORKING*************************/
|
||||
void _transform_to_WORKING();
|
||||
void _reprice_in_WORKING();
|
||||
void _handle_events_in_WORKING(const EventData& event_data);
|
||||
/************External Event Handling in WAITING_FOR_CANCEL_ACK*****************/
|
||||
void _transform_to_WAITING_FOR_CANCEL_ACK();
|
||||
void _handle_events_in_WAITING_FOR_CANCEL_ACK(const EventData& event_data);
|
||||
/**********************External Event Handling in END**************************/
|
||||
void _transform_to_END();
|
||||
void _handle_events_in_END(const EventData& event_data);
|
||||
/*******************************************************************************/
|
||||
// based on the state, calles the appropriate
|
||||
// function to handle the event data
|
||||
void _fire_event(const EventData& event_data);
|
||||
// using the size ratios returns the size of this leg, corresponding
|
||||
// to the size of an other leg.
|
||||
double _get_equivelant_quantity(size_t leg_index, Quantity quantity)
|
||||
{
|
||||
bts_assert(leg_index < _leg_data.size());
|
||||
const LegData& leg_data = _leg_data.at(leg_index);
|
||||
const LegData& this_leg_data = _leg_data.at(_this_leg_index);
|
||||
return static_cast<double> (quantity.value() * this_leg_data.contract.quantity() / leg_data.contract.quantity());
|
||||
}
|
||||
// using the market of the other legs, computes the
|
||||
// quoting price and quantity, and if the quantity > 0
|
||||
void _compute_price_quantity(Price& price, Quantity& quantity);
|
||||
Price _compute_leg_price(const std::vector<PQPair>& leg_values);
|
||||
};
|
||||
}}
|
||||
@@ -7,6 +7,9 @@
|
||||
|
||||
namespace bts {
|
||||
namespace spreader {
|
||||
|
||||
// collection of helper functions used by the quoter, hedger
|
||||
// and the spreader glue
|
||||
class SpreaderUtils
|
||||
{
|
||||
public:
|
||||
@@ -19,11 +22,57 @@ public:
|
||||
const bts::spark::LegSpreaderParams& rv = it->second;
|
||||
return rv;
|
||||
}
|
||||
|
||||
// based on the side of the spreader order, returns the
|
||||
// side of the leg
|
||||
api::BuySell static get_leg_side(api::BuySell spreader_order_side, bts::api::ContractLeg const& quote_leg)
|
||||
{
|
||||
if (spreader_order_side == api::BuySell::BUY)
|
||||
return quote_leg.side();
|
||||
else
|
||||
return invert_side(quote_leg.side());
|
||||
}
|
||||
// finds the index of a security in an key->index map
|
||||
// (
|
||||
bool static find_index(const boost::container::flat_map<uint32_t, size_t>& key_to_index_map, uint32_t sec_key,
|
||||
size_t& out_index)
|
||||
{
|
||||
auto it = key_to_index_map.find(sec_key);
|
||||
if (it == key_to_index_map.end())
|
||||
return false;
|
||||
else
|
||||
{
|
||||
out_index = it->second;
|
||||
bts_assert_d(out_index < key_to_index_map.size());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// inverts the buy/sell side
|
||||
api::BuySell static invert_side(api::BuySell side)
|
||||
{
|
||||
return side == api::BuySell::BUY ? api::BuySell::SELL : api::BuySell::BUY;
|
||||
}
|
||||
// CRITICAL PATH
|
||||
// returns the leg index of a leg in a security
|
||||
// (asserts if the security is not a synthetic)
|
||||
// (asserts if the leg is not found)
|
||||
size_t static get_leg_index(api::Security const& spread_security, bts::api::ContractLeg const& this_leg)
|
||||
{
|
||||
bts_assert(spread_security.has_synthetic());
|
||||
size_t rv_idx = 0;
|
||||
for (auto it = spread_security.synthetic().legs().begin(); it != spread_security.synthetic().legs().end(); it++)
|
||||
{
|
||||
if (it->security().id() == this_leg.security().id())
|
||||
return rv_idx;
|
||||
rv_idx++;
|
||||
}
|
||||
bts_assert(false);
|
||||
}
|
||||
void static parse_order(const OrderState& order_state,
|
||||
Quantity current_aggregated_fill,
|
||||
bool& filled,
|
||||
bool& terminal_state,
|
||||
bool& encountered_error)
|
||||
bool& encountered_error,
|
||||
bool& new_fills)
|
||||
{
|
||||
filled = order_state.status == api::orderman::OrderStatus::OS_FILLED;
|
||||
terminal_state = order_state.status == api::orderman::OrderStatus::OS_CANCELED ||
|
||||
@@ -32,7 +81,67 @@ public:
|
||||
order_state.status == api::orderman::OrderStatus::OS_FILLED ||
|
||||
order_state.status == api::orderman::OrderStatus::OS_REJECTED ||
|
||||
order_state.status == api::orderman::OrderStatus::OS_STOPPED;
|
||||
bts_assert(order_state.filled_quantity.value() >= current_aggregated_fill.value());
|
||||
new_fills = order_state.filled_quantity.value() > current_aggregated_fill.value();
|
||||
encountered_error = !order_state.error_msg.empty();
|
||||
}
|
||||
// CRITICAL PATH
|
||||
// this traverses the depth of the book, accumulating the quantity,
|
||||
// till the target quantity is reached. if the target quantity is not
|
||||
// reached the out_quantity < target_quantity
|
||||
void static traverse_depth_for_quantity(const Book& leg_book, const api::BuySell book_side, Quantity target_quantity,
|
||||
Price& out_price, Quantity& out_quantity)
|
||||
{
|
||||
double out_price_d = 0.0;
|
||||
uint32_t out_quantity_u = 0;
|
||||
const size_t n_levels = leg_book.n_levels();
|
||||
for (size_t level = 0; level < n_levels; level++)
|
||||
{
|
||||
std::tuple<Price, Quantity> level_price_qty = leg_book.level(book_side, level);
|
||||
Quantity level_qty = std::get<1>(level_price_qty);
|
||||
out_price = std::get<0>(level_price_qty);
|
||||
out_quantity_u += level_qty.value();
|
||||
if (out_quantity_u >= target_quantity.value())
|
||||
{
|
||||
out_quantity_u = target_quantity.value();
|
||||
break;
|
||||
}
|
||||
if (level_qty.value() == 0)
|
||||
break;
|
||||
}
|
||||
out_price = Price(out_price_d);
|
||||
out_quantity = Quantity(out_quantity_u);
|
||||
}
|
||||
// given the new price, applies the quoting parameters (mute etc)
|
||||
// to see if quote needs to be repriced
|
||||
static bool reprice_quote_mute_zone(double current_price, double new_price, api::BuySell quote_side, double tick_size,
|
||||
const bts::spark::QuoteMuteZone& quote_mute_zone)
|
||||
{
|
||||
double price_change = new_price - current_price;
|
||||
// inside_market_limit and outside_market_limit can be cached
|
||||
double inside_market_limit = quote_mute_zone.inside_ticks() * tick_size,
|
||||
outside_market_limit = quote_mute_zone.outside_ticks() * tick_size;
|
||||
/// TODO, when price is not in multiples of ticks
|
||||
/// TODO, check for negative values
|
||||
if (price_change != 0)
|
||||
{
|
||||
if (quote_side == api::BuySell::BUY)
|
||||
{
|
||||
if (price_change > 0)
|
||||
return price_change > inside_market_limit;
|
||||
else
|
||||
return fabs(price_change) > outside_market_limit;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (price_change < 0)
|
||||
return fabs(price_change) > inside_market_limit;
|
||||
else
|
||||
return price_change > outside_market_limit;
|
||||
}
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}}
|
||||
Reference in new issue
Block a user