How I use ChatGPT as a Modern day Slave

I have a very detailed prompt Like

expiry_json = json.load(open('/root/apps/cdn/expiry.json'))
expiry_json["stockwise"]

symbol="BANKNIFTY"
tradevariation = "options"
expiry_json["stockwise"][tradevariation][symbol]


Now expiry_json["stockwise"][tradevariation][symbol] looks like 

['2023-09-20',
 '2023-09-28',
 '2023-10-04',
 '2023-10-11',
 '2023-10-18',
 '2023-10-26',
 '2023-11-30',
 '2023-12-28',
 '2024-03-28',
 '2024-06-27',
 '2023-09-20',
 '2023-09-28',
 '2023-10-04',
 '2023-10-11',
 '2023-10-18',
 '2023-10-26',
 '2023-11-30',
 '2023-12-28',
 '2024-03-28',
 '2024-06-27',
 '2023-11-08',
 '2023-11-15',
 '2023-11-22',
 '2023-11-30',
 '2023-12-06',
 '2023-12-28',
 '2024-01-25',
 '2024-03-28',
 '2024-06-27',
 '2024-09-26',
 '2023-11-08',
 '2023-11-15',
 '2023-11-22',
 '2023-11-30',
 '2023-12-06',
 '2023-12-28',
 '2024-01-25',
 '2024-03-28',
 '2024-06-27',
 '2024-09-26']

I want you to find today's date and the preceeding date matches with this expiry day or not. if it matches then it will print yes. 

holiday_json = json.load(open('/root/apps/cdn/holidays.json', 'r'))
holiday_json["FO"]

this looks like 
[{'tradingDate': '26-Jan-2023',
  'weekDay': 'Thursday',
  'description': 'Republic Day',
  'Sr_no': 1},
 {'tradingDate': '18-Feb-2023',
  'weekDay': 'Saturday',
  'description': 'Mahashivratri',
  'Sr_no': 2},
 {'tradingDate': '07-Mar-2023',
  'weekDay': 'Tuesday',
  'description': 'Holi',
  'Sr_no': 3},
 {'tradingDate': '30-Mar-2023',
  'weekDay': 'Thursday',
  'description': 'Ram Navami',
  'Sr_no': 4},
 {'tradingDate': '04-Apr-2023',
  'weekDay': 'Tuesday',
  'description': 'Mahavir Jayanti',
  'Sr_no': 5},
 {'tradingDate': '07-Apr-2023',
  'weekDay': 'Friday',
  'description': 'Good Friday',
  'Sr_no': 6},
 {'tradingDate': '14-Apr-2023',
  'weekDay': 'Friday',
  'description': 'Dr. Baba Saheb Ambedkar Jayanti',
  'Sr_no': 7},
 {'tradingDate': '22-Apr-2023',
  'weekDay': 'Saturday',
  'description': 'Id-Ul-Fitr (Ramzan ID)',
  'Sr_no': 8},
 {'tradingDate': '01-May-2023',
  'weekDay': 'Monday',
  'description': 'Maharashtra Day',
  'Sr_no': 9},
 {'tradingDate': '29-Jun-2023',
  'weekDay': 'Thursday',
  'description': 'Bakri Id',
  'Sr_no': 10},
 {'tradingDate': '29-Jul-2023',
  'weekDay': 'Saturday',
  'description': 'Moharram',
  'Sr_no': 11},
 {'tradingDate': '15-Aug-2023',
  'weekDay': 'Tuesday',
  'description': 'Independence Day',
  'Sr_no': 12},
 {'tradingDate': '19-Sep-2023',
  'weekDay': 'Tuesday',
  'description': 'Ganesh Chaturthi',
  'Sr_no': 13},
 {'tradingDate': '02-Oct-2023',
  'weekDay': 'Monday',
  'description': 'Mahatma Gandhi Jayanti',
  'Sr_no': 14},
 {'tradingDate': '24-Oct-2023',
  'weekDay': 'Tuesday',
  'description': 'Dussehra',
  'Sr_no': 15},
 {'tradingDate': '12-Nov-2023',
  'weekDay': 'Sunday',
  'description': 'Diwali-Laxmi Pujan*',
  'Sr_no': 16},
 {'tradingDate': '14-Nov-2023',
  'weekDay': 'Tuesday',
  'description': 'Diwali-Balipratipada',
  'Sr_no': 17},
 {'tradingDate': '27-Nov-2023',
  'weekDay': 'Monday',
  'description': 'Gurunanak Jayanti',
  'Sr_no': 18},
 {'tradingDate': '25-Dec-2023',
  'weekDay': 'Monday',
  'description': 'Christmas',
  'Sr_no': 19}]

