def sell_more_batch(coin_name, time_type, latest_price, lever_rate = 20):
    global processing
    processing = True
    jRet = json.loads(okFuture.future_position_4fix(coin_name+"_usd", time_type, "1"))
    print(jRet)
    flag = True
    ret = u'没有做多订单'
    while len(jRet["holding"]) > 0:
        cancel_uncompleted_order(coin_name, time_type)
        if flag:
            flag = False
            amount = jRet["holding"][0]["buy_available"]
            order_data = gen_orders_data(latest_price, amount, 3, 5)
            ret = okFuture.future_batchTrade(coin_name + "_usd", time_type, order_data, lever_rate)
        else:
            buy_available = jRet["holding"][0]["buy_available"]
            ret = okFuture.future_trade(coin_name + "_usd", time_type, '', buy_available, 3, 1, lever_rate)
        if 'true' in ret:
            time.sleep(2)
            jRet = json.loads(okFuture.future_position_4fix(coin_name + "_usd", time_type, "1"))

    sell_more_suc()
    email_msg = "做多%s批量卖出成交, 时间: %s, 成交结果: %s" \
                % (coin_name, timestamp2string(time.time()), ret)
    thread.start_new_thread(send_email, (email_msg,))
    processing = False
    return True
Exemplo n.º 2
0
def sell_more_batch(coin_name, time_type, latest_price, lever_rate=20):
    global processing, more2less
    processing = True
    jRet = json.loads(
        okFuture.future_position_4fix(coin_name + "_usd", time_type, "1"))
    print(jRet)
    flag = True
    ret = u'没有做多订单'
    while len(jRet["holding"]) > 0:
        cancel_uncompleted_order(coin_name, time_type)
        if flag:
            flag = False
            amount = jRet["holding"][0]["buy_available"]
            order_data = gen_orders_data(latest_price, amount, 3, 5)
            ret = okFuture.future_batchTrade(coin_name + "_usd", time_type,
                                             order_data, lever_rate)
        else:
            buy_available = jRet["holding"][0]["buy_available"]
            ret = okFuture.future_trade(coin_name + "_usd", time_type, '',
                                        buy_available, 3, 1, lever_rate)
        if 'true' in ret:
            time.sleep(5)
            jRet = json.loads(
                okFuture.future_position_4fix(coin_name + "_usd", time_type,
                                              "1"))

    sell_more_suc()
    if more2less == 1:
        if buyin_less(coin_name, time_type, latest_price):
            info = u'发出反手做空信号!!!买入价格:' + str(latest_price)
            with codecs.open(file_transaction, 'a+', 'utf-8') as f:
                f.writelines(info + '\n')
        else:
            more2less = 0
    email_msg = "做多%s批量卖出成交, 时间: %s, 成交结果: %s" \
                % (coin_name, timestamp2string(time.time()), ret)
    thread.start_new_thread(send_email, (email_msg, ))
    processing = False
    return True
def on_message(ws, message):
    if 'pong' in message or 'addChannel' in message:
        return
    global more, less, buy_price, write_lines
    jmessage = json.loads(message)
    coin_name = jmessage[0]['channel'].split('_')[3]
    data = jmessage[0]['data']
    index = float(data['futureIndex'])
    timestamp = int(data['timestamp'])
    now_time = timestamp2string(timestamp)
    index_entity = IndexEntity(coin_name, index, timestamp)
    coin_info = "coin: %s, index: %.2f, time: %s" % (coin_name, index, now_time)

    if coin_name == 'btc':
        handle_deque(btc_deque_1s, index_entity, btc_ind_1s)
        handle_deque(btc_deque_1min, index_entity, btc_ind_1min)
        handle_deque(btc_deque_5min, index_entity, btc_ind_5min)
    elif coin_name == 'eth':
        handle_deque(eth_deque_1s, index_entity, eth_ind_1s)
        handle_deque(eth_deque_1min, index_entity, eth_ind_1min)
        handle_deque(eth_deque_5min, index_entity, eth_ind_5min)
    elif coin_name == 'ltc':
        handle_deque(ltc_deque_1s, index_entity, ltc_ind_1s)
        handle_deque(ltc_deque_1min, index_entity, ltc_ind_1min)
        handle_deque(ltc_deque_5min, index_entity, ltc_ind_5min)

    btc_avg_1s_price = btc_ind_1s.cal_avg_price()
    btc_avg_min_price = btc_ind_1min.cal_avg_price()
    btc_avg_5m_price = btc_ind_5min.cal_avg_price()

    eth_avg_1s_price = eth_ind_1s.cal_avg_price()
    eth_avg_min_price = eth_ind_1min.cal_avg_price()
    eth_avg_5m_price = eth_ind_5min.cal_avg_price()

    ltc_avg_1s_price = ltc_ind_1s.cal_avg_price()
    ltc_avg_min_price = ltc_ind_1min.cal_avg_price()
    ltc_avg_5m_price = ltc_ind_5min.cal_avg_price()

    btc_1m_change = cal_rate(btc_avg_1s_price, btc_avg_min_price)
    btc_5m_change = cal_rate(btc_avg_1s_price, btc_avg_5m_price)

    eth_1m_change = cal_rate(eth_avg_1s_price, eth_avg_min_price)
    eth_5m_change = cal_rate(eth_avg_1s_price, eth_avg_5m_price)

    ltc_1m_change = cal_rate(ltc_avg_1s_price, ltc_avg_min_price)
    ltc_5m_change = cal_rate(ltc_avg_1s_price, ltc_avg_5m_price)

    weighted_1min_rate = btc_1m_change * btc_weight + eth_1m_change * eth_weight + ltc_1m_change * ltc_weight
    weighted_5min_rate = btc_5m_change * btc_weight + eth_5m_change * eth_weight + ltc_5m_change * ltc_weight

    if more == 1 and not processing:
        if weighted_5min_rate <= 0:
            thread.start_new_thread(sell_more_batch, (coin.name, time_type, latest_price,))
    elif less == 1 and not processing:
        if weighted_5min_rate >= 0:
            thread.start_new_thread(sell_less_batch, (coin.name, time_type, latest_price,))

    elif weighted_1min_rate >= incr_1m_rate and weighted_5min_rate >= incr_5m_rate \
            and btc_5m_change >= incr_1m_rate and eth_5m_change >= incr_1m_rate and ltc_5m_change >= incr_1m_rate:
        if buyin_more(coin.name, time_type, latest_price):
            more = 1
            thread.start_new_thread(ensure_buyin_more, (coin.name, time_type, latest_price,))
            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 weighted_1min_rate <= -incr_1m_rate and weighted_5min_rate <= -incr_5m_rate \
            and btc_5m_change < -incr_1m_rate and eth_5m_change < -incr_1m_rate and ltc_5m_change < -incr_1m_rate:
        if buyin_less(coin.name, time_type, latest_price):
            less = 1
            thread.start_new_thread(ensure_buyin_less, (coin.name, time_type, latest_price,))
            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')

    btc_rate_info = u'btc      1min_rate: %.2f%%, 5min_rate: %.2f%%' % (btc_1m_change, btc_5m_change)

    eth_rate_info = u'eth      1min_rate: %.2f%%, 5min_rate: %.2f%%' % (eth_1m_change, eth_5m_change)

    ltc_rate_info = u'ltc      1min_rate: %.2f%%, 5min_rate: %.2f%%' % (ltc_1m_change, ltc_5m_change)

    weighted_rate_info = u'weighted 1min_rate: %.3f%%, 5min_rate: %.3f%%' % (weighted_1min_rate, weighted_5min_rate)

    print(coin_info)
    print(btc_rate_info)
    print(eth_rate_info)
    print(ltc_rate_info)
    print(weighted_rate_info)
    write_info = coin_info + '\n' + btc_rate_info + '\n' + eth_rate_info + '\n' + ltc_rate_info + '\n' \
                 + weighted_rate_info + '\r\n'
    write_lines.append(write_info)
    if len(write_lines) >= 10:
        with codecs.open(file_deal, 'a+', 'UTF-8') as f:
            f.writelines(write_lines)
            write_lines = []
