Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get total sum of nested list's total value in a while loop?

I am trying to get buy and sell orders from binance api(python-binance) which has a limit of 500 values.(500 ask,500 buy). I already can get this with creating 500 variables with index numbers but it seems to me there has to be a better way than to write 500 lines of code.
This is the code I am trying to make it happen.

#!/usr/bin/python
# -*- coding: utf-8 -*-
from binance.client import Client

user_key = ''
secret_key = ''
binance_client = Client(user_key, secret_key)

while True:
    alis = binance_client.futures_order_book(symbol='XRPUSDT')

    binance_buy = alis['bids'][0]
    binance_buy1 = alis['bids'][1]
    binance_buy2 = alis['bids'][2]
    binance_buy3 = alis['bids'][3]
    binance_buy4 = alis['bids'][4]
    binance_buy5 = alis['bids'][5]
    binance_buy6 = alis['bids'][6]
    binance_buy7 = alis['bids'][7]
    binance_buy8 = alis['bids'][8]
    binance_buy9 = alis['bids'][9]
    binance_buy10 = alis['bids'][10]
    binance_buy11 = alis['bids'][11]
    binance_buy12 = alis['bids'][12]
    binance_buy13 = alis['bids'][13]
    binance_buy14 = alis['bids'][14]
    binance_buy15 = alis['bids'][15]
    binance_buy16 = alis['bids'][16]
    binance_buy17 = alis['bids'][17]
    binance_buy18 = alis['bids'][18]
    binance_buy19 = alis['bids'][19]
    binance_buy20 = alis['bids'][20]
    binance_sell = alis['asks'][0]
    binance_sell1 = alis['asks'][1]
    binance_sell2 = alis['asks'][2]
    binance_sell3 = alis['asks'][3]
    binance_sell4 = alis['asks'][4]
    binance_sell5 = alis['asks'][5]
    binance_sell6 = alis['asks'][6]
    binance_sell7 = alis['asks'][7]
    binance_sell8 = alis['asks'][8]
    binance_sell9 = alis['asks'][9]
    binance_sell10 = alis['asks'][10]
    binance_sell11 = alis['asks'][11]
    binance_sell12 = alis['asks'][12]
    binance_sell13 = alis['asks'][13]
    binance_sell14 = alis['asks'][14]
    binance_sell15 = alis['asks'][15]
    binance_sell16 = alis['asks'][16]
    binance_sell17 = alis['asks'][17]
    binance_sell18 = alis['asks'][18]
    binance_sell19 = alis['asks'][19]
    binance_sell20 = alis['asks'][20]

    binance_buy_demand = float(binance_buy[1]) + float(binance_buy1[1]) \
        + float(binance_buy2[1]) + float(binance_buy3[1]) \
        + float(binance_buy4[1]) + float(binance_buy5[1]) \
        + float(binance_buy6[1]) + float(binance_buy7[1]) \
        + float(binance_buy8[1]) + float(binance_buy9[1]) \
        + float(binance_buy10[1]) + float(binance_buy11[1]) \
        + float(binance_buy12[1]) + float(binance_buy13[1]) \
        + float(binance_buy14[1]) + float(binance_buy15[1]) \
        + float(binance_buy16[1]) + float(binance_buy17[1]) \
        + float(binance_buy18[1]) + float(binance_buy19[1]) \
        + float(binance_buy20[1])

    for i in range(0, 500):
        print (alis['asks'][i][0], alis['asks'][i][1])

    binance_sell_demand = float(binance_sell[1]) \
        + float(binance_sell1[1]) + float(binance_sell2[1]) \
        + float(binance_sell3[1]) + float(binance_sell4[1]) \
        + float(binance_sell5[1]) + float(binance_sell6[1]) \
        + float(binance_sell7[1]) + float(binance_sell8[1]) \
        + float(binance_sell9[1]) + float(binance_sell10[1]) \
        + float(binance_sell11[1]) + float(binance_sell12[1]) \
        + float(binance_sell13[1]) + float(binance_sell14[1]) \
        + float(binance_sell15[1]) + float(binance_sell16[1]) \
        + float(binance_sell17[1]) + float(binance_sell18[1]) \
        + float(binance_sell19[1]) + float(binance_sell20[1])

there is 500 bids and 500 asks
after getting data I sum bids ands asks like this

I tried to make for loop but only could print this values cant sum it in for loop this is the code I tried:
sample output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

0.9315 18328.6
0.9316 18201.2
0.9317 23544.0
0.9318 260.4
0.9319 689.5
0.9320 20410.5
0.9321 47.7
0.9322 294.2
0.9323 446.6
0.9324 104.0
0.9325 3802.3
0.9326 100.1
0.9327 20122.9
0.9328 1410.0
0.9329 7745.1
0.9330 9094.4
0.9331 10389.9
0.9332 248.5
0.9333 71559.7
0.9334 18024.1
0.9335 7404.5
0.9336 1366.6
0.9337 21972.4
0.9338 1224.8
0.9339 49.9
0.9340 17590.5
0.9341 17967.1
0.9342 272.3
0.9343 704.4
0.9344 3581.7
0.9345 3896.8

the first items are price second is quantity

I am trying to make a function that sum and divide avg.price and also want to know how many bids total and asks total.

>Solution :

You can sum in a for loop like this:

total_ask_volume = 0
total_bid_volume = 0
for i  in range(0, 500):
    total_ask_volume += float(alis["asks"][i][1])
    total_bid_volume += float(alis["bids"][i][1])
print(total_ask_volume, total_bid_volume)

Another option is to skip the i index, and go through the values directly:

total_ask_volume = 0
for ask in alis["asks"]:
    total_ask_volume += float(ask[1])

total_bid_volume = 0
for bid in alis["bids"]:
    total_bid_volume += float(bid[1])
print(total_ask_volume, total_bid_volume)

This can sometimes be clearer, particularly in situations where you’d otherwise have multiple indexes (i, j, k and so on) which could get confusing.

As a side note, float often rounds in unintuitive ways; it probably doesn’t matter in this particular circumstance, but in most situations involving money you probably want to use Decimal instead (or multiply by 100 and count whole numbers of cents).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading