示例#1
0
def cancel_uncompleted_order(coin_name, time_type):
    ret = okFuture.future_orderinfo(coin_name + "_usd", time_type, -1, 1, None,
                                    None)
    order_id_list = []
    for each_order in json.loads(ret)["orders"]:
        order_id_list.append(str(each_order['order_id']))
    if len(order_id_list) > 0:
        order_id = ",".join(order_id_list)
        ret = okFuture.future_cancel(coin_name + "_usd", time_type, order_id)
        print(ret)
        if 'true' in ret:
            email_msg = "撤单%s成功, 时间: %s, 成交结果: %s" \
                        % (coin_name, timestamp2string(time.time()), ret)
            thread.start_new_thread(send_email, (email_msg, ))
            return True
        else:
            email_msg = "撤单%s失败, 时间: %s, 失败详情: %s" \
                        % (coin_name, timestamp2string(time.time()), ret)
            thread.start_new_thread(send_email, (email_msg, ))
            return cancel_uncompleted_order(coin_name, time_type)
    return False
示例#2
0
def buyin_more(coin_name, time_type, latest_price, lever_rate=20):
    json_ret = json.loads(okFuture.future_userinfo_4fix())
    balance = float(json_ret["info"][coin_name]["balance"])
    amount = math.floor(balance * lever_rate * latest_price / 10)
    while amount > 0:
        amount = math.floor(amount * 0.95)
        ret = okFuture.future_trade(coin_name + "_usd", time_type, None,
                                    amount, 1, 1, lever_rate)
        print(ret)
        if 'true' in ret:
            email_msg = "做多%s成功,最新价格: %.4f, 成交张数: %d, 时间: %s, 成交结果: %s" \
                        % (coin_name, latest_price, amount, timestamp2string(time.time()), ret)
            thread.start_new_thread(send_email, (email_msg, ))
            return True
    return False
示例#3
0
def sell_less(coin_name, time_type, leverRate=20):
    cancel_uncompleted_order(coin_name, time_type)
    jRet = json.loads(
        okFuture.future_position_4fix(coin_name + "_usd", time_type, "1"))

    while len(jRet["holding"]) > 0:
        sell_available = jRet["holding"][0]["sell_available"]
        ret = okFuture.future_trade(coin_name + "_usd", time_type, '',
                                    sell_available, 4, 1, leverRate)
        print(ret)
        if 'true' in ret:
            email_msg = "卖出做空%s成功, 时间: %s, 成交结果: %s" \
                        % (coin_name, timestamp2string(time.time()), ret)
            thread.start_new_thread(send_email, (email_msg, ))
            return True
    return True
示例#4
0
def buyin_more_batch(coin_name,
                     time_type,
                     latest_price,
                     lever_rate=20,
                     amount=None):
    json_ret = json.loads(okFuture.future_userinfo_4fix())
    balance = float(json_ret["info"][coin_name]["balance"])
    if amount is None:
        amount = math.floor(balance * lever_rate * latest_price / 10)
    while amount >= 5:
        order_data = gen_orders_data(latest_price, amount, 1, 5)
        ret = okFuture.future_batchTrade(coin_name + "_usd", time_type,
                                         order_data, lever_rate)
        if 'true' in ret:
            email_msg = "批量下单做多%s成功,最新价格: %.4f, 成交张数: %d, 时间: %s, 成交结果: %s" \
                        % (coin_name, latest_price, amount, timestamp2string(time.time()), ret)
            thread.start_new_thread(send_email, (email_msg, ))
            return True
        amount *= 0.95
    return False
示例#5
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)