def ensure_sell_less(coin_name, time_type, lever_rate=20):
    sleep_time = 30
    while sleep_time > 0:
        time.sleep(sleep_time)
        jRet = json.loads(
            okFuture.future_position_4fix(coin_name + "_usd", time_type, "1"))
        if len(jRet["holding"]) > 0:
            cancel_uncompleted_order(okFuture, coin_name, time_type)
            time.sleep(1)
            jRet = json.loads(
                okFuture.future_position_4fix(coin_name + "_usd", time_type,
                                              "1"))
            sell_available = jRet["holding"][0]["sell_available"]
            ret = okFuture.future_trade(coin_name + "_usd", time_type, '',
                                        sell_available, 4, 1, lever_rate)
            with codecs.open(file_transaction, 'a+', 'utf-8') as f:
                f.writelines(ret + '\n')
        else:
            break
    ts = time.time()
    now_time = timestamp2string(ts)
    info = u'做空卖出成功!!!卖出价格:' + str(latest_price) + u', 收益: ' + str(buy_price - latest_price) \
           + ', ' + now_time
    thread.start_new_thread(send_email, (info, ))
    with codecs.open(file_transaction, 'a+', 'utf-8') as f:
        f.writelines(info + '\n')
def sell_less_batch(coin_name, time_type, latest_price, lever_rate=20):
    jRet = json.loads(
        okFuture.future_position_4fix(coin_name + "_usd", time_type, "1"))
    ret = u'没有做空订单'
    while len(jRet["holding"]) > 0:
        amount = jRet["holding"][0]["sell_available"]
        order_data = gen_orders_data(latest_price, amount, 4, 5)
        ret = okFuture.future_batchTrade(coin_name + "_usd", time_type,
                                         order_data, lever_rate)
        if 'true' in ret:
            break
        else:
            sell_available = jRet["holding"][0]["sell_available"]
            ret = okFuture.future_trade(coin_name + "_usd", time_type, '',
                                        sell_available, 4, 1, lever_rate)
            if 'true' in ret:
                break
            else:
                return False

    return True