def on_message(ws, message):
    global sell_price, sell_times
    message = bytes.decode(inflate(message), 'utf-8')  # data decompress
    if 'pong' in message or 'addChannel' in message:
        return
    jmessage = json.loads(message)

    for each_message in jmessage:
        data = each_message['data']
        asks = data['asks'][::-1]
        bids = data['bids']
        ask_price = float(asks[0][0])
        bid_price = float(bids[0][0])
        if ask_price < sell_price:
            spotAPI.revoke_order(instrument_id, old_order_id)
        #下单
        if ask_price >= bid_price * 1.0008:
            new_order_id = spot_sell(spotAPI, instrument_id, amount,
                                     ask_price - 0.0001)
            if new_order_id:
                old_order_id = new_order_id
                sell_price = ask_price - 0.0001
                sell_times += 1

                bid_info = 'ask_price: %.4f, sell_price: %.4f, sell_times: %d' % (
                    ask_price, sell_price, sell_times)
                print(bid_info)
Beispiel #2
0
def on_message(ws, message):
    global buy_price, sell_price, buy_queue, sell_queue, bid_price, ask_price, last_time_sec, latest_deal_price, last_time_account_sec
    message = bytes.decode(inflate(message), 'utf-8')  # data decompress
    if 'pong' in message or 'addChannel' in message:
        return
    jmessage = json.loads(message)
    each_message = jmessage[0]
    channel = each_message['channel']
    now_time_sec = float(time.time())
    if channel == 'ok_sub_spot_%s_usdt_depth_5' % coin_name:
        data = each_message['data']
        asks = data['asks'][::-1]
        bids = data['bids']
        ask_price = float(asks[0][0])
        bid_price = float(bids[0][0])
        ask_price_2 = float(asks[1][0])
        bid_price_2 = float(bids[1][0])
        mid_price = (ask_price + bid_price) / 2

    elif channel == ('ok_sub_spot_%s_usdt_deals' % coin_name):

        jdata = each_message['data'][0]
        latest_deal_price = float(jdata[1])
        mid_price = (ask_price + bid_price) / 2
        deal_entity = DealEntity(jdata[0], float(jdata[1]),
                                 round(float(jdata[2]), 3), now_time_sec,
                                 jdata[4])
        handle_deque(deque_3s, deal_entity, now_time_sec, ind_3s)
        avg_3s_price = ind_3s.cal_avg_price()

        price_3s_change = cal_rate(latest_deal_price, avg_3s_price)
        spot_maker.timeLog(
            "最新成交价: %.4f, 中间价: %.4f, 买一价: %.4f, 卖一价: %.4f, 3秒平均价: %.4f, 波动: %.3f%%"
            % (latest_deal_price, mid_price, bid_price, ask_price,
               avg_3s_price, price_3s_change))
        if price_3s_change > 0.03 or price_3s_change < -0.03:
            return
        elif latest_deal_price >= bid_price * 1.0003 and ask_price >= latest_deal_price * 1.0003:
            buy_price = bid_price + 0.0001
            sell_price = ask_price - 0.0001
        elif ask_price > bid_price * 1.0006:
            buy_price = bid_price + 0.0001
            sell_price = ask_price - 0.0001
        else:
            # 不操作
            return
        try:
            buy_order_id = spot_buy(spotAPI, instrument_id, amount, buy_price)
        except Exception as e:
            buy_order_id = False
            traceback.print_exc()
        try:
            sell_order_id = spot_sell(spotAPI, instrument_id, amount,
                                      sell_price)
        except Exception as e:
            sell_order_id = False
            traceback.print_exc()
