将单个dictionary转换为dictionary列表并对其进行迭代



我正在使用nsepython库,在那里我想迭代每个openInterest键,检查该键的前3个值,并打印执行价格、OI、Volume、changeinopenInterest。但当我提取特定脚本的数据时,它会为我的每个执行价格提供单独的dictionary,我无法对其进行迭代。

{
"strikePrice": 2400,
"expiryDate": "27-May-2021",
"underlying": "TCS",
"identifier": "OPTSTKTCS27-05-2021CE2400.00",
"openInterest": 1,
"changeinOpenInterest": 0,
"pchangeinOpenInterest": 0,
"totalTradedVolume": 0,
"impliedVolatility": 0,
"lastPrice": 0,
"change": 0,
"pChange": 0,
"totalBuyQuantity": 5700,
"totalSellQuantity": 11400,
"bidQty": 5700,
"bidprice": 635.75,
"askQty": 11400,
"askPrice": 785.45,
"underlyingValue": 3077.3
}

我似乎收到了nsepython:版本0.0.94的执行价格的完整列表

>>> import nsepython
>>> c = nsepython.option_chain('TCS')
>>> interests = [(rec.get('PE', {'openInterest': 0})['openInterest'], rec.get('PE', {'strikePrice': 0})['strikePrice'], rec.get('PE', {'totalTradedVolume': 0})['totalTradedVolume'], rec.get('PE', {'changeinOpenInterest': 0})['changeinOpenInterest']) for rec in c['records']['data']]
>>> interests.sort(reverse=True)  # sort by changeinOpenInterest
>>> interests[:3]  # get top 3 of (openInterest, strikePrice, totalTradedVolume, changeinOpenInterest)
[(1115, 3000, 1399, 18), (599, 3100, 1704, 12), (560, 2800, 156, -4)]

最新更新