Created
October 9, 2024 09:48
-
-
Save MtkN1/fa7a202c65b41744feff56818d7580c3 to your computer and use it in GitHub Desktop.
Add “ts” to the Bybit Ticker store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import pybotters | |
class CustomTicker(pybotters.DataStore): | |
_KEYS = ["symbol"] | |
def _onmessage(self, msg, topic_ext: list[str]) -> None: | |
item = msg["data"] | |
item["ts"] = msg["ts"] | |
self._update([item]) | |
class CustomBybitDataStore(pybotters.BybitDataStore): | |
def __init__(self) -> None: | |
super().__init__() | |
self._create("tickers", datastore_class=CustomTicker) | |
@property | |
def ticker(self) -> "CustomTicker": | |
return self._get("tickers", CustomTicker) | |
async def main(): | |
async with pybotters.Client() as client: | |
store = CustomBybitDataStore() | |
await client.ws_connect( | |
"wss://stream.bybit.com/v5/public/spot", | |
send_json={ | |
"op": "subscribe", | |
"args": ["tickers.BTCUSDT"], | |
}, | |
hdlr_json=store.onmessage, | |
) | |
with store.ticker.watch() as stream: | |
async for change in stream: | |
print(change.data) | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment