dxFeed Graal CXX API v4.0.0
Loading...
Searching...
No Matches
DXFeed Struct Reference

Main entry class for dxFeed API (read it first). More...

#include <DXFeed.hpp>

+ Inheritance diagram for DXFeed:

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< DXFeedSubscriptioncreateSubscription (const EventTypeEnum &eventType)
 Creates new subscription for a single event type that is attached to this feed.
 
template<typename EventTypeIt >
std::shared_ptr< DXFeedSubscriptioncreateSubscription (EventTypeIt begin, EventTypeIt end)
 Creates new subscription for multiple event types that is attached to this feed.
 
std::shared_ptr< DXFeedSubscriptioncreateSubscription (std::initializer_list< EventTypeEnum > eventTypes)
 Creates new subscription for multiple event types that is attached to this feed.
 
template<typename EventTypesCollection >
std::shared_ptr< DXFeedSubscriptioncreateSubscription (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< DXFeedgetInstance ()
 Returns a default application-wide singleton instance of feed.
 

Friends

struct DXEndpoint
 

Detailed Description

Main entry class for dxFeed API (read it first).

Sample usage

This section gives sample usage scenarios.

Default singleton instance

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.

Subscribe for single event type

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.

Subscribe for multiple event types

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");

Subscribe for event and query periodically its last value

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);
}

Threads and locks

This class is thread-safe and can be used concurrently from multiple threads without external synchronization.

Member Function Documentation

◆ attachSubscription()

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.

Implementation notes

This method adds a non-serializable ObservableSubscriptionChangeListener for the given subscription via DXFeedSubscription::addChangeListener method.

Parameters
subscriptionThe subscription.
See also
DXFeedSubscription

◆ createSubscription() [1/4]

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

◆ createSubscription() [2/4]

◆ createSubscription() [3/4]

◆ createSubscription() [4/4]

std::shared_ptr< DXFeedSubscription > DXFeed::createSubscription ( std::initializer_list< EventTypeEnum > eventTypes)

◆ detachSubscription()

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.

Implementation notes

This method removes ObservableSubscriptionChangeListener from the given subscription via DXFeedSubscription::removeChangeListener method.

Parameters
subscriptionThe subscription.
See also
DXFeedSubscription

◆ detachSubscriptionAndClear()

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.

Parameters
subscriptionThe subscription.
See also
DXFeed::detachSubscription()

◆ getIndexedEventsPromise()

template<Derived< IndexedEvent > E>
std::shared_ptr< Promise< std::vector< std::shared_ptr< E > > > > DXFeed::getIndexedEventsPromise ( const SymbolWrapper & symbol,
const IndexedEventSource & source ) const
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).

Event source

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.

Event flags and consistent snapshot

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.

Template Parameters
EThe type of event.
Parameters
symbolThe symbol.
sourceThe source.
Returns
The promise for the result of the request.

◆ getInstance()

DXFCPP_BEGIN_NAMESPACE std::shared_ptr< DXFeed > DXFeed::getInstance ( )
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().

Returns
The DXFeed instance

◆ getLastEvent()

