|
| 1 | +/* |
| 2 | + * !++ |
| 3 | + * QDS - Quick Data Signalling Library |
| 4 | + * !- |
| 5 | + * Copyright (C) 2002 - 2019 Devexperts LLC |
| 6 | + * !- |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 8 | + * If a copy of the MPL was not distributed with this file, You can obtain one at |
| 9 | + * http://mozilla.org/MPL/2.0/. |
| 10 | + * !__ |
| 11 | + */ |
| 12 | +package com.dxfeed.event.market; |
| 13 | + |
| 14 | +import com.dxfeed.api.DXFeed; |
| 15 | +import com.dxfeed.api.DXPublisher; |
| 16 | +import com.dxfeed.model.AbstractIndexedEventModel; |
| 17 | +import com.dxfeed.model.IndexedEventModel; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import javax.xml.bind.annotation.XmlRootElement; |
| 21 | +import javax.xml.bind.annotation.XmlType; |
| 22 | + |
| 23 | +/** |
| 24 | + * Represents an extension of {@link Order} introducing analytic information, e.g. adding to this order iceberg related |
| 25 | + * information ({@link #icebergPeakSize}, {@link #icebergHiddenSize}, {@link #icebergExecutedSize}). |
| 26 | + * The collection of analytic order events of a symbol represents the most recent analytic information |
| 27 | + * that is available about orders on the market at any given moment of time. |
| 28 | + * |
| 29 | + * <p> Analytic order is similar to a regular {@link Order}. In addition this event has few additional properties: |
| 30 | + * <ul> |
| 31 | + * <li>{@link #icebergPeakSize} - the size of the peak, i.e. the visible part of the iceberg, |
| 32 | + * that is being continually refilled until the order is fully traded or cancelled; |
| 33 | + * <li>{@link #icebergHiddenSize} - the prediction of current hidden size of the iceberg, as inferred by the model; |
| 34 | + * <li>{@link #icebergExecutedSize} - the executed size of the iceberg order. For {@link IcebergType#SYNTHETIC} type |
| 35 | + * represents total executed size of all orders сomposing current iceberg; |
| 36 | + * <li>{@link #getIcebergType()} - type of the iceberg, either native (exchange-managed) or synthetic (managed outside of the exchange). |
| 37 | + * </ul> |
| 38 | + * |
| 39 | + * <p> Like regular orders, analytic order events arrive from |
| 40 | + * multiple sources for the same market symbol and are distinguished by their |
| 41 | + * {@link #getIndex index}. |
| 42 | + * It is unique across all the sources of depth information for the symbol. |
| 43 | + * The event with {@link #getSizeAsDouble() sizeAsDouble} either {@code 0} or {@link Double#NaN NaN} |
| 44 | + * is a signal to remove previously received order for the corresponding index. |
| 45 | + * The method {@link #hasSize() hasSize} is a convenient method to test for size presence. |
| 46 | + * |
| 47 | + * <h3>Properties</h3> |
| 48 | + * <p> |
| 49 | + * {@code Analytic} event has the following properties: |
| 50 | + * |
| 51 | + * <ul> |
| 52 | + * <li>{@link #getEventSymbol() eventSymbol} - underlying symbol of this event; |
| 53 | + * <li>{@link #getSource() source} - source of this event; |
| 54 | + * <li>{@link #getEventFlags() eventFlags} - transactional event flags; |
| 55 | + * <li>{@link #getIndex() index} - unique per-symbol index of this order; |
| 56 | + * <li>{@link #getTime() time} - time of this order; |
| 57 | + * <li>{@link #getTimeNanoPart() timeNanoPart} - microseconds and nanoseconds time part of this order; |
| 58 | + * <li>{@link #getSequence() sequence} - sequence of this order; |
| 59 | + * <li>{@link #getPrice() price} - price of this order; |
| 60 | + * <li>{@link #getSize() size} - size of this order as integer number (rounded toward zero); |
| 61 | + * <li>{@link #getSizeAsDouble() sizeAsDouble} - size of this order as floating number with fractions; |
| 62 | + * <li>{@link #getExecutedSize() executedSize} - executed size of this order; |
| 63 | + * <li>{@link #getCount() count} - number of individual orders in this aggregate order; |
| 64 | + * <li>{@link #getExchangeCode() exchangeCode} - exchange code of this order; |
| 65 | + * <li>{@link #getOrderSide() orderSide} - side of this order; |
| 66 | + * <li>{@link #getScope() scope} - scope of this order; |
| 67 | + * <li>{@link #getMarketMaker() marketMaker} - market maker or other aggregate identifier of this order; |
| 68 | + * <li>{@link #getIcebergPeakSize() icebergPeakSize} - iceberg peak size of this analytic order; |
| 69 | + * <li>{@link #getIcebergHiddenSize() icebergHiddenSize} - iceberg hidden size of this analytic order; |
| 70 | + * <li>{@link #getIcebergExecutedSize() icebergExecutedSize} - iceberg executed size of this analytic order; |
| 71 | + * <li>{@link #getIcebergType() icebergType} - iceberg type of this analytic order. |
| 72 | + * </ul> |
| 73 | + * |
| 74 | + * <h3><a name="eventFlagsSection">Event flags, transactions and snapshots</a></h3> |
| 75 | + * <p> |
| 76 | + * Analytic order event sources provide a consistent view of the analytic order book. Their updates |
| 77 | + * may incorporate multiple changes to individual orders that have to be processed at the same time. |
| 78 | + * The corresponding information is carried in {@link #getEventFlags() eventFlags} property. |
| 79 | + * |
| 80 | + * <p> See <a href="OrderBase.html#eventFlagsSection">Event Flags</a> section of {@link OrderBase} |
| 81 | + * class documentation for details. |
| 82 | + * |
| 83 | + * <p>{@link IndexedEventModel} class handles all the snapshot and transaction logic and conveniently represents |
| 84 | + * a list current of events. |
| 85 | + * It relies on the code of {@link AbstractIndexedEventModel} to handle this logic. |
| 86 | + * Use the source code of {@link AbstractIndexedEventModel} for clarification on transactions and snapshot logic. |
| 87 | + * |
| 88 | + * <h3>Publishing order books</h3> |
| 89 | + * <p> |
| 90 | + * When publishing an order event with {@link DXPublisher#publishEvents(Collection) DXPublisher.publishEvents} |
| 91 | + * method, least significant 32 bits of order {@link #getIndex() index} must be in a range of from 0 to |
| 92 | + * {@link Integer#MAX_VALUE} inclusive. |
| 93 | + * Use {@link #setSource(OrderSource) setSource} method after {@link #setIndex(long) setIndex} to properly |
| 94 | + * include source identifier into the index. |
| 95 | + * <p> |
| 96 | + * A snapshot has to be published in the <em>descending</em> order of {@link #getIndex() index}, starting with |
| 97 | + * an event with the largest index and marking it with {@link #SNAPSHOT_BEGIN} bit in {@link #getEventFlags() eventFlags}, |
| 98 | + * and finishing the snapshot with an event that has zero 32 least significant bits of index. |
| 99 | + * {@link #SNAPSHOT_END} bit in {@link #getEventFlags() eventFlags} is optional during publishing. |
| 100 | + * It will be properly set on receiving end anyway. |
| 101 | + * |
| 102 | + * <h3>Limitations</h3> |
| 103 | + * <p> |
| 104 | + * This event type cannot be used with {@link DXFeed#getLastEvent DXFeed.getLastEvent} method. |
| 105 | + * |
| 106 | + * <h3>Implementation details</h3> |
| 107 | + * <p> |
| 108 | + * This event is implemented on top of QDS records {@code AnalyticOrder#<source-id>}, |
| 109 | + * where {@code <source-id>} is up to 4 ASCII characters with a mnemonic for the source like "GLBX". |
| 110 | + */ |
| 111 | +@XmlRootElement(name = "AnalyticOrder") |
| 112 | +@XmlType(propOrder = {"icebergPeakSize", "icebergHiddenSize", "icebergExecutedSize", "icebergType"}) |
| 113 | +public class AnalyticOrder extends Order { |
| 114 | + private static final long serialVersionUID = 0; |
| 115 | + |
| 116 | + // ========================= private static ========================= |
| 117 | + |
| 118 | + /* |
| 119 | + * Analytic flags property has several significant bits that are packed into an integer in the following way: |
| 120 | + * 31...2 1 0 |
| 121 | + * +--------------+-------+-------+ |
| 122 | + * | | IcebergType | |
| 123 | + * +--------------+-------+-------+ |
| 124 | + */ |
| 125 | + |
| 126 | + // TYPE values are taken from Type enum. |
| 127 | + static final int ICEBERG_TYPE_MASK = 3; |
| 128 | + static final int ICEBERG_TYPE_SHIFT = 0; |
| 129 | + |
| 130 | + // ========================= instance ========================= |
| 131 | + |
| 132 | + private double icebergPeakSize = Double.NaN; |
| 133 | + private double icebergHiddenSize = Double.NaN; |
| 134 | + private double icebergExecutedSize = Double.NaN; |
| 135 | + private int icebergFlags; |
| 136 | + |
| 137 | + /** |
| 138 | + * Creates new analytic order with default values. |
| 139 | + */ |
| 140 | + public AnalyticOrder() { |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Creates new analytic order with the specified event symbol. |
| 145 | + * |
| 146 | + * @param eventSymbol event symbol. |
| 147 | + */ |
| 148 | + public AnalyticOrder(String eventSymbol) { |
| 149 | + super(eventSymbol); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns iceberg peak size of this analytic order. |
| 154 | + * |
| 155 | + * @return iceberg peak size of this analytic order. |
| 156 | + */ |
| 157 | + public double getIcebergPeakSize() { |
| 158 | + return icebergPeakSize; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Changes iceberg peak size of this analytic order. |
| 163 | + * |
| 164 | + * @param icebergPeakSize iceberg peak size of this analytic order. |
| 165 | + */ |
| 166 | + public void setIcebergPeakSize(double icebergPeakSize) { |
| 167 | + this.icebergPeakSize = icebergPeakSize; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Returns iceberg hidden size of this analytic order. |
| 172 | + * |
| 173 | + * @return iceberg hidden size of this analytic order. |
| 174 | + */ |
| 175 | + public double getIcebergHiddenSize() { |
| 176 | + return icebergHiddenSize; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Changes iceberg hidden size of this analytic order. |
| 181 | + * |
| 182 | + * @param icebergHiddenSize iceberg hidden size of this analytic order. |
| 183 | + */ |
| 184 | + public void setIcebergHiddenSize(double icebergHiddenSize) { |
| 185 | + this.icebergHiddenSize = icebergHiddenSize; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Returns iceberg executed size of this analytic order. |
| 190 | + * |
| 191 | + * @return iceberg executed size of this analytic order. |
| 192 | + */ |
| 193 | + |
| 194 | + public double getIcebergExecutedSize() { |
| 195 | + return icebergExecutedSize; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Changes iceberg executed size of this analytic order. |
| 200 | + * |
| 201 | + * @param icebergExecutedSize iceberg executed size of this analytic order. |
| 202 | + */ |
| 203 | + public void setIcebergExecutedSize(double icebergExecutedSize) { |
| 204 | + this.icebergExecutedSize = icebergExecutedSize; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Returns iceberg type of this analytic order. |
| 209 | + * |
| 210 | + * @return iceberg type of this analytic order. |
| 211 | + */ |
| 212 | + public IcebergType getIcebergType() { |
| 213 | + return IcebergType.valueOf(Util.getBits(icebergFlags, ICEBERG_TYPE_MASK, ICEBERG_TYPE_SHIFT)); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Changes iceberg type of this analytic order. |
| 218 | + * |
| 219 | + * @param icebergType iceberg type of this analytic order. |
| 220 | + */ |
| 221 | + public void setIcebergType(IcebergType icebergType) { |
| 222 | + icebergFlags = Util.setBits(icebergFlags, ICEBERG_TYPE_MASK, ICEBERG_TYPE_SHIFT, icebergType.getCode()); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Returns string representation of this analytic order. |
| 227 | + * |
| 228 | + * @return string representation of this analytic order. |
| 229 | + */ |
| 230 | + @Override |
| 231 | + public String toString() { |
| 232 | + return "AnalyticOrder{" + baseFieldsToString() + |
| 233 | + ", icebergPeakSize=" + icebergPeakSize + |
| 234 | + ", icebergHiddenSize=" + icebergHiddenSize + |
| 235 | + ", icebergExecutedSize=" + icebergExecutedSize + |
| 236 | + ", icebergType=" + getIcebergType() + |
| 237 | + "}"; |
| 238 | + } |
| 239 | + |
| 240 | + // ========================= package private access for delegate ========================= |
| 241 | + |
| 242 | + /** |
| 243 | + * Returns implementation-specific flags relevant only for iceberg related part of analytic order. |
| 244 | + * <b>Do not use this method directly.</b> |
| 245 | + * It may be removed or changed in the future versions. |
| 246 | + * |
| 247 | + * @return iceberg flags. |
| 248 | + */ |
| 249 | + int getIcebergFlags() { |
| 250 | + return icebergFlags; |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Changes implementation-specific flags relevant only for iceberg related part of analytic order. |
| 255 | + * <b>Do not use this method directly.</b> |
| 256 | + * It may be removed or changed in the future versions. |
| 257 | + * |
| 258 | + * @param icebergFlags iceberg flags. |
| 259 | + */ |
| 260 | + void setIcebergFlags(int icebergFlags) { |
| 261 | + this.icebergFlags = icebergFlags; |
| 262 | + } |
| 263 | +} |
0 commit comments