예제 #1
0
    def update(self):
        # Get current state of trade history before placing orders
        log.info("Update entered")
        
        now = str(datetime.datetime.now().isoformat()[:-7].replace("T", " "))
        last_trade_price = self.exchange.get_last_trade_price()

        first_order = get_first_order(self.mutex_UUID)
        current_history = self.exchange.get_my_trade_history(first_order)
        last_order = get_last_order(self.mutex_UUID)
        new_history = get_new_history(current_history, last_order)
        print('first_order', first_order)
        print('last_order', last_order)
        print('new_history', new_history)
        new_transactions = []
        
        if len(new_history) > 0:
            # We have new transactions
            log.info('we have new history')
            log.debug("New transactions: {}".format(new_history))
            new_transactions = self.rebalance_orders(new_history)
            #self.merge_orders()
            
        # context to be used for GUI plotting
        context = {"price": (now, last_trade_price),
                   "filled_orders": new_transactions,
                   "open_orders": self.exchange.get_my_open_orders(context_formatted=True),
                   "balances": self.exchange.get_balances(),
                   "orderbook": self.exchange.get_all_orders()
                   }
        
        return context
예제 #2
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))
예제 #3
0
    def update(self):
        ''' TODO: Function comment
        '''
        log.info("Update entered\n")

        now = str(datetime.datetime.now().isoformat()[:-7].replace("T", " "))
        last_trade_price = self.exchange.get_last_trade_price()
        if last_trade_price == "EOF":
            # Test merkato datastream ended
            print("test datastream ended")
            return "stuffs"

        first_order = get_first_order(self.mutex_UUID)
        last_order = get_last_order(self.mutex_UUID)

        current_history = self.exchange.get_my_trade_history()
        new_history = get_new_history(current_history, last_order)
        log.info(
            'update new_history: {} first_order: {} last_order: {} \n'.format(
                new_history, first_order, last_order))
        new_transactions = []

        if len(new_history) > 0:
            log.info('we have new history')
            log.debug("New transactions: {} \n".format(new_history))

            new_transactions = self.rebalance_orders(new_history)
            #self.merge_orders()
            # todo: Talk about whether merging 'close enough' orders is reasonable.

        # context to be used for GUI plotting
        context = {
            "price": (now, last_trade_price),
            "filled_orders": new_transactions,
            "open_orders":
            self.exchange.get_my_open_orders(context_formatted=True),
            "balances": self.exchange.get_balances(),
            "orderbook": self.exchange.get_all_orders(),
            "starting_price": self.starting_price,
            "starting_base": self.bid_reserved_balance * 4,
            "starting_quote": self.ask_reserved_balance * 4,
            "spread": self.spread,
            "step": self.step
        }

        return context
예제 #4
0
    def __init__(self, configuration, coin, base, spread,
                 bid_reserved_balance, ask_reserved_balance,
                 user_interface=None, profit_margin=0, first_order=''):

        validate_merkato_initialization(configuration, coin, base, spread)
        self.initialized = False
        UUID = configuration[EXCHANGE] + "coin={}_base={}".format(coin,base)
        self.mutex_UUID = UUID
        self.distribution_strategy = 1
        self.spread = float(spread)
        self.profit_margin = profit_margin
        # Create ladders from the bid and ask bidget here
        self.bid_reserved_balance = bid_reserved_balance
        self.ask_reserved_balance = ask_reserved_balance
        self.user_interface = user_interface
        exchange_class = get_relevant_exchange(configuration[EXCHANGE])
        self.exchange = exchange_class(configuration, coin=coin, base=base)
        merkato_does_exist = merkato_exists(UUID)

        if not merkato_does_exist:
            log.info("Creating New Merkato")
            self.cancelrange(ONE_SATOSHI, ONE_BITCOIN)
            total_pair_balances = self.exchange.get_balances()
            log.info("total pair balances", total_pair_balances)
            allocated_pair_balances = get_allocated_pair_balances(configuration['exchange'], base, coin)
            check_reserve_balances(total_pair_balances, allocated_pair_balances, coin_reserve=ask_reserved_balance, base_reserve=bid_reserved_balance)
            insert_merkato(configuration[EXCHANGE], UUID, base, coin, spread, bid_reserved_balance, ask_reserved_balance, first_order)
            history = self.exchange.get_my_trade_history()
            print('initial history', history)
            if len(history) > 0:
                print('updating history', history[0]['orderId'])
                new_last_order = history[0]['orderId']
                update_merkato(self.mutex_UUID, LAST_ORDER, new_last_order)
            self.distribute_initial_orders(total_base=bid_reserved_balance, total_alt=ask_reserved_balance)

        else:
            #self.history = get_old_history(self.exchange.get_my_trade_history(), self.mutex_UUID)
            first_order = get_first_order(self.mutex_UUID)
            current_history = self.exchange.get_my_trade_history(first_order)
            last_order = get_last_order(self.mutex_UUID)
            new_history = get_new_history(current_history, last_order)
            self.rebalance_orders(new_history)
        self.initialized = True  # to avoid being updated before orders placed
