将单个字典转换为字典列表并迭代它

问题描述 投票:0回答:1

我正在使用 nsepython 库,我想迭代每个 openInterest 键并检查该键的前 3 个值并打印执行价格、OI、交易量、openInterest 变化。 但是,当我提取特定脚本的数据时,它为每个执行价格提供了单独的字典,并且我无法迭代它。

{
  "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
}
python python-3.x loops dictionary stock
1个回答
0
投票

我似乎收到了 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)]
© www.soinside.com 2019 - 2024. All rights reserved.