def test_get_quote_stock_der(self): """ 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 """ n = datetime.datetime.now() stexp = get_expiry_date(n.year, n.month, index=False, stock=True) self.assertEqual(len(stexp), 1) if n.date() > list(stexp)[0]: try: stexp = get_expiry_date( n.year, n.month + 1, index=False, stock=True) except: stexp = get_expiry_date(n.year + 1, 1, index=False, stock=True) self.assertEqual(len(stexp), 1) exp = min([x for x in stexp if x > n.date()]) q = get_quote(symbol='SBIN', instrument='FUTSTK', expiry=exp) comp_name = q['data'][0]['instrumentType'] self.assertEqual(comp_name, "FUTSTK") exp = min([x for x in stexp if x > n.date()]) q = get_quote(symbol='SBIN', instrument='OPTSTK', expiry=exp, option_type="CE", strike=300) comp_name = q['data'][0]['instrumentType'] self.assertEqual(comp_name, "OPTSTK")
def test_get_quote_index_der(self): """ 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 """ n = datetime.datetime.now() stexp = get_expiry_date(n.year, n.month) # 4 weekly expiry per month # Corner case where 1 Feb is a holiday and its non-leap year Feb will only get 3 expiries in the month # So test for only >=3 self.assertGreaterEqual(len(stexp), 3) if n.date() > max(stexp): try: stexp = get_expiry_date(n.year, n.month + 1) except: stexp = get_expiry_date(n.year + 1, 1) self.assertGreaterEqual(len(stexp), 3) exp = max([x for x in stexp if x > n.date()]) q = get_quote(symbol='NIFTY', instrument='FUTIDX', expiry=exp) comp_name = q['data'][0]['instrumentType'] self.assertEqual(comp_name, "FUTIDX") exp = min([x for x in stexp if x > n.date()]) q = get_quote(symbol='NIFTY', instrument='OPTIDX', expiry=exp, option_type="CE", strike=11000) comp_name = q['data'][0]['instrumentType'] self.assertEqual(comp_name, "OPTIDX")
def fetch_data_nsepy(symbol, t_from=None, t_to=None, index=False, futures=False, option_type='', t_expiry=None, strike_price='', series='EQ', date_settings=None, months_hist=6, verbose=False): # Some checks if t_from is None and t_expiry is None: raise ValueError( 'At least one of t_from or t_expiry should not be None.') # endif if option_type not in ['CE', 'PE', '']: raise ValueError('option_type should be one of CE, PE or ""') # endif if option_type in ['CE', 'PE']: if strike_price is None or strike_price == '': raise ValueError( 'strike price should be an integer when optio_type is valid (PE or CE)' ) # endif # endif # Set from_date automatically if t_expiry is provided. It's calculated by subtracting 6 months before expiry date from_date = dateparser.parse(t_expiry, settings=date_settings) - dateutil.relativedelta.relativedelta(months=months_hist) \ if t_expiry else dateparser.parse(t_from, settings=date_settings) # Set end_date to always "now" if not already set to_date = dateparser.parse( t_to, settings=date_settings) if t_to else datetime.datetime.now() # Set expiry date _exp_date = dateparser.parse(t_expiry) if t_expiry else None exp_list = nsepy.get_expiry_date(year=_exp_date.year, month=_exp_date.month, index=index, stock=not index) exp_date = sorted(list(exp_list))[-1] if verbose: print('[start_date, end_date, exp_date] = [{}, {}, {}]'.format( from_date, to_date, exp_date)) print('[index, futures] = [{}, {}]'.format( index, futures, not index)) print('[option_type, strike_price] = [{}, {}]'.format( option_type, strike_price)) # endif return nsepy.get_history(symbol=symbol, start=from_date, end=to_date, index=index, futures=futures, option_type=option_type, expiry_date=exp_date)
def test_get_quote_der(self): """ 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 """ n = datetime.datetime.now() exp = get_expiry_date(n.year, n.month) if n.date() > exp: try: exp = get_expiry_date(n.year, n.month + 1) except: exp = get_expiry_date(n.year + 1, 1) q = get_quote(symbol='SBIN', instrument='FUTSTK', expiry=exp) comp_name = q['instrumentType'] self.assertEqual(comp_name, "FUTSTK")