Position based limits in order manager code complete.

This commit is contained in:
Pedro Pinto committed 2019-07-29 12:58:16 -04:00
1 parent 4aa0d878a6
commit 38bf521132
12 files changed
+172 -55

No files matched your search

+16 -16
View File
@@ -31,7 +31,7 @@ OrderStatus map_external_status(NativeOrderStatus);
struct NativeOrder::PImpl {
PImpl()
: services(nullptr), session_services(), sec(nullptr),
previous_position_contribution(0), definition(), state(),
previous_working_position(0), definition(), state(),
price_encoding_precision(7), exchange_order_id() {
exchange_order_id.reserve(100);
}
@@ -40,7 +40,7 @@ struct NativeOrder::PImpl {
SessionServices *session_services;
const api::Security *sec;
Quantity previous_position_contribution;
Quantity previous_working_position;
OrderDefinition definition;
OrderState state;
@@ -171,7 +171,7 @@ void NativeOrder::on_init_recovered(OrderRecord const &rec,
me->definition = def;
me->state = rec.state();
me->sec = serv.sec_db.resolve(SecurityKey(def.security_key()));
me->previous_position_contribution = Quantity(rec.state().filled_quantity());
me->previous_working_position = Quantity(0);
auto const &native_state = get_native_state(me->state);
switch (me->state.status()) {
@@ -1059,22 +1059,20 @@ void NativeOrder::on_gateway_status(const GatewayStatusChangeEvent &ev) {
void NativeOrder::update_working_position() {
if (me->sec) {
auto current_position = position_contribution();
auto previous_position = me->previous_position_contribution;
me->previous_position_contribution = current_position;
auto current_position = working_position();
auto previous_position = me->previous_working_position;
me->previous_working_position = current_position;
if (current_position.value() > previous_position.value())
me->services->limiter().add(
me->services->limiter().add_to_working_position(
*me->sec, AccountID(me->definition.account_id()),
me->definition.side(),
Quantity(current_position.value() - previous_position.value()),
true);
Quantity(current_position.value() - previous_position.value()));
else if (previous_position.value() > current_position.value())
me->services->limiter().remove(
me->services->limiter().remove_from_working_position(
*me->sec, AccountID(me->definition.account_id()),
me->definition.side(),
Quantity(previous_position.value() - current_position.value()),
true);
Quantity(previous_position.value() - current_position.value()));
} else
LOG(OrderMan, Error,
"Order {} ignored for purposes of limit enforcing "
@@ -1338,20 +1336,22 @@ void NativeOrder::transition_to_error(
transition(ORDER_STATUS_ERROR, msg);
}
Quantity NativeOrder::position_contribution() {
Quantity NativeOrder::working_position() {
switch (me->state.status()) {
case ORDER_STATUS_WORKING:
case ORDER_STATUS_PENDING_WORKING:
case ORDER_STATUS_DISCONNECTED:
return Quantity(
std::max(me->state.quantity(), me->state.filled_quantity()));
if (me->state.quantity() > me->state.filled_quantity())
return Quantity(me->state.quantity() - me->state.filled_quantity());
else
return Quantity(0);
case ORDER_STATUS_REJECTED_BY_SPARK:
case ORDER_STATUS_ERROR:
case ORDER_STATUS_COMPLETED:
case ORDER_STATUS_CANCELED:
return Quantity(me->state.filled_quantity());
return Quantity(0);
case ORDER_STATUS_INACTIVE:
case OrderStatus_INT_MIN_SENTINEL_DO_NOT_USE_: