Example #1
0
def orderbook_demo():
    import time, random
    ITERS = 100000
    max_price = 10
    ob = OrderBook("FOOBAR", max_price=max_price)
    start = time.clock()
    for i in range(ITERS):
        os.system("clear")
        buysell, qty, price, trader = random.choice([0,1]), random.randrange(1,50), \
                random.randrange(1,max_price), 'trader %s' % random.randrange(1000)
        print "NEW ORDER: %s %s %s @ %s" % (trader, "BUY" if buysell==0 else "SELL", qty, price)
        ob.limit_order(buysell, qty, price, trader)
        print ob.render()
        raw_input()
Example #2
0
class Engine:
    def __init__(self):
        self.orderbook = OrderBook()
        self.orderbook.on_event = self.on_event
        self.trades = []
        self._next_order_id = 0
        self.all_orders = {}

    def next_order_id(self):
        n = self._next_order_id
        self._next_order_id += 1
        return n

    def limit_order(self, side, price, amount):
        o = Order(self.next_order_id(), side, price, amount)
        self.all_orders[o.id] = o
        self.orderbook.limit_order(o)
        self.validate()
        return o

    def cancel_order(self, orderid):
        o = self.orderbook.cancel_order(self.all_orders[orderid].price,
                                        orderid)
        self.validate()
        return o

    def on_event(self, name, evt):
        if name == 'trade':
            self.trades.append(evt)

    def validate(self):
        book = self.orderbook
        assert not book.bids or not book.asks or book.bids.max() < book.asks.min(), \
            'bids asks shouldn\'t cross'
        assert all(price == self.all_orders[makerid].price
                   for _, makerid, _, price in self.trades), \
            'trade price equals to maker price'
        assert all(better_price(price,
                                self.all_orders[takerid].price,
                                self.all_orders[takerid].side)
                   for takerid, _, _, price in self.trades), \
            'trade price equal or better than taker price'
        assert all(order.price == price
                   for price, lvl in book.levels.items()
                   for order in lvl.orders), \
            'level price is correct'
        assert all(sum(o.size for o in lvl.orders) == lvl.volume
                   for price, lvl in book.levels.items()), \
            'level volume is correct'
Example #3
0
def orderbook_demo():
    import time, random
    ITERS = 100000
    max_price = 10
    ob = OrderBook("FOOBAR", max_price=max_price)
    start = time.clock()
    for i in range(ITERS):
        os.system("clear")
        buysell, qty, price, trader = random.choice([0,1]), random.randrange(1,50), \
                random.randrange(1,max_price), 'trader %s' % random.randrange(1000)
        print "NEW ORDER: %s %s %s @ %s" % (trader, "BUY" if buysell == 0 else
                                            "SELL", qty, price)
        ob.limit_order(buysell, qty, price, trader)
        print ob.render()
        raw_input()
Example #4
0
def bench(n):
    book = OrderBook()
    length = len(tests)
    for i in range(n):
        o = tests[i % length]
        book.limit_order(Order(o.id, o.side, o.price, o.original_size))