Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

algorithmic trading - Split values separated by commas into variables in Python

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...