def exec_reverse_trade(self, symbol_21, symbol_23, symbol_31, volume_31,
                           price_21, price_23, price_31):
        utils.log_to_slack(
            'start REVERSE trade, detected prices:\n{}: {}\n{}: {}\n{}: {}'.
            format(symbol_21, price_21, symbol_23, price_23, symbol_31,
                   price_31))

        cur3_amount = self.trade(symbol_31, 'buy', volume_31, price_31)
        time.sleep(1)

        volume_23 = cur3_amount / price_23
        decrease_step = volume_23 * 0.004
        # 若價格瞬間上漲,cur3_amount 可能不夠買 volume_23,就減少 volume_23,買到為止
        while 1:
            try:
                cur2_amount = self.trade(symbol_23, 'buy', volume_23, price_23)
                break
            except InsufficientFundsException:
                utils.log_to_slack(
                    'Insufficient funds.\nDecrease volume by {}'.format(
                        decrease_step))
                volume_23 = volume_23 - decrease_step
        time.sleep(1)

        cur1_amount = self.trade(symbol_21, 'sell', cur2_amount, price_21)
        return cur1_amount
def log_balance(trader):
    info = trader.get_balance_info()
    balance_msg = "\n".join(info)
    print('[NEW BALANCE INFO]')
    print(balance_msg)
    write_log('balance', balance_msg)
    utils.log_to_slack(balance_msg)
def log_trade(formatted_time, direction, first_currency, bridge_currency,
              second_currency, take_volume, ratio):
    trade_msg = '{}, {}, {}, {}, {}, {}, {}'.format(formatted_time, direction,
                                                    first_currency,
                                                    bridge_currency,
                                                    second_currency,
                                                    take_volume, ratio)
    print(trade_msg)
    write_log('trade', trade_msg)
    utils.log_to_slack(trade_msg)
Beispiel #4
0
def log_trade(formatted_time, direction, cur1, cur2, cur3, take_volume, ratio):
    if 'forward' == direction:
        start_cur = cur2
    elif 'reverse' == direction:
        start_cur = cur3
    else:
        raise ValueError('direction must be forward or reverse')
    trade_msg = '[{0}]\n{1}: {2}-{3}-{4}\nVolume: {5:.8f}{6}\nRatio: {7:.8f}'.format(
        formatted_time, direction.upper(), cur1, cur2, cur3, take_volume,
        start_cur, ratio)
    print(trade_msg)
    utils.log_to_slack(trade_msg)
    utils.write_log('trade', trade_msg)
    def exec_forward_trade(self, symbol_21, symbol_23, symbol_31, volume_21,
                           price_21, price_23, price_31):
        utils.log_to_slack(
            'start FORWARD trade, detected prices:\n{}: {}\n{}: {}\n{}: {}'.
            format(symbol_21, price_21, symbol_23, price_23, symbol_31,
                   price_31))

        cur2_amount = self.trade(symbol_21, 'buy', volume_21, price_21)
        time.sleep(1)

        cur3_amount = self.trade(symbol_23, 'sell', cur2_amount, price_23)
        time.sleep(1)

        cur1_amount = self.trade(symbol_31, 'sell', cur3_amount, price_31)
        return cur1_amount
Beispiel #6
0
def log_balance(exchange, amounts, amounts_before=None):
    info = [
        '[{}]'.format(time.strftime('%c')), 'Exchange: {}'.format(exchange)
    ]
    for symbol in amounts:
        amount = amounts[symbol]
        if isinstance(amounts_before, dict):
            amount_before = amounts_before[symbol]
            diff = amount - amount_before
            info.append('{0}: {1:.8f} ({2:.8f})'.format(symbol, amount, diff))
        else:
            info.append('{0}: {1:.8f}'.format(symbol, amount))
    msg = "\n".join(info)
    print('[NEW BALANCE INFO]')
    print(msg)
    utils.log_to_slack(msg)
    utils.write_log('balance', msg)
    def trade(self, pair_symbol, side, amount, price):
        utils.log_to_slack('Trade start: {0}\n{1} volume: {2:.8f}'.format(
            pair_symbol, side, amount))
        exchange_adapter = self.exchange_adapter
        response = {}
        try:
            if 'buy' == side:
                acceptable_price = price * 1.01
                response = exchange_adapter.create_limit_buy_order(
                    pair_symbol, amount, acceptable_price)
            elif 'sell' == side:
                acceptable_price = price * 0.99
                response = exchange_adapter.create_limit_sell_order(
                    pair_symbol, amount, acceptable_price)
        except ccxt.InvalidOrder as e:
            msg = 'TRADE SKIPPED - invalid order: {0}. pair: {1}, side: {2}, amount: {3:.8f}'.format(
                str(e), pair_symbol, side, amount)
            raise InsufficientFundsException(msg)
        except ccxt.InsufficientFunds:
            msg = 'TRADE SKIPPED - insufficient balance. pair: {0}, side: {1}, amount: {2:.8f}'.format(
                pair_symbol, side, amount)
            raise InsufficientFundsException(msg)
        except Exception as e:
            msg = 'Unknown error: {0}. pair: {1}, side: {2}, amount: {3:.8f}'.format(
                str(e), pair_symbol, side, amount)
            raise TradeSkippedException(msg)

        order_id = response['id']
        tmp_timestamp = time.time()
        while 1:
            try:
                order = exchange_adapter.fetch_order(order_id, pair_symbol)
            except Exception:
                order = exchange_adapter.fetch_orders(pair_symbol)[-1]
                if order['id'] != order_id:
                    time.sleep(0.5)
                    continue

            if 'closed' != order['status'] and 'canceled' != order['status']:
                current_timestamp = time.time()
                if (current_timestamp - tmp_timestamp) > 1800:
                    utils.log_to_slack('The trade is still uncompleted...')
                    tmp_timestamp = current_timestamp
                time.sleep(0.5)
                continue

            taker_fee_rate = self.get_fee_rate('taker')

            # TODO: use adapter.fetch_trade()
            if 'buy' == side:
                try:
                    fee = order['fee']['cost']
                except Exception as e:
                    fee = order['filled'] * taker_fee_rate
                got_amount = order['filled'] - fee
            else:
                fee = order['cost'] * taker_fee_rate
                got_amount = order['cost'] - fee
            utils.log_to_slack(
                'Trade complete: {0}\ngot amount: {1:.8f}'.format(
                    pair_symbol, got_amount))
            return got_amount