public final class OrderBookModel extends Object implements AutoCloseable
Order
event class and
arranges incoming orders into a lists of buyOrders
and
sellOrders
that are, in turn, arranged by price, so that
the first element of the list (at index 0) is at the top of the corresponding side of the book.
Users of order book model only see the order book in a consistent state. This model delays incoming events which
are part of incomplete snapshot or ongoing transaction until snapshot is complete or transaction has ended.
These pending events cannot be seen neither via get methods of buy/sell order lists nor via listener calls, and so
eventFlags
of orders in the model are set to zero.
The eventFlags are only used and must be taken into account when processing orders directly via low-level
DXFeedSubscription
class.
dxFeed API comes with a set of samples.DXFeed
feed = ...; OrderBookModel model = newOrderBookModel
(); model.setFilter
(OrderBookModelFilter
.ALL
); model.setSymbol
("AAPL"); model.addListener
(newOrderBookModelListener
() { public void modelChanged(OrderBookModelListener.Change
change) { System.out.println("Buy orders:"); for (Order order : model.getBuyOrders
()) System.out.println(order); System.out.println("Sell orders:"); for (Order order : model.getSellOrders
()) System.out.println(order); System.out.println(); } }); model.attach
(feed);
DXFeedMarketDepth
sample is a very simple UI application
that shows how to use this model, concentrating your
effort on data representation logic, while delegating all the data-handling logic to this model.
The convenient way to detach model from the feed is to call its close
method. Closed model
becomes permanently detached from all feeds, removes all its listeners and is guaranteed to be reclaimable by
the garbage collector as soon as all external references to it are cleared.
attach
, detach
and close
.
See AbstractIndexedEventModel
class documentation for details and constrains on
the usage of this class.
Installed ObservableListModelListener
instances are invoked from a separate thread via the executor.
Default executor for all models is configured with DXEndpoint.executor
method. Each model can individually override its executor with setExecutor
method. The corresponding
modelChanged
notification is guaranteed to never be concurrent, even though it may happen from different
threads if executor is multi-threaded.
Custom executor can be used by backend applications that do not need to immediately update this model on
arrival of new events, but want to update the model at a later time, for example, from inside of a servlet request.
This approach is explained with code samples in
Threads and locks
section of DXFeedSubscription
class documentation.
Constructor and Description |
---|
OrderBookModel()
Creates new model.
|
Modifier and Type | Method and Description |
---|---|
void |
addListener(OrderBookModelListener listener)
Adds a listener to this order book model.
|
void |
attach(DXFeed feed)
Attaches model to the specified feed.
|
void |
clear()
Clears subscription symbol and, subsequently, all events in this model.
|
void |
close()
Closes this model and makes it permanently detached.
|
void |
detach(DXFeed feed)
Detaches model from the specified feed.
|
ObservableListModel<Order> |
getBuyOrders()
Returns the view of bid side (buy orders) of the order book.
|
Executor |
getExecutor()
Returns executor for processing event notifications on this model.
|
OrderBookModelFilter |
getFilter()
Returns filter for the model.
|
int |
getLotSize()
Returns lot size.
|
ObservableListModel<Order> |
getSellOrders()
Returns the view of offer side (sell orders) of the order book.
|
String |
getSymbol()
Returns order book symbol, or
null for empty subscription. |
void |
removeListener(OrderBookModelListener listener)
Removes a listener from this order book model.
|
void |
setExecutor(Executor executor)
Changes executor for processing event notifications on this model.
|
void |
setFilter(OrderBookModelFilter filter)
Sets the specified filter to the model.
|
void |
setLotSize(int lotSize)
Sets the lot size.
|
void |
setSymbol(String symbol)
Sets symbol for the order book to subscribe for.
|
public OrderBookModel()
OrderBookModelFilter.ALL
.
Use setSymbol(java.lang.String)
to specify subscription symbol and
attach(com.dxfeed.api.DXFeed)
method to specify feed to start receiving events.public void attach(DXFeed feed)
feed
- feed to attach to.public void detach(DXFeed feed)
feed
- feed to detach from.public void close()
This method ensures that model can be safely garbage-collected when all outside references to it are lost.
close
in interface AutoCloseable
public Executor getExecutor()
null
if default executor of the attached DXFeed
is used.public void setExecutor(Executor executor)
executor
- executor for processing event notifications on this model,
or null
if default executor of the attached DXFeed
is used.public void clear()
setSymbol
(null)
.public OrderBookModelFilter getFilter()
OrderBookModelFilter
public void setFilter(OrderBookModelFilter filter)
filter
- model filterpublic String getSymbol()
null
for empty subscription.public void setSymbol(String symbol)
symbol
- order book symbol, use null
to unsubscribe.public int getLotSize()
Scope.COMPOSITE
,
Scope.REGIONAL
, and Scope.AGGREGATE
orders.public void setLotSize(int lotSize)
lotSize
- lot size multipliergetLotSize()
public ObservableListModel<Order> getBuyOrders()
public ObservableListModel<Order> getSellOrders()
public void addListener(OrderBookModelListener listener)
Note, that the listener currently provides only information about the
source of the change (what model has changed) and does not actually provide
information about what orders in the model has changed.
The model has to be queried using lists of buy
and sell
orders to get the new set of active orders in the book.
listener
- the listener for listening to the list changes.public void removeListener(OrderBookModelListener listener)
listener
- the listener to remove.Copyright © 2002–2023 Devexperts LLC. All rights reserved.