Skip to content

Instantly share code, notes, and snippets.

@MtkN1
Created October 9, 2024 09:48
Show Gist options
  • Save MtkN1/fa7a202c65b41744feff56818d7580c3 to your computer and use it in GitHub Desktop.
Save MtkN1/fa7a202c65b41744feff56818d7580c3 to your computer and use it in GitHub Desktop.
Add “ts” to the Bybit Ticker store
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