ITCH 5.0 Market Data Handler 1.0.0
NASDAQ ITCH 5.0 Market Data Feed Handler
Loading...
Searching...
No Matches
b2bits::nasdaq::itch50::Session Class Reference

ITCH 5.0 Market Data Session. More...

Detailed Description

ITCH 5.0 Market Data Session.

This class implements a complete ITCH 5.0 market data feed handler, providing:

  • Real-time market data processing via MoldUDP64 multicast
  • Snapshot recovery via SoupBinTCP
  • Order book management (aggregated or order-level)
  • Instrument filtering and lifecycle management
  • Gap detection and automatic recovery
  • Event-driven listener notifications

The Session maintains a collection of Instrument objects representing tracked order books. It processes incoming ITCH messages, updates the order books accordingly, and notifies registered listeners of events.

Note
This class is thread-safe when used with a single IO context
Listeners are called synchronously on the IO thread
See also
Instrument
SessionHandlers
moldudp64::Session
Example:
Basic session usage
settings.ss_settings_.remote_a_.ip_ = "127.0.0.1";
settings.ss_settings_.remote_a_.port_ = 65023;
settings.market_depth_ = 10;
settings.aggregated_mode_ = true;
auto session = std::make_shared<b2bits::nasdaq::itch50::Session>(settings);
session->attach_listener([](auto&&... args) {
// Handle market data events
});
session->start();
session->run(); // Blocking call
net::Endpoint remote_a_
Primary server endpoint (IP:Port).
Definition Settings.h:28
bool aggregated_mode_
Enable aggregated order book mode (price level aggregation).
Definition Session.h:38
soupbintcp::Settings ss_settings_
SoupBinTCP session settings for TCP connectivity.
Definition Session.h:41
std::uint16_t market_depth_
Maximum depth of order book to maintain (number of price levels).
Definition Session.h:44
Combined settings for ITCH 5.0 session.
Definition Session.h:59
std::uint16_t port_
Port number (0-65535, 0 = invalid).
Definition Endpoint.h:50
std::string ip_
IP address (IPv4 or IPv6 as string, e.g., "192.168.1.1" or "::1").
Definition Endpoint.h:49

#include <nasdaq/itch50/Session.h>

Inheritance diagram for b2bits::nasdaq::itch50::Session:

Classes

class  EOP
 End-of-packet marker type. More...

Public Types

using Timestamp = BookTraits::Timestamp
 Timestamp type for book updates (nanoseconds since epoch).
using ListenerType
 Listener delegate type.
Public Types inherited from b2bits::nasdaq::moldudp64::Session
enum class  State { InSync , Recovering , OutOfSync }
 Session synchronization state. More...
enum class  Source { Realtime , Snapshot , Cache }
 Message source indicator. More...
enum class  Event {
  LinkUp , LinkDown , EndOfSession , RecoveryStarted ,
  RecoveryStopped , RecoveryFailed , SecdefsAvailable
}
 Session lifecycle and recovery events. More...
using error_code = std::error_code
 Error code type alias.
using Seqnum = decltype(std::declval<UDPPacket>().get_sn())
 Sequence number type.

Public Member Functions

 Session (Settings s)
 Construct a new ITCH 5.0 Session.
 ~Session ()
 Destructor.
bool aggregated_mode () const
 Check if session is in aggregated mode.
template<typename Handler>
void attach_listener (Handler &&h)
 Attach a listener for market data events.
void detach_listener ()
 Detach the current listener.
void add_instrument (const OrderbookDirectory *info, std::any &&cookie=std::any{})
 Manually add an instrument to track.
const std::unordered_map< OrderbookID, std::unique_ptr< Instrument > > & get_instruments () const noexcept
 Get all tracked instruments.
void operator() (Instrument *, Seqnum sn, const AddOrder *)
 Handle add order update for an instrument.
template<class T>
void operator() (Instrument *, Seqnum sn, const T *, const Instrument::Order *)
 Handle generic order update (executed, cancel, replace).
void operator() (Instrument *, Timestamp timestamp, const Instrument::BookLevelUpdate &update)
 Handle aggregated book level update.
Public Member Functions inherited from b2bits::nasdaq::moldudp64::Session
 Session (std::shared_ptr< b2bits::IOContext > ctx, Settings s, const char *log_pfx=nullptr)
 Construct a MoldUDP64 session.
 ~Session ()
 Destructor.
bool is_started () const
 Check if session is started.
State get_state () const
 Get current session state.
const std::string & get_session () const
 Get active MoldUDP64 session name.
std::int64_t get_msg_recv_time () const
 Get timestamp of last received message.
auto get_packet_recv_time ()
 Get timestamp of last received packet.
void start ()
 Start the session.
void run ()
 Run the session (blocking).
void stop ()
 Stop the session (synchronous).
template<typename Handler>
void async_stop (Handler &&h)
 Stop the session asynchronously.
void pause_udp ()
 Pause UDP multicast reception.
void resume_udp ()
 Resume UDP multicast reception.
void operator() (Sequencer &, std::uint64_t sn, std::int64_t timestamp, const MessageBlock *h)
 Handle in-sequence message from sequencer.
void operator() (Sequencer &, Sequencer::ResetEvent)
 Handle sequencer reset event.
void operator() (Sequencer &, Sequencer::ReplayEvent)
 Handle sequencer replay event.

Additional Inherited Members

Protected Member Functions inherited from b2bits::nasdaq::moldudp64::Session
std::shared_ptr< b2bits::AsyncSentry > get_sentry ()
 Get the async operation sentry.
void set_session (const std::string &s)
 Set the active session name.
void set_msg_recv_time (std::int64_t val)
 Set the message receive timestamp.
void on_recovery_finished (Seqnum next_sn, const error_code &ec)
 Notify recovery completion.
Protected Attributes inherited from b2bits::nasdaq::moldudp64::Session
const std::string log_prefix_
 Prefix for log messages.
const Settings settings_
 Session configuration.
const std::int32_t id_
 Session identifier.
std::string active_session_
 Current MoldUDP64 session name.
bool link_is_up_ = true
 Multicast link status.
std::int64_t msg_recv_time_ = 0
 Timestamp of last received message.
std::shared_ptr< b2bits::IOContext > ctx_
 IO context for async operations.

Class Documentation

◆ b2bits::nasdaq::itch50::Session::EOP

class b2bits::nasdaq::itch50::Session::EOP

End-of-packet marker type.

Used as a sentinel value in listener callbacks to indicate the end of a MoldUDP64 packet.

Member Typedef Documentation

◆ ListenerType

Initial value:
void(Session*, const Instrument*, Seqnum, const SystemEvent*),
void(Session*, const Instrument*, Seqnum, const OrderBookState*),
void(Session*, const Instrument*, Seqnum, const OrderbookDirectory*),
void(Session*, const Instrument*, Seqnum, const AddOrder*),
void(Session*, const Instrument*, Seqnum, const OrderExecuted*, const Instrument::Order*),
void(Session*, const Instrument*, Seqnum, const OrderReplace*, const Instrument::Order*),
void(Session*, const Instrument*, Seqnum, const Trade*),
void(Session*, const Instrument*, Seqnum, const ExecutionDone*),
void(Session*, const Instrument*, Seqnum, const Information*),
void(Session*, const Instrument*, Seqnum, const BrokenTrade*),
void(Session*, const Instrument*, Timestamp timestamp, const Instrument::BookLevelUpdate&),
void(Session*, EOP),
void(Session*, Event)>
Multi-signature delegate (callback) container.
Definition Delegate.h:69
Represents a single trading instrument and its order book.
Definition Instrument.h:62
LevelUpdateEvent< BookTraits > BookLevelUpdate
Type alias for aggregated book level updates.
Definition Instrument.h:65
Represents a single order in the order book.
Definition Instrument.h:74
Session(Settings s)
Construct a new ITCH 5.0 Session.
BookTraits::Timestamp Timestamp
Timestamp type for book updates (nanoseconds since epoch).
Definition Session.h:120
End-of-packet marker type.
Definition Session.h:129
Event
Session lifecycle and recovery events.
Definition Session.h:138
decltype(std::declval< UDPPacket >().get_sn()) Seqnum
Sequence number type.
Definition Session.h:149
Definition Messages.h:404
Definition Messages.h:662
Definition Messages.h:620
Definition Messages.h:638
Definition Messages.h:446
Definition Messages.h:545
Definition Messages.h:362
Definition Messages.h:590

