Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
user_strategy = Strategy(env.event_bus, scope, ucontext, should_run_init)
env.user_strategy = user_strategy
if (should_resume and not should_run_init) or not should_resume:
with run_with_user_log_disabled(disabled=should_resume):
user_strategy.init()
if config.extra.context_vars:
for k, v in six.iteritems(config.extra.context_vars):
if isinstance(v, RqAttrDict):
v = v.__dict__
setattr(ucontext, k, v)
if persist_helper:
env.event_bus.publish_event(Event(EVENT.BEFORE_SYSTEM_RESTORED))
env.event_bus.publish_event(Event(EVENT.DO_RESTORE))
env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_RESTORED))
init_succeed = True
if should_resume and should_run_init:
user_strategy.init()
executor.run(bar_dict)
if env.profile_deco:
output_profile_result(env)
except CustomException as e:
if init_succeed and persist_helper and env.config.base.persist_mode == const.PERSIST_MODE.ON_CRASH:
persist_helper.persist()
def check_before_trading(e):
if self._last_before_trading == event.trading_dt.date():
return False
if self._env.config.extra.is_hold:
return False
if self._last_before_trading:
# don't publish settlement on first day
publish_settlement(e)
self._last_before_trading = e.trading_dt.date()
update_time(e)
event_bus.publish_event(PRE_BEFORE_TRADING)
event_bus.publish_event(Event(EVENT.BEFORE_TRADING, calendar_dt=e.calendar_dt, trading_dt=e.trading_dt))
event_bus.publish_event(POST_BEFORE_TRADING)
return True
trading_dt=trading_dt - datetime.timedelta(minutes=30))
if self._universe_changed:
self._universe_changed = False
last_dt = calendar_dt
exit_loop = False
break
# yield handle bar
yield Event(EVENT.BAR, calendar_dt=calendar_dt, trading_dt=trading_dt)
if exit_loop:
done = True
dt = date.replace(hour=15, minute=30)
yield Event(EVENT.AFTER_TRADING, calendar_dt=dt, trading_dt=dt)
dt = date.replace(hour=17, minute=0)
yield Event(EVENT.SETTLEMENT, calendar_dt=dt, trading_dt=dt)
month=date.month,
day=date.day)
else:
trading_dt = calendar_dt
if before_trading_flag:
before_trading_flag = False
yield Event(EVENT.BEFORE_TRADING,
calendar_dt=calendar_dt - datetime.timedelta(minutes=30),
trading_dt=trading_dt - datetime.timedelta(minutes=30))
if self._universe_changed:
self._universe_changed = False
last_dt = calendar_dt
exit_loop = False
break
# yield handle bar
yield Event(EVENT.BAR, calendar_dt=calendar_dt, trading_dt=trading_dt)
if exit_loop:
done = True
dt = date.replace(hour=15, minute=30)
yield Event(EVENT.AFTER_TRADING, calendar_dt=dt, trading_dt=dt)
dt = date.replace(hour=17, minute=0)
yield Event(EVENT.SETTLEMENT, calendar_dt=dt, trading_dt=dt)
def submit_order(self, order):
account = self._env.get_account(order.order_book_id)
self._env.event_bus.publish_event(Event(EVENT.ORDER_PENDING_NEW, account=account, order=order))
if order.is_final():
return
if self._env.config.base.frequency == '1d' and not self._match_immediately:
self._delayed_orders.append((account, order))
return
self._open_orders.append((account, order))
order.active()
self._env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_PASS, account=account, order=order))
if self._match_immediately:
self._match()
def publish_settlement(e=None):
if e:
previous_trading_date = self._env.data_proxy.get_previous_trading_date(e.trading_dt).date()
if self._env.trading_dt.date() != previous_trading_date:
self._env.trading_dt = datetime.combine(previous_trading_date, self._env.trading_dt.time())
self._env.calendar_dt = datetime.combine(previous_trading_date, self._env.calendar_dt.time())
event_bus.publish_event(PRE_SETTLEMENT)
event_bus.publish_event(Event(EVENT.SETTLEMENT))
event_bus.publish_event(POST_SETTLEMENT)
def output_profile_result(env):
stdout_trap = six.StringIO()
env.profile_deco.print_stats(stdout_trap)
profile_output = stdout_trap.getvalue()
profile_output = profile_output.rstrip()
six.print_(profile_output)
env.event_bus.publish_event(Event(EVENT.ON_LINE_PROFILER_RESULT, result=profile_output))
EVENT.BEFORE_TRADING,
calendar_dt=calendar_dt - datetime.timedelta(minutes=30),
trading_dt=trading_dt - datetime.timedelta(minutes=30)
)
if self._universe_changed:
self._universe_changed = False
last_dt = calendar_dt
exit_loop = False
break
# yield handle bar
yield Event(EVENT.BAR, calendar_dt=calendar_dt, trading_dt=trading_dt)
if exit_loop:
done = True
dt = self._get_after_trading_dt(date)
yield Event(EVENT.AFTER_TRADING, calendar_dt=dt, trading_dt=dt)
elif frequency == "tick":
data_proxy = self._env.data_proxy
for day in data_proxy.get_trading_dates(start_date, end_date):
date = day.to_pydatetime()
last_tick = None
last_dt = None
dt_before_day_trading = date.replace(hour=8, minute=30)
while True:
for tick in data_proxy.get_merge_ticks(self._get_universe(), date, last_dt):
# find before trading time
calendar_dt = tick.datetime
if calendar_dt < dt_before_day_trading:
trading_dt = calendar_dt.replace(year=date.year, month=date.month, day=date.day)
else:
if dt < dt_before_day_trading:
trading_dt = dt.replace(year=date.year, month=date.month, day=date.day)
else:
trading_dt = dt
yield Event(EVENT.TICK, calendar_dt=dt, trading_dt=trading_dt, tick=tick)
if self._universe_changed:
self._universe_changed = False
last_dt = dt
break
else:
break
dt = date.replace(hour=15, minute=30)
yield Event(EVENT.AFTER_TRADING, calendar_dt=dt, trading_dt=dt)
dt = date.replace(hour=17, minute=0)
yield Event(EVENT.SETTLEMENT, calendar_dt=dt, trading_dt=dt)
def _get_events_for_d(self, start_date, end_date, frequency):
num = int(frequency[:-1])
for day in islice(self._env.data_proxy.get_trading_dates(start_date, end_date), None, None, num):
date = day.to_pydatetime()
dt_before_trading = date.replace(hour=0, minute=0)
dt_bar = date.replace(hour=15, minute=0)
dt_after_trading = date.replace(hour=15, minute=30)
dt_settlement = date.replace(hour=17, minute=0)
yield Event(EVENT.BEFORE_TRADING, calendar_dt=dt_before_trading, trading_dt=dt_before_trading)
yield Event(EVENT.BAR, calendar_dt=dt_bar, trading_dt=dt_bar)
yield Event(EVENT.AFTER_TRADING, calendar_dt=dt_after_trading, trading_dt=dt_after_trading)
yield Event(EVENT.SETTLEMENT, calendar_dt=dt_settlement, trading_dt=dt_settlement)