if len(order_id_queue) > 0:
                print("order id list: ", order_id_queue)
            for i in range(len(order_id_queue)):
                old_order_id = order_id_queue[i]
                order_info = get_order_info(okFuture, coin.name, time_type, old_order_id)
                # 完全成交的订单或已经撤单的订单
                status = order_info['status']
                order_type = int(order_info['type'])
                ret = okFuture.future_ticker(coin.name + "_usd", time_type)['ticker']
                buy_price = ret['buy'] + 0.001
                sell_price = ret['sell'] - 0.001
                if status == 2:
                    print("order %s 已完全成交" % str(old_order_id))
                    del_list.append(old_order_id)
                    if order_type == 3:
                        order_id = buyin_less_price(okFuture, coin.name, time_type, sell_price)
                        if order_id:
                            order_id_queue.append(order_id)
                            print("挂单开空, order_id: %s, 价格: %.3f" % (str(order_id), sell_price))
                            less = 1
                    elif order_type == 4:
                        order_id = buyin_more_price(okFuture, coin.name, time_type, buy_price)
                        if order_id:
                            order_id_queue.append(order_id)
                            print("挂单开多, order_id: %s, 价格: %.3f" % (str(order_id), buy_price))
                            more = 1

                elif status == -1:
                    del_list.append(old_order_id)
                elif int(ts) > int(order_info['create_date'] / 1000) + 10:
                    okFuture.future_cancel(coin.name + "_usd", time_type, old_order_id)
