syntax = "proto3";

package dbag.energy.m7t.order.v7;

import "dbag/energy/m7t/common/v7/header.proto";
import "google/protobuf/timestamp.proto";
import "dbag/energy/m7t/common/v7/order_common.proto";

option java_multiple_files = true;
option java_package = "com.deutscheboerse.energy.m7.api.order.v7";

/**
 * Subscription is the first message on the channel and
 * client sends it to initiate the OrderMessage stream.
 * It contains the auth token.
 */
message Subscription {
  string token = 1; // Unique token for user authentication. M7 provides the token within the v6 UserRprt.
}

/**
 * PrivateDataMessage contains a header with metadata like sequence and timestamps, an optional correlation ID and OrderMessage.
 */
message PrivateDataMessage {
  dbag.energy.m7t.common.v7.Header header = 1; // Message metadata.
  optional string correlation_id = 2; // Content's correlation ID.
  oneof content {
    OrderMessage order_message = 3; // Order message.
    SynchronizationComplete synchronization_complete = 4; // Synchronization complete message.
  }
}

/**
 * The Synchronization Complete message is sent immediately after the last snapshot of the initial stream synchronization.
 */
message SynchronizationComplete {}

/**
 * OrderMessage contains either OrderSnapshot or OrderExecutionReport.
 * Snapshots are sent only at the beginning of the stream, one OrderSnapshot for each subscribed order-book key.
 * Then the stream of OrderMessages contains only updates.
 */
message OrderMessage {
  oneof content {
    OrderSnapshot order_snapshot = 1; // Snapshot of order on the market at the moment of subscription.
    OrderExecutionReport order_execution_report = 2; // Order execution report.
  }
}

/**
 * Snapshot of orders on the market for the logged user at the moment of subscription.
 */
message OrderSnapshot {
  string account_id = 2; // Account for which the order was entered.
  optional string pre_arranged_account_id = 3; // The account id of the counterparty in case of a pre-arranged order.

  repeated Order order = 1;
}

/**
 * Order execution report.
 */
message OrderExecutionReport {
  // NOTE: numbers 1,2,3 are reserved for backward compatibility

  string account_id = 5; // Account for which the order was entered.
  optional string pre_arranged_account_id = 6; // The account id of the counterparty in case of a pre-arranged order.

  repeated Order order = 4; // Order details.
}

/**
 * Represent the order in the system.
 */
