示例#1
0
 def cancel_order_from_replace_order(self, replace_order_message, reason = b'U'):
     m = OuchClientMessages.CancelOrder(
         order_token = replace_order_message['replacement_order_token'],
         shares = 0
         )
     m.meta = replace_order_message.meta
     return m
示例#2
0
 def cancel_order_from_enter_order(self, enter_order_message, reason = b'U'):
     m = OuchClientMessages.CancelOrder(
         order_token = enter_order_message['order_token'],
         shares = 0
         )
     m.meta = enter_order_message.meta
     return m
示例#3
0
	def sendOrder(self):
		order = OuchClientMessages.EnterOrder(
			order_token='{:014d}'.format(0).encode('ascii'),
			buy_sell_indicator=b'B',
			shares=randrange(1,10**6-1),
			stock=b'AMAZGOOG',
			price=randrange(1,10**9-100),
			time_in_force=randrange(0,99999),
			firm=b'OUCH',
			display=b'N',
			capacity=b'O',
			intermarket_sweep_eligibility=b'N',
			minimum_quantity=1,
			cross_type=b'N',
			customer_type=b' ')
		self.transport.write(bytes(order))
示例#4
0
 def sendCSVorder(self):
     order = OuchClientMessages.EnterOrder(
         order_token='{:014d}'.format(0).encode('ascii'),
         buy_sell_indicator=self.buy_sell_indicator[self.i],
         shares=b'1',
         stock=b'AMAZGOOG',
         price=self.price[self.i],
         time_in_force=self.time_in_force[self.i],
         firm=b'OUCH',
         display=b'N',
         capacity=b'O',
         intermarket_sweep_eligibility=b'N',
         minimum_quantity=1,
         cross_type=b'N',
         customer_type=b' ')
     # increment through the orders
     self.i += 1
     #self.transport.write(bytes(order))
     print("Sent order: ", order)
示例#5
0
    def sendOrder(self, priceDelta, buyOrSell):
        price = self.V + priceDelta

        order = OuchClientMessages.EnterOrder(
            order_token='{:014d}'.format(0).encode('ascii'),
            buy_sell_indicator=buyOrSell,
            shares=1,
            stock=b'AMAZGOOG',
            price=int(price * 10000),
            time_in_force=4,
            firm=b'OUCH',
            display=b'N',
            capacity=b'O',
            intermarket_sweep_eligibility=b'N',
            minimum_quantity=1,
            cross_type=b'N',
            customer_type=b' ')
        self.client.transport.write(bytes(order))

        waitingTime, priceDelta, buyOrSell = self.generateNextOrder()
        reactor.callLater(waitingTime, self.sendOrder, priceDelta, buyOrSell)
示例#6
0
    def dataReceived(self, data):
        print("\nDATA RECEIVED FROM CLIENT: ", data)
        traderID = self.factory.clients.index(self)

        print('received from client', traderID, ': ' + data.decode())
        self.factory.book.append(data)

        # dictionaries used to calculate BB and BO
        buyStock = {}  # key is price, value is quantity
        sellStock = {}  # key is price, value is quantity

        #1. parse the message into our database TODO:  this has to work with replace orders
        text1 = data.decode()
        token = text1.split(':')
        print("Printing out token:\n")
        for t in token:
            print(t)
        print("\nFinished printing out token.")
        if (token[2][0] == 'B'):
            temp = token[2].split('B')
        else:
            temp = token[2].split('S')
        print("Printing out temp:\n")
        for t in temp:
            print(t)
        print("\nFinished printing out temp.")
        qShares = temp[1].split('x')
        stockName = qShares[1].split('@')

        # appending to dictionaries
        if (token[2][0] == 'B'):
            buyStock[stockName[1]] = qShares[0]
        else:
            sellStock[stockName[1]] = qShares[0]

        #2. send OUCH message to exchange server
        request = OuchClientMessages.EnterOrder(
            order_token='{:014d}'.format(traderID).encode(
                'ascii'),  #change to orderID after you figure that out
            buy_sell_indicator=token[1][0].encode(),
            shares=int(qShares[0]),
            stock=stockName[0].encode(),
            price=int(stockName[1]),
            time_in_force=randrange(0, 99999),
            firm=b'OUCH',
            display=b'N',
            capacity=b'O',
            intermarket_sweep_eligibility=b'N',
            minimum_quantity=1,
            cross_type=b'N',
            customer_type=b' ')

        self.factory.exchangeServer.sendall(bytes(request))

        print('sent to exchange: ' + data.decode())

        #3. calculate best bid and best offer
        i = 0
        for key in sorted(buyStock):
            i += 1
            if (i == len(buyStock)):
                bestBid = (key, buyStock[key])
            bestBid = (key, buyStock[key])
            #print("bestBid\n", bestBid)
            break
            #print ("%s: %s" % (key, buyStock[key]))
        for key in sorted(sellStock):
            bestOffer = (key, sellStock[key])
            #print("bestOffer\n", bestBid.key)
            #print("bestOffer\n", bestBid.sellStock[key])
            break

        #4. broadcast BB BO to all the clients
        for c in self.factory.clients:
            request = "BB2x3BO5x6"
            c.transport.write(bytes(request.encode()))
        print('sent to all clients: ' + request)
