i get values from an orderbook as a list like this:
list1 = [...,'ethbtc', '0.077666', '10', '0.077680', '15',...]
------------------------^symbol-----^value-----^quantity--
There are around 100 symbols in this list and 40 values for each symbol. They are always in the same order.
I would like to find out at what maximum price my system buys in this moment if I pay say 100 % of my balance.
So if I want to buy 11 ETH at 0.077666 the real price would be 0.077680 because there are only 10 ETH available at first price.
I dont want to get the average because that would be to much at the moment
My code has a nested for loop and loops through 2 lists:
- coinlist = where all 100 symbols are listed like this
symbollist = [ethbtc, eoseth,...] - list of indexes called
abecause the values and quantities are always at the same spot
a = ['1', '3', '5', ...]
My Code:
for symbolnow in symbollist:
sumlist = []
for i in a:
quantity = float(list1[list1.index(symbolnow) + (i+1)] if symbolnow in list1 else 0)
sumlist.append(quantity)
if sum(sumlist) > mycurrentbalance:
maxvalue = float(list1[list1.index(symbolnow) + i] if symbolnow in list1 else -1)
break
else:
maxvalue = -1
So what does this code do:
1) loop through every symbol in the symbollist
2) for every found symbol i look for the available quantity
3) if my balance (i.e. 10 ETH) is smaller than qty the loop breaks
4) if not keeps searching and summarizing every qty in a sum list until there is enough.
The code works as intended but not that fast. As expected list1.index takes long to execute..
Question
How would a faster code work. Is a list comprehension better in this scenario or even regex? Is my code very ugly?
Thank you in advance!
EDIT:
to clarify the input and desired output, a sample:
list1 = [...,'ethbtc', '0.077666', '1', '0.077680', '1.5', '0.077710', '3', '0.078200', '4',...]
mycurrentbalance = 5.5 <-- balance is in ETH
every third entry in list1 is the quantity in ETH so in the list it would be ['1', '1.5', '3', '4']
so if i want to sell all of my ETH (in this case 5.5) the max value would be '0.077710'
list1 contains 100 symbols so before and after 'ethbtc' there are other values quantities and symbols