Mitch
Mitch

Reputation: 11

python search index of index for highest value

I am trying to find the highest daily high within daily kline data from binance. I am able to API call the list of lists with this code.

client.get_historical_klines('maticbtc', Client.KLINE_INTERVAL_1DAY, "2 days ago UTC")

The output is a list of daily historical prices in 'open' 'high' 'low' 'close' 'volume' format.

[[1599264000000,
  '0.00000185',
  '0.00000192',
  '0.00000171',
  '0.00000177',
  '208963036.00000000',
  1599350399999,
  '377.04825679',
  14595,
  '82785887.00000000',
  '150.17277108',
  '0'],
 [1599350400000,
  '0.00000177',
  '0.00000185',
  '0.00000170',
  '0.00000182',
  '114643846.00000000',
  1599436799999,
  '204.99814224',
  9620,
  '55503278.00000000',
  '99.62131279',
  '0']]

I would like to find the highest 'high' value in this list. I am currently able to reference a single daily 'high' value using this code:

client.get_historical_klines('maticbtc', Client.KLINE_INTERVAL_1DAY, "30 days ago UTC")[0][2]

output:

0.00000192

Thank you for your suggestions!

Upvotes: 0

Views: 152

Answers (2)

Regis May
Regis May

Reputation: 3456

I would like to find the highest 'high' value in this list.

The example you present is not a list, it is a list of lists:

data30days = [
    [ ... ],
    [ ... ],
    ...
]

In general the "highest" value is nothing else than the maximum. Therefore the code finding the maximums in such a list of lists would be:

maxima30days = [ max(x) for x in data30days ]
totalMaximum = max(maxima30days)

But there is one thing that is odd: The return data of your API. You do not receive a list of numeric values but a record of data of mixed type. Luckily the documentation of binance provides the information which value is the value you are looking for: It seems to be the third. (See: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#klinecandlestick-data) Why this value is returned as string is unclear to me.

Therefore your code would be:

maxima30days = [ float(x[2]) for x in data30days ]
totalMaximum = max(maxima30days)

One little detail: The next time you ask a question please provide information which module you're using. In other situations this piece of information might be very essential! (Luckily here it is not.)

Please have in mind that I am not able to test the code above as unfortunately you did not provide a working example I could build upon. Therefore please test it yourself. Feel free to add a comment to my answer if you encounter any errors, I'll then try to resolve any further issues if there are any.

Upvotes: 1

alani
alani

Reputation: 13069

Assuming that you have your data as above in a variable called values,

>>> values
[[1599264000000, '0.00000185', '0.00000192', '0.00000171', '0.00000177', '208963036.00000000', 1599350399999, '377.04825679', 14595, '82785887.00000000', '150.17277108', '0'], [1599350400000, '0.00000177', '0.00000185', '0.00000170', '0.00000182', '114643846.00000000', 1599436799999, '204.99814224', 9620, '55503278.00000000', '99.62131279', '0']]

if you want the maximum value of the third element in each sublist of values (converted to float because you don't want to do a string comparison), you can do for example:

>>> max(float(lst[2]) for lst in values)
1.92e-06

Upvotes: 0

Related Questions