syntax = "proto3";

package dbag.energy.m7t.orderbook.v7;

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

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

/**
 * Subscription is the first message on the channel and
 * client sends it to initiate the OrderBookMessage stream.
 * It contains the auth token and order-book keys to subscribe to.
 */
message Subscription {
  string token = 1; // Unique token for user authentication. M7 provides the token within the v6 UserRprt.
  repeated OrderBookKey subscribed_keys = 2; // List of OrderBooks user wants to receive in the stream.
}

/**
 * OrderBookMessage contains a header with metadata like sequence and timestamps, and
 * either OrderBookSnapshot or OrderBookDelta.
 * Snapshots are sent only at the beginning of the stream, usually one OrderBookSnapshot for each subscribed order-book key. For more details, please refer to the chapter Sequence number in orderbook stream.
 * Then the stream of OrderBookMessages contains only deltas.
 */
message OrderBookMessage {
  dbag.energy.m7t.common.v7.Header header = 1; // Message metadata.
  oneof content {
    OrderBookSnapshot order_book_snapshot = 2; // Snapshot of an order-book at the moment of subscription.
    OrderBookDelta order_book_delta = 3; // Relative change in the order-book as a result of an order addition, modification, deletion or execution.
    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 {}

/**
 * The order-book snapshot gives a complete overview of the requested public order-book at the moment of the subscription.
 */
message OrderBookSnapshot {
  OrderBookKey key = 1; // OrderBookKey identifies the order book.
  repeated OrderBook order_books = 2; // List of orders books, one per contract, that exist on the market.
}

/**
 * The order-book delta contains relative change in the order book as a result of an order addition, modification, deletion or execution.
 */
message OrderBookDelta {
  OrderBookKey key = 1; // OrderBookKey identifies the order-book.
  repeated OrderBook order_books = 2; // List of orders-books (one per contract) that has been changed.
}

/**
 * The order-book key identifies order-books that belong to the same product and delivery area.
 */
message OrderBookKey {
  string product = 1; // product name, e.g. "Quarterly_Hour_Power".
  string delivery_area = 2; // EIC code of the delivery area, e.g. "10YAT-DE------I".
}

/**
 * Order-book entries for a single contract.
 */
message OrderBook {
  uint64 contract_id = 1; // Id of the contract.
  uint32 revision = 2; // Order-book revision. It is increased in case of any change of the order book. Starts from 1.
  optional int32 last_price = 3; // Last traded price.
  optional uint32 last_quantity = 4; // Last traded quantity.
  optional uint32 total_quantity = 5; // Total quantity of individual orders that were matched in a trade on this contract and delivery area. If the trade is created from 2 orders on the same delivery area, then it includes the quantity of both orders. For remote trades it is provided by XBID.
  optional google.protobuf.Timestamp last_trade_time = 6; // Timestamp of the last execution.
  optional int32 highest_price = 7; // The highest traded price since the start of the trading period.
  optional int32 lowest_price = 8; // 	The lowest traded price since the start of the trading period.
  PriceDirection price_direction = 9; // Direction of the price movement.
  repeated OrderBookOrder buys = 10; // List of buy orders.
  repeated OrderBookOrder sells = 11; // List of sell orders.
}

/**
 * A single order, or a change of it, inside an Order Book.
 */
message OrderBookOrder {
  uint64 order_id = 1; // Unique id number of an order, as determined by the backend system.
  uint32 quantity = 2; // The quantity of the order which is exposed in that order book.
  int32 price = 3; // 	The limit price of the order.
  google.protobuf.Timestamp order_entry_time = 4; // The creation timestamp of the order.
  OrderExecutionRestriction order_execution_restriction = 5; // Execution restriction of the order.
  OrderType order_type = 6; // Type of order.
}

/**
 * Defines the direction of the price movement.
 */
enum PriceDirection {
  PRICE_DIRECTION_UNSPECIFIED = 0; // Price Direction is unknown.
  PRICE_DIRECTION_DECREASED = 1; // Price has decreased.
  PRICE_DIRECTION_UNCHANGED = 2; // Price is unchanged.
  PRICE_DIRECTION_INCREASED = 3; // Price has increased.
}

/**
 * Execution restriction of the order.
 */
enum OrderExecutionRestriction {
  ORDER_EXECUTION_RESTRICTION_UNSPECIFIED = 0; // Order Execution Restriction is unknown.
  ORDER_EXECUTION_RESTRICTION_NOT_RESTRICTED = 1; // Order Execution is not restricted.
  ORDER_EXECUTION_RESTRICTION_AON = 2; // Order Execution is restricted to AON.
}

/**
 * Type of order.
 */
enum OrderType {
  ORDER_TYPE_UNSPECIFIED = 0; // Order type is unknown.
  ORDER_TYPE_REGULAR = 1; // Regular limit order.
  ORDER_TYPE_BLOCK = 2; // User defined block order.
  ORDER_TYPE_BALANCE = 3; // Balance order.
  ORDER_TYPE_NOT_PROVIDED_BY_SOB = 4; // Order type was not provided by SOB.
}
