示例#1
0
    def on_orders_match(self, order: Order):
        """订单撮合"""
        hq = self.hq_client.get_realtime_data(order.pt_symbol)

        if hq is not None:
            now_price = float(hq.loc[0, "price"])
            if order.price_type == PriceType.MARKET.value:
                order.order_price = now_price
                # 订单成交
                self.on_orders_deal(order)
                return

            elif order.price_type == PriceType.LIMIT.value:
                if order.order_type == OrderType.BUY.value:
                    if order.order_price >= now_price:
                        if order.status == Status.SUBMITTING.value:
                            order.trade_price = now_price
                        # 订单成交
                        self.on_orders_deal(order)
                        return
                else:
                    if order.order_price <= now_price:
                        if order.status == Status.SUBMITTING.value:
                            order.trade_price = now_price
                        # 订单成交
                        self.on_orders_deal(order)
                        return

            # 没有成交更新订单状态
            self.on_orders_status_modify(order)
示例#2
0
def on_order_deal(order: Order, db):
    """订单成交处理"""
    # 持仓处理
    on_position_update(order, db)

    # 账户处理
    on_account_update(order, db)

    if order.volume == order.traded:
        order.status = Status.ALLTRADED.value
    else:
        order.status = Status.PARTTRADED.value

    on_order_update(order, db)
示例#3
0
    def on_orders_deal(self, order: Order):
        """订单成交"""
        if not order.trade_price:
            order.trade_price = order.order_price
        order.traded = order.volume
        order.trade_type = self.turnover_mode

        on_order_deal(order, self.db)

        self.write_log(
            "处理订单:账户:{}, 订单号:{}, 结果:{}".format(
                order.account_id,
                order.order_id,
                "全部成交"))
示例#4
0
    def on_back_verification(self, order: Order):
        """后端验证"""
        for k, verification in self.verification.items():
            result, msg = verification(order)

            if not result:
                order.status = Status.REJECTED.value
                order.error_msg = msg
                self.on_order_refused(order)
                self.write_log("处理订单:账户:{}, 订单号:{}, 结果:{}".format(
                    order.account_id, order.order_id, msg))

                return False

        return True
示例#5
0
def on_orders_insert(order: Order, db):
    """订单插入"""
    order.order_id = str(time.time())
    token = order.account_id
    order.status = Status.NOTTRADED.value

    raw_data = {}
    raw_data['flt'] = {'order_id': order.order_id}
    raw_data['data'] = order
    db_data = DBData(db_name=SETTINGS['TRADE_DB'],
                     db_cl=token,
                     raw_data=raw_data)

    if db.on_replace_one(db_data):
        return True, order
    else:
        return False, "交易表新增订单失败"
示例#6
0
def cancel_order_generate(token, order_id):
    """撤销订单生成器"""
    try:
        order = Order(code="",
                      exchange="",
                      account_id=token,
                      order_id=order_id,
                      order_type=OrderType.CANCEL.value)
        return order
    except Exception:
        raise ValueError("订单数据有误")
示例#7
0
    def on_order_deal(self, order: Order):
        """订单成交处理"""
        # 买入处理
        if order.order_type == OrderType.BUY.value:
            pos_val_diff = self.__on_position_append(order)
            self.__on_account_buy(order, pos_val_diff)
        # 卖出处理
        else:
            pos_val_diff = self.__on_position_reduce(order)
            self.__on_account_sell(order, pos_val_diff)

        if order.volume == order.traded:
            order.status = Status.ALLTRADED.value
        else:
            order.status = Status.PARTTRADED.value

        # 订单更新
        new_order = copy.copy(order)
        self.orders[order.order_id] = new_order

        # 订单更新事件
        self.__make_event(EVENT_ORDER_UPDATE, order)
