-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_one.py
84 lines (73 loc) · 2.62 KB
/
lib_one.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from tabulate import tabulate
from datetime import date
import pandas as pd
import time, os
import custom_exceptions
'''
11013: 1분기
11012: 반기
11014: 3분기
11011: 사업보고서
'''
def timer(func):
def Wrapper(*args, **kwargs):
print(func.__name__)
start = time.time()
obj = func(*args, **kwargs)
end = time.time()
print(end - start)
print("-" * 20)
return obj
return Wrapper
def get_date_today():
today = date.today()
x = today.weekday()
today = pd.to_datetime(today)
while x > 4:
today -= pd.to_timedelta(1, 'D')
x -= 1
return today
def get_last_week(today: pd.Timestamp) -> pd.Timestamp:
return (today - pd.to_timedelta(7, 'D'))
def get_six_years_list(year: str) -> list:
'''
Returns: List of strings, past six yrs.
'''
year = int(year)
year_list = []
for i in range(year-5, year+1):
year_list.append(str(i))
return year_list
def get_stock_price(sp_data, date: str) -> dict:
'''
Arguments: a dataframe object containing all the stock information of the company, the date
Returns: the high/low/close stock price closest from the past to the date given.
'''
date = pd.to_datetime(date)
#HAVE TO DO THIS CUZ THE sp_data returns Date as default index. So changing it back to column
if sp_data.index[0] != 0: #IF THE INDEX OF THE DATA IS THE TIME, NOT 0,1,...
sp_data.reset_index(inplace=True) #SET TIME AS COLUMN AND REPLACE DEFAULT
#If the latest stock price is not within one week range
no_new_data = (date - sp_data["Date"].max()) > pd.Timedelta(days=7)
if no_new_data or sp_data['Date'].min() > date:
#If the there isn't any stock value equivalent
raise custom_exceptions.StockPriceError("Given date not in the dataframe 'sp_data'", date)
else:
value = {}
while True:
if sum(sp_data['Date'] == date) > 0:
sp_data = sp_data[sp_data['Date'] == date]
value["Low"] = sp_data['Low'].iloc[0]
value["High"] = sp_data['High'].iloc[0]
value["Close"] = sp_data['Close'].iloc[0]
break
else:
date -= pd.to_timedelta(1, 'day')
return value
def get_cmpny_stock(cmpnyname: str):
try:
file_path = os.path.join(os.getcwd(), 'Data', 'Stocks', f"{cmpnyname}_stock.csv")
df = pd.read_csv(file_path, encoding="euc-kr", parse_dates=["Date"])
except FileNotFoundError:
raise custom_exceptions.YoungCmpny(corp_code=cmpnyname, end_year="2022") #When the Stock doesn't match
return df