예제 #5
0
    def __init__(self,
                 configuration,
                 coin,
                 base,
                 spread,
                 bid_reserved_balance,
                 ask_reserved_balance,
                 user_interface=None,
                 profit_margin=0,
                 first_order='',
                 starting_price=.018,
                 increased_orders=0,
                 step=1.0033,
                 distribution_strategy=1,
                 init_base_balance=0,
                 init_quote_balance=0,
                 base_profit=0,
                 quote_profit=0,
                 buy_volume=0,
                 sell_volume=0):

        validate_merkato_initialization(configuration, coin, base, spread)
        self.initialized = False
        UUID = configuration[EXCHANGE] + "coin={}_base={}".format(coin, base)
        self.mutex_UUID = UUID
        self.distribution_strategy = distribution_strategy
        self.spread = Decimal(spread)
        self.profit_margin = Decimal(profit_margin)
        self.starting_price = starting_price
        self.step = step
        self.increased_orders = increased_orders
        self.quote_profit = Decimal(quote_profit)
        self.base_profit = Decimal(base_profit)
        self.bid_reserved_balance = Decimal(float(bid_reserved_balance))
        self.ask_reserved_balance = Decimal(float(ask_reserved_balance))
        self.init_base_balance = init_base_balance
        self.init_quote_balance = init_quote_balance
        # The current sum of all partially filled orders
        self.base_partials_balance = 0
        self.quote_partials_balance = 0
        self.buy_volume = buy_volume
        self.sell_volume = sell_volume
        self.last_placed_UUID = ''  #this assures that no faulty doubled up orders will be placed sequentially

        self.user_interface = user_interface

        exchange_class = get_relevant_exchange(configuration[EXCHANGE])
        self.exchange = exchange_class(configuration, coin=coin, base=base)

        merkato_does_exist = merkato_exists(self.mutex_UUID)

        if not merkato_does_exist:
            log.info("Creating New Merkato")

            self.cancelrange(ONE_SATOSHI, ONE_BITCOIN)

            total_pair_balances = self.exchange.get_balances()

            log.info("total pair balances: {}".format(total_pair_balances))

            allocated_pair_balances = get_allocated_pair_balances(
                configuration['exchange'], base, coin)
            check_reserve_balances(total_pair_balances,
                                   allocated_pair_balances,
                                   coin_reserve=ask_reserved_balance,
                                   base_reserve=bid_reserved_balance)

            insert_merkato(configuration[EXCHANGE],
                           self.mutex_UUID,
                           base,
                           coin,
                           spread,
                           bid_reserved_balance,
                           ask_reserved_balance,
                           first_order,
                           starting_price,
                           init_base_balance=bid_reserved_balance,
                           init_quote_balance=ask_reserved_balance,
                           step=step)
            history = self.exchange.get_my_trade_history()

            log.debug('initial history: {}'.format(history))

            if len(history) > 0:
                log.debug('updating history first ID: {}'.format(
                    history[0][ID]))
                new_last_order = history[0][ID]
                update_merkato(self.mutex_UUID, LAST_ORDER, new_last_order)
            self.distribute_initial_orders(total_base=bid_reserved_balance,
                                           total_alt=ask_reserved_balance)

        else:
            first_order = get_first_order(self.mutex_UUID)
            current_history = self.exchange.get_my_trade_history()
            last_order = get_last_order(self.mutex_UUID)
            new_history = get_new_history(current_history, last_order)
            self.rebalance_orders(new_history)

        self.initialized = True  # to avoid being updated before orders placed
