-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
107 lines (84 loc) · 3.45 KB
/
utils.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import datetime
import calendar
import time
from enum import Enum
def his_data_filepath(pair, granularity):
"""
It takes a pair and a granularity and returns a filepath
:param pair: The currency pair to download data for
:param granularity: The candlestick chart's time interval. Valid values are:
:return: A list of dictionaries.
"""
return f"his_data/{pair}_{granularity}.pkl"
# returns month start and end dates in Unix time
def get_months(num_months) -> tuple:
today = time.time()
current_year = time.localtime(today).tm_year
current_month = time.localtime(today).tm_mon
current_day = time.localtime(today).tm_mday
current_hour = time.localtime(today).tm_hour
current_minute = time.localtime(today).tm_min
current_second = time.localtime(today).tm_sec
start_dates = []
end_dates = []
for i in range(num_months):
year = current_year
month = current_month - i
if month <= 0:
year -= 1
month = 12 + month
last_day = calendar.monthrange(year, month)[1]
last_day = min(last_day, current_day if month == current_month else last_day)
first_date_unix = calendar.timegm((year, month, 1, 0, 0, 0))
if month == current_month:
end_date_unix = calendar.timegm((year, month, last_day, current_hour, current_minute, current_second))
else:
end_date_unix = calendar.timegm((year, month, last_day, 23, 59, 59))
start_dates.append(first_date_unix)
end_dates.append(end_date_unix)
return start_dates, end_dates
# returns week start and end dates in Unix time
def get_weeks(num_weeks) -> tuple:
today = time.time()
current_year = time.localtime(today).tm_year
current_month = time.localtime(today).tm_mon
current_day = time.localtime(today).tm_mday
current_hour = time.localtime(today).tm_hour
current_minute = time.localtime(today).tm_min
current_second = time.localtime(today).tm_sec
start_dates = []
end_dates = []
for i in range(num_weeks):
current_weekday = calendar.weekday(current_year, current_month, current_day)
days_to_subtract = current_weekday + 7 * i
year, month, day = current_year, current_month, current_day
if days_to_subtract >= day:
month -= 1
if month <= 0:
year -= 1
month = 12 + month
day = calendar.monthrange(year, month)[1] - (days_to_subtract - day)
else:
day -= days_to_subtract
first_date_unix = calendar.timegm((year, month, day, 0, 0, 0))
if current_weekday == 6 or i == 0:
if i == 0:
end_date_unix = calendar.timegm((current_year, current_month, current_day, current_hour, current_minute, current_second))
else:
days_to_add = 6 - current_weekday
end_date_unix = calendar.timegm((year, month, day + days_to_add, 23, 59, 59))
else:
days_to_add = 6 - current_weekday
end_date_unix = calendar.timegm((year, month, day + days_to_add, 23, 59, 59))
start_dates.append(first_date_unix)
end_dates.append(end_date_unix)
return start_dates, end_dates
class Color(Enum):
# Define ANSI color codes
RED ="\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA_BG = "\u001b[45m"
BLACK = "\u001b[40m"
RESET = "\033[0m" # Reset text attributes to default