-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexchange.py
61 lines (48 loc) · 1.71 KB
/
exchange.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from datetime import datetime
from dateutil.relativedelta import relativedelta, TH
list_of_months = {1: 'Jan', 2: 'Feb', 3: 'Mar',
4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul',
8: 'Aug', 9: 'Sep', 10: 'Oct',
11: 'Nov', 12: 'Dec'
}
def convert_date(date):
"""
Change format of a date from `%d-%m-%Y %b`
to `%d%b%Y`.
example : 19-09-2021 Sep -> 19Sep2021
Returns
-------
datetime.date
Date object with changed format.
"""
date = datetime.strptime(str(date),"%d-%m-%Y %b").date()
date = date.strftime("%d%b%Y")
return date
def check_if_last(date):
exdate = datetime.date(datetime.strptime(date,"%d%b%Y"))
exmonth = exdate.month
exday = exdate.day
for i in range(1, 6):
t = exdate + relativedelta(weekday=TH(i))
if t.month!=exmonth:
t = t + relativedelta(weekday=TH(-2))
break
return exday==t.day
def exchange_name(instru, expiry, strike_price, call):
if type(strike_price)!=str:
strike_price = str(strike_price)
# print("EXPIRY ",expiry)
date = datetime.date(datetime.strptime(expiry,"%d%b%Y"))
year = str(date.year)
month = date.month
day = date.day
day_ = "0"+str(day) if day>0 and day<10 else str(day)
month_ = list_of_months[month][0].upper() if month>9 else str(month)
# print(date, year, month,)
if check_if_last(expiry) :
ex = instru+year[-2:]+list_of_months[date.month].upper()+strike_price+call
else:
ex = instru+year[-2:]+month_+day_+strike_price+call
return ex
if __name__=='__main__':
print(exchange_name("NIFTY","28Oct2021","18000","CE"))