Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if is_holiday_today():
time.sleep(60)
continue
dt = datetime.datetime.now()
if dt.strftime("%H:%M:%S") >= "08:30:00" and dt.date() > self.before_trading_fire_date:
self.event_queue.put((dt, EVENT.BEFORE_TRADING))
self.before_trading_fire_date = dt.date()
elif dt.strftime("%H:%M:%S") >= "15:10:00" and dt.date() > self.after_trading_fire_date:
self.event_queue.put((dt, EVENT.AFTER_TRADING))
self.after_trading_fire_date = dt.date()
if is_tradetime_now():
self.event_queue.put((dt, EVENT.BAR))
def register_event(self):
event_bus = Environment.get_instance().event_bus
event_bus.add_listener(EVENT.TRADE, self._on_trade)
event_bus.add_listener(EVENT.ORDER_PENDING_NEW, self._on_order_pending_new)
event_bus.add_listener(EVENT.ORDER_CREATION_REJECT, self._on_order_unsolicited_update)
event_bus.add_listener(EVENT.ORDER_UNSOLICITED_UPDATE, self._on_order_unsolicited_update)
event_bus.add_listener(EVENT.ORDER_CANCELLATION_PASS, self._on_order_unsolicited_update)
event_bus.add_listener(EVENT.PRE_BEFORE_TRADING, self._on_before_trading)
event_bus.add_listener(EVENT.SETTLEMENT, self._on_settlement)
event_bus.add_listener(EVENT.BAR, self._update_last_price)
event_bus.add_listener(EVENT.TICK, self._update_last_price)
# 计算今仓
trades = []
trade_list = self._get_trade_list()
for trade_dict in trade_list:
trade = self._create_trade(
trade_dict["order_book_id"],
trade_dict["last_quantity"],
SIDE_DICT[trade_dict["side"]],
POSITION_EFFECT_DICT[trade_dict["position_effect"]],
)
trades.append(trade)
system_log.info("today trades: {}", trades)
self.booking_account.fast_forward([], trades=trades)
env.event_bus.add_listener(EVENT.BEFORE_SYSTEM_RESTORED, self.on_before_system_restore)
def register_event(self):
event_bus = Environment.get_instance().event_bus
event_bus.add_listener(EVENT.PRE_SETTLEMENT, self._settlement)
event_bus.add_listener(EVENT.ORDER_PEDING_NEW, self._on_order_pending_new)
event_bus.add_listener(EVENT.ORDER_CREATION_REJECT, self._on_order_creation_reject)
event_bus.add_listener(EVENT.ORDER_CANCELLATION_PASS, self._on_order_unsolicited_update)
event_bus.add_listener(EVENT.ORDER_UNSOLICITED_UPDATE, self._on_order_unsolicited_update)
event_bus.add_listener(EVENT.TRADE, self._on_trade)
self._user_context = ucontext
self._current_universe = set()
self._init = scope.get('init', None)
self._handle_bar = scope.get('handle_bar', None)
self._handle_tick = scope.get('handle_tick', None)
func_before_trading = scope.get('before_trading', None)
if func_before_trading is not None and func_before_trading.__code__.co_argcount > 1:
self._before_trading = lambda context: func_before_trading(context, None)
user_system_log.warn(_(u"deprecated parameter[bar_dict] in before_trading function."))
else:
self._before_trading = func_before_trading
self._after_trading = scope.get('after_trading', None)
if self._before_trading is not None:
event_bus.add_listener(EVENT.BEFORE_TRADING, self.before_trading)
if self._handle_bar is not None:
event_bus.add_listener(EVENT.BAR, self.handle_bar)
if self._handle_tick is not None:
event_bus.add_listener(EVENT.TICK, self.handle_tick)
if self._after_trading is not None:
event_bus.add_listener(EVENT.AFTER_TRADING, self.after_trading)
self._before_day_trading = scope.get('before_day_trading', None)
self._before_night_trading = scope.get('before_night_trading', None)
if self._before_day_trading is not None:
user_system_log.warn(_(u"[deprecated] before_day_trading is no longer used. use before_trading instead."))
if self._before_night_trading is not None:
user_system_log.warn(_(u"[deprecated] before_night_trading is no longer used. use before_trading instead."))
self._force_run_before_trading = force_run_before_trading
def __init__(self, frequency):
self._registry = []
self._today = None
self._this_week = None
self._this_month = None
self._last_minute = 0
self._current_minute = 0
self._stage = None
self._ucontext = None
self._frequency = frequency
event_bus = Environment.get_instance().event_bus
event_bus.add_listener(EVENT.PRE_BEFORE_TRADING, self.next_day_)
event_bus.add_listener(EVENT.BEFORE_TRADING, self.before_trading_)
event_bus.add_listener(EVENT.BAR, self.next_bar_)
def __init__(self, env):
self._env = env
self._config = env.config
self._universe_changed = False
self._env.event_bus.add_listener(EVENT.POST_UNIVERSE_CHANGED, self._on_universe_changed)
def start_up(self, env, mod_config):
self._show = mod_config.show
self._env = env
if self._show:
env.event_bus.add_listener(EVENT.POST_SYSTEM_INIT, self._init)
env.event_bus.add_listener(EVENT.POST_AFTER_TRADING, self._tick)
def __init__(self, env, mod_config):
self._env = env
self._mod_config = mod_config
self._matcher = Matcher(env, mod_config)
self._match_immediately = mod_config.matching_type == MATCHING_TYPE.CURRENT_BAR_CLOSE
self._open_orders = []
self._delayed_orders = []
self._frontend_validator = {}
# 该事件会触发策略的before_trading函数
self._env.event_bus.add_listener(EVENT.BEFORE_TRADING, self.before_trading)
# 该事件会触发策略的handle_bar函数
self._env.event_bus.add_listener(EVENT.BAR, self.on_bar)
# 该事件会触发策略的handel_tick函数
self._env.event_bus.add_listener(EVENT.TICK, self.on_tick)
# 该事件会触发策略的after_trading函数
self._env.event_bus.add_listener(EVENT.AFTER_TRADING, self.after_trading)