예제 #6
0
파일: merkato.py 프로젝트: evdc/merkato
    def __init__(self,
                 configuration,
                 coin,
                 base,
                 spread,
                 bid_reserved_balance,
                 ask_reserved_balance,
                 user_interface=None,
                 profit_margin=0,
                 first_order='',
                 starting_price=.018,
                 quote_volume=0,
                 base_volume=0):

        validate_merkato_initialization(configuration, coin, base, spread)
        self.initialized = False
        UUID = configuration[EXCHANGE] + "coin={}_base={}".format(coin, base)
        self.mutex_UUID = UUID
        self.distribution_strategy = 1
        self.spread = Decimal(spread)
        self.profit_margin = Decimal(profit_margin)
        self.starting_price = starting_price
        self.quote_volume = Decimal(quote_volume)
        self.base_volume = Decimal(base_volume)
        self.step = 1.0033
        # Exchanges have a maximum number of orders every user can place. Due
        # to this, every Merkato has a reserve of coins that are not currently
        # allocated. As the price approaches unallocated regions, the reserves
        # are deployed.
        self.bid_reserved_balance = Decimal(float(bid_reserved_balance))
        self.ask_reserved_balance = Decimal(float(ask_reserved_balance))

        # The current sum of all partially filled orders
        self.base_partials_balance = 0
        self.quote_partials_balance = 0

        self.user_interface = user_interface

        exchange_class = get_relevant_exchange(configuration[EXCHANGE])
        self.exchange = exchange_class(configuration, coin=coin, base=base)

        merkato_does_exist = merkato_exists(self.mutex_UUID)

        if not merkato_does_exist:
            log.info("Creating New Merkato")

            self.cancelrange(ONE_SATOSHI, ONE_BITCOIN)

            total_pair_balances = self.exchange.get_balances()

            log.info("total pair balances: {}".format(total_pair_balances))

            allocated_pair_balances = get_allocated_pair_balances(
                configuration['exchange'], base, coin)
            check_reserve_balances(total_pair_balances,
                                   allocated_pair_balances,
                                   coin_reserve=ask_reserved_balance,
                                   base_reserve=bid_reserved_balance)

            insert_merkato(configuration[EXCHANGE], self.mutex_UUID, base,
                           coin, spread, bid_reserved_balance,
                           ask_reserved_balance, first_order, starting_price)
            history = self.exchange.get_my_trade_history()

            log.debug('initial history: {}'.format(history))

            if len(history) > 0:
                log.debug('updating history first ID: {}'.format(
                    history[0][ID]))
                new_last_order = history[0][ID]
                update_merkato(self.mutex_UUID, LAST_ORDER, new_last_order)
            self.distribute_initial_orders(total_base=bid_reserved_balance,
                                           total_alt=ask_reserved_balance)

        else:
            first_order = get_first_order(self.mutex_UUID)
            current_history = self.exchange.get_my_trade_history(first_order)
            last_order = get_last_order(self.mutex_UUID)
            new_history = get_new_history(current_history, last_order)
            self.rebalance_orders(new_history)

        self.initialized = True  # to avoid being updated before orders placed