message Order {
  // NOTE: numbers 6, 20, 43 are reserved for backward compatibility

  uint64 order_id = 1; // Order Id as returned by the backend system.
  uint64 initial_order_id = 2; // In the event of an order modification, this value contains the Id of the first order in the modification chain.
  optional uint64 parent_order_id = 3; // In the event of an order modification, this field contains the Id of the modified order.
  uint64 revision = 4; // Order revision number.
  optional string clearing_account_type = 5; // Defines if the order is entered on its own account, or as an agent. For valid values please refer to values from attribute allowedClearingAcctTypes in the SystemInfoResp message (i.e. A,P for spot markets).
  uint64 contract_id = 7; // Define the underlying contract of the order.
  OrderSide side = 8; // Define on which side of the market the order is entered.
  int64 price = 9; // Limit price of the order.
  optional int64 stop_price = 10; // Stop price for stop limit orders.
  optional int64 peak_price_delta = 11; // Peak price delta for iceberg orders.
  uint32 quantity = 12; // Quantity exposed to the market.
  optional uint32 hidden_quantity = 13; // Hidden quantity of the Iceberg order.
  optional uint32 displayed_quantity = 14; // Displayed quantity of the Iceberg order.
  uint32 initial_quantity = 15; // Initial quantity of the order.
  optional OrderExecutionRestriction order_execution_restriction = 16; // Execution restriction of the order.
  string delivery_area = 17; // Delivery area of the order.
  optional string client_order_id = 18; // Can be used by the client to define its own order ID.
  bool pre_arranged = 19; // Flag that indicates if the order is pre-arranged.
  OrderType type = 21; // Type of the order.
  OrderState state = 22; // State of the order.
  optional OrderAggressorIndicator aggressor_indicator = 23; // Indicates if the executed order was a trade aggressor or originator.
  string user_code = 24; // User code of the user who last performed a successful action on the order.
  google.protobuf.Timestamp timestamp = 25; // Timestamp of the order entry as determined by the backend.
  optional google.protobuf.Timestamp validity_time = 26; // Mandatory field in the event of GTD validity restriction. Define the date until which the order is valid.
  optional OrderValidityRestriction validity_restriction = 27; // Validity restriction of the order.
  OrderAction action = 28; // Defines the action that was performed on the order.
  string last_update_user_info = 29; // Information (concatenation of accountId and usrCode) about the user who last updated the order. If the action is any of the system actions (FEXE, SADD etc.), then the update has been created by the system and the last_update_user_info contains the system user (7777777777777777SYSTEM).
  optional OrderOpenCloseIndicator open_close_indicator = 30; // Open Close Indicator for futures & cross-product spreads.
  optional uint32 broker_user_id = 31; // User ID of the broker user.
  optional bool counter_order = 32; // Indicates if the order is an artificial counter order generated by cross-product matching process.
  optional uint64 remote_order_id = 33; // ID of the order as returned by the remote backed system (e.g. XBID).
  optional google.protobuf.Timestamp remote_last_update_time = 34; // Timestamp of the last modification of the order in the remote system (e.g. XBID).
  google.protobuf.Timestamp last_update_time = 35; // Timestamp of the last modification of the order.
  optional bool aot = 36; // Indicates if the order is an AOT order.
  optional uint64 pre_aot_id = 37; // Local order ID of the remote order that this order originated from during AOT.
  optional string location = 38; // Location within the delivery area. Available only for products with Locations enabled.
  optional bool market_based = 39; // Indicates if the order is market based. Available only for products with Locations enabled.
  optional string contract_reference = 40; // Reference to a contract between supplier and network operator. Available only for products with Locations enabled.
  optional string facility_type = 41; // Indicates the type of the network connected facility. Available only for products with Locations enabled.
  optional uint32 usage_fraction = 42; // Indicates min/max percentage value of order's usage. Available only for products with Locations enabled.
  string text = 44; // Text entered by the client.
  optional uint32 on_behalf_user_id = 45; // UserID of the target user in case of On-behalf trading (see On-behalf Trading chapter in DFS180).
  optional uint64 basket_id = 46; // ID for the basket determined by the backend.
  optional dbag.energy.m7t.common.v7.ExecutionInstruction execution_instruction = 47; // Defines the execution instruction for a whole list of orders.
  optional bool fast_order = 48; // Indicates whether the order is fast_order or not.
}

/**
 * Order sides.
 */
enum OrderSide {
  ORDER_SIDE_UNSPECIFIED = 0; // Order side is not specified.
  ORDER_SIDE_BUY = 1; // Order side is buy.
  ORDER_SIDE_SELL = 2; // Order side is sell.
}

/**
 * Order execution restrictions.
 */
enum OrderExecutionRestriction {
  ORDER_EXECUTION_RESTRICTION_UNSPECIFIED = 0; // Order Execution Restriction is not specified.
  ORDER_EXECUTION_RESTRICTION_NOT_RESTRICTED = 1; // Order Execution is not restricted.
  ORDER_EXECUTION_RESTRICTION_AON = 2; // Order Execution is restricted to AON.
  ORDER_EXECUTION_RESTRICTION_FOK = 3; // Order Execution is restricted to FOK.
  ORDER_EXECUTION_RESTRICTION_IOC = 4; // Order Execution is restricted to IOC.
}

/**
 * Order types.
 */
enum OrderType {
  ORDER_TYPE_UNSPECIFIED = 0; // Order type is not specified.
  ORDER_TYPE_REGULAR = 1; // Regular limit order(default value).
  ORDER_TYPE_BLOCK = 2; // User-defined block order.
  ORDER_TYPE_BALANCE = 3; // Balance order.
  ORDER_TYPE_ICEBERG = 4; // Iceberg order.
  ORDER_TYPE_STOP = 5; // Stop order.
  ORDER_TYPE_EXCHANGE_PRE_ARRANGED = 6; // Pre-arranged trade on exchange.
  ORDER_TYPE_PRIVATE = 7; // Private and confidential order.
}