示例#7
0
    async def sender(self, loop):
        if self.reader is None:
            reader, writer = await asyncio.streams.open_connection(
                options.host, options.port, loop=loop)
            self.reader = reader
            self.writer = writer

        for index in itertools.count():
            request = OuchClientMessages.EnterOrder(
                order_token='{:014d}'.format(index).encode('ascii'),
                buy_sell_indicator=b'B' if randint(0, 1) == 1 else b'S',
                shares=1,  #randrange(1,10**6-1),
                stock=b'AMAZGOOG',
                price=rounduprounddown(randrange(1, 100), 40, 60, 0,
                                       2147483647),
                time_in_force=options.time_in_force,
                firm=b'OUCH',
                display=b'N',
                capacity=b'O',
                intermarket_sweep_eligibility=b'N',
                minimum_quantity=1,
                cross_type=b'N',
                customer_type=b' ')
            #print('send message: ', request)
            #log.info("Sending Ouch message: %s", request)
            await self.send(request)

            reprequest = OuchClientMessages.ReplaceOrder(
                existing_order_token='{:014d}'.format(index).encode('ascii'),
                replacement_order_token='{:014d}'.format(
                    900000000 + index).encode('ascii'),
                shares=2 * request['shares'],
                price=request['price'],
                time_in_force=options.time_in_force,
                display=b'N',
                intermarket_sweep_eligibility=b'N',
                minimum_quantity=1)
            #print('send message: ', request)
            #log.info("Sending Ouch message: %s", request)
            await self.send(reprequest)

            reprequestii = OuchClientMessages.ReplaceOrder(
                existing_order_token='{:014d}'.format(900000000 +
                                                      index).encode('ascii'),
                replacement_order_token='{:014d}'.format(
                    910000000 + index).encode('ascii'),
                shares=2 * request['shares'],
                price=request['price'],
                time_in_force=options.time_in_force,
                display=b'N',
                intermarket_sweep_eligibility=b'N',
                minimum_quantity=1)
            #print('send message: ', request)
            #log.info("Sending Ouch message: %s", request)
            await self.send(reprequestii)

            cancelhalf = OuchClientMessages.CancelOrder(
                order_token='{:014d}'.format(910000000 +
                                             index).encode('ascii'),
                shares=request['shares'])
            #print('send message: ', request)
            #log.info("Sending Ouch message: %s", request)
            await self.send(cancelhalf)

            if index % 1000 == 0:
                print('sent {} messages'.format(index))
            await asyncio.sleep(options.delay)
示例#8
0
def decodeClientOUCH(data):
    header = chr(data[0]).encode('ascii')
    msg_type = OuchClientMessages.lookup_by_header_bytes(header)
    msg = msg_type.from_bytes(data[1:], header=False)
    return header, msg