0

I'm trying to get variables of a live trading order book using the 'python-binance' library.

depth = client.get_order_book(symbol='BTCUSDT')
print(depth)

This is the result.

'bids': [['34657.70000000', '0.57150000'], ['34655.76000000', '0.00035500'], ['34654.28000000', '0.01431800'][...]
'asks': [['34657.70000000', '0.57150000'], ['34655.76000000', '0.00035500'], ['34654.28000000', '0.01431800'][...]

The first value of each row is the price and the second one is the volume.

I would like to unify all 'price' values and 'volume' values into separate variables, so I would be able to sum all prices and all volumes separately.

Tried to find a related example using numpy or pandas but as you see, I'm such a newbie.

Thank you all

Delacroix
  • 49
  • 1
  • 8

2 Answers2

0

I predict you have those results in a dict.

price_vol = {}

for key in ['bids','asks']:
    for elem in depth[key]:
        price_vol[elem[0]] = elem[1]

That's it.

By the way, your exemple is wrong. You can't have same price for bids and ask positions at the same time ;)

Synthase
  • 5,849
  • 2
  • 12
  • 34
0

Assuming you are working with a dictionary you can use zip and a list comprehension that flattens a nested list to get what you want -

d = client.get_order_book(symbol='EURUSD')

bp, bv, ap, av = [j for i in d.values() for j in list(zip(*i))]

print('bids price:', bp)
print('bids volume:', bv)
print('asks price:', ap)
print('asks volume:', av)
bids price: ('34657.70000000', '34655.76000000', '34654.28000000')
bids volume: ('0.57150000', '0.00035500', '0.01431800')
asks price: ('34657.70000000', '34655.76000000', '34654.28000000')
asks volume: ('0.57150000', '0.00035500', '0.01431800')
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • I'm not using any dictionary. Unfortunately I have no idea how to apply them for this, could you please provide an example? I would really appreciate it. – Delacroix Jan 12 '21 at 19:10
  • My friend, `depth = client.get_order_book(symbol='EURUSD')`, in this code `depth` is a variable. It stores the order book. Once you have run this, you use `print(depth)`. Instead of that can you type `print(type(depth))` and tell me what you get? – Akshay Sehgal Jan 12 '21 at 19:15
  • You are right, it is the dict. Sorry for that. As last question, how can the variable 'd' take the values directly from 'depth', instead of putting the values manually as you did here? d = {..} – Delacroix Jan 12 '21 at 20:09
  • updated my answer, it should work now. Yes exactly how you mention in your comment. Do mark the answer if it helped you to solve your question as it encourages me to help you in the future as well. Thanks! – Akshay Sehgal Jan 12 '21 at 21:01
  • Thanks for your help. I'm getting this error : TypeError: zip() argument after * must be an iterable, not int – Delacroix Jan 12 '21 at 22:41
  • can you run `print(d.values()` and tell me what that returns? – Akshay Sehgal Jan 12 '21 at 23:06
  • I'm getting the error : SyntaxError: unexpected EOF while parsing – Delacroix Jan 13 '21 at 09:52
  • Sure. This is the traceback : Traceback (most recent call last): File "c:\Users\Dan\volume\orderbook.py", line 9, in bp, bv, ap, av = [j for i in d.values() for j in list(zip(*i))] File "c:\Users\Dan\volume\orderbook.py", line 9, in bp, bv, ap, av = [j for i in d.values() for j in list(zip(*i))] TypeError: zip() argument after * must be an iterable, not int – Delacroix Jan 13 '21 at 14:49
  • But this is `TypeError:`, you said you are getting `SyntaxError: unexpected EOF` – Akshay Sehgal Jan 13 '21 at 15:38