template<Derived< LastingEvent > E>
std::shared_ptr< E > DXFeed::getLastEvent ( std::shared_ptr< E > event)
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 sameevent` 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).

Template Parameters
EThe type of event.
Parameters
eventThe event.
Returns
The same event.

◆ getLastEventIfSubscribed()

template<Derived< LastingEvent > E>
std::shared_ptr< E > DXFeed::getLastEventIfSubscribed ( const SymbolWrapper & symbol)
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)).

Template Parameters
EThe type of event.
Parameters
symbolThe symbol.
Returns
the event or std::shared_ptr<E>(nullptr) if there is no subscription for the specified event type and symbol.

◆ getLastEventPromise()

template<Derived< LastingEvent > E>
std::shared_ptr< Promise< std::shared_ptr< E > > > DXFeed::getLastEventPromise ( const SymbolWrapper & symbol) const
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).

Template Parameters
EThe type of event.
Parameters
symbolThe symbol.
Returns
The promise for the result of the request.

◆ getLastEvents()

template<typename Collection , typename Element = std::decay_t<decltype(std::begin(Collection()))>, typename Event = std::decay_t<decltype(*Element())>>
const Collection & DXFeed::getLastEvents ( const Collection & events)
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.

Template Parameters
CollectionThe collection type.
Parameters
eventsThe collection of shared ptrs of events.
Returns
The same collection of shared ptrs of events.

◆ getLastEventsPromises() [1/3]

template<Derived< LastingEvent > E>
std::shared_ptr< PromiseList< E > > DXFeed::getLastEventsPromises ( std::initializer_list< SymbolWrapper > collection) const
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:

auto promises = DXFeed::getInstance()->getLastEventsPromises<Quote>({"AAPL&Q", "IBM&Q"});
// combine the list of promises into one with Promises utility method and wait
Promises::allOf(*promises)->awaitWithoutException(std::chrono::seconds(timeout));
// now iterate the promises to retrieve results
for (const auto& promise : *promises) {
doSomethingWith(promise->getResult()); // InvalidArgumentException if result is nullptr
}
Quote event is a snapshot of the best bid and ask prices, and other fields that change with each quot...
Definition Quote.hpp:32
static std::shared_ptr< DXFeed > getInstance()
Returns a default application-wide singleton instance of feed.
Definition DXFeed.cpp:19
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.
Definition DXFeed.hpp:478
static std::shared_ptr< Promise< void > > allOf(Collection &&collection)
Returns a new promise that completes when all promises from the given collection complete normally or...
Definition Promises.hpp:38

Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).

Template Parameters
EThe event type.
Parameters
collectionThe symbols collection.
Returns
The list of promises for the result of the requests, one item in list per symbol.

◆ getLastEventsPromises() [2/3]

template<Derived< LastingEvent > E, typename SymbolIt >
std::shared_ptr< PromiseList< E > > DXFeed::getLastEventsPromises ( SymbolIt begin,
SymbolIt end ) const
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:

std::vector<dxfcpp::SymbolWrapper> symbols{"AAPL&Q", "IBM&Q"};
auto promises = DXFeed::getInstance()->getLastEventsPromises<Quote>(symbols.begin(), symbols.end());
// combine the list of promises into one with Promises utility method and wait
Promises::allOf(*promises)->awaitWithoutException(std::chrono::seconds(timeout));
// now iterate the promises to retrieve results
for (const auto& promise : *promises) {
doSomethingWith(promise->getResult()); // InvalidArgumentException if result is nullptr
}

Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).

Template Parameters
EThe event type.
SymbolItThe symbols collection's iterator type.
Parameters
beginThe beginning of the collection of symbols (SymbolWrapper).
endThe end of the collection of symbols (SymbolWrapper).
Returns
The list of promises for the result of the requests, one item in list per symbol.

◆ getLastEventsPromises() [3/3]

template<Derived< LastingEvent > E, ConvertibleToSymbolWrapperCollection SymbolsCollection>
std::shared_ptr< PromiseList< E > > DXFeed::getLastEventsPromises ( SymbolsCollection && collection) const
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:

std::vector<dxfcpp::SymbolWrapper> symbols{"AAPL&Q", "IBM&Q"};
auto promises = DXFeed::getInstance()->getLastEventsPromises<Quote>(symbols);
// combine the list of promises into one with Promises utility method and wait
Promises::allOf(*promises)->awaitWithoutException(std::chrono::seconds(timeout));
// now iterate the promises to retrieve results
for (const auto& promise : *promises) {
doSomethingWith(promise->getResult()); // InvalidArgumentException if result is nullptr
}

Note, that this method does not work when DXEndpoint was created with STREAM_FEED role (promise completes exceptionally).

Template Parameters
EThe event type.
SymbolsCollectionThe symbols collection's type.
Parameters
collectionThe symbols collection.
Returns
The list of promises for the result of the requests, one item in list per symbol.

◆ getTimeSeriesPromise()

template<Derived< TimeSeriesEvent > E>
std::shared_ptr< Promise< std::vector< std::shared_ptr< E > > > > DXFeed::getTimeSeriesPromise ( const SymbolWrapper & symbol,
std::int64_t fromTime,
std::int64_t toTime ) const
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.

Event flags

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.

Template Parameters
EThe type of event.
Parameters
symbolThe symbol.
fromTimeThe time, inclusive, to request events from (see TimeSeriesEvent::getTime()).
toTimeThe 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.
Returns
The promise for the result of the request.

◆ toString()

std::string DXFeed::toString ( ) const
overridevirtual

Returns a string representation of the current object.

Returns
a string representation

Reimplemented from SharedEntity.