示例#8
0
def liq_order_generate(token, symbol, price, check_date):
    """清算订单生成器"""
    try:
        code, exchange = symbol.split('.')
        order = Order(code=code,
                      exchange=exchange,
                      account_id=token,
                      order_price=price,
                      order_type=OrderType.LIQ.value,
                      order_date=check_date)
        return order
    except Exception:
        raise ValueError("订单数据有误")
示例#9
0
    def on_orders_arrived(self, order: Order):
        """订单到达"""
        # 接收订单前的验证
        if SETTINGS['VERIFICATION']:
            result, msg = self.__on_front_verification(order)
            if not result:
                return result, msg

        # 生成订单ID
        order.order_id = str(time.time())

        # 补充订单信息
        if order.order_price == 0:
            order.price_type = PriceType.MARKET.value
        else:
            order.price_type = PriceType.LIMIT.value

        self.orders[order.order_id] = order

        # 推送订单保存事件
        self.__make_event(EVENT_ORDER_INSERT, order)

        return True, order
示例#10
0
def new_order_generate(d: dict):
    """新订单生成器,提高了容错并简化了数据,也是接收订单数据的标准"""
    try:
        vol = d.get('vol', 0)
        volume = d.get('volume', vol)
        order = Order(code=d['code'],
                      exchange=d['exchange'],
                      account_id=d['account_id'],
                      order_type=d['order_type'],
                      order_price=d.get('order_price', 0),
                      volume=volume,
                      order_date=d['order_date'],
                      order_time=d['order_time'])
        return order
    except Exception:
        raise ValueError("订单数据有误")
示例#11
0
def order_generate(d: dict):
    """订单生成器"""
    try:
        order = Order(code=d['code'],
                      exchange=d['exchange'],
                      account_id=d['account_id'],
                      order_id=d['order_id'],
                      order_type=d['order_type'],
                      price_type=d['price_type'],
                      trade_type=d['trade_type'],
                      order_price=d['order_price'],
                      trade_price=d['trade_price'],
                      volume=d['volume'],
                      traded=d['traded'],
                      status=d['status'],
                      order_date=d['order_date'],
                      order_time=d['order_time'],
                      error_msg=d['error_msg'])
        return order
    except Exception:
        raise ValueError("订单数据有误")
示例#12
0
    def on_orders_match(self, order: Order):
        """订单撮合"""
        try:
            hq = self.hq_client.get_realtime_data(order.pt_symbol)

            if len(hq):
                ask1 = float(hq.loc[0, "ask1"])
                bid1 = float(hq.loc[0, 'bid1'])

                if order.order_type == OrderType.BUY.value:
                    # 涨停
                    if ask1 == 0:
                        return
                    # 市价委托即时成交
                    if order.price_type == PriceType.MARKET.value:
                        order.order_price = ask1
                        order.trade_price = ask1
                        self.on_order_deal(order)
                        return True
                    # 限价委托
                    elif order.price_type == PriceType.LIMIT.value:
                        if order.order_price >= ask1:
                            order.trade_price = ask1
                            # 订单成交
                            self.on_order_deal(order)
                            return True
                else:
                    # 跌停
                    if bid1 == 0:
                        return
                    # 市价委托即时成交
                    if order.price_type == PriceType.MARKET.value:
                        order.order_price = bid1
                        order.trade_price = bid1
                        self.on_order_deal(order)
                        return True
                    # 限价委托
                    elif order.price_type == PriceType.LIMIT.value:
                        if order.order_price <= bid1:
                            order.trade_price = bid1
                            # 订单成交
                            self.on_order_deal(order)
                            return True
        except Exception as e:
            self.write_log(traceback.format_exc())
            return False
示例#13
0
 def on_order_cancel(self, order: Order):
     """取消订单"""
     order.status = Status.CANCELLED.value
     self.on_order_refuse(order)
示例#14
0
    def on_order_deal(self, order: Order):
        """订单成交"""
        order.traded = order.volume
        order.trade_type = self.turnover_mode

        self.account_engine.orders_deal(order)