0

我正在使用下面的代码从 NSEpy 模块中提取实时 Nifty 报价,但出现错误。

代码:

from nsepy import get_quote
nifty = get_quote('NIFTY', series='EQ', instrument='FUTIDX', expiry='26SEP2019')
print(nifty)

错误:

    nifty = get_quote('NIFTY', series='EQ', instrument='FUTIDX', expiry='26SEP2019')
  File "C:\ProgramData\Anaconda3\lib\site-packages\nsepy\live.py", line 26, in get_quote
    expiry_str = "%02d%s%d"%(expiry.day, months[expiry.month][0:3].upper(), expiry.year)
AttributeError: 'str' object has no attribute 'day'
4

2 回答 2

0

到期日期是一个列表,这就是您收到错误的原因。使用下面的代码更改。

from pprint import pprint
from datetime import date
from nsepy import get_quote
from nsepy.derivatives import get_expiry_date

expiry = date(2020,5,28)
print(expiry)

data = get_quote('BANKNIFTY', series='EQ', instrument='FUTIDX', expiry=expiry, option_type='CE', strike=300)
pprint(data)
于 2020-05-09T09:50:59.990 回答
0

我可以使这个工作:

#!/usr/bin/python

from nsepy import get_quote
from nsepy.derivatives import get_expiry_date

expiry = get_expiry_date(year=2019, month=9)
print(expiry)

data = get_quote('NIFTY', series='EQ', instrument='FUTIDX', expiry=expiry, option_type='CE', strike=300)
print(data)

输出:

building dictionary
2019-09-26
{'annualisedVolatility': 18.15, 'bestBuy': 10.5, 'totalSellQuantity': 263550, 'vwap': 11032.65, 'clientWisePositionLimits': 14074147, 'optionType': '-', 'highPrice': 11118.0, 'dailyVolatility': 0.95, 'bestSell': 11.31, 'marketLot': 75, 'sellQuantity5': 450, 'marketWidePositionLimits': '-', 'sellQuantity4': 150, 'sellQuantity3': 375, 'sellQuantity2': 150, 'underlying': 'NIFTY', 'sellQuantity1': 1275, 'pChange': 1.07, 'premiumTurnover': '-', 'totalBuyQuantity': 538425, 'turnoverinRsLakhs': 1219692.56, 'changeinOpenInterest': -377325, 'strikePrice': '-', 'openInterest': 16778250, 'buyPrice2': 11110.95, 'buyPrice1': 11111.0, 'openPrice': 10990.0, 'prevClose': 10996.45, 'expiryDate': '26SEP2019', 'lowPrice': 10962.15, 'buyPrice4': 11110.2, 'buyPrice3': 11110.45, 'buyPrice5': 11110.15, 'numberOfContractsTraded': 147404, 'instrumentType': 'FUTIDX', 'sellPrice1': 11113.7, 'sellPrice2': 11114.0, 'sellPrice3': 11114.5, 'sellPrice4': 11114.6, 'sellPrice5': 11114.7, 'change': 118.05, 'pchangeinOpenInterest': -2.2, 'ltp': 11.54, 'impliedVolatility': '-', 'underlyingValue': 11075.9, 'buyQuantity4': 1425, 'buyQuantity3': 75, 'buyQuantity2': 75, 'buyQuantity1': 1500, 'buyQuantity5': 150, 'settlementPrice': 11105.55, 'closePrice': 11105.55, 'lastPrice': 11114.5}

NSEpy 库中的文档似乎不正确,它说expiry应该是 ( ddMMMyyyy) 格式并且strike(strike_price) 是一个强制性参数。

def get_quote(symbol, series='EQ', instrument=None, expiry=None, option_type=None, strike=None):
    """
    1. Underlying security (stock symbol or index name)
    2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX)
    3. expiry (ddMMMyyyy)
    4. type (CE/PE for options, - for futures
    5. strike (strike price upto two decimal places
    """

https://nsepy.readthedocs.io/en/latest/#quick-hand-on

于 2019-09-15T09:01:58.057 回答