Listener delegate type.

Multi-signature delegate supporting all ITCH 5.0 market data events. Listeners receive callbacks for orders, trades, book updates, and session lifecycle events.

See also
SessionHandlers for a fluent interface wrapper

◆ Timestamp

Timestamp type for book updates (nanoseconds since epoch).

Constructor & Destructor Documentation

◆ Session()

b2bits::nasdaq::itch50::Session::Session ( Settings s)

Construct a new ITCH 5.0 Session.

Initializes the session with the provided settings, including network configuration, order book depth, and recovery parameters.

Parameters
sComplete session settings (ITCH + MoldUDP64)
Exceptions
std::runtime_errorif initialization fails

◆ ~Session()

b2bits::nasdaq::itch50::Session::~Session ( )

Destructor.

Ensures proper cleanup of resources and termination of the session. If the session is still running, it will be stopped before destruction.

Member Function Documentation

◆ add_instrument()

void b2bits::nasdaq::itch50::Session::add_instrument ( const OrderbookDirectory * info,
std::any && cookie = std::any{} )

Manually add an instrument to track.

Creates a new Instrument object for the given directory information. The instrument will be tracked and maintained by the session, receiving updates from incoming market data messages.

Parameters
infoOrderbook directory message containing instrument definition
cookieOptional user-defined data to associate with the instrument
Note
Instruments can also be added automatically based on directory messages

◆ aggregated_mode()

bool b2bits::nasdaq::itch50::Session::aggregated_mode ( ) const

Check if session is in aggregated mode.

Returns
true if order book aggregation is enabled
false if maintaining order-level detail

◆ attach_listener()

template<typename Handler>
void b2bits::nasdaq::itch50::Session::attach_listener ( Handler && h)
inline

Attach a listener for market data events.

Registers a callback handler that will be invoked for all market data events. The handler must be compatible with the ListenerType delegate signature (supports multiple overloads for different event types).

Template Parameters
HandlerType of the handler (typically a lambda or SessionHandlers)
Parameters
hHandler to attach (perfect-forwarded)
Note
Only one listener can be attached at a time
The handler is called synchronously on the IO thread
See also
SessionHandlers for a convenient fluent interface

◆ detach_listener()

void b2bits::nasdaq::itch50::Session::detach_listener ( )
inline

Detach the current listener.

Removes the currently attached listener. After this call, no events will be delivered until a new listener is attached.

◆ get_instruments()

const std::unordered_map< OrderbookID, std::unique_ptr< Instrument > > & b2bits::nasdaq::itch50::Session::get_instruments ( ) const
noexcept

Get all tracked instruments.

Returns a read-only reference to the map of all instruments currently being tracked by this session.

Returns
const reference to map of OrderbookID -> Instrument
Note
The map is keyed by OrderbookID (NASDAQ instrument identifier)

◆ operator()() [1/3]

void b2bits::nasdaq::itch50::Session::operator() ( Instrument * ,
Seqnum sn,
const AddOrder *  )

Handle add order update for an instrument.

Parameters
instTarget instrument
snSequence number
msgAdd order message

◆ operator()() [2/3]

template<class T>
void b2bits::nasdaq::itch50::Session::operator() ( Instrument * ,
Seqnum sn,
const T * ,
const Instrument::Order *  )

Handle generic order update (executed, cancel, replace).

Template Parameters
TMessage type (OrderExecuted, OrderCancel, etc.)
Parameters
instTarget instrument
snSequence number
msgOrder message
orderCurrent order state

◆ operator()() [3/3]

void b2bits::nasdaq::itch50::Session::operator() ( Instrument * ,
Timestamp timestamp,
const Instrument::BookLevelUpdate & update )

Handle aggregated book level update.

Parameters
instTarget instrument
timestampEvent timestamp
updateBook level update data

The documentation for this class was generated from the following file: