Beispiel #1
0
    def match_one(self, id):

        order = self.services.bus.get_orderbook()[id]
        if not isinstance(order.leaves, int): order.leaves = order.size
        market = self.services.bus.get_market_data()[order.symbol]
        executions = []
        if order.side == 1:
            limit = 0
            while market.has_key("ask-%d" % limit) and order.price >= market[
                    "ask-%d" % limit]["price"]:  #buy order check over ask
                # build the execution report for this limit
                e = ExecutionReport()
                e.price = market["ask-%d" % limit]["price"]
                e.size = min(market["ask-%d" % limit]["size"], order.leaves)
                e.symbol = order.symbol
                e.id = order.id
                executions.append(e)

                # update the order
                order.executed += e.size
                order.leaves -= e.size
                if order.leaves <= 0:
                    break

                limit += 1

        if order.side == -1 and order.price <= market["bid-0"][
                "price"]:  # sell order check over bid
            limit = 0
            while market.has_key("bid-%d" % limit) and order.price <= market[
                    "bid-%d" % limit]["price"]:  #buy order check over bid
                # build the execution report for this limit
                e = ExecutionReport()
                e.price = market["bid-%d" % limit]["price"]
                e.size = min(market["bid-%d" % limit]["size"], order.leaves)
                e.symbol = order.symbol
                e.id = order.id
                executions.append(e)

                # update the order
                order.executed += e.size
                order.leaves -= e.size
                if order.leaves <= 0:
                    break

                limit += 1

        if order.leaves <= 0:
            del order

        if executions:
            # push execution in the bus
            self.services.bus._last_execution[id] = executions
            self.services.events['Execution'].emit(id)
Beispiel #2
0
 def match_one(self, id):        
             
     order = self.services.bus.get_orderbook()[id]
     if not isinstance(order.leaves, int): order.leaves = order.size
     market = self.services.bus.get_market_data()[order.symbol]
     executions = []
     if order.side == 1: 
         limit = 0
         while market.has_key("ask-%d" % limit) and order.price >= market["ask-%d" % limit]["price"]: #buy order check over ask                         
             # build the execution report for this limit
             e = ExecutionReport()
             e.price = market["ask-%d" % limit]["price"]
             e.size = min(market["ask-%d" % limit]["size"], order.leaves)                
             e.symbol = order.symbol
             e.id = order.id
             executions.append(e)
             
             # update the order
             order.executed += e.size
             order.leaves   -= e.size
             if order.leaves <= 0:                    
                 break 
         
             limit += 1
             
     if order.side == -1 and order.price <= market["bid-0"]["price"]: # sell order check over bid
         limit = 0
         while market.has_key("bid-%d" % limit) and order.price <= market["bid-%d" % limit]["price"]: #buy order check over bid                         
             # build the execution report for this limit
             e = ExecutionReport()
             e.price = market["bid-%d" % limit]["price"]
             e.size = min(market["bid-%d" % limit]["size"], order.leaves)                
             e.symbol = order.symbol
             e.id = order.id
             executions.append(e)
             
             # update the order
             order.executed += e.size
             order.leaves   -= e.size
             if order.leaves <= 0:                    
                 break 
             
             limit += 1
     
     if order.leaves <= 0:
         del order
     
     if executions:
         # push execution in the bus
         self.services.bus._last_execution[id] = executions    
         self.services.events['Execution'].emit(id)
    def match_one(self, id):
        order = self.services.bus._new_order
        order.leaves = order.size
        order.executed = 0
        executions = []
        if order.side == 1:  # buy
            if order.symbol not in self.services.bus._private_data_sell.keys():
                return
            ask = self.services.bus._private_data_sell[order.symbol]
            prices = sorted(ask.keys())
            i = 0
            current_price = prices[i]
            while order.leaves > 0 and current_price <= order.price:
                for o in ask[current_price]:
                    e = ExecutionReport()
                    e.price = current_price
                    e.size = min(o.leaves, order.leaves)
                    e.symbol = order.symbol
                    e.id = order.id
                    e.parent = order.parent
                    executions.append(e)

                    e = ExecutionReport()
                    e.price = current_price
                    e.size = min(o.leaves, order.leaves)
                    e.symbol = order.symbol
                    e.id = o.id
                    e.parent = o.parent

                    o.leaves -= e.size
                    o.executed += e.size

                    id = o.id
                    if o.leaves <= 0:
                        self.services.logger.debug(
                            "Order %s fully filled. Removing from market" % o)
                        del o

                    self.services.bus._last_execution[id] = []
                    self.services.bus._last_execution[id].append(e)
                    self.services.events['Execution'].emit(id)

                    order.leaves -= e.size
                    order.executed += e.size
                    if order.leaves == 0:
                        self.services.bus._new_order = None
                        break

                i = i + 1
                current_price = prices[i]
        elif order.side == -1:  # buy
            if order.symbol not in self.services.bus._private_data_buy.keys():
                return
            bid = self.services.bus._private_data_buy[order.symbol]
            prices = sorted(bid.keys(), reverse=True)
            i = 0
            current_price = prices[i]
            while order.leaves > 0 and current_price >= order.price:
                for o in bid[current_price]:
                    e = ExecutionReport()
                    e.price = current_price
                    e.size = min(o.leaves, order.leaves)
                    e.symbol = order.symbol
                    e.id = order.id
                    e.parent = order.parent
                    executions.append(e)

                    e = ExecutionReport()
                    e.price = current_price
                    e.size = min(o.leaves, order.leaves)
                    e.symbol = order.symbol
                    e.id = o.id
                    e.parent = o.parent

                    o.leaves -= e.size
                    o.executed += e.size

                    id = o.id
                    if o.leaves <= 0:
                        self.services.logger.debug(
                            "Order %s fully filled. Removing from market" % o)
                        del o

                    self.services.bus._last_execution[id] = []
                    self.services.bus._last_execution[id].append(e)
                    self.services.events['Execution'].emit(id)

                    order.leaves -= e.size
                    order.executed += e.size
                    if order.leaves == 0:
                        self.services.bus._new_order = None
                        break

                i = i + 1
                current_price = prices[i]

        if len(executions) > 0:
            self.services.bus._last_execution[order.id] = []
            self.services.bus._last_execution[order.id].extend(executions)
            self.services.events['Execution'].emit(order.id)

        if str(order.timeinforce).upper() == 'IOC':
            self.services.bus._last_reject[order.id] = order
            #self.services.bus._last_reject[order.id].append(order)
            self.services.events['Reject'].emit(order.id)

        return False