-1

This is my script with 2 functions...

from jugaad_data.nse import derivatives_df
from datetime import timedelta, date
from datetime import datetime
import pandas as pd
import requests
import json

def oi_data(Symbols):
  baseurl = "https://www.nseindia.com/"
  url = f'https://www.nseindia.com/api/option-chain-equities'
  headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, ''like Gecko) ''Chrome/80.0.3987.149 Safari/537.36','accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'}
  session = requests.Session()
  request = session.get(baseurl, headers=headers, timeout=30)
  cookies = dict(request.cookies)
  params=[('symbol', Symbols)]
  res = session.get(url, headers=headers, params=params, cookies=cookies, timeout=30)
  res_text = res.text
  data = json.loads(res_text)
  exp_dt1 = data["records"]["expiryDates"][0]
  datetime_obj = datetime.strptime(exp_dt1, '%d-%b-%Y')
  exp_dt = datetime_obj.strftime("%Y, %m, %d")
  print(exp_dt)
  return exp_dt
  
def fut_data(Symbols):
    ticker_df = derivatives_df(symbol=Symbols, from_date = date.today() - timedelta(days = 30), to_date= date.today(),expiry_date=date(2023,11,30), instrument_type="FUTSTK")
    fut_df = ticker_df
    print(fut_df)
    return fut_df

Symbols = ['MARUTI']
for symbol in Symbols:
    exp_dt = oi_data(symbol)
    fut_df = fut_data(symbol)

Here, from oi_data function I'm getting exp_dt in specific format which is required in case of my second function fut_data.

Now I want to use this exp_dt in my fut_data function. More clearly I'm trying to write my 2nd function as....

def fut_data(Symbols):
    ticker_df = derivatives_df(symbol=Symbols, from_date = date.today() - timedelta(days = 30), to_date= date.today(),expiry_date=exp_dt, instrument_type="FUTSTK")
    fut_df = ticker_df
    print(fut_df)
    return fut_df

But here I'm getting error as ...

AttributeError: 'str' object has no attribute 'strftime'

What is the solution here plz ????

1 Answer 1

1

it seems your expiry_date (I mean the return "exp_dt") is a string and must be inputted as datetime variable. Your issue should be solved removing:

exp_dt = datetime_obj.strftime("%Y, %m, %d")

and writing instead:

exp_dt = datetime_obj.date()

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help me. Thanks a lot. My query is solved now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.