【问题标题】:converting individual dictonary to list of dictonary and iterating over it将单个字典转换为字典列表并对其进行迭代
【发布时间】:2021-04-30 10:08:55
【问题描述】:

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

{
  "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
}

【问题讨论】:

  • 能贴出获取字典数据的代码吗?
  • from nsepython import* from pprint import pprint from nsepython import* print(option_chain("TCS"))
  • 好的。似乎并非所有记录都包含PE 的值,如何处理?

标签: python-3.x loops dictionary stock


【解决方案1】:

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

>>> 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)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多