OrderState
Represents the lifecycle state of an order. States are divided into two categories: - ACTIVE states: Order is still "alive" and may fill - TERMINAL states: Order has reached a final state
OrderState.java
package com.bookmap.ordermanagement.model;
import velox.api.layer1.data.OrderStatus;
/**
* Represents the lifecycle state of an order.
*
* States are divided into two categories:
* - ACTIVE states: Order is still "alive" and may fill
* - TERMINAL states: Order has reached a final state
*/
public enum OrderState {
// Active states
CREATED("CREATED", true, false),
SUBMITTED("SUBMITTED", true, false),
WORKING("WORKING", true, false),
PARTIALLY_FILLED("PARTIAL", true, false),
// Terminal states
FILLED("FILLED", false, true),
CANCELLED("CANCELLED", false, true),
REJECTED("REJECTED", false, true);
private final String label;
private final boolean active;
private final boolean terminal;
OrderState(String label, boolean active, boolean terminal) {
this.label = label;
this.active = active;
this.terminal = terminal;
}
/**
* Returns true if the order is still active (may still fill).
*/
public boolean isActive() {
return active;
}
/**
* Returns true if the order has reached a terminal state.
*/
public boolean isTerminal() {
return terminal;
}
/**
* Returns true if this is a successful fill (full or partial).
*/
public boolean isFilled() {
return this == FILLED || this == PARTIALLY_FILLED;
}
/**
* Convert from Bookmap OrderStatus.
*
* @param brokerStatus The OrderStatus from Bookmap callback
* @param filled Number of contracts filled
* @param unfilled Number of contracts remaining
* @return Corresponding OrderState
*/
public static OrderState fromBrokerStatus(OrderStatus brokerStatus, int filled, int unfilled) {
if (brokerStatus.isActive()) {
if (filled > 0 && unfilled > 0) {
return PARTIALLY_FILLED;
}
return WORKING;
}
// Terminal states
return switch (brokerStatus) {
case FILLED -> FILLED;
case CANCELLED -> CANCELLED;
case REJECTED -> REJECTED;
default -> CANCELLED; // Handle any other terminal status as CANCELLED
};
}
@Override
public String toString() {
return label;
}
}