I not 100% sure I understand your question, but if so this may help. I hope the sample data I used is sufficient. I use comments in-line to explain:
# from BybitWebsocket import BybitWebsocket
from time import sleep
def sample_gen():
"""Generator return one list item for each order with updates."""
samples = [
{"update": [{"price": "1", "side": "Buy", "size": 10}]},
{"update": [{"price": "1", "side": "Buy", "size": 11}]},
{
"update": [
{"price": "1", "side": "Buy", "size": 12},
{"price": "1", "side": "Buy", "size": 13},
{"price": "2", "side": "Sell", "size": 21},
]
},
[],
{
"update": [
{"price": "1", "side": "Buy", "size": 14},
{"price": "2", "side": "Sell", "size": 22},
]
},
[],
{"update": [{"price": "1", "side": "Sell", "size": 11}]},
{"update": [{"price": "1", "side": "Buy", "size": 15}]},
]
for sample in samples:
yield sample
if __name__ == "__main__":
# Commenting out the websockets bit.
# ws = BybitWebsocket(
# wsURL="wss://stream.bybit.com/realtime", api_key="", api_secret=""
# )
# ws.subscribe_orderBookL2("XRPUSD")
# Since tuples are hashable and you are operating based on size per
# combination of price and side, I suggest using price and side as
# a tuple key for a slightly different data structure.
price_side = {} # Initialized here for scope.
g = sample_gen() # Creating the generator for the sample data.
while True: # While True I see more commonly that `While 1`, but they are the same.
# Get one item from the sample data, and for each at the `update`
# key or an empty list.
order = next(g)
# order = ws.get_data("orderBookL2_25.XRPUSD")
if order:
for update in order.get("update", []):
price, side, size = (
update.get("price"),
update.get("side"),
update.get("size"),
)
# Using setdefault lets us start an empty list or append.
# This way for each price, side combination we can keep a
# running list of all size updates.
price_side.setdefault((price, side), []).append(size)
if len(price_side[(price, side)]) < 2: # Case where list has only 1.
print(f"{price} {side}: new price/side with size {size}")
continue # Continue to next update.
if price_side[(price, side)][-1] != price_side[(price, side)][-2]:
print(f"{price} {side}: price/side updated with size {size}")
sleep(0.1)
This yields:
1 Buy: new price/side with size 10
1 Buy: price/side updated with size 11
1 Buy: price/side updated with size 12
1 Buy: price/side updated with size 13
2 Sell: new price/side with size 21
1 Buy: price/side updated with size 14
2 Sell: price/side updated with size 22
1 Sell: new price/side with size 11
1 Buy: price/side updated with size 15
Traceback (most recent call last):
File "/Users/h4s/projects/github.com/theherk/tmp-py/ws/./main.py", line 50, in <module>
for order in next(g).get("update", []):
StopIteration
The StopIteration
here is just getting to the end of the generator.
With your code in place of sample data, this would be:
from BybitWebsocket import BybitWebsocket
from time import sleep
if __name__ == "__main__":
ws = BybitWebsocket(
wsURL="wss://stream.bybit.com/realtime", api_key="", api_secret=""
)
ws.subscribe_orderBookL2("XRPUSD")
price_side = {}
while True:
order = ws.get_data("orderBookL2_25.XRPUSD")
if order:
for update in order.get("update", []):
price, side, size = (
update.get("price"),
update.get("side"),
update.get("size"),
)
price_side.setdefault((price, side), []).append(size)
if len(price_side[(price, side)]) < 2:
print(f"{price} {side}: new price/side with size {size}")
continue
if price_side[(price, side)][-1] != price_side[(price, side)][-2]:
print(f"{price} {side}: price/side updated with size {size}")
sleep(0.1)