-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_utils.py
91 lines (73 loc) · 2.28 KB
/
time_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
import datetime
import logging
import os
import arrow
from coretypes import FrameType
from omicron.dal.cache import cache
from omicron.models.timeframe import TimeFrame
from fetchers.abstract_quotes_fetcher import AbstractQuotesFetcher
logger = logging.getLogger(__name__)
def split_securities(all_secs_in_cache):
all_secs = set()
all_indexes = set()
for sec in all_secs_in_cache:
code = sec["code"]
_type = sec["type"]
if _type == "stock":
all_secs.add(code)
else:
all_indexes.add(code)
return all_secs, all_indexes
def get_cache_keyname(ft: FrameType):
if ft in (
FrameType.MIN1,
FrameType.MIN5,
FrameType.MIN15,
FrameType.MIN30,
FrameType.MIN60,
FrameType.DAY,
FrameType.WEEK,
FrameType.MONTH,
):
return "datascan:cursor:%s" % ft.value
else:
raise TypeError("FrameType not supported!")
async def check_running_conditions(instance):
# 周六需要取数据
dt1 = datetime.time(2, 0, 0)
dt2 = datetime.time(3, 30, 0)
# 仅限周六取数据
dt3 = datetime.time(8, 0, 0)
dt4 = datetime.time(9, 32, 0)
now = datetime.datetime.now()
nowtime = now.time()
quota = await instance.get_quota()
# quota = {"spare": 5000000}
logger.info("current quota: %d", quota["spare"])
# in trade day and saturday
if nowtime > dt1 and nowtime < dt2:
return False
if nowtime > dt3 and nowtime < dt4:
return False
if not TimeFrame.is_trade_day(now):
if quota["spare"] < 10 * 10000:
logger.error("quota less than 10,0000, break...")
return False
return True
# 工作日需要保留400万给白天使用(实际需要256万)
if nowtime < dt4:
if quota["spare"] < 400 * 10000:
logger.error("quota less than 4,000,000, break...")
return False
else:
if quota["spare"] < 10 * 10000:
logger.error("quota less than 10,0000, break...")
return False
return True
async def get_latest_day_str():
key = get_cache_keyname(FrameType.DAY)
day_start_str = await cache.sys.get(key)
if day_start_str is None:
return None
else:
return arrow.get(day_start_str).date()