Skip to content

Commit 1af22c8

Browse files
authored
Merge pull request #74 from GoneInactive/develop
Add public market data unsubscription methods to KrakenWebSocket
2 parents 5fbbd02 + fff654a commit 1af22c8

3 files changed

Lines changed: 119 additions & 1 deletion

File tree

data/trades/5_trades.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
{"timestamp": "2025-08-03 13:06:22.359621", "side": "BUY", "pair": "EURQ/USD", "quantity": 1.0074, "price": 1.1536, "total_value": 1.16213664, "account_id": 5, "base_currency": "EURQ", "quote_currency": "ZUSD", "balances_after": {"EURQ": 6.0074000000000005, "ZUSD": 48.1963899}}
2+
{"timestamp": "2025-08-04 15:54:52.890240", "side": "BUY", "pair": "EURQ/USD", "quantity": 1.97044, "price": 1.154, "total_value": 2.2738877599999996, "account_id": 5, "base_currency": "EURQ", "quote_currency": "ZUSD", "balances_after": {"EURQ": 7.9778400000000005, "ZUSD": 45.92250214}}
3+
{"timestamp": "2025-08-04 18:40:22.338490", "side": "BUY", "pair": "EURQ/USD", "quantity": 1.0, "price": 1.1548, "total_value": 1.1548, "account_id": 5, "base_currency": "EURQ", "quote_currency": "ZUSD", "balances_after": {"EURQ": 8.97784, "ZUSD": 44.76770214}}

kraken_ws/kraken_ws.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,122 @@ async def subscribe_book(self, symbols: List[str], depth: int = 10, handler: Opt
106106
self.subscriptions['book'] = subscription
107107
logger.info(f"Subscribed to order book for symbols: {symbols}, depth: {depth}")
108108

109+
# --- Public Market Data Unsubscription Methods (v2 format) ---
110+
111+
async def unsubscribe_book(self, symbols: List[str]):
112+
"""Unsubscribe from order book data using v2 format."""
113+
subscription = {
114+
"method": "unsubscribe",
115+
"params": {
116+
"channel": "book",
117+
"symbol": symbols
118+
}
119+
}
120+
121+
await self._send_public_subscription(subscription)
122+
123+
# Remove from stored subscriptions
124+
if 'book' in self.subscriptions:
125+
del self.subscriptions['book']
126+
127+
logger.info(f"Unsubscribed from order book for symbols: {symbols}")
128+
129+
async def unsubscribe_trades(self, symbols: List[str]):
130+
"""Unsubscribe from trade data using v2 format."""
131+
subscription = {
132+
"method": "unsubscribe",
133+
"params": {
134+
"channel": "trade",
135+
"symbol": symbols
136+
}
137+
}
138+
139+
await self._send_public_subscription(subscription)
140+
141+
# Remove from stored subscriptions
142+
if 'trade' in self.subscriptions:
143+
del self.subscriptions['trade']
144+
145+
logger.info(f"Unsubscribed from trades for symbols: {symbols}")
146+
147+
async def unsubscribe_ticker(self, symbols: List[str]):
148+
"""Unsubscribe from ticker data using v2 format."""
149+
subscription = {
150+
"method": "unsubscribe",
151+
"params": {
152+
"channel": "ticker",
153+
"symbol": symbols
154+
}
155+
}
156+
157+
await self._send_public_subscription(subscription)
158+
159+
# Remove from stored subscriptions
160+
if 'ticker' in self.subscriptions:
161+
del self.subscriptions['ticker']
162+
163+
logger.info(f"Unsubscribed from ticker for symbols: {symbols}")
164+
165+
async def unsubscribe_ohlc(self, symbols: List[str], interval: int = 1):
166+
"""Unsubscribe from OHLC data using v2 format."""
167+
subscription = {
168+
"method": "unsubscribe",
169+
"params": {
170+
"channel": "ohlc",
171+
"symbol": symbols,
172+
"interval": interval
173+
}
174+
}
175+
176+
await self._send_public_subscription(subscription)
177+
178+
# Remove from stored subscriptions
179+
if 'ohlc' in self.subscriptions:
180+
del self.subscriptions['ohlc']
181+
182+
logger.info(f"Unsubscribed from OHLC for symbols: {symbols}, interval: {interval}")
183+
184+
async def unsubscribe_all_public(self):
185+
"""Unsubscribe from all public market data subscriptions."""
186+
channels_to_unsubscribe = list(self.subscriptions.keys())
187+
188+
for channel in channels_to_unsubscribe:
189+
if channel in ['book', 'trade', 'ticker', 'ohlc']:
190+
subscription_info = self.subscriptions[channel]
191+
symbols = subscription_info['params']['symbol']
192+
193+
if channel == 'book':
194+
await self.unsubscribe_book(symbols)
195+
elif channel == 'trade':
196+
await self.unsubscribe_trades(symbols)
197+
elif channel == 'ticker':
198+
await self.unsubscribe_ticker(symbols)
199+
elif channel == 'ohlc':
200+
interval = subscription_info['params'].get('interval', 1)
201+
await self.unsubscribe_ohlc(symbols, interval)
202+
203+
logger.info("Unsubscribed from all public market data subscriptions")
204+
205+
def remove_handler(self, event_type: str, handler: Optional[Callable] = None):
206+
"""
207+
Remove a message handler for a specific data type.
208+
If handler is None, removes all handlers for that event type.
209+
"""
210+
if event_type in self.handlers:
211+
if handler is None:
212+
# Remove all handlers for this event type
213+
del self.handlers[event_type]
214+
logger.info(f"Removed all handlers for {event_type}")
215+
else:
216+
# Remove specific handler
217+
if handler in self.handlers[event_type]:
218+
self.handlers[event_type].remove(handler)
219+
logger.info(f"Removed specific handler for {event_type}")
220+
221+
# If no handlers left, remove the event type
222+
if not self.handlers[event_type]:
223+
del self.handlers[event_type]
224+
109225
async def subscribe_trades(self, symbols: List[str], handler: Optional[Callable] = None):
110226
"""Subscribe to trade data using v2 format."""
111227
if handler:

resources/data/settings/settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
program:
22
name: "TradeByte"
3-
version: "2.2.1"
3+
version: "2.2.2"
44
debug: false
55
version_notes:
66
- Version 2.2.1 8/2/2025

0 commit comments

Comments
 (0)