Skip to content

Commit

Permalink
Merge b5ca4cc into 3845f21
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-JiaJun committed May 25, 2022
2 parents 3845f21 + b5ca4cc commit ca096d0
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions rqalpha/mod/rqalpha_mod_sys_scheduler/scheduler.py
Expand Up @@ -27,22 +27,28 @@
from rqalpha.core.events import EVENT
from inspect import signature
from rqalpha.utils.exception import patch_user_exc, ModifyExceptionFromType
from rqalpha.utils.logger import system_log
from rqalpha.utils.datetime_func import convert_dt_to_int, convert_date_to_int


def market_close(hour=0, minute=0):
if Environment.get_instance().config.base.accounts.get("STOCK") is None:
system_log.warning("market_close using in stock market")
minutes_since_midnight = 15 * 60 - hour * 60 - minute
if minutes_since_midnight < 13 * 60:
minutes_since_midnight -= 90
return minutes_since_midnight


def market_open(hour=0, minute=0):
if Environment.get_instance().config.base.accounts.get("STOCK") is None:
system_log.warning("market_open using in stock market")
minutes_since_midnight = 9 * 60 + 31 + hour * 60 + minute
if minutes_since_midnight > 11 * 60 + 30:
minutes_since_midnight += 90
return minutes_since_midnight


def physical_time(hour=0, minute=0):
return hour * 60 + minute


def _verify_function(name, func):
if not callable(func):
raise patch_user_exc(ValueError('scheduler.{}: func should be callable'.format(name)))
Expand All @@ -58,8 +64,8 @@ def __init__(self, frequency):
self._today = None # type: Optional[datetime.date]
self._this_week = None # type: Optional[List[datetime.date]]
self._this_month = None # type: Optional[List[datetime.date]]
self._last_minute = 0 # type: Optional[int]
self._current_minute = 0 # type: Optional[int]
self._last_dt = 0 # type: Optional[int]
self._current_dt = 0 # type: Optional[int]
self._stage = None
self._frequency = frequency
self._trading_calendar = None
Expand Down Expand Up @@ -106,11 +112,18 @@ def _is_nth_trading_day_in_month(self, n):
return False

def _should_trigger(self, n):
# 非股票交易时间段不触发
if self._current_minute < 9*60+31 or self._current_minute > 15*60:
if self._stage == "before_trading":
# 只处理交易时间段内的定时任务
return False

return self._last_minute < n <= self._current_minute
if self._frequency == "1d":
# 日频直接触发
return True
if n > self.ucontext.now.hour * 60 + self.ucontext.now.minute:
# n > 当前minute 表示是前一个交易日的这个时刻
dt = convert_date_to_int(self.ucontext.now - datetime.timedelta(days=1)) + n // 60 * 10000 + n % 60 * 100
else:
dt = convert_date_to_int(self.ucontext.now) + n // 60 * 10000 + n % 60 * 100
return self._last_dt < dt <= self._current_dt

def _is_before_trading(self):
return self._stage == 'before_trading'
Expand All @@ -123,8 +136,8 @@ def _time_rule_for(self, time_rule):
raise patch_user_exc(ValueError(
'invalid time_rule, "before_trading" or int expected, got {}'.format(repr(time_rule))
))

time_rule = time_rule if time_rule else self._minutes_since_midnight(9, 31)
# 期货交易的交易时段存在0点
time_rule = time_rule if time_rule is not None else self._minutes_since_midnight(9, 31)
return lambda: self._should_trigger(time_rule)

@ExecutionContext.enforce_phase(EXECUTION_PHASE.ON_INIT)
Expand Down Expand Up @@ -183,8 +196,6 @@ def next_day_(self, event):
return

self._today = Environment.get_instance().trading_dt.date()
self._last_minute = 0
self._current_minute = 0
if not self._this_week or self._today > self._this_week[-1]:
self._fill_week()
if not self._this_month or self._today > self._this_month[-1]:
Expand All @@ -196,13 +207,13 @@ def _minutes_since_midnight(hour, minute):

def next_bar_(self, event):
bars = event.bar_dict
self._current_minute = self._minutes_since_midnight(self.ucontext.now.hour, self.ucontext.now.minute)
self._current_dt = convert_dt_to_int(self.ucontext.now)
for day_rule, time_rule, func in self._registry:
if day_rule() and time_rule():
with ExecutionContext(EXECUTION_PHASE.SCHEDULED):
with ModifyExceptionFromType(EXC_TYPE.USER_EXC):
func(self.ucontext, bars)
self._last_minute = self._current_minute
self._last_dt = self._current_dt

def before_trading_(self, event):
self._stage = 'before_trading'
Expand All @@ -212,6 +223,12 @@ def before_trading_(self, event):
with ModifyExceptionFromType(EXC_TYPE.USER_EXC):
func(self.ucontext, None)
self._stage = None
if self._last_dt == 0:
# 回测刚开始时将_last_df定义为前一个交易日的收盘时间,为0时,期货夜盘会触发前一个交易日早盘的任务
if self._today == self.ucontext.now.date():
self._last_dt = convert_date_to_int(self.ucontext.now - datetime.timedelta(days=1)) + 15 * 10000 + 30 * 100
else:
self._last_dt = convert_date_to_int(self.ucontext.now) + 15 * 10000 + 30 * 100

def _fill_week(self):
weekday = self._today.isoweekday()
Expand All @@ -236,7 +253,7 @@ def _fill_month(self):
def set_state(self, state):
r = json.loads(state.decode('utf-8'))
self._today = parse(r['today']).date()
self._last_minute = r['last_minute']
self._last_dt = r['last_dt']
self._fill_month()
self._fill_week()

Expand All @@ -246,5 +263,5 @@ def get_state(self):

return json.dumps({
'today': self._today.strftime('%Y-%m-%d'),
'last_minute': self._last_minute
'last_dt': self._last_dt
}).encode('utf-8')
Binary file modified tests/outs/test_f_buy_and_hold.pkl
Binary file not shown.
Binary file modified tests/outs/test_f_macd.pkl
Binary file not shown.
Binary file modified tests/outs/test_f_macd_signal.pkl
Binary file not shown.
Binary file modified tests/outs/test_f_mean_reverting.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_buy_and_hold.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_dual_thrust.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_scheduler.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_tick_size.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_turtle.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_turtle_signal.pkl
Binary file not shown.
Binary file modified tests/outs/test_sf_buy_and_hold.pkl
Binary file not shown.

0 comments on commit ca096d0

Please sign in to comment.