dxFeed Graal CXX API v4.0.0
|
Main entry class for dxFeed API (read it first). More...
#include <DXFeed.hpp>
Public Types | |
using | Ptr = std::shared_ptr<DXFeed> |
The alias to a type of shared pointer to the DXFeed object. | |
using | Unique = std::unique_ptr<DXFeed> |
The alias to a type of unique pointer to the DXFeed object. | |
Public Types inherited from SharedEntity | |
using | Ptr = std::shared_ptr<SharedEntity> |
The alias to a type of shared pointer to the SharedEntity object. | |
Public Member Functions | |
void | attachSubscription (std::shared_ptr< DXFeedSubscription > subscription) |
Attaches the given subscription to this feed. | |
void | detachSubscription (std::shared_ptr< DXFeedSubscription > subscription) |
Detaches the given subscription from this feed. | |
void | detachSubscriptionAndClear (std::shared_ptr< DXFeedSubscription > subscription) |
Detaches the given subscription from this feed and clears data delivered to this subscription by publishing empty events. | |
template<Derived< LastingEvent > E> | |
std::shared_ptr< E > | getLastEvent (std::shared_ptr< E > event) |
Returns the last event for the specified event instance. | |
template<typename Collection , typename Element = std::decay_t<decltype(std::begin(Collection()))>, typename Event = std::decay_t<decltype(*Element())>> | |
const Collection & | getLastEvents (const Collection &events) |
Returns the last events for the specified list of event instances. | |
template<Derived< LastingEvent > E> | |
std::shared_ptr< E > | getLastEventIfSubscribed (const SymbolWrapper &symbol) |
Returns the last event for the specified event type and symbol if there is a subscription for it. | |
std::shared_ptr< DXFeedSubscription > | createSubscription (const EventTypeEnum &eventType) |
Creates new subscription for a single event type that is attached to this feed. | |
template<typename EventTypeIt > | |
std::shared_ptr< DXFeedSubscription > | createSubscription (EventTypeIt begin, EventTypeIt end) |
Creates new subscription for multiple event types that is attached to this feed. | |
std::shared_ptr< DXFeedSubscription > | createSubscription (std::initializer_list< EventTypeEnum > eventTypes) |
Creates new subscription for multiple event types that is attached to this feed. | |
template<typename EventTypesCollection > | |
std::shared_ptr< DXFeedSubscription > | createSubscription (EventTypesCollection &&eventTypes) |
Creates new subscription for multiple event types that is attached to this feed. | |
template<Derived< LastingEvent > E> | |
std::shared_ptr< Promise< std::shared_ptr< E > > > | getLastEventPromise (const SymbolWrapper &symbol) const |
Requests the last event for the specified event type and symbol. | |
template<Derived< LastingEvent > E, typename SymbolIt > | |
std::shared_ptr< PromiseList< E > > | getLastEventsPromises (SymbolIt begin, SymbolIt end) const |
Requests the last events for the specified event type and a collection of symbols. | |
template<Derived< LastingEvent > E, ConvertibleToSymbolWrapperCollection SymbolsCollection> | |
std::shared_ptr< PromiseList< E > > | getLastEventsPromises (SymbolsCollection &&collection) const |
Requests the last events for the specified event type and a collection of symbols. | |
template<Derived< LastingEvent > E> | |
std::shared_ptr< PromiseList< E > > | getLastEventsPromises (std::initializer_list< SymbolWrapper > collection) const |
Requests the last events for the specified event type and a collection of symbols. | |
template<Derived< IndexedEvent > E> | |
std::shared_ptr< Promise< std::vector< std::shared_ptr< E > > > > | getIndexedEventsPromise (const SymbolWrapper &symbol, const IndexedEventSource &source) const |
Requests a container of indexed events for the specified event type, symbol, and source. | |
template<Derived< TimeSeriesEvent > E> | |
std::shared_ptr< Promise< std::vector< std::shared_ptr< E > > > > | getTimeSeriesPromise (const SymbolWrapper &symbol, std::int64_t fromTime, std::int64_t toTime) const |
Requests time series of events for the specified event type, symbol, and a range of time. | |
std::string | toString () const override |
Returns a string representation of the current object. | |
Public Member Functions inherited from SharedEntity | |
template<typename T > | |
bool | is () const noexcept |
Checks that pointer to the current type could be converted to type T* In other words: whether type T belongs to the type hierarchy in which the current type resides. | |
template<typename T > | |
std::shared_ptr< T > | sharedAs () noexcept |
Returns a pointer to the current object wrapped in a smart pointer to type T. | |
template<typename T > | |
std::shared_ptr< T > | sharedAs () const noexcept |
Returns a pointer to the current object wrapped in a smart pointer to type T. | |
Public Member Functions inherited from Entity | |
virtual | ~Entity () noexcept=default |
The default virtual d-tor. | |
Static Public Member Functions | |
static std::shared_ptr< DXFeed > | getInstance () |
Returns a default application-wide singleton instance of feed. | |
Friends | |
struct | DXEndpoint |
Main entry class for dxFeed API (read it first).
This section gives sample usage scenarios.
There is a singleton instance of the feed that is returned by DXFeed::getInstance() method. It is created on the first use with default configuration properties that are explained in detail in documentation for DXEndpoint class in the "Default properties" section.
In particular, you can provide a default address to connect and credentials using "@ref DXEndpoint::DXFEED_ADDRESS_PROPERTY "dxfeed.address"", "@ref DXEndpoint::DXFEED_USER_PROPERTY "dxfeed.user"", and "@ref DXEndpoint::DXFEED_PASSWORD_PROPERTY "dxfeed.password"" system properties or by putting them into "@ref DXEndpoint::DXFEED_PROPERTIES_PROPERTY "dxfeed.properties"" file in the same directory. dxFeed API samples come with a ready-to-use "<b>dxfeed.properties</b>" file that contains an address of dxFeed demo feed at "<b>demo.dxfeed.com:7300</b>" and demo access credentials.
The following code creates listener that prints mid price for each quote and subscribes for quotes on SPDR S&P 500 ETF symbol:
auto sub = DXFeed::getInstance()->createSubscription(Quote::TYPE);
sub->addEventListener<Quote>([](const auto& quotes) {
for (const auto& quote : quotes) {
std::cout << "Mid = " + (quote->getBidPrice() + quote->getAskPrice()) / 2) << std::endl;
}
});
sub->addSymbols("SPY");
Note, that order of calls is important here. By attaching listeners first and then setting subscription we ensure that the current quote gets received by the listener. See DXFeedSubscription::addSymbols() for details. If a set of symbols is changed first, then sub->addEventListener raises an IllegalStateException in JVM to protected from hard-to-catch bugs with potentially missed events.
The following code creates listener that prints each received event and subscribes for quotes and trades on SPDR S&P 500 ETF symbol:
auto sub = DXFeed::getInstance()->createSubscription({Quote::TYPE, Trade::TYPE});
sub->addEventListener([](auto&& events) {
for (const auto& event : events) {
std::cout << event << std::endl;
}
});
sub->addSymbols("SPY");
The following code subscribes for trades on SPDR S&P 500 ETF symbol and prints last trade every second.
using namespace std::chrono_literals;
auto sub = DXFeed::getInstance()->createSubscription({Trade::TYPE});
sub->addSymbols("SPY");
auto feed = DXFeed::getInstance();
while (true) {
std::cout << System.out.println(feed->getLastEvent(Trade::create("SPY")));
std::this_thread::sleep_for(1000ms);
}
This class is thread-safe and can be used concurrently from multiple threads without external synchronization.
void DXFeed::attachSubscription | ( | std::shared_ptr< DXFeedSubscription > | subscription | ) |
Attaches the given subscription to this feed.
This method does nothing if the corresponding subscription is already attached to this feed.
This feed publishes data to the attached subscription. Application can attach event listener via DXFeedSubscription::addEventListener to get notified about data changes and can change its data subscription via DXFeedSubscription methods.
This method adds a non-serializable ObservableSubscriptionChangeListener for the given subscription via DXFeedSubscription::addChangeListener method.
subscription | The subscription. |
std::shared_ptr< DXFeedSubscription > DXFeed::createSubscription | ( | const EventTypeEnum & | eventType | ) |
Creates new subscription for a single event type that is attached to this feed.
This method creates new DXFeedSubscription and invokes Example: {cpp} auto sub = dxfcpp::DXFeed::getInstance()->createSubscription(dxfcpp::Quote::TYPE); eventType The type of event The new subscription
|
inline |
Creates new subscription for multiple event types that is attached to this feed.
This method creates new DXFeedSubscription and invokes Example: {cpp} auto eventTypes = {dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE}; auto sub = dxfcpp::DXFeed::getInstance()->createSubscription(eventTypes.begin(), eventTypes.end()); {cpp} std::vector types{dxfcpp::Quote::TYPE, dxfcpp::Trade::TYPE, dxfcpp::Summary::TYPE}; auto sub = dxfcpp::DXFeedSubscription::create(types.begin(), types.end()); {cpp} std::set types{dxfcpp::Quote::TYPE, dxfcpp::Trade::TYPE, dxfcpp::Summary::TYPE}; auto endpoint = dxfcpp::DXEndpoint::newBuilder()->withRole(dxfcpp::DXEndpoint::Role::FEED)->build(); auto sub = endpoint->getFeed()->createSubscription(eventTypes.begin(), eventTypes.end()); endpoint->connect("demo.dxfeed.com:7300"); EventTypeIt The iterator type of the collection of event types begin The start iterator end The end iterator The new subscription
|
inline |
Creates new subscription for multiple event types that is attached to this feed.
This method creates new DXFeedSubscription and invokes Example: {cpp} auto sub = dxfcpp::DXFeed::getInstance()->createSubscription(std::unordered_set{dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE}); {cpp} std::vector types = {dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE}; auto sub = dxfcpp::DXFeed::getInstance()->createSubscription(types); EventTypesCollection The class of the collection of event types eventTypes The collection of event types The new subscription
std::shared_ptr< DXFeedSubscription > DXFeed::createSubscription | ( | std::initializer_list< EventTypeEnum > | eventTypes | ) |
Creates new subscription for multiple event types that is attached to this feed.
This method creates new DXFeedSubscription and invokes Example: {cpp} auto sub = dxfcpp::DXFeed::getInstance()->createSubscription({dxfcpp::Quote::TYPE, dxfcpp::TimeAndSale::TYPE}); eventTypes The initializer list of event types The new subscription
void DXFeed::detachSubscription | ( | std::shared_ptr< DXFeedSubscription > | subscription | ) |
Detaches the given subscription from this feed.
This method does nothing if the corresponding subscription is not attached to this feed.
This method removes ObservableSubscriptionChangeListener from the given subscription via DXFeedSubscription::removeChangeListener method.
subscription | The subscription. |
void DXFeed::detachSubscriptionAndClear | ( | std::shared_ptr< DXFeedSubscription > | subscription | ) |
Detaches the given subscription from this feed and clears data delivered to this subscription by publishing empty events.
This method does nothing if the corresponding subscription is not attached to this feed.
subscription | The subscription. |
|
inline |
Requests a container of indexed events for the specified event type, symbol, and source.
This method works only for event types that implement IndexedEvent "interface". This method requests the data from the uplink data provider, creates a container of events of the specified eventType
, and completes the resulting promise with this container. The events are ordered by index in the container.
This method is designed for retrieval of a snapshot only. Use IndexedEventModel if you need a container of indexed events that updates in real time.
The promise is cancelled when the underlying DXEndpoint is closed. If the events are not available for any transient reason (no subscription, no connection to uplink, etc.), then the resulting promise completes when the issue is resolved, which may involve an arbitrarily long wait. Use Promise::await() method to specify timeout while waiting for promise to complete. If the events are permanently not available (not supported), then the promise completes exceptionally with JavaException "IllegalArgumentException".
Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).
Use the DEFAULT value for source
with events that do not have multiple sources (like Series). For events with multiple sources (like Order, AnalyticOrder, OtcMarketsOrder and SpreadOrder), use an event-specific source class (for example, OrderSource). This method does not support synthetic sources of orders (orders that are automatically generated from Quote events).
This method does not accept an instance of IndexedEventSubscriptionSymbol as a symbol
. The later class is designed for use with DXFeedSubscription and to observe source-specific subscription in DXPublisher.
This method completes promise only when a consistent snapshot of indexed events has been received from the data feed. The eventFlags property of the events in the resulting list is always zero.
Note, that the resulting list should not be used with DXPublisher::publishEvents() method, because the latter expects events in a different order and with an appropriate flags set. See documentation on a specific event class for details on how they should be published.
E | The type of event. |
symbol | The symbol. |
source | The source. |
|
static |
Returns a default application-wide singleton instance of feed.
Most applications use only a single data-source and should rely on this method to get one. This is a shortcut to DXEndpoint::getInstance()->getFeed().
|
inline |
Returns the last event for the specified event instance.
This method works only for event types that implement LastingEvent marker interface. This method does not make any remote calls to the uplink data provider. It just retrieves last received event from the local cache of this feed. The events are stored in the cache only if there is some attached DXFeedSubscription that is subscribed to the corresponding symbol and event type. WildcardSymbol::ALL subscription does not count for that purpose.
Use getLastEventPromise method if an event needs to be requested in the absence of subscription.
This method fills in the values for the last event into the event argument. If the last event is not available for any reason (no subscription, no connection to uplink, etc.) then the event object is not changed. This method always returns the same
event` instance that is passed to it as an argument.
This method provides no way to distinguish a case when there is no subscription from the case when there is a subscription, but the event data have not arrived yet. It is recommended to use getLastEventIfSubscribed method instead of this getLastEvent
method to fail-fast in case when the subscription was supposed to be set by the logic of the code, since getLastEventIfSubscribed method returns null when there is no subscription.
Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (never fills in the event).
E | The type of event. |
event | The event. |
|
inline |
Returns the last event for the specified event type and symbol if there is a subscription for it.
This method works only for event types that implement LastingEvent marker interface. This method does not make any remote calls to the uplink data provider. It just retrieves last received event from the local cache of this feed. The events are stored in the cache only if there is some attached DXFeedSubscription that is subscribed to the corresponding event type and symbol. The subscription can also be permanently defined using DXEndpoint properties. WildcardSymbol::ALL subscription does not count for that purpose. If there is no subscription, then this method returns std::shared_ptr<E>(nullptr)
.
If there is a subscription, but the event has not arrived from the uplink data provider, this method returns an non-initialized event object: its eventSymbol property is set to the requested symbol, but all the other properties have their default values.
Use getLastEventPromise method if an event needs to be requested in the absence of subscription.
Note, that this method does not work when DXEndpoint} was created with STREAM_FEED role (always returns std::shared_ptr<E>(nullptr)
).
E | The type of event. |
symbol | The symbol. |
std::shared_ptr<E>(nullptr)
if there is no subscription for the specified event type and symbol.
|
inline |
Requests the last event for the specified event type and symbol.
This method works only for event types that implement LastingEvent marker "interface". This method requests the data from the uplink data provider, creates new event of the specified event type, and completes the resulting promise with this event.
The promise is cancelled when the underlying DXEndpoint is closed. If the event is not available for any transient reason (no subscription, no connection to uplink, etc), then the resulting promise completes when the issue is resolved, which may involve an arbitrarily long wait. Use Promise::await() method to specify timeout while waiting for promise to complete. If the event is permanently not available (not supported), then the promise completes exceptionally with JavaException "IllegalArgumentException".
There is a bulk version of this method that works much faster for a single event type and multiple symbols. See getLastEventsPromises() .
Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).
E | The type of event. |
symbol | The symbol. |
|
inline |
Returns the last events for the specified list of event instances.
This is a bulk version of getLastEvent method.
Note, that this method does not work when DXEndpoint was created with STREAM_FEED role.
Collection | The collection type. |
events | The collection of shared ptrs of events. |
|
inline |
Requests the last events for the specified event type and a collection of symbols.
This method works only for event types that implement LastingEvent marker "interface". This method requests the data from the the uplink data provider, creates new events of the specified evet type, and completes the resulting promises with these events.
This is a bulk version of DXFeed::getLastEventPromise() method.
The promise is cancelled when the the underlying DXEndpoint is closed. If the event is not available for any transient reason (no subscription, no connection to uplink, etc), then the resulting promise completes when the issue is resolved, which may involve an arbitrarily long wait. Use Promise::await() method to specify timeout while waiting for promise to complete. If the event is permanently not available (not supported), then the promise completes exceptionally with JavaException "IllegalArgumentException".
Use the following pattern of code to acquire multiple events (either for multiple symbols and/or multiple events) and wait with a single timeout for all of them:
Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).
E | The event type. |
collection | The symbols collection. |
|
inline |
Requests the last events for the specified event type and a collection of symbols.
This method works only for event types that implement LastingEvent marker "interface". This method requests the data from the the uplink data provider, creates new events of the specified evet type, and completes the resulting promises with these events.
This is a bulk version of DXFeed::getLastEventPromise() method.
The promise is cancelled when the the underlying DXEndpoint is closed. If the event is not available for any transient reason (no subscription, no connection to uplink, etc), then the resulting promise completes when the issue is resolved, which may involve an arbitrarily long wait. Use Promise::await() method to specify timeout while waiting for promise to complete. If the event is permanently not available (not supported), then the promise completes exceptionally with JavaException "IllegalArgumentException".
Use the following pattern of code to acquire multiple events (either for multiple symbols and/or multiple events) and wait with a single timeout for all of them:
Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).
E | The event type. |
SymbolIt | The symbols collection's iterator type. |
begin | The beginning of the collection of symbols (SymbolWrapper). |
end | The end of the collection of symbols (SymbolWrapper). |
|
inline |
Requests the last events for the specified event type and a collection of symbols.
This method works only for event types that implement LastingEvent marker "interface". This method requests the data from the the uplink data provider, creates new events of the specified evet type, and completes the resulting promises with these events.
This is a bulk version of DXFeed::getLastEventPromise() method.
The promise is cancelled when the the underlying DXEndpoint is closed. If the event is not available for any transient reason (no subscription, no connection to uplink, etc), then the resulting promise completes when the issue is resolved, which may involve an arbitrarily long wait. Use Promise::await() method to specify timeout while waiting for promise to complete. If the event is permanently not available (not supported), then the promise completes exceptionally with JavaException "IllegalArgumentException".
Use the following pattern of code to acquire multiple events (either for multiple symbols and/or multiple events) and wait with a single timeout for all of them:
Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).
E | The event type. |
SymbolsCollection | The symbols collection's type. |
collection | The symbols collection. |
|
inline |
Requests time series of events for the specified event type, symbol, and a range of time.
This method works only for event types that implement TimeSeriesEvent "interface". This method requests the data from the uplink data provider, creates a list of events of the specified eventType
, and completes the resulting promise with this container. The events are ordered by time in the container.
This method is designed for retrieval of a snapshot only. Use TimeSeriesEventModel if you need a list of time-series events that updates in real time.
The range and depth of events that are available with this service is typically constrained by upstream data provider.
The promise is cancelled when the underlying DXEndpoint is closed.
If events are not available for any transient reason (no subscription, no connection to uplink, etc.), then the resulting promise completes when the issue is resolved, which may involve an arbitrarily long wait. Use EventsPromiseMixin::await() method to specify timeout while waiting for promise to complete. If events are permanently not available (not supported), then the promise completes exceptionally with JavaException "IllegalArgumentException".
Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).
This method does not accept an instance of TimeSeriesSubscriptionSymbol as a symbol
. The later class is designed for use with DXFeedSubscription and to observe time-series subscription in DXPublisher.
This method completes promise only when a consistent snapshot of time series has been received from the data feed. The eventFlags property of the events in the resulting container is always zero.
Note, that the resulting container should not be used with DXPublisher::publishEvents() method, because the latter expects events in a different order and with an appropriate flags set. See documentation on a specific event class for details on how they should be published.
E | The type of event. |
symbol | The symbol. |
fromTime | The time, inclusive, to request events from (see TimeSeriesEvent::getTime()). |
toTime | The time, inclusive, to request events to (see TimeSeriesEvent::getTime()). Use std::numeric_limits<std::int64_t>::max() or LLONG_MAX macro to retrieve events without an upper limit on time. |
|
overridevirtual |
Returns a string representation of the current object.
Reimplemented from SharedEntity.