Issue
I am trying to find the lowest number value in this dict which is 1.12498
This is my code:
data = {'instrument': 'EUR_USD', 'granularity': 'H1', 'candles': [{'complete': True, 'volume': 7787, 'time': '2021-11-19T10:00:00.000000000Z', 'bid': {'o': '1.12987', 'h': '1.13046', 'l': '1.12830', 'c': '1.13000'}, 'mid': {'o': '1.12994', 'h': '1.13052', 'l': '1.12838', 'c': '1.13008'}, 'ask': {'o': '1.13001', 'h': '1.13059', 'l': '1.12846', 'c': '1.13015'}}, {'complete': True, 'volume': 5721, 'time': '2021-11-19T11:00:00.000000000Z', 'bid': {'o': '1.13002', 'h': '1.13023', 'l': '1.12839', 'c': '1.12892'}, 'mid': {'o': '1.13009', 'h': '1.13030', 'l': '1.12846', 'c': '1.12900'}, 'ask': {'o': '1.13016', 'h': '1.13037', 'l': '1.12853', 'c': '1.12907'}}, {'complete': True, 'volume': 7464, 'time': '2021-11-19T12:00:00.000000000Z', 'bid': {'o': '1.12894', 'h': '1.12968', 'l': '1.12516', 'c': '1.12565'}, 'mid': {'o': '1.12902', 'h': '1.12975', 'l': '1.12524', 'c': '1.12574'}, 'ask': {'o': '1.12909', 'h': '1.12982', 'l': '1.12533', 'c': '1.12583'}}, {'complete': True, 'volume': 9911, 'time': '2021-11-19T13:00:00.000000000Z', 'bid': {'o': '1.12565', 'h': '1.12935', 'l': '1.12488', 'c': '1.12873'}, 'mid': {'o': '1.12572', 'h': '1.12942', 'l': '1.12498', 'c': '1.12880'}, 'ask': {'o': '1.12580', 'h': '1.12950', 'l': '1.12508', 'c': '1.12887'}}, {'complete': True, 'volume': 11112, 'time': '2021-11-19T14:00:00.000000000Z', 'bid': {'o': '1.12874', 'h': '1.13206', 'l': '1.12870', 'c': '1.13066'}, 'mid': {'o': '1.12882', 'h': '1.13214', 'l': '1.12878', 'c': '1.13074'}, 'ask': {'o': '1.12889', 'h': '1.13223', 'l': '1.12885', 'c': '1.13081'}}, {'complete': True, 'volume': 8523, 'time': '2021-11-19T15:00:00.000000000Z', 'bid': {'o': '1.13065', 'h': '1.13208', 'l': '1.12996', 'c': '1.13172'}, 'mid': {'o': '1.13072', 'h': '1.13215', 'l': '1.13003', 'c': '1.13178'}, 'ask': {'o': '1.13079', 'h': '1.13223', 'l': '1.13009', 'c': '1.13184'}}, {'complete': True, 'volume': 5485, 'time': '2021-11-19T16:00:00.000000000Z', 'bid': {'o': '1.13170', 'h': '1.13215', 'l': '1.13122', 'c': '1.13178'}, 'mid': {'o': '1.13176', 'h': '1.13222', 'l': '1.13128', 'c': '1.13184'}, 'ask': {'o': '1.13183', 'h': '1.13229', 'l': '1.13135', 'c': '1.13190'}}, {'complete': True, 'volume': 6994, 'time': '2021-11-19T17:00:00.000000000Z', 'bid': {'o': '1.13176', 'h': '1.13191', 'l': '1.12916', 'c': '1.12977'}, 'mid': {'o': '1.13183', 'h': '1.13196', 'l': '1.12924', 'c': '1.12984'}, 'ask': {'o': '1.13190', 'h': '1.13203', 'l': '1.12930', 'c': '1.12990'}}, {'complete': True, 'volume': 4885, 'time': '2021-11-19T18:00:00.000000000Z', 'bid': {'o': '1.12977', 'h': '1.13022', 'l': '1.12861', 'c': '1.12864'}, 'mid': {'o': '1.12985', 'h': '1.13029', 'l': '1.12868', 'c': '1.12870'}, 'ask': {'o': '1.12993', 'h': '1.13036', 'l': '1.12874', 'c': '1.12877'}}, {'complete': True, 'volume': 3054, 'time': '2021-11-19T19:00:00.000000000Z', 'bid': {'o': '1.12863', 'h': '1.12914', 'l': '1.12848', 'c': '1.12888'}, 'mid': {'o': '1.12870', 'h': '1.12922', 'l': '1.12855', 'c': '1.12895'}, 'ask': {'o': '1.12876', 'h': '1.12929', 'l': '1.12862', 'c': '1.12902'}}, {'complete': True, 'volume': 2292, 'time': '2021-11-19T20:00:00.000000000Z', 'bid': {'o': '1.12885', 'h': '1.12911', 'l': '1.12854', 'c': '1.12875'}, 'mid': {'o': '1.12893', 'h': '1.12918', 'l': '1.12862', 'c': '1.12884'}, 'ask': {'o': '1.12901', 'h': '1.12926', 'l': '1.12870', 'c': '1.12892'}}, {'complete': True, 'volume': 1028, 'time': '2021-11-19T21:00:00.000000000Z', 'bid': {'o': '1.12874', 'h': '1.12892', 'l': '1.12795', 'c': '1.12808'}, 'mid': {'o': '1.12882', 'h': '1.12900', 'l': '1.12808', 'c': '1.12819'}, 'ask': {'o': '1.12890', 'h': '1.12910', 'l': '1.12820', 'c': '1.12830'}}]}
lowest_key = min(data, key=data.get)
lowest_value = data[lowest_key]
print(lowest_value)
But it give me this error: TypeError: '<' not supported between instances of 'list' and 'str'
I think it not working because there list on the dict
Solution
from itertools import chain
min(chain(*(candle['mid'].values() for candle in data['candles'])))
values = (candle['mid'].values() for candle in data['candles'])
extracts the values of the mid
dictionary of each candle. You can then use your favourite way to flatten the list of lists and then just find the min value.
Answered By - Reti43
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.