def update_tick(self, tick: TickData): """ Update new tick data into generator and new_shared time data. """ new_minute = False self.last_price = tick.last_price self.open_interest = tick.open_interest self.volume = tick.volume self.molecule = self.molecule + tick.last_price * tick.volume self.denominator = self.denominator + tick.volume try: self.average_price = self.molecule / self.denominator except ZeroDivisionError: self.average_price = tick.last_price if self.last_volume is None: self.last_volume = tick.volume if self.local_symbol is None: self.local_symbol = tick.local_symbol if not self.bar: new_minute = True elif self.bar.datetime.minute != tick.datetime.minute: self.bar.datetime = self.bar.datetime.replace( second=0, microsecond=0 ) self.bar.interval = 1 event = Event(type=EVENT_BAR, data=self.bar) self.rpo.put(event) [self.update_bar(x, getattr(self, "min_{}_bar".format(x)), self.bar) for x in self.XMIN] new_minute = True if new_minute: shared = SharedData(last_price=round(self.last_price, 2), datetime=tick.datetime, local_symbol=self.local_symbol, open_interest=self.open_interest, average_price=round(self.average_price, 2), volume=self.volume - self.last_volume, gateway_name=tick.gateway_name) self.last_volume = tick.volume event = Event(type=EVENT_SHARED, data=shared) self.rpo.put(event) self.bar = BarData( symbol=tick.symbol, exchange=tick.exchange, datetime=tick.datetime, gateway_name=tick.gateway_name, open_price=tick.last_price, high_price=tick.last_price, low_price=tick.last_price, close_price=tick.last_price, ) else: self.bar.high_price = max(self.bar.high_price, tick.last_price) self.bar.low_price = min(self.bar.low_price, tick.last_price) self.bar.close_price = tick.last_price self.bar.datetime = tick.datetime if self.last_tick: volume_change = tick.volume - self.last_tick.volume self.bar.volume += max(volume_change, 0) self.last_tick = tick
def on_event(self, type, data): if type == EVENT_TICK: event = Event(type=type, data=data) signal = getattr(common_signals, f"{type}_signal") signal.send(event) else: event = Event(type=type, data=data) signal = getattr(self.app_signal, f"{type}_signal") signal.send(event)
def connect(self, info): try: self.client.login(**info) title = '广发证券核新网上交易系统7.65' self.api = self.client.bind(title).init() event = Event(EVENT_LOG, data="股票账户登录成功") self.event_engine.put(event) except Exception: event = Event(EVENT_LOG, data="账户登录失败 ") self.event_engine.put(event)
def generate(self): if self.bar is not None: self.bar.interval = 1 event = Event(type=EVENT_BAR, data=self.bar) self.rpo.put(event) for x in self.XMIN: if self.bar is not None: bar = getattr(self, "min_{}_bar".format(x)) bar.interval = x event = Event(type=EVENT_BAR, data=bar) self.rpo.put(event) self.bar = None [setattr(self, "min_{}_bar".format(x), None) for x in self.XMIN]
def update_bar(self, xmin, xmin_bar: BarData, bar: BarData): """ Update 1 minute bar into generator """ if not xmin_bar: xmin_bar = BarData(symbol=bar.symbol, exchange=bar.exchange, datetime=bar.datetime, gateway_name=bar.gateway_name, open_price=bar.open_price, high_price=bar.high_price, low_price=bar.low_price) else: xmin_bar.high_price = max(xmin_bar.high_price, bar.high_price) xmin_bar.low_price = min(xmin_bar.low_price, bar.low_price) xmin_bar.close_price = bar.close_price xmin_bar.volume += int(bar.volume) if not (bar.datetime.minute + 1) % xmin: xmin_bar.datetime = xmin_bar.datetime.replace(second=0, microsecond=0) xmin_bar.interval = xmin event = Event(type=EVENT_BAR, data=xmin_bar) self.rpo.put(event) xmin_bar = None
def update_bar(self, xmin, bar: BarData): """ Update 1 minute bar into generator """ xmin_bar = getattr(self, f"min_{xmin}_bar", None) if not xmin_bar: xmin_bar = BarData(symbol=bar.symbol, exchange=bar.exchange, datetime=bar.datetime, gateway_name=bar.gateway_name, open_price=bar.open_price, high_price=bar.high_price, low_price=bar.low_price) setattr(self, f"min_{xmin}_bar", xmin_bar) else: xmin_bar.high_price = max(xmin_bar.high_price, bar.high_price) xmin_bar.low_price = min(xmin_bar.low_price, bar.low_price) xmin_bar.close_price = bar.close_price xmin_bar.volume += int(bar.volume) if not (bar.datetime.minute + 1) % xmin: xmin_bar.datetime = xmin_bar.datetime.replace(second=0, microsecond=0) xmin_bar.interval = xmin event = Event(type=EVENT_BAR, data=xmin_bar) common_signals.bar_signal.send(event) setattr(self, f"min_{xmin}_bar", None)
def _push_order(self, order: OrderData): """ 将订单回报推送到策略层,这个地方将order请求转换为order数据, 从而简化代码 :param order: :return: """ event = Event(EVENT_ORDER, deepcopy(order)) self.event_engine.put(event)
def send_order(self, order_req: OrderRequest) -> AnyStr: """发单""" result = self.risk_gateway.send(self) if False in result: event = Event(type=EVENT_LOG, data="风控阻止下单") self.event_engine.put(event) return send_monitor.send(order_req) return self.trader.send_order(order_req)
def update_tick(self, tick: TickData): """ Update new tick data into generator and new_shared time data. """ new_minute = False self.last_price = tick.last_price self.open_interest = tick.open_interest self.volume = tick.volume if self.last_volume is None: self.last_volume = tick.volume if self.local_symbol is None: self.local_symbol = tick.local_symbol if not self.bar: new_minute = True elif self.bar.datetime.minute != tick.datetime.minute: self.bar.datetime = self.bar.datetime.replace( second=0, microsecond=0 ) self.bar.interval = 1 event = Event(type=EVENT_BAR, data=self.bar) common_signals.bar_signal.send(event) [self.update_bar(x, getattr(self, "min_{}_bar".format(x)), self.bar) for x in self.XMIN] new_minute = True if new_minute: self.last_volume = tick.volume self.bar = BarData( symbol=tick.symbol, exchange=tick.exchange, datetime=tick.datetime, gateway_name=tick.gateway_name, open_price=tick.last_price, high_price=tick.last_price, low_price=tick.last_price, close_price=tick.last_price, ) else: self.bar.high_price = max(self.bar.high_price, tick.last_price) self.bar.low_price = min(self.bar.low_price, tick.last_price) self.bar.close_price = tick.last_price self.bar.datetime = tick.datetime if self.last_tick: volume_change = tick.volume - self.last_tick.volume self.bar.volume += max(volume_change, 0) self.last_tick = tick
def log(self, log): event = Event(EVENT_LOG, data=log) self.app.event_engine.put(event)
def _push_trade(self, trade): """ 将成交回报推送到策略层 """ event = Event(EVENT_TRADE, deepcopy(trade)) self.event_engine.put(event)
def on_event(type, data): event = Event(type=type, data=data) rpo.put(event)
def connect(self, info): self.userid = info.get("userid") # 初始化策略 event = Event(EVENT_INIT_FINISHED, True) self.event_engine.put(event)
def export_log(self, log): event = Event(EVENT_LOG, log) self.app.event_engine.put(event)
def push_bar(self, bar): event = Event(EVENT_BAR, bar) self.app.event_engine.put(event)
def put_stop_order_event(self, stop_order: StopOrder): """ Put an event to update stop order status. """ event = Event(EVENT_CTA_STOPORDER, stop_order) self.event_engine.put(event)
def query_account(self): data = AccountData(account_id=self.api.account, balance=self.api.balance, frozen=self.api.balance - self.api.assets) event = Event(EVENT_ACCOUNT, data=data) self.event_engine.put(data)
def running_timer(common_signal): while True: event = Event(type=EVENT_TIMER) common_signal.timer_signal.send(event) sleep(self.config['TIMER_INTERVAL'])
def query_position(self): event = Event(type=EVENT_POSITION, data=self.api.position) self.event_engine.put(event)
def onRspTic(self, data, bool): tick = loads(data) event = Event(type=EVENT_TICK, data=tick) self.event_engine.put(event)
def on_event(self, type, data): event = Event(type=type, data=data) self.event_engine.put(event)
def put_stop_order_event(self, stop_order: StopOrder): """ 推送事件到停止单处理函数更新 """ event = Event(EVENT_CTA_STOPORDER, stop_order) self.event_engine.put(event)