示例#1
0
    def handle_market_order(self, amount, price, type_to_place, tx_id):
        log.info(
            'handle market order price: {}, amount: {}, type_to_place: {}'.
            format(price, amount, type_to_place))

        last_id_before_market = get_last_order(self.mutex_UUID)

        if type_to_place == BUY:
            self.exchange.market_buy(amount, price)

        elif type_to_place == SELL:
            self.exchange.market_sell(amount, price)

        current_history = self.exchange.get_my_trade_history()
        if self.exchange.name != 'tux':
            self.exchange.process_new_transactions(current_history)
        market_history = get_new_history(current_history,
                                         last_id_before_market)
        market_data = get_market_results(market_history)

        # The sell gave us some BTC. The buy is executed with that BTC.
        # The market buy will get us X xmr in return. All of that xmr
        # should be placed at the original order's matching price.
        #
        # We need to do something here about the partials if it doesnt fully fill
        amount_executed = Decimal(market_data['amount_executed'])
        price_numerator = Decimal(market_data['price_numerator'])
        last_txid = market_data['last_txid']
        log.info('market data: {}'.format(market_data))
        update_merkato(self.mutex_UUID, LAST_ORDER, last_txid)

        market_order_filled = amount <= amount_executed
        if market_order_filled:
            if type_to_place == BUY:
                price = price * Decimal(1 + self.spread)
                self.exchange.sell(amount_executed,
                                   price)  # Should never market order
            elif type_to_place == SELL:
                price = price * Decimal(1 - self.spread)
                self.exchange.buy(amount_executed, price)
        else:
            log.info(
                'handle_market_order: partials affected, amount: {} amount_executed: {}'
                .format(amount, amount_executed))
            if type_to_place == BUY:
                self.quote_partials_balance += amount_executed
                update_merkato(self.mutex_UUID, 'quote_partials_balance',
                               float(self.quote_partials_balance))
                log.info('market buy partials after: {}'.format(
                    self.quote_partials_balance))
            else:
                self.base_partials_balance += amount_executed * price_numerator
                update_merkato(self.mutex_UUID, 'base_partials_balance',
                               float(self.base_partials_balance))
                log.info('market sell partials after {}'.format(
                    self.base_partials_balance))
示例#2
0
    def rebalance_orders(self, new_txes):
        # This function places a matching order for every new transaction since last run
        #
        # profit_margin is a number from 0 to 1 representing the percent of the spread to return
        # to the user's balance before placing the matching order.
        #
        # TODO: Modify so that the parent function only passes in the new transactions, don't
        # do the index check internally.

        # new_history is an array of transactions
        # new_txes is the number of new transactions contained in new_history
        factor = self.spread*self.profit_margin/2
        ordered_transactions = new_txes
        log.info('ordered transactions rebalanced: {}'.format(ordered_transactions))
        for tx in ordered_transactions:
            log.info('length of ordered_transactions length length: {}'.format(len(ordered_transactions)))
            if tx['type'] == SELL:
                log.info("Found sell {} corresponding buy {}".format(tx, sell_price))
                
                amount = float(tx['amount']) * float(tx[PRICE])*(1-factor)
                price = float(tx[PRICE])
                buy_price = price * ( 1  - self.spread)
                log.info("found sell {}; corresponding buy {}".format(tx, buy_price))
                market = self.exchange.buy(amount, buy_price)
                if market == MARKET:
                    log.info('market sell', market)
                    last_order_time = str(int(time.time()))
                    self.exchange.market_buy(amount, buy_price)
                    market_history = self.exchange.get_my_trade_history(start=last_order_time)
                    market_data = get_market_results(market_history)

                    # We have a sell executed. We want to place a matching buy order.
                    # If the whole order is executed, no edge case.
                    # If the order has a remainder, the remainder will be on the books at
                    # the appropriate price. So no problem. 
                    # If the remainder is too small to have a matching order, it could 
                    # disappear, but this is such a minor edge case we can ignore it.
                    # 
                    # The sell gave us some BTC. The buy is executed with that BTC.
                    # The market buy will get us X xmr in return. All of that xmr
                    # should be placed at the original order's matching price.
                    amount_executed = float(market_data['amount_executed'])
                    last_orderid    = market_data['last_orderid']
                    log.info('market data: {}'.format(market_data))

                    self.exchange.sell(amount_executed, price) # Should never market order

                    # A market buy occurred, so we need to update the db with the latest tx
                    update_merkato(self.mutex_UUID, LAST_ORDER, last_orderid)

            if tx['type'] == BUY:
                amount = float(tx['amount'])*float((1-factor))
                price = tx[PRICE]
                sell_price = float(price) * ( 1  + self.spread)
                log.info("Found buy {} corresponding sell {}".format(tx, sell_price))
                market = self.exchange.sell(amount, sell_price)
                if market == MARKET:
                    log.info('market buy {}'.format(market))
                    last_order_time = str(int(time.time()))
                    self.exchange.market_sell(amount, sell_price)
                    market_history = self.exchange.get_my_trade_history(start=last_order_time)
                    market_data = get_market_results(market_history)

                    # We have a buy executed. We want to place a matching sell order.
                    # If the whole order is executed, no edge case.
                    # If the order has a remainder, the remainder will be on the books at
                    # the appropriate price. So no problem. 
                    # If the remainder is too small to have a matching order, it could 
                    # disappear, but this is such a minor edge case we can ignore it.
                    # 
                    # The buy gave us some alt. The sell is executed with that alt.
                    # The market sell will get us X btc in return. All of that btc
                    # should be placed at the original order's matching price.
                    amount_executed = float(market_data['total_gotten'])
                    last_orderid    = market_data['last_orderid']
                    log.info('market data {}'.format(market_data))
                    self.exchange.buy(amount_executed, float(price)) # Should never market order

                    # A market buy occurred, so we need to update the db with the latest tx
                    update_merkato(self.mutex_UUID, LAST_ORDER, last_orderid)

            if market != MARKET: 
                log.info('market != MARKET market != MARKET')
                update_merkato(self.mutex_UUID, LAST_ORDER, tx['orderId'])
            
            first_order = get_first_order(self.mutex_UUID)
            no_first_order = first_order == ''
            if no_first_order:
                update_merkato(self.mutex_UUID, FIRST_ORDER, tx['orderId'])

        self.log_new_transactions(ordered_transactions)
        
        return ordered_transactions