dxFeed Graal CXX API
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) noexcept
 Attaches the given subscription to this feed.
 
void detachSubscription (std::shared_ptr< DXFeedSubscription > subscription) noexcept
 Detaches the given subscription from this feed.
 
void detachSubscriptionAndClear (std::shared_ptr< DXFeedSubscription > subscription) noexcept
 Detaches the given subscription from this feed and clears data delivered to this subscription by publishing empty events.
 
std::shared_ptr< DXFeedSubscriptioncreateSubscription (const EventTypeEnum &eventType) noexcept
 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) noexcept
 Creates new subscription for multiple event types that is attached to this feed.
 
std::shared_ptr< DXFeedSubscriptioncreateSubscription (std::initializer_list< EventTypeEnum > eventTypes) noexcept
 Creates new subscription for multiple event types that is attached to this feed.
 
template<typename EventTypesCollection >
std::shared_ptr< DXFeedSubscriptioncreateSubscription (EventTypesCollection &&eventTypes) noexcept
 Creates new subscription for multiple event types that is attached to this feed.
 
std::string toString () const noexcept 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 () noexcept
 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)
noexcept

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)
noexcept

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

References EventTypeEnum::getName(), and toString().

◆ createSubscription() [2/4]

◆ createSubscription() [3/4]

◆ createSubscription() [4/4]

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

◆ detachSubscription()

void DXFeed::detachSubscription ( std::shared_ptr< DXFeedSubscription > subscription)
noexcept

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)
noexcept

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()

◆ getInstance()

DXFCPP_BEGIN_NAMESPACE std::shared_ptr< DXFeed > DXFeed::getInstance ( )
staticnoexcept

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

◆ toString()

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

Returns a string representation of the current object.

Returns
a string representation

Reimplemented from SharedEntity.

Referenced by createSubscription().