@@ -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 :
0 commit comments