def on_message(ws, message):
    global buy_price, sell_price, buy_queue, sell_queue, bid_price, ask_price, last_time_sec,latest_deal_price,last_time_account_sec
    message = bytes.decode(inflate(message), 'utf-8')  # data decompress
    if 'pong' in message or 'addChannel' in message:
        return
    jmessage = json.loads(message)
    each_message = jmessage[0]
    channel = each_message['channel']
    now_time_sec = float(time.time())
    if channel == 'ok_sub_spot_%s_usdt_depth_5' % coin_name:
        data = each_message['data']
        asks = data['asks'][::-1]
        bids = data['bids']
        ask_price = float(asks[0][0])
        bid_price = float(bids[0][0])
        ask_price_2 = float(asks[1][0])
        bid_price_2 = float(bids[1][0])
        mid_price = (ask_price + bid_price) / 2
        if now_time_sec > last_time_sec + 0.2:
            last_time_sec = now_time_sec
            print('撤单前sell_queue length: ', len(sell_queue))
            new_queue = []
            for order in sell_queue:
                is_revoke_suc = spot_maker.revoke_order(order.order_id, now_time_sec)
                if is_revoke_suc:
                    if latest_deal_price >= ask_price * 0.9999:
                        # 上涨行情, 追加买单
                        buy_price = mid_price
                        sell_price = max(buy_price * 1.001, ask_price_2)
                        buy_order_id = spot_maker.take_buy_order(2 * amount, buy_price)
                        if buy_order_id:
                            new_buy_order = Order(buy_order_id, buy_price, amount, 'buy', now_time_sec)
                            buy_queue.append(new_buy_order)
                            spot_maker.timeLog("挂买入单成功,时间:%s, 价格: %.4f, order_id: %s" % (
                                timestamp2string(now_time_sec), buy_price, buy_order_id))
                        continue
                    else:
                        sell_price = ask_price - 0.0001
                    if ask_price > bid_price * 1.0006:
                        sell_order_id = spot_maker.take_sell_order(amount, sell_price)
                        if sell_order_id:
                            new_sell_order = Order(sell_order_id, sell_price, amount, 'sell',
                                                   timestamp2string(now_time_sec))
                            new_queue.append(new_sell_order)
                            spot_maker.timeLog("挂卖出单成功,时间:%s, 价格: %.4f, order_id: %s" % (
                                timestamp2string(now_time_sec), sell_price, sell_order_id))

                else:
                    # 撤单失败,retry
                    spot_maker.timeLog('%s撤单失败,重试')
                    spot_maker.revoke_order(order.order_id, now_time_sec)

            sell_queue = copy.deepcopy(new_queue)
            new_queue = []
            print('撤单后sell_queue length: ', len(sell_queue))
            print('撤单前buy_queue length: ', len(buy_queue))
            for order in buy_queue:
                if spot_maker.revoke_order(order.order_id, now_time_sec):
                    if latest_deal_price <= bid_price * 1.0001:
                        # 下跌行情, 追加卖单
                        sell_price = mid_price
                        buy_price = min(sell_price * 0.999, bid_price_2)
                        sell_order_id = spot_maker.take_sell_order(amount, sell_price)
                        if sell_order_id:
                            new_sell_order = Order(sell_order_id, sell_price, 2 * amount, 'sell',
                                                   timestamp2string(now_time_sec))
                            sell_queue.append(new_sell_order)
                            spot_maker.timeLog("挂卖出单成功,时间:%s, 价格: %.4f, order_id: %s" % (
                                timestamp2string(now_time_sec), sell_price, sell_order_id))
                        continue
                    else:
                        buy_price = bid_price + 0.0001
                    if ask_price > bid_price * 1.0006:
                        buy_order_id = spot_maker.take_buy_order(amount, buy_price)
                        if buy_order_id:
                            new_buy_order = Order(buy_order_id, buy_price, amount, 'buy', now_time_sec)
                            new_queue.append(new_buy_order)
                            spot_maker.timeLog("挂买入单成功,时间:%s, 价格: %.4f, order_id: %s" % (
                                timestamp2string(now_time_sec), buy_price, buy_order_id))

            buy_queue = copy.deepcopy(new_queue)
            print('撤单后buy_queue length: ', len(buy_queue))

    elif channel == ('ok_sub_spot_%s_usdt_deals' % coin_name):


        jdata = each_message['data'][0]
        latest_deal_price = float(jdata[1])
        mid_price = (ask_price + bid_price) / 2
        deal_entity = DealEntity(jdata[0], float(jdata[1]), round(float(jdata[2]), 3), now_time_sec, jdata[4])
        handle_deque(deque_3s, deal_entity, now_time_sec, ind_3s)
        avg_3s_price = ind_3s.cal_avg_price()
        if now_time_sec > last_time_account_sec + 60:
            last_time_account_sec = now_time_sec
            spot_maker.get_account_money(coin_name)
            spot_maker.delete_overdue_order(latest_deal_price)
        price_3s_change = cal_rate(latest_deal_price, avg_3s_price)
        spot_maker.timeLog("最新成交价: %.4f, 中间价: %.4f, 买一价: %.4f, 卖一价: %.4f, 3秒平均价: %.4f, 波动: %.3f%%"
                           % (latest_deal_price, mid_price, bid_price, ask_price, avg_3s_price, price_3s_change))
        if price_3s_change > 0.03 or price_3s_change < -0.03:
            return
        elif latest_deal_price > bid_price * 1.0002 and ask_price > latest_deal_price * 1.0002 \
                and buy_price != bid_price + 0.0001 and sell_price != ask_price - 0.0001:
            buy_price = bid_price + 0.0001
            sell_price = ask_price - 0.0001
        elif ask_price > bid_price * 1.0006 and buy_price != bid_price + 0.0001 and sell_price != ask_price - 0.0001:
            if latest_deal_price < mid_price:
                buy_price = bid_price + 0.0001
                sell_price = buy_price * 1.0005
            else:
                sell_price = ask_price - 0.0001
                buy_price = ask_price * 0.9995
        else:
            # 不操作
            return
        try:
            buy_order_id = spot_buy(spotAPI, instrument_id, amount, buy_price)
        except Exception as e:
            buy_order_id = False
            traceback.print_exc()
        try:
            sell_order_id = spot_sell(spotAPI, instrument_id, amount, sell_price)
        except Exception as e:
            sell_order_id = False
            traceback.print_exc()
        if buy_order_id:
            buy_order = Order(buy_order_id, buy_price, amount, 'buy', timestamp2string(now_time_sec))
            buy_queue.append(buy_order)
            spot_maker.timeLog("挂买入单成功,时间:%s, 价格: %.4f, order_id: %s" % (
            timestamp2string(time.time()), buy_price, buy_order_id))
        if sell_order_id:
            sell_order = Order(sell_order_id, sell_price, amount, 'sell', timestamp2string(now_time_sec))
            sell_queue.append(sell_order)
            spot_maker.timeLog("挂卖出单成功,时间:%s, 价格: %.4f, order_id: %s" % (
            timestamp2string(time.time()), sell_price, sell_order_id))