now i want you to check that if the preceeding day is holiday. if it is holiday then it will check the day before that. 

The output is apt and timesaver-

In order to solve this task, you can follow these steps:

  1. Get today’s date.
  2. Find the nearest preceding date from the expiry dates list.
  3. Check if the day before the nearest preceding date is a holiday, if it is, check the day before that, and so on until you find a non-holiday day or reach the start of the list.

Here’s how you could implement this in Python:

pythonCopy code

import datetime
import json

# Load the data
expiry_json = json.load(open('/root/apps/cdn/expiry.json'))
holiday_json = json.load(open('/root/apps/cdn/holidays.json', 'r'))

# Get today's date
today = datetime.datetime.now().date()

# Get the expiry dates and holiday dates
expiry_dates = expiry_json["stockwise"]["options"]["BANKNIFTY"]
holiday_dates = [datetime.datetime.strptime(item['tradingDate'], '%d-%b-%Y').date() for item in holiday_json["FO"]]

# Find the nearest preceding expiry date
nearest_expiry_date = None
for date_str in expiry_dates:
    expiry_date = datetime.datetime.strptime(date_str, '%Y-%m-%d').date()
    if expiry_date < today:
        nearest_expiry_date = expiry_date
    else:
        break  # Stop as soon as we find an expiry date that is after today

# Check the preceding days for holidays
if nearest_expiry_date:
    preceding_date = nearest_expiry_date - datetime.timedelta(days=1)
    while preceding_date in holiday_dates:
        preceding_date -= datetime.timedelta(days=1)  # Go one day back if it's a holiday

    # Now preceding_date is the nearest non-holiday day before the nearest_expiry_date
    print("Yes" if preceding_date == nearest_expiry_date - datetime.timedelta(days=1) else "No")
else:
    print("No expiry date found before today.")

In this code:

  1. You load the expiry and holiday data from the specified files.
  2. You get today’s date using datetime.datetime.now().date().
  3. You convert the expiry dates and holiday dates to datetime.date objects.
  4. You iterate through the expiry dates to find the nearest expiry date before today.
  5. You check the days preceding the nearest expiry date to see if they are holidays, going one day back at a time until you find a non-holiday day.
  6. Finally, you check if the preceding_date is the day before the nearest_expiry_date, and print “Yes” if it is, and “No” otherwise.
1 Like

Now to use it as a slave, You need to be master as well.

I found a critical mistake as you can see in my next prompt -

for date_str in expiry_dates:
    expiry_date = datetime.datetime.strptime(date_str, '%Y-%m-%d').date()
    if expiry_date < today:
        nearest_expiry_date = expiry_date
    else:
        break  # Stop as soon as we find an expiry date that is after today

it is assuming that expiry_dates are ordered but what if it is not ordered? and why not use the symbol and tradevariation variables?
1 Like

It did more wrong and wrong. the last input that went fine for it is -


The approach is wrong.

First you find today
Then you see the last working day. Last working day means the previous day should not be sunday saturday or be in the list of the holiday dates. 

so print today and print last_working_day

then then see if the last_working_day matches with anyone from the list of expiry_dates
1 Like