def _search_for_quantity_in_price_level_total(self, tick_entry, quantity_to_trade, order):
        if tick_entry.quantity <= Quantity(0):
            return quantity_to_trade, []

        trading_quantity = quantity_to_trade
        quantity_to_trade = Quantity(0)

        self._logger.debug("Match with the id (%s) was found for order (%s). Price: %i, Quantity: %i)",
                           str(tick_entry.order_id), str(order.order_id), int(tick_entry.price), int(trading_quantity))

        reserved = order.reserve_quantity_for_tick(tick_entry.tick.order_id, trading_quantity)

        if not reserved:  # Error happened
            self._logger.warn("Something went wrong")
            return self._search_for_quantity_in_price_level(tick_entry.next_tick(), quantity_to_trade, order)

        proposed_trades = [Trade.propose(
            self.order_book.message_repository.next_identity(),
            order.order_id,
            tick_entry.order_id,
            tick_entry.price,
            trading_quantity,
            Timestamp.now()
        )]

        return quantity_to_trade, proposed_trades
    def _search_for_quantity_in_price_level_partial(self, tick_entry, quantity_to_trade, order):
        quantity_to_trade -= tick_entry.quantity

        self._logger.debug("Match with the id (%s) was found for order (%s) ", str(tick_entry.order_id),
                           str(order.order_id))

        reserved = order.reserve_quantity_for_tick(tick_entry.tick.order_id, tick_entry.quantity)

        if not reserved:  # Error happened
            self._logger.warn("Something went wrong")
            return self._search_for_quantity_in_price_level(tick_entry.next_tick(), quantity_to_trade, order)

        proposed_trades = [Trade.propose(
            self.order_book.message_repository.next_identity(),
            order.order_id,
            tick_entry.order_id,
            tick_entry.price,
            tick_entry.quantity,
            Timestamp.now()
        )]

        # Search the next tick
        quantity_to_trade, trades = self._search_for_quantity_in_price_level(tick_entry.next_tick(),
                                                                             quantity_to_trade,
                                                                             order)

        proposed_trades = proposed_trades + trades
        return quantity_to_trade, proposed_trades
Ejemplo n.º 3
0
    def create_bitcoin_payment(self, message_id, transaction, multi_chain_payment):
        bitcoin_payment = BitcoinPayment(message_id, transaction.transaction_id, multi_chain_payment.bitcoin_address,
                                         multi_chain_payment.transferee_price, Timestamp.now())
        transaction.add_payment(bitcoin_payment)
        self.transaction_repository.update(transaction)

        return bitcoin_payment
Ejemplo n.º 4
0
    def create_multi_chain_payment(self, message_id, transaction):
        payment = transaction.next_payment()
        multi_chain_payment = MultiChainPayment(message_id, transaction.transaction_id, BitcoinAddress(''), payment[0],
                                                payment[1],
                                                Timestamp.now())
        transaction.add_payment(multi_chain_payment)
        self.transaction_repository.update(transaction)

        return multi_chain_payment
Ejemplo n.º 5
0
    def add_trade(self, other_order_id, quantity):
        self._logger.debug("Adding trade for order %s with quantity %s (other id: %s)",
                          str(self.order_id), quantity, str(other_order_id))
        self._traded_quantity += quantity
        try:
            self.release_quantity_for_tick(other_order_id)
        except TickWasNotReserved:
            pass
        assert self.available_quantity >= Quantity(0)

        if self.is_complete():
            self._completed_timestamp = Timestamp.now()
Ejemplo n.º 6
0
    def create_bid_order(self, price, quantity, timeout):
        """
        Create a bid order (buy order)

        :param price: The price for the order
        :param quantity: The quantity of the order
        :param timeout: The timeout of the order, when does the order need to be timed out
        :type price: Price
        :type quantity: Quantity
        :type timeout: Timeout
        :return: The order that is created
        :rtype: Order
        """
        assert isinstance(price, Price), type(price)
        assert isinstance(quantity, Quantity), type(quantity)
        assert isinstance(timeout, Timeout), type(timeout)

        order = Order(self.order_repository.next_identity(), price, quantity,
                      timeout, Timestamp.now(), False)
        self.order_repository.add(order)

        self._logger.info("Bid order created with id: " + str(order.order_id))

        return order
Ejemplo n.º 7
0
 def is_valid(self):
     """
     :return: True if valid, False otherwise
     :rtype: bool
     """
     return not self._timeout.is_timed_out(Timestamp.now())
Ejemplo n.º 8
0
 def cancel(self):
     self._timeout = Timestamp.now()
Ejemplo n.º 9
0
def test_now():
    """Test that Timestamp.now() returns some reasonable number."""

    assert Timestamp.now() > 1628000000
Ejemplo n.º 10
0
    def create_from_start_transaction(self, start_transaction, timeout):
        """
        :type start_transaction: StartTransaction
        :type timeout: Timeout
        :rtype: Transaction
        """
        assert isinstance(start_transaction, StartTransaction), type(start_transaction)
        assert isinstance(timeout, Timeout), type(timeout)

        transaction = Transaction(start_transaction.transaction_id, start_transaction.transaction_id.trader_id,
                                  start_transaction.price, start_transaction.quantity, timeout, Timestamp.now())
        self.transaction_repository.add(transaction)

        self._logger.info("Transaction created with id: %s, quantity: %s, price: %s",
                          str(transaction.transaction_id), str(transaction.total_quantity), str(transaction.price))

        return transaction