pub struct KrakenSpotWebSocketClient { /* private fields */ }Expand description
WebSocket client for the Kraken Spot v2 streaming API.
Implementations§
Source§impl KrakenSpotWebSocketClient
impl KrakenSpotWebSocketClient
Sourcepub fn new(
config: KrakenDataClientConfig,
cancellation_token: CancellationToken,
proxy_url: Option<String>,
) -> Self
pub fn new( config: KrakenDataClientConfig, cancellation_token: CancellationToken, proxy_url: Option<String>, ) -> Self
Creates a new client for the configured public/private endpoint.
Sourcepub fn l3(
config: KrakenDataClientConfig,
cancellation_token: CancellationToken,
proxy_url: Option<String>,
) -> Self
pub fn l3( config: KrakenDataClientConfig, cancellation_token: CancellationToken, proxy_url: Option<String>, ) -> Self
Creates a new client configured for the Kraken Spot level3 WebSocket endpoint.
Selects config.ws_l3_url() and otherwise mirrors Self::new. Level3
subscriptions are treated as authenticated and must follow authenticate().
Sourcepub fn req_id_counter(&self) -> Arc<AtomicU64> ⓘ
pub fn req_id_counter(&self) -> Arc<AtomicU64> ⓘ
Returns the shared request-id counter.
Sourcepub async fn handler_command_sender(
&self,
) -> UnboundedSender<SpotHandlerCommand>
pub async fn handler_command_sender( &self, ) -> UnboundedSender<SpotHandlerCommand>
Returns a clone of the handler command channel sender.
Sourcepub fn handler_command_handle(
&self,
) -> Arc<RwLock<UnboundedSender<SpotHandlerCommand>>> ⓘ
pub fn handler_command_handle( &self, ) -> Arc<RwLock<UnboundedSender<SpotHandlerCommand>>> ⓘ
Returns the shared cmd_tx handle. Unlike
handler_command_sender (a snapshot
clone), this exposes the RwLock so callers see the live sender
after connect() swaps it in.
Sourcepub async fn auth_token(&self) -> Option<String>
pub async fn auth_token(&self) -> Option<String>
Returns the current cached authentication token, if any.
Sourcepub fn auth_token_blocking(&self) -> Option<String>
pub fn auth_token_blocking(&self) -> Option<String>
Returns the current cached authentication token without awaiting.
Returns None when the lock is contended or no token is cached. Used by
the synchronous order-routing path where the auth token is normally
uncontended; callers fall back to REST when the lock is unavailable.
Sourcepub fn auth_token_handle(&self) -> Arc<RwLock<Option<String>>> ⓘ
pub fn auth_token_handle(&self) -> Arc<RwLock<Option<String>>> ⓘ
Returns a clone of the auth token handle for components that need
non-async, lock-free read access (e.g. compensating cancels triggered
from the timeout task in OrderRequestState).
Sourcepub async fn connect(&mut self) -> Result<(), KrakenWsError>
pub async fn connect(&mut self) -> Result<(), KrakenWsError>
Connects to the WebSocket server.
Sourcepub async fn disconnect(&mut self) -> Result<(), KrakenWsError>
pub async fn disconnect(&mut self) -> Result<(), KrakenWsError>
Disconnects from the WebSocket server.
Sourcepub async fn close(&mut self) -> Result<(), KrakenWsError>
pub async fn close(&mut self) -> Result<(), KrakenWsError>
Closes the WebSocket connection.
Sourcepub async fn wait_until_active(
&self,
timeout_secs: f64,
) -> Result<(), KrakenWsError>
pub async fn wait_until_active( &self, timeout_secs: f64, ) -> Result<(), KrakenWsError>
Waits until the connection is active or timeout.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Returns true if the WebSocket is authenticated for private subscriptions.
Sourcepub async fn wait_until_authenticated(
&self,
timeout_secs: f64,
) -> Result<(), KrakenWsError>
pub async fn wait_until_authenticated( &self, timeout_secs: f64, ) -> Result<(), KrakenWsError>
Waits until the WebSocket is authenticated or the timeout elapses.
Returns an error on timeout or explicit auth failure.
Sourcepub async fn authenticate(&self) -> Result<(), KrakenWsError>
pub async fn authenticate(&self) -> Result<(), KrakenWsError>
Authenticates with the Kraken API to enable private subscriptions.
Sourcepub fn cancel_all_requests(&self)
pub fn cancel_all_requests(&self)
Cancels all pending requests.
Sourcepub fn cancellation_token(&self) -> &CancellationToken
pub fn cancellation_token(&self) -> &CancellationToken
Returns the cancellation token for this client.
Sourcepub async fn subscribe(
&self,
channel: KrakenWsChannel,
symbols: Vec<Ustr>,
depth: Option<u32>,
) -> Result<(), KrakenWsError>
pub async fn subscribe( &self, channel: KrakenWsChannel, symbols: Vec<Ustr>, depth: Option<u32>, ) -> Result<(), KrakenWsError>
Subscribes to a channel for the given symbols.
Sourcepub async fn unsubscribe(
&self,
channel: KrakenWsChannel,
symbols: Vec<Ustr>,
) -> Result<(), KrakenWsError>
pub async fn unsubscribe( &self, channel: KrakenWsChannel, symbols: Vec<Ustr>, ) -> Result<(), KrakenWsError>
Unsubscribes from a channel for the given symbols.
Sourcepub async fn send_ping(&self) -> Result<(), KrakenWsError>
pub async fn send_ping(&self) -> Result<(), KrakenWsError>
Sends a ping message to keep the connection alive.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns true if connected (not closed).
Sourcepub fn get_subscriptions(&self) -> Vec<String>
pub fn get_subscriptions(&self) -> Vec<String>
Returns all active subscriptions.
Sourcepub fn subscriptions_contains(&self, topic: &str) -> bool
pub fn subscriptions_contains(&self, topic: &str) -> bool
Returns true if a topic is currently subscribed (confirmed).
Sourcepub fn set_account_id(&self, account_id: AccountId)
pub fn set_account_id(&self, account_id: AccountId)
Sets the account ID for execution report parsing.
Sourcepub fn account_id(&self) -> Option<AccountId>
pub fn account_id(&self) -> Option<AccountId>
Returns the account ID if set.
Sourcepub fn cache_instrument(&self, instrument: InstrumentAny)
pub fn cache_instrument(&self, instrument: InstrumentAny)
Caches an instrument for execution report parsing.
Returns a shared reference to the account ID.
Sourcepub fn truncated_id_map(&self) -> &Arc<AtomicMap<String, ClientOrderId>> ⓘ
pub fn truncated_id_map(&self) -> &Arc<AtomicMap<String, ClientOrderId>> ⓘ
Returns a shared reference to the truncated ID map.
Sourcepub fn cache_client_order(
&self,
client_order_id: ClientOrderId,
_venue_order_id: Option<VenueOrderId>,
_instrument_id: InstrumentId,
_trader_id: TraderId,
_strategy_id: StrategyId,
)
pub fn cache_client_order( &self, client_order_id: ClientOrderId, _venue_order_id: Option<VenueOrderId>, _instrument_id: InstrumentId, _trader_id: TraderId, _strategy_id: StrategyId, )
Caches a client order for truncated ID resolution.
Sourcepub fn stream(
&mut self,
) -> Result<impl Stream<Item = KrakenSpotWsMessage> + use<>, KrakenWsError>
pub fn stream( &mut self, ) -> Result<impl Stream<Item = KrakenSpotWsMessage> + use<>, KrakenWsError>
Returns a stream of WebSocket messages.
§Errors
Returns an error if:
- The stream receiver has already been taken
- Other clones of this client still hold references to the receiver
Sourcepub async fn subscribe_book(
&self,
instrument_id: InstrumentId,
depth: Option<u32>,
) -> Result<(), KrakenWsError>
pub async fn subscribe_book( &self, instrument_id: InstrumentId, depth: Option<u32>, ) -> Result<(), KrakenWsError>
Subscribes to order book updates for the given instrument.
Sourcepub async fn subscribe_book_l3(
&self,
symbol: Ustr,
depth: u32,
) -> Result<(), KrakenWsError>
pub async fn subscribe_book_l3( &self, symbol: Ustr, depth: u32, ) -> Result<(), KrakenWsError>
Subscribes to the level3 channel for the given Kraken symbol.
depth must be one of 10, 100, or 1000. The depth is recorded in
the per-client l3_depths map so the message handler and resync_book_l3
can recover it without a separate side-table.
If the symbol is already subscribed, the existing depth must match — Kraken
streams one depth per (symbol, channel) pair, so a second subscribe with
a different depth would corrupt the local runtime state. Mismatch returns
an error without mutating state.
§Errors
Returns an error if depth is invalid, the auth token is not cached
(call authenticate first), the requested depth differs from an existing
subscription, or the message cannot be sent. On any of those failures
the reference count and pending state are rolled back fully so callers
retry from a clean state.
Sourcepub async fn unsubscribe_book_l3(
&self,
symbol: Ustr,
) -> Result<(), KrakenWsError>
pub async fn unsubscribe_book_l3( &self, symbol: Ustr, ) -> Result<(), KrakenWsError>
Unsubscribes from the level3 channel for the given Kraken symbol.
Mirrors the existing KrakenSpotWebSocketClient::unsubscribe pattern:
SubscriptionState is mutated optimistically before send_command. If
the send fails, the local state reflects an unsubscribe that the venue
never received; the next reconnect’s payload replay will not include the
topic (it was removed from subscription_payloads). Tightening this to
roll-back-on-send-fail is a codebase-wide pattern change (every channel
has it) and is out of scope for this PR — the L3 path matches the
existing surface area rather than introducing an inconsistent improvement.
§Errors
Returns an error if the message cannot be sent.
Sourcepub async fn resync_book_l3(
&self,
symbol: Ustr,
depth: u32,
) -> Result<(), KrakenWsError>
pub async fn resync_book_l3( &self, symbol: Ustr, depth: u32, ) -> Result<(), KrakenWsError>
Resynchronizes the level3 book for symbol after a checksum mismatch.
Refreshes the auth token unconditionally and issues a venue-level
unsubscribe followed by a subscribe with snapshot=true, bypassing
SubscriptionState reference counts so the user’s logical
subscription survives. The reference count is not changed; if multiple
callers hold references to the same symbol, all of them continue to see
the symbol as subscribed throughout the resync.
§Errors
Returns an error if the auth token cannot be refreshed or the unsubscribe/subscribe messages cannot be sent.
Sourcepub fn validate_l3_checksum(&self) -> bool
pub fn validate_l3_checksum(&self) -> bool
Returns whether L3 checksum validation is enabled for this client.
Sourcepub fn has_credentials(&self) -> bool
pub fn has_credentials(&self) -> bool
Returns true if the client has API credentials configured
(post-environment-variable resolution).
Sourcepub fn instruments_handle(&self) -> Arc<AtomicMap<InstrumentId, InstrumentAny>> ⓘ
pub fn instruments_handle(&self) -> Arc<AtomicMap<InstrumentId, InstrumentAny>> ⓘ
Returns a shared handle to the per-client instrument map.
L3 stream-loop consumers read instruments through this handle so that
cache_instrument() updates made after connect() are observed by the
runtime book reconstruction without needing a re-connect.
Sourcepub fn l3_depths_handle(&self) -> Arc<Mutex<AHashMap<String, u32>>> ⓘ
pub fn l3_depths_handle(&self) -> Arc<Mutex<AHashMap<String, u32>>> ⓘ
Returns a shared handle to the per-symbol L3 depth map.
Stream-loop consumers read this map to drive process_l3_message’s
resync depth lookup; subscribe_book_l3 writes the depth.
Sourcepub async fn subscribe_quotes(
&self,
instrument_id: InstrumentId,
) -> Result<(), KrakenWsError>
pub async fn subscribe_quotes( &self, instrument_id: InstrumentId, ) -> Result<(), KrakenWsError>
Subscribes to quote updates for the given instrument.
Uses the Ticker channel with event_trigger: "bbo" for updates only on
best bid/offer changes.
Sourcepub async fn subscribe_trades(
&self,
instrument_id: InstrumentId,
) -> Result<(), KrakenWsError>
pub async fn subscribe_trades( &self, instrument_id: InstrumentId, ) -> Result<(), KrakenWsError>
Subscribes to trade updates for the given instrument.
Sourcepub async fn subscribe_bars(
&self,
bar_type: BarType,
) -> Result<(), KrakenWsError>
pub async fn subscribe_bars( &self, bar_type: BarType, ) -> Result<(), KrakenWsError>
Subscribes to bar/OHLC updates for the given bar type.
§Errors
Returns an error if the bar aggregation is not supported by Kraken.
Sourcepub async fn subscribe_executions(
&self,
snap_orders: bool,
snap_trades: bool,
) -> Result<(), KrakenWsError>
pub async fn subscribe_executions( &self, snap_orders: bool, snap_trades: bool, ) -> Result<(), KrakenWsError>
Subscribes to execution updates (order and fill events).
Requires authentication - call authenticate() first.
Sourcepub async fn unsubscribe_book(
&self,
instrument_id: InstrumentId,
) -> Result<(), KrakenWsError>
pub async fn unsubscribe_book( &self, instrument_id: InstrumentId, ) -> Result<(), KrakenWsError>
Unsubscribes from order book updates for the given instrument.
Sourcepub async fn unsubscribe_quotes(
&self,
instrument_id: InstrumentId,
) -> Result<(), KrakenWsError>
pub async fn unsubscribe_quotes( &self, instrument_id: InstrumentId, ) -> Result<(), KrakenWsError>
Unsubscribes from quote updates for the given instrument.
Sourcepub async fn unsubscribe_trades(
&self,
instrument_id: InstrumentId,
) -> Result<(), KrakenWsError>
pub async fn unsubscribe_trades( &self, instrument_id: InstrumentId, ) -> Result<(), KrakenWsError>
Unsubscribes from trade updates for the given instrument.
Sourcepub async fn unsubscribe_bars(
&self,
bar_type: BarType,
) -> Result<(), KrakenWsError>
pub async fn unsubscribe_bars( &self, bar_type: BarType, ) -> Result<(), KrakenWsError>
Unsubscribes from bar/OHLC updates for the given bar type.
§Errors
Returns an error if the bar aggregation is not supported by Kraken.
Trait Implementations§
Source§impl Clone for KrakenSpotWebSocketClient
impl Clone for KrakenSpotWebSocketClient
Source§impl Debug for KrakenSpotWebSocketClient
impl Debug for KrakenSpotWebSocketClient
impl DerefToPyAny for KrakenSpotWebSocketClient
Source§impl<'a, 'py> FromPyObject<'a, 'py> for KrakenSpotWebSocketClientwhere
Self: Clone,
impl<'a, 'py> FromPyObject<'a, 'py> for KrakenSpotWebSocketClientwhere
Self: Clone,
Source§impl<'py> IntoPyObject<'py> for KrakenSpotWebSocketClient
impl<'py> IntoPyObject<'py> for KrakenSpotWebSocketClient
Source§type Target = KrakenSpotWebSocketClient
type Target = KrakenSpotWebSocketClient
Source§type Output = Bound<'py, <KrakenSpotWebSocketClient as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <KrakenSpotWebSocketClient as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl PyClass for KrakenSpotWebSocketClient
impl PyClass for KrakenSpotWebSocketClient
Source§impl PyClassImpl for KrakenSpotWebSocketClient
impl PyClassImpl for KrakenSpotWebSocketClient
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// WebSocket client for the Kraken Spot v2 streaming API.
const RAW_DOC: &'static CStr = /// WebSocket client for the Kraken Spot v2 streaming API.
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type Layout = <<KrakenSpotWebSocketClient as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<KrakenSpotWebSocketClient>
type Layout = <<KrakenSpotWebSocketClient as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<KrakenSpotWebSocketClient>
Source§type ThreadChecker = NoopThreadChecker
type ThreadChecker = NoopThreadChecker
type Inventory = Pyo3MethodsInventoryForKrakenSpotWebSocketClient
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
§fn dict_offset() -> Option<PyObjectOffset>
fn dict_offset() -> Option<PyObjectOffset>
§fn weaklist_offset() -> Option<PyObjectOffset>
fn weaklist_offset() -> Option<PyObjectOffset>
Source§impl PyClassNewTextSignature for KrakenSpotWebSocketClient
impl PyClassNewTextSignature for KrakenSpotWebSocketClient
const TEXT_SIGNATURE: &'static str = "(environment=None, private=False, base_url=None, heartbeat_secs=None, api_key=None, api_secret=None, proxy_url=None, l3=False, validate_l3_checksum=True, base_url_http=None)"
Source§impl PyStubType for KrakenSpotWebSocketClient
impl PyStubType for KrakenSpotWebSocketClient
Source§fn type_output() -> TypeInfo
fn type_output() -> TypeInfo
§fn type_input() -> TypeInfo
fn type_input() -> TypeInfo
Source§impl PyTypeInfo for KrakenSpotWebSocketClient
impl PyTypeInfo for KrakenSpotWebSocketClient
Source§const NAME: &str = <Self as ::pyo3::PyClass>::NAME
const NAME: &str = <Self as ::pyo3::PyClass>::NAME
prefer using ::type_object(py).name() to get the correct runtime value
Source§const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
prefer using ::type_object(py).module() to get the correct runtime value
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.Auto Trait Implementations§
impl !RefUnwindSafe for KrakenSpotWebSocketClient
impl !UnwindSafe for KrakenSpotWebSocketClient
impl Freeze for KrakenSpotWebSocketClient
impl Send for KrakenSpotWebSocketClient
impl Sync for KrakenSpotWebSocketClient
impl Unpin for KrakenSpotWebSocketClient
impl UnsafeUnpin for KrakenSpotWebSocketClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<'py, T> FromPyObjectOwned<'py> for Twhere
T: for<'a> FromPyObject<'a, 'py>,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read more§impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
§fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§fn type_check(object: &Bound<'_, PyAny>) -> bool
fn type_check(object: &Bound<'_, PyAny>) -> bool
§fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
isinstance and issubclass function. Read more