Exemplo n.º 4
0
def on_message(ws, message):
    if 'pong' in message or 'addChannel' in message:
        return
    global latest_price, last_avg_price, buy_price, last_last_price, more, less, deque_3s, deque_10s, deque_min, \
        deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, write_lines
    jmessage = json.loads(message)
    ts = time.time()
    now_time = timestamp2string(ts)
    jdata = jmessage[0]['data'][0]
    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 avg_10s_price <= 1.001 * avg_5m_price:
            # 按买一价出售
            if sell_more(coin.name, time_type):
                info = u'发出卖出信号!!!卖出价格:' + str(latest_price) + u', 收益: ' + str(latest_price - buy_price) \
                       + ', ' + now_time
                with codecs.open(file_transaction, 'a+', 'utf-8') as f:
                    f.writelines(info + '\n')
                more = 0
    elif less == 1:
        if avg_10s_price >= 0.999 * avg_5m_price:
            if sell_less(coin.name, time_type):
                info = u'发出卖出信号!!!卖出价格:' + str(latest_price) + u', 收益: ' + str(buy_price - latest_price) \
                       + ', ' + now_time
                with codecs.open(file_transaction, 'a+', 'utf-8') as f:
                    f.writelines(info + '\n')
                less = 0
    elif check_vol():
        if latest_price > avg_3s_price > avg_10s_price > last_avg_price > last_last_price \
                and price_5m_change >= incr_5m_rate and price_1m_change >= incr_1m_rate and price_10s_change >= 0.1\
                and ind_1min.bid_vol > float(2 * ind_1min.ask_vol) and ind_3s.bid_vol > float(2 * ind_3s.ask_vol):
            if buyin_more_batch(coin.name, time_type, latest_price):
                more = 1
                thread.start_new_thread(ensure_buyin_more, (
                    coin.name,
                    time_type,
                    latest_price,
                ))
                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 latest_price < avg_3s_price < avg_10s_price < last_avg_price < last_last_price \
                and price_5m_change <= -1 * incr_5m_rate and price_1m_change <= -1 * incr_1m_rate and price_10s_change <= -0.1 \
                and ind_1min.ask_vol > float(2 * ind_1min.bid_vol) and ind_3s.ask_vol > float(2 * ind_3s.bid_vol):
            if buyin_less_batch(coin.name, time_type, latest_price):
                less = 1
                thread.start_new_thread(ensure_buyin_less, (
                    coin.name,
                    time_type,
                    latest_price,
                ))
                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')

    if last_avg_price != avg_10s_price:
        last_last_price = last_avg_price
        last_avg_price = avg_10s_price

    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%%' \
                % (price_10s_change, price_1m_change, price_5m_change)
    write_info = price_info + u', ' + vol_info + u', ' + rate_info + u', ' + now_time + '\r\n'
    write_lines.append(write_info)
    if len(write_lines) >= 100:
        with codecs.open(file_deal, 'a+', 'UTF-8') as f:
            f.writelines(write_lines)
            write_lines = []

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