Exemplo n.º 2
0
def on_message(ws, message):
    message = bytes.decode(inflate(message), 'utf-8')  # data decompress
    if 'pong' in message or 'addChannel' in message:
        return
    global latest_price, last_avg_price, buy_price, more, less, deque_3s, deque_10s, deque_min, \
        deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, write_lines, last_5min_macd, last_5min_macd_ts
    jmessage = json.loads(message)

    ts = time.time()
    now_time = timestamp2string(ts)
    if int(ts) - int(last_5min_macd_ts) >= 300:
        last_5min_macd_ts = int(ts)
        print(last_5min_macd_ts)
        if more == 0 and less == 0:
            ret = json.loads(
                okFuture.future_position_4fix("etc_usd", "quarter", "1"))
            print(ret)
            if len(ret["holding"]) > 0:
                buy_available = ret["holding"][0]["buy_available"]
                sell_available = ret["holding"][0]["sell_available"]
                if buy_available > 0:
                    thread.start_new_thread(ensure_sell_more, (
                        coin.name,
                        time_type,
                    ))
                if sell_available > 0:
                    thread.start_new_thread(ensure_sell_less, (
                        coin.name,
                        time_type,
                    ))
            else:
                print("确认未持仓")

    for each_message in jmessage:
        for jdata in each_message['data']:
            latest_price = float(jdata[1])
            deal_entity = DealEntity(jdata[0], float(jdata[1]),
                                     round(float(jdata[2]), 3), ts, jdata[4])

            handle_deque(deque_3s, deal_entity, ts, ind_3s)
            handle_deque(deque_10s, deal_entity, ts, ind_10s)
            handle_deque(deque_min, deal_entity, ts, ind_1min)
            handle_deque(deque_5m, deal_entity, ts, ind_5m)

            avg_3s_price = ind_3s.cal_avg_price()
            avg_10s_price = ind_10s.cal_avg_price()
            avg_min_price = ind_1min.cal_avg_price()
            avg_5m_price = ind_5m.cal_avg_price()
            price_10s_change = cal_rate(avg_3s_price, avg_10s_price)
            price_1m_change = cal_rate(avg_3s_price, avg_min_price)
            price_5m_change = cal_rate(avg_3s_price, avg_5m_price)

            if more == 1:
                if price_10s_change < 0:
                    cancel_uncompleted_order(coin.name, time_type)
                    if sell_more_batch(coin.name, time_type, latest_price):
                        more = 0
                        thread.start_new_thread(ensure_sell_more, (
                            coin.name,
                            time_type,
                        ))

            elif less == 1:
                if price_10s_change > 0:
                    cancel_uncompleted_order(coin.name, time_type)
                    if sell_less_batch(coin.name, time_type, latest_price):
                        less = 0
                        thread.start_new_thread(ensure_sell_less, (
                            coin.name,
                            time_type,
                        ))

            elif check_vol():
                if price_10s_change > incr_10s_rate:
                    if ind_3s.bid_vol >= vol_3s_bal * ind_3s.ask_vol and ind_1min.bid_vol >= vol_1m_bal * ind_1min.ask_vol:
                        latest_order_id = buyin_more_price(
                            coin.name, time_type, latest_price)
                        if latest_order_id:
                            more = 1
                            thread.start_new_thread(pend_order, (
                                coin.name,
                                time_type,
                                latest_order_id,
                                'more',
                            ))
                            buy_price = latest_price
                            info = u'发出做多信号!!!买入价格:' + str(
                                buy_price) + u', ' + now_time
                            with codecs.open(file_transaction, 'a+',
                                             'utf-8') as f:
                                f.writelines(info + '\n')

                elif price_10s_change < -incr_10s_rate:
                    if ind_3s.ask_vol >= vol_3s_bal * ind_3s.bid_vol and ind_1min.ask_vol >= vol_1m_bal * ind_1min.bid_vol:
                        latest_order_id = buyin_less_price(
                            coin.name, time_type, latest_price)
                        if latest_order_id:
                            less = 1
                            thread.start_new_thread(pend_order, (
                                coin.name,
                                time_type,
                                latest_order_id,
                                'less',
                            ))
                            buy_price = latest_price
                            info = u'发出做空信号!!!买入价格:' + str(
                                buy_price) + u', ' + now_time
                            with codecs.open(file_transaction, 'a+',
                                             'utf-8') as f:
                                f.writelines(info + '\n')

            price_info = deal_entity.type + u' now_price: %.4f, 3s_price: %.4f, 10s_price: %.4f, 1m_price: %.4f, ' \
                                            u'5min_price: %.4f' \
                         % (latest_price, avg_3s_price, avg_10s_price, avg_min_price, avg_5m_price)
            vol_info = u'cur_vol: %.3f, 3s vol: %.3f, 10s vol: %.3f, 1min vol: %.3f, ask_vol: %.3f, bid_vol: %.3f, 3s_ask_vol: %.3f, 3s_bid_vol: %.3f' \
                       % (deal_entity.amount, ind_3s.vol, ind_10s.vol, ind_1min.vol, ind_1min.ask_vol, ind_1min.bid_vol,
                          ind_3s.ask_vol, ind_3s.bid_vol)
            rate_info = u'10s_rate: %.2f%%, 1min_rate: %.2f%%, 5min_rate: %.2f%%, 5min_macd: %.6f' \
                        % (price_10s_change, price_1m_change, price_5m_change, last_5min_macd)

            print(price_info + '\r\n' + vol_info + '\r\n' + rate_info + u', ' +
                  now_time)
Exemplo n.º 3
0
def on_message(ws, message):
    message = bytes.decode(inflate(message), 'utf-8')  # data decompress
    if 'pong' in message or 'addChannel' in message:
        return
    global latest_price, buy_price, more, less, deque_3s, deque_10s, deque_min, count, \
        deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, write_lines, last_5min_macd, last_5min_macd_ts, prev_5m_price, prev_15m_price, \
        prev_5min_price_less_15min, prev_5min_price_more_15min
    jmessage = json.loads(message)

    ts = time.time()
    now_time = timestamp2string(ts)
    if int(ts) - int(last_5min_macd_ts) >= 60:
        last_5min_macd_ts = int(ts)
        last_5min_macd, tmp_ts = get_macd("etc_usd", time_type, "1min")
        print(last_5min_macd, last_5min_macd_ts)
        if more == 0 and less == 0:
            ret = json.loads(
                okFuture.future_position_4fix("etc_usd", "quarter", "1"))
            print(ret)
            if len(ret["holding"]) > 0:
                buy_available = ret["holding"][0]["buy_available"]
                sell_available = ret["holding"][0]["sell_available"]
                if buy_available > 0:
                    thread.start_new_thread(ensure_sell_more, (
                        coin.name,
                        time_type,
                    ))
                if sell_available > 0:
                    thread.start_new_thread(ensure_sell_less, (
                        coin.name,
                        time_type,
                    ))
            else:
                print("确认未持仓")

    for each_message in jmessage:
        for jdata in each_message['data']:
            latest_price = float(jdata[1])
            deal_entity = DealEntity(jdata[0], float(jdata[1]),
                                     round(float(jdata[2]), 3), ts, jdata[4])

            handle_deque(deque_3s, deal_entity, ts, ind_3s)
            handle_deque(deque_10s, deal_entity, ts, ind_10s)
            handle_deque(deque_min, deal_entity, ts, ind_1min)
            handle_deque(deque_5m, deal_entity, ts, ind_5m)
            handle_deque(deque_15m, deal_entity, ts, ind_15m)

            avg_3s_price = ind_3s.cal_avg_price()
            avg_10s_price = ind_10s.cal_avg_price()
            avg_min_price = ind_1min.cal_avg_price()
            avg_5m_price = ind_5m.cal_avg_price()
            avg_15m_price = ind_15m.cal_avg_price()
            price_10s_change = cal_rate(avg_3s_price, avg_10s_price)
            price_1m_change = cal_rate(avg_3s_price, avg_min_price)
            price_5m_change = cal_rate(avg_3s_price, avg_5m_price)

            if more == 1:
                if avg_5m_price < prev_5m_price:
                    count += 1
                    if count > 3:
                        if sell_more(coin.name, time_type, latest_price):
                            more = 0
                            thread.start_new_thread(ensure_sell_more, (
                                coin.name,
                                time_type,
                            ))

            elif less == 1:
                if avg_5m_price > prev_5m_price:
                    count += 1
                    if count > 3:
                        if sell_less(coin.name, time_type, latest_price):
                            less = 0
                            thread.start_new_thread(ensure_sell_less, (
                                coin.name,
                                time_type,
                            ))

            elif prev_5m_price <= prev_15m_price and avg_5m_price > avg_15m_price:
                if buyin_more_price(coin.name, time_type,
                                    latest_price - 0.002):
                    more = 1
                    count = 0
                    buy_price = latest_price - 0.002
                    info = u'发出做多信号!!!买入价格:' + str(
                        buy_price) + u', ' + now_time
                    with codecs.open(file_transaction, 'a+', 'utf-8') as f:
                        f.writelines(info + '\n')

            elif prev_5m_price >= prev_15m_price and avg_5m_price < avg_15m_price:
                if buyin_less_price(coin.name, time_type,
                                    latest_price + 0.002):
                    less = 1
                    count = 0
                    buy_price = latest_price + 0.002
                    info = u'发出做空信号!!!买入价格:' + str(
                        buy_price) + u', ' + now_time
                    with codecs.open(file_transaction, 'a+', 'utf-8') as f:
                        f.writelines(info + '\n')

            price_info = deal_entity.type + u' now_price: %.4f, 3s_price: %.4f, 10s_price: %.4f, 1m_price: %.4f, ' \
                                            u'5min_price: %.4f' \
                         % (latest_price, avg_3s_price, avg_10s_price, avg_min_price, avg_5m_price)
            vol_info = u'cur_vol: %.3f, 3s vol: %.3f, 10s vol: %.3f, 1min vol: %.3f, ask_vol: %.3f, bid_vol: %.3f, 3s_ask_vol: %.3f, 3s_bid_vol: %.3f' \
                       % (deal_entity.amount, ind_3s.vol, ind_10s.vol, ind_1min.vol, ind_1min.ask_vol, ind_1min.bid_vol,
                          ind_3s.ask_vol, ind_3s.bid_vol)
            rate_info = u'10s_rate: %.2f%%, 1min_rate: %.2f%%, 5min_rate: %.2f%%, 5min_macd: %.6f, 15min_price: %.4f' \
                        % (price_10s_change, price_1m_change, price_5m_change, last_5min_macd, avg_15m_price)

            print(price_info + '\r\n' + vol_info + '\r\n' + rate_info + u', ' +
                  now_time)

            prev_5m_price = avg_5m_price
            prev_15m_price = avg_15m_price