Trading Client Example#
A comprehensive production-ready trading client example demonstrating all OUCH 5.0 features.
Overview#
The trading_client.cpp example demonstrates:
Full-featured
TradingClientclass with async operationsSession initialization and configuration
Async connection with UserRefNum recovery
Order state tracking
Error handling and reconnection logic
Message buffering and retransmission
All OUCH 5.0 message types
TagValue appendage usage
Manual failover support
Features#
Async connection/disconnection with futures
Automatic UserRefNum recovery after connection
Order tracking and state management
Comprehensive error handling
Reconnection with manual failover
Message buffering for retransmission
All OUCH 5.0 features demonstrated
Usage#
./Ouch5TradingClient
Key Components#
TradingClient Class#
class TradingClient {
public:
// Async connection
std::future<void> connect_async();
// Async disconnection
std::future<void> disconnect_async();
// Send order
void send_order(const Order& order);
// Get order state
OrderState get_order_state(UInt32 user_ref_num);
private:
std::shared_ptr<IOContext> io_ctx_;
std::unique_ptr<Session> session_;
std::map<UInt32, OrderState> order_states_;
};
Async Connection#
std::future<void> TradingClient::connect_async() {
auto promise = std::make_shared<std::promise<void>>();
auto future = promise->get_future();
session_->async_login("", 0, std::chrono::milliseconds(2000),
[this, promise](std::uint64_t seq, const std::error_code& ec) {
if (!ec) {
// Recover UserRefNum
recover_user_refnum();
promise->set_value();
} else {
promise->set_exception(std::make_exception_ptr(std::runtime_error(ec.message())));
}
});
return future;
}
Order State Tracking#
void on_order_accepted(Session* s, std::uint64_t seq, const OrderAccepted* msg) {
UInt32 user_ref_num = msg->get_user_ref_num();
order_states_[user_ref_num] = OrderState::Accepted;
}
void on_order_executed(Session* s, std::uint64_t seq, const OrderExecuted* msg) {
UInt32 user_ref_num = msg->get_user_ref_num();
auto& state = order_states_[user_ref_num];
state.executed_quantity_ += msg->get_quantity();
}
Error Handling#
void on_rejected(Session* s, std::uint64_t seq, const Rejected* msg) {
UInt32 user_ref_num = msg->get_user_ref_num();
RejectReason reason = msg->get_reason();
std::cerr << "Order rejected: UserRefNum=" << user_ref_num
<< " Reason=" << static_cast<int>(reason) << std::endl;
// Update order state
order_states_[user_ref_num] = OrderState::Rejected;
}
Complete Example#
See the full source code in nasdaq/ouch50/examples/trading_client.cpp for the complete implementation.