forked from namuan/trading-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrading_hours.py
47 lines (37 loc) · 1.08 KB
/
trading_hours.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
from datetime import datetime
from pytz import timezone
def eastern_datetime(dt):
eastern = timezone("US/Eastern")
return dt.astimezone(eastern)
def outside_trading_hours():
return not inside_trading_hours()
def after_hour_during_trading_day(given_hr):
if outside_trading_hours():
return False
now = datetime.now()
eastern = eastern_datetime(now)
eastern_hour = eastern.time().hour
about_time = eastern_hour == given_hr
if about_time:
print(f"=> After given hour {given_hr}")
return True
else:
return False
def inside_trading_hours():
now = datetime.now()
is_weekday = now.weekday() < 5
start_hr = 9
end_hr = 16
eastern = eastern_datetime(now)
eastern_hour = eastern.time().hour
within_time = start_hr <= eastern_hour <= end_hr
print(
"Checking {} => EST: {} - Inside Trading Hour - {}".format(
now, eastern_hour, within_time
)
)
if is_weekday and within_time:
print("=> Inside Trading Hour")
return True
else:
return False