/**
 * Order states.
 */
enum OrderState {
  ORDER_STATE_UNSPECIFIED = 0; // Order state is not specified.
  ORDER_STATE_ACTIVE = 1; // Order is entered and is immediately exposed to the market execution.
  ORDER_STATE_INACTIVE = 2; // Order is deleted.
  ORDER_STATE_HIBERNATED = 3; // Order is entered into the backend system but is not exposed to the market.
  ORDER_STATE_UNKNOWN = 4; // Order state is unknown.
}

/**
 * Order aggressor indicator.
 */
enum OrderAggressorIndicator {
  ORDER_AGGRESSOR_INDICATOR_UNSPECIFIED = 0; // Aggressor Indicator is not specified.
  ORDER_AGGRESSOR_INDICATOR_YES = 1; // Trade aggressor.
  ORDER_AGGRESSOR_INDICATOR_NO = 2; // Trade originator.
  ORDER_AGGRESSOR_INDICATOR_UNKNOWN = 3; // Aggressor Indicator is unknown.
}

/**
 * Order validity restrictions.
 */
enum OrderValidityRestriction {
  ORDER_VALIDITY_RESTRICTION_UNSPECIFIED = 0; // Order Validity Restriction is not specified.
  ORDER_VALIDITY_RESTRICTION_NONE = 1; // Order has no validity restriction.
  ORDER_VALIDITY_RESTRICTION_GFS = 2; // Good For Trading Session - Order is valid for the current trading session.
  ORDER_VALIDITY_RESTRICTION_GTD = 3; // Good Till Date - Order is valid until validity_date.
}

/**
 * Order actions.
 */
enum OrderAction {
  ORDER_ACTION_UNSPECIFIED = 0; // Order action is not specified.
  ORDER_ACTION_UADD = 1; // Order added by the user.
  ORDER_ACTION_UHIB = 2; // Order deactivated by the user.
  ORDER_ACTION_UMOD = 3; // Order modified by the user.
  ORDER_ACTION_UDEL = 4; // Order deleted by the user.
  ORDER_ACTION_UREJ = 5; // Pre-arranged order rejected by the user.
  ORDER_ACTION_AADD = 6; // Order added by market operations on behalf.
  ORDER_ACTION_AHIB = 7; // Order deactivated by market operations on behalf.
  ORDER_ACTION_AMOD = 8; // Order modified by market operations on behalf.
  ORDER_ACTION_ADEL = 9; // Order deleted by market operations on behalf.
  ORDER_ACTION_AREJ = 10; // Pre-arranged order rejected by market operations on behalf.
  ORDER_ACTION_SADD = 11; // Order added by the system.
  ORDER_ACTION_SHIB = 12; // Order deactivated by the system.
  ORDER_ACTION_SMOD = 13; // Order modified by the system.
  ORDER_ACTION_SDEL = 14; // Order deleted by the system.
  ORDER_ACTION_SREJ = 15; // Pre-arranged order rejected by system.
  ORDER_ACTION_FEXE = 16; // Order is fully executed.
  ORDER_ACTION_PEXE = 17; // Partial execution of order.
  ORDER_ACTION_IADD = 18; // A new slice of an Iceberg order was added to the service.
  ORDER_ACTION_SERR = 19; // The order validation failed on SOB side, or the request sent to SOB timed out. This is only valid for remote orders.
  ORDER_ACTION_SNAV = 20; // Order state is unknown due to SOB unavailability. This is only valid for remote orders.
}

/**
 * Order open close indicator.
 */
enum OrderOpenCloseIndicator {
  ORDER_OPEN_CLOSE_INDICATOR_UNSPECIFIED = 0; // Open Close Indicator is not specified.
  ORDER_OPEN_CLOSE_INDICATOR_OPEN = 1; // Open.
  ORDER_OPEN_CLOSE_INDICATOR_CLOSE = 2; // Close.
}
