-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDatainfo.py
More file actions
157 lines (124 loc) · 5.81 KB
/
getDatainfo.py
File metadata and controls
157 lines (124 loc) · 5.81 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from dateutil.parser import *
from dateutil.relativedelta import *
from dateutil.rrule import *
import warnings
import time
import pandas as pd
from datetime import datetime
from datetime import timedelta
warnings.simplefilter(action='ignore', category=FutureWarning) # FutureWaring 제거
opendf = pd.read_csv('openDate.csv', index_col='index') # 2002-09-13~2023-03-31까지의 개장일 csv파일 읽기
opendf['Opendate'] = pd.to_datetime(opendf['Opendate'], format='%Y-%m-%d', errors='raise') # 원소를 datetime타입으로 변경
datetimeList = []
for date in opendf['Opendate']:
date = pd.Timestamp(date).strftime('%Y-%m-%d')
datetimeList.append(datetime.strptime(date, '%Y-%m-%d'))
def getPayInDateInfo(start_date, end_date, month_type): # 납입일 계산 (월초: 0, 월말: 1)
rtList = []
if month_type == '0':
a = list(rrule(MONTHLY,
byweekday=(MO, TU, WE, TH, FR),
bysetpos=1,
dtstart=parse(start_date),
until=parse(end_date))) # 지정된 기간의 매월 첫 평일
for day in a:
while 1: # 개장일 까지
if day not in datetimeList: # 개장일이 아니면
day = day + timedelta(days=1) # 하루 +
else: # 개장일인 경우 빠져나감
break
rtList.append(day.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
elif month_type == '1':
a = list(rrule(MONTHLY,
byweekday=(MO, TU, WE, TH, FR),
bysetpos=-1,
dtstart=parse(start_date),
until=parse(end_date))) # 지정된 기간의 매월 마지막 평일
for day in a:
while 1: # 개장일 까지
if day not in datetimeList: # 개장일이 아니면
day = day + timedelta(days=-1) # 하루 +
else: # 개장일인 경우 빠져나감
break
rtList.append(day.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
return rtList # 납입 예정일 리스트 출력
def getDailyDateInfo(start_date, end_date):
rtList = []
for day in opendf['Opendate'][start_date:end_date]:
rtList.append(pd.Timestamp(day).strftime('%Y-%m-%d'))
return rtList
def getYearlyDateInfo(start_date, end_date):
rtList = []
a = list(rrule(YEARLY,
byweekday=(MO, TU, WE, TH, FR),
bysetpos=1,
dtstart=parse(start_date),
until=parse(end_date))) # 지정된 기간의 매월 첫 평일
for day in a:
while 1:
if day not in datetimeList: # 개장일에 포함되어 있으면
day = day + timedelta(days=1)
else:
break
rtList.append(day.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
return rtList
def getRebalanceDateInfo(start_date, end_date, month_type, interval): # 리밸런싱 날짜 계산 (월초 or 월말)
rtList = [] # 반환할 리스트
sd = datetime.strptime(start_date, '%Y-%m-%d') # 시작날짜 저장
# month_type 0: 월초, 1: 월말
a = [] # 월초/월말 날짜 저장할 리스트
if month_type == '0':
a = list(rrule(MONTHLY,
interval=interval,
byweekday=(MO, TU, WE, TH, FR),
bysetpos=1,
dtstart=parse(start_date),
until=parse(end_date))) # 지정된 기간의 매월 첫 평일 (월초)
for day in a:
while 1:
if day not in datetimeList: # 개장일에 포함되어 있으면
day = day + timedelta(days=1)
else:
break
rtList.append(day.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
if sd in datetimeList: # 시작날짜가 개장일이라면
rtList.insert(0, sd.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
else: # 시작 날짜가 개장일이 아니면
while 1:
if sd not in datetimeList: # 개장일에 포함되어 있으면
sd = sd + timedelta(days=1)
else:
break
rtList.insert(0, sd.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
if month_type == '1':
a = list(rrule(MONTHLY,
interval=interval,
byweekday=(MO, TU, WE, TH, FR),
bysetpos=-1,
dtstart=parse(start_date),
until=parse(end_date))) # 지정된 기간의 매월 첫 평일 (월초)
if sd in datetimeList: # 시작날짜가 개장일이라면
rtList.insert(0, sd.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
else: # 시작 날짜가 개장일이 아니면
while 1:
if sd not in datetimeList: # 개장일에 포함되어 있으면
sd = sd + timedelta(days=1)
else:
break
rtList.insert(0, sd.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
for day in a:
while 1:
if day not in datetimeList: # 개장일에 포함되어 있으면
day = day + timedelta(days=-1)
else:
break
rtList.append(day.strftime('%Y-%m-%d')) # yyyy-mm-dd 형식 변환
rt = [] #중복 제거
for d in rtList:
if d not in rt:
rt.append(d)
return rt # 납입 예정일 리스트 출력
# print(getDailyDateInfo('2023-01-01', '2023-03-07'))
# print(getYearlyDateInfo('2023-01-01', '2023-03-07'))
# print(getPayInDateInfo('2023-01-01', '2023-03-07', '1'))
# print(getRebalanceDateInfo('2022-01-01', '2023-03-01', '1', 3))