Example #1
0
def balance_soc_wicc():
    print "balance_soc_wicc方法调用了-----------进行协整策略之前要先进行币的分配"
    # 获取当前账户两个币种的数量
    get_wicc_soc_count()
    # 获取当前价格
    soc_buy_price, soc_sell_price = get_huobi_price("socusdt", False)
    wicc_buy_price, wicc_sell_price = get_huobi_price("wiccusdt", False)

    if soc_buy_price and soc_sell_price and wicc_buy_price and wicc_buy_price:
        # 4个价格不为0, 说明网络获取成功

        # soc价格更高的时候, 找soc的买盘价格(卖soc) - wicc的卖盘价格(买wicc) / wicc的卖盘价格
        soc_to_wicc_minus = calc_by_soc(soc_buy_price) - wicc_sell_price
        soc_to_wicc_percent = common_utils.get_round((soc_to_wicc_minus * 100 / wicc_sell_price), 4)

        # wicc价格更高的时候, 找wicc的买盘价格(卖wicc) - soc的卖盘价格(买soc) / soc的卖盘价格
        wicc_to_soc_minus = wicc_buy_price - calc_by_soc(soc_sell_price)
        wicc_to_soc_percent = common_utils.get_round((wicc_to_soc_minus * 100 / calc_by_soc(soc_sell_price)), 4)

        if calc_by_soc(soc_buy_price) >= wicc_sell_price:
            print "soc价格贵时候的调配"
            # 结束 soc 比 wicc 贵的协整逻辑
            return

        if wicc_buy_price >= calc_by_soc(soc_sell_price):
            print "wicc价格贵时候的调配"
            # 结束 wicc 比 soc 贵的协整的逻辑
            return
    else:
        print "在协整方法中, 获取soc和wicc的价格中有0, 协整失败, 停止程序"
        soc_wicc_helper.exit()
Example #2
0
def insert_soc_wicc(create_time, buy_soc_price, sell_soc_price, buy_wicc_price, sell_wicc_price, soc_to_wicc_minus,
                    wicc_to_soc_minus, soc_to_wicc_percent, wicc_to_soc_percent):
    conn = connect_db()
    c = conn.cursor()

    soc_to_wicc_percent = common_utils.get_round(soc_to_wicc_percent, 3)
    wicc_to_soc_percent = common_utils.get_round(wicc_to_soc_percent, 3)
    values = " VALUES ('%s', %f, %f,%f, %f, %f,%f, %.3f, %.3f)" % (
        str(create_time), buy_soc_price, sell_soc_price, buy_wicc_price, sell_wicc_price, soc_to_wicc_minus,
        wicc_to_soc_minus, soc_to_wicc_percent, wicc_to_soc_percent)
    sql = "INSERT INTO soc_wicc (create_time, buy_soc_price, sell_soc_price, buy_wicc_price, sell_wicc_price," \
          " soc_to_wicc_minus,wicc_to_soc_minus, soc_to_wicc_percent,wicc_to_soc_percent )" + values
    try:
        c.execute(sql)
    except Exception, e:
        print e.message
Example #3
0
def create_config():
    global g_K
    global g_N
    params = common_utils.read_json("params.json")

    g_K = params['g_K']
    g_N = params['g_N']

    config = {'is_debug': True, 'soc': [], 'wicc': []}
    point_size = params['point_size']
    max_index = params['max_index']
    one_deviation, two_deviation = get_deviation(point_size, max_index)
    for i in xrange(1, point_size + 1):
        if i <= max_index:
            point = i * one_deviation
        else:
            # 9 - 5 + 1 = 5      6 - 5 + 1
            point = (point_size - max_index + 1 -
                     (i - max_index + 1) + 1) * two_deviation
        point = common_utils.get_round(point, 4)
        soc_obj = {'percent': point, 'state': 0, 'trade_count': 0}
        wicc_obj = {'percent': point, 'state': 0, 'trade_count': 0}
        config['soc'].append(soc_obj)
        config['wicc'].append(wicc_obj)
    json_count = {"soc_count": 0, "wicc_count": 1000}
    # json_count = {"soc_count": 100, "wicc_count": 0}

    if json_count["wicc_count"] == 0:
        # 单边开局(一开局只有soc), 记录wicc转换的数据
        for i in xrange(len(config['wicc'])):
            trade_count = json_count['soc_count'] / 200 * config['wicc'][i][
                'percent']
            config['wicc'][i]['trade_count'] = common_utils.get_round(
                trade_count, 2)
            config['wicc'][i]['state'] = 1
    elif json_count['soc_count'] == 0:
        # 单边开局(一开局只有wicc), 记录soc转换的数据
        for i in xrange(len(config['soc'])):
            trade_count = json_count['wicc_count'] / 200 * config['soc'][i][
                'percent']
            config['soc'][i]['trade_count'] = common_utils.get_round(
                trade_count, 2)
            config['soc'][i]['state'] = 1

    common_utils.write_json("config.json", config, map)
    common_utils.write_json("debug_count.json", json_count, map)
Example #4
0
def get_coin_balance(coin):
    balance = HuobiService.get_balance()
    result = 0
    for e in balance["data"]["list"]:
        if e["type"] == "trade" and e["currency"] == coin:
            # result = float(int(float(e["balance"]) * 10000)) / 10000
            result = common_utils.get_round(float(e["balance"]), 4)
        if result:
            # 获取到想要的值, 跳出循环
            break
    return result
Example #5
0
def get_coin_balance(coin):
    if not is_debug:
        # 正式请求
        balance = HuobiService.get_balance()
        if balance['status'] == 'ok':
            result = 0
            for e in balance["data"]["list"]:
                if e["type"] == "trade" and e["currency"] == coin:
                    # result = float(int(float(e["balance"]) * 10000)) / 10000
                    result = common_utils.get_round(float(e["balance"]), 2)
                if result:
                    # 获取到想要的值, 跳出循环
                    break
            return result
        else:
            time.sleep(2)
            # 第一次请求失败, 再试一次
            balance = HuobiService.get_balance()
            if balance['status'] == 'ok':
                result = 0
                for e in balance["data"]["list"]:
                    if e["type"] == "trade" and e["currency"] == coin:
                        # result = float(int(float(e["balance"]) * 10000)) / 10000
                        result = common_utils.get_round(float(e["balance"]), 2)
                    if result:
                        # 获取到想要的值, 跳出循环
                        break
                return result
            else:
                # 第二次还失败, 这里停掉程序
                return -100
    else:
        # 测试模式, 设置 回测的数据
        json_count = common_utils.read_json("debug_count.json")
        if "soc" == coin:
            return json_count['soc_count']
        if "wicc" == coin:
            return json_count['wicc_count']
        return -100
Example #6
0
def sell_wicc_and_buy_soc(sell_nums):
    if not is_debug:
        print "正式环境启动sell_wicc_and_buy_soc, 卖wicc买soc"
        # 正式环境
        sell_wicc_result = sell_wicc(sell_nums)
        print "sell_wicc_result = %s" % (str(sell_wicc_result))
        if sell_wicc_result:
            buy_soc_result = buy_soc()
            print "buy_soc_result = %s" % (str(buy_soc_result))
            if buy_soc_result:
                time.sleep(1)
                # 获取成交了的数量
                count = get_buy_count(buy_soc_result)
                if count != -1:
                    return count
                else:
                    exit()
                    return 0
            else:
                #  卖wicc成功, 买soc却失败了, 再试一次
                two_result = buy_soc()
                if two_result:
                    time.sleep(1)
                    # 获取成交了的数量
                    count = get_buy_count(two_result)
                    if count != -1:
                        return count
                    else:
                        exit()
                        return 0
                else:
                    # 卖了却没买成功, 程序出问题了, 退出
                    exit()
                    return 0
    else:
        # 测试环境进行回测
        print "测试环境进行回测sell_wicc_and_buy_soc, 卖wicc买soc"
        soc_buy_price, soc_sell_price, wicc_buy_price, wicc_sell_price = robot_dao.get_debug_soc_wicc_data(
            soc_skip - 1, True)
        count = wicc_buy_price * sell_nums * FEE_LEFT_PERCENT / soc_sell_price * FEE_LEFT_PERCENT
        count = common_utils.get_round(count, 4)
        return count
Example #7
0
def get_soc_wicc_data():
    global g_soc_count
    global g_wicc_count
    global g_max_has_soc_count, g_max_trans_soc_count, g_max_has_wicc_count, g_max_trans_wicc_count

    soc_buy_price, soc_sell_price = get_huobi_price("socusdt", False)
    wicc_buy_price, wicc_sell_price = get_huobi_price("wiccusdt", False)
    msg = "%s 2.0的 get_soc_wicc_data启动了  --------  " \
          "\n soc_buy_price = %s" \
          "; soc_sell_price = %s" \
          "; wicc_buy_price = %s" \
          "; wicc_sell_price = %s" \
          % (get_format_time(), str(soc_buy_price), str(soc_sell_price), str(wicc_buy_price), str(wicc_sell_price))

    if soc_buy_price and soc_sell_price and wicc_buy_price and wicc_buy_price:
        # 4个价格不为0, 说明网络获取成功

        # soc价格更高的时候, 找soc的买盘价格(卖soc) - wicc的卖盘价格(买wicc) / wicc的卖盘价格
        soc_to_wicc_minus = calc_by_soc(soc_buy_price) - wicc_sell_price
        soc_to_wicc_percent = common_utils.get_round((soc_to_wicc_minus * 100 / wicc_sell_price), 4)

        # wicc价格更高的时候, 找wicc的买盘价格(卖wicc) - soc的卖盘价格(买soc) / soc的卖盘价格
        wicc_to_soc_minus = wicc_buy_price - calc_by_soc(soc_sell_price)
        wicc_to_soc_percent = common_utils.get_round((wicc_to_soc_minus * 100 / calc_by_soc(soc_sell_price)), 4)

        # 计算一下全部转化为soc会有多少
        all_trans_soc_count = wicc_buy_price * g_wicc_count * 0.998 / soc_sell_price * 0.998 + g_soc_count
        # 计算一下全部转化为wicc会有多少
        all_trans_wicc_count = soc_buy_price * g_soc_count * 0.998 / wicc_sell_price * 0.998 + g_wicc_count

        g_max_has_soc_count = max(g_max_has_soc_count, g_soc_count)
        g_max_trans_soc_count = max(g_max_trans_soc_count, all_trans_soc_count)

        g_max_has_wicc_count = max(g_max_has_wicc_count, g_wicc_count)
        g_max_trans_wicc_count = max(g_max_trans_wicc_count, all_trans_wicc_count)

        per_msg = "\n soc_to_wicc_minus = %s" \
                  "; wicc_to_soc_minus = %s" \
                  "; soc_to_wicc_percent = %s" \
                  "; wicc_to_soc_percent = %s" \
                  "\n all_trans_soc_count = %s" \
                  "; all_trans_wicc_count = %s" \
                  "; soc_count = %s" \
                  "; wicc_count = %s" % (
                      str(soc_to_wicc_minus),
                      str(wicc_to_soc_minus),
                      str(soc_to_wicc_percent),
                      str(wicc_to_soc_percent),
                      str(all_trans_soc_count),
                      str(all_trans_wicc_count),
                      str(g_soc_count),
                      str(g_wicc_count))

        msg = msg + per_msg
        if g_config['is_debug']:
            per_msg = "\n max_has_soc_count = %s" \
                      "; max_trans_soc_count = %s" \
                      "; max_has_wicc_count = %s" \
                      "; max_trans_wicc_count = %s" % (
                          str(g_max_has_soc_count),
                          str(g_max_trans_soc_count),
                          str(g_max_has_wicc_count),
                          str(g_max_trans_wicc_count))
            msg = msg + per_msg

        print msg

        if not g_config['is_debug']:
            # 正式交易的时候记录每次获取的数据
            robot_dao.insert_soc_wicc(get_format_time(), float(soc_buy_price), float(soc_sell_price),
                                      float(wicc_buy_price), float(wicc_sell_price), float(soc_to_wicc_minus),
                                      float(wicc_to_soc_minus), float(soc_to_wicc_percent), float(wicc_to_soc_percent))

        if calc_by_soc(soc_buy_price) >= wicc_sell_price:
            print "soc贵, 卖soc买wicc流程"
            # =========================  1. 价格变成soc贵了, wicc上还有点位的话这里就卖掉  =========================
            change_count = 0
            end = len(g_config['wicc'])
            for i in xrange(0, end):
                if g_config['wicc'][i]['state'] == HAS_TRADE_STATE:
                    g_config['wicc'][i]['state'] = SOC_TO_WICC_TEMP_STATE
                    # 记录需要转换的数量
                    change_count = change_count + g_config['wicc'][i]['trade_count']

            if change_count != 0:
                change_count = common_utils.get_round(change_count, COUNT_DECIMAL)
                buy_wicc_count = soc_wicc_helper.sell_soc_and_buy_wicc(change_count)
                print "......卖%s个soc买%s个wicc" % (str(change_count), str(buy_wicc_count))
                if buy_wicc_count:
                    for i in xrange(0, end):
                        if g_config['wicc'][i]['state'] == SOC_TO_WICC_TEMP_STATE:
                            # 重置标识位
                            g_config['wicc'][i]['state'] = NORMAL_STATE
                            # 转换数量清零
                            g_config['wicc'][i]['trade_count'] = 0
                    # 记录soc数量的变化
                    g_soc_count = common_utils.get_round(g_soc_count - change_count, COUNT_DECIMAL)
                    # 记录wicc数量的变化
                    g_wicc_count = common_utils.get_round(g_wicc_count + buy_wicc_count, COUNT_DECIMAL)

                    minus = soc_to_wicc_minus
                    percent = common_utils.get_round(soc_to_wicc_percent, 3)
                    state = "卖soc买wicc"
                    soc_wicc_helper.save_trade_data(get_format_time(), state, soc_buy_price, wicc_sell_price, minus,
                                                    percent, g_soc_count, g_wicc_count)
                    # 更新配置信息
                    save_config()
            # =========================  2. 当前点位下, 比该点位小的点位有没有进行转换(soc -> wicc)  =========================
            # 注意, 虽然是1 - n的点位划分的, 但是索引是从0开始的
            point = int(soc_to_wicc_percent)
            index = point - 1
            # 防止越界
            end = min(point, len(g_config['soc']))

            has_soc_to_wicc_percent = 0
            need_soc_to_wicc_percent = 0
            for i in xrange(0, end):
                # 比如现在是偏离 6个点, 索引就是0-5, 将里面state  == 0(这部分应该要转为wicc的)的值取出来
                if g_config['soc'][i]['state'] == NORMAL_STATE:
                    # 还需要买的
                    need_soc_to_wicc_percent = need_soc_to_wicc_percent + g_config['soc'][i]['percent']
                    # 把 标识位改为1(已经转为wicc), 等网络请求结果成功后, 再将原先的soc替换
                    g_config['soc'][i]['state'] = SOC_TO_WICC_TEMP_STATE
                elif g_config['soc'][i]['state'] == HAS_TRADE_STATE:
                    # 记录已经买了的百分比
                    has_soc_to_wicc_percent = has_soc_to_wicc_percent + g_config['soc'][i]['percent']

            # soc转wicc操作
            if need_soc_to_wicc_percent != 0:
                # 需要卖soc买wicc, 计算数量
                soc_sell_count = float(need_soc_to_wicc_percent) / (100 - has_soc_to_wicc_percent) * g_soc_count
                soc_sell_count = common_utils.get_round(soc_sell_count, COUNT_DECIMAL)
                # 卖soc买wicc
                buy_wicc_count = soc_wicc_helper.sell_soc_and_buy_wicc(soc_sell_count)
                print "......卖%s个soc买%s个wicc" % (str(soc_sell_count), str(buy_wicc_count))

                if buy_wicc_count != 0:
                    for i in xrange(0, end):
                        if g_config['soc'][i]['state'] == SOC_TO_WICC_TEMP_STATE:
                            # 修改标识位为已购买状态
                            g_config['soc'][i]['state'] = HAS_TRADE_STATE
                            # 计算购买的数量
                            there_count = buy_wicc_count * g_config['soc'][i]['percent'] / need_soc_to_wicc_percent
                            # 记录购买了的数量
                            g_config['soc'][i]['trade_count'] = common_utils.get_round(there_count, COUNT_DECIMAL)

                    # 记录soc数量的变化
                    g_soc_count = common_utils.get_round(g_soc_count - soc_sell_count, COUNT_DECIMAL)
                    # 记录wicc数量的变化
                    g_wicc_count = common_utils.get_round(g_wicc_count + buy_wicc_count, COUNT_DECIMAL)
                    minus = soc_to_wicc_minus
                    percent = common_utils.get_round(soc_to_wicc_percent, 3)
                    state = "卖soc买wicc"
                    soc_wicc_helper.save_trade_data(get_format_time(), state, soc_buy_price, wicc_sell_price, minus,
                                                    percent, g_soc_count, g_wicc_count)
                    # 更新配置信息
                    save_config()
            # =========================  3. 当前点位下, 比该点位大g_delta_point的点位有没有换回来(wicc -> soc)  =========================
            # 在soc还是比wicc贵的情况下, 如果15个点的时候soc转wicc了, 现在滑到10个点, 那就把15个点转的wicc再转回soc
            # 这里是卖wicc买soc, 但是soc的价格比wicc贵
            percent = common_utils.get_round((calc_by_soc(soc_sell_price) - wicc_buy_price) * 100 / wicc_buy_price, 4)
            # 当前点位档次(注意, 索引从0开始总会比点位档次小1)
            point = int(percent)
            # 防止越界
            end = len(g_config['soc'])
            change_count = 0
            # 如果 point + g_delta_point >= end 了的话不会进入循环, change_count 一样等于0
            for i in xrange(point - 1 + g_delta_point, end):
                # 这里有点绕, 假设点位档次14, 索引是13 (point - 1), 应该转换15 (索引  + g_delta_point)以上档次的
                if g_config['soc'][i]['state'] == HAS_TRADE_STATE:
                    # 将标识位改为 想要转换的标识位
                    g_config['soc'][i]['state'] = WICC_TO_SOC_TEMP_STATE
                    # 计算之前一共买了多少个wicc
                    change_count = change_count + g_config['soc'][i]['trade_count']

            if change_count != 0:
                # 卖wicc买soc
                buy_soc_count = soc_wicc_helper.sell_wicc_and_buy_soc(change_count)
                print "......卖%s个wicc买%s个soc" % (str(change_count), str(buy_soc_count))
                if buy_soc_count != 0:
                    for i in xrange(point - 1 + g_delta_point, end):
                        if g_config['soc'][i]['state'] == WICC_TO_SOC_TEMP_STATE:
                            # 重置标识位
                            g_config['soc'][i]['state'] = NORMAL_STATE
                            # 转换数量清零
                            g_config['soc'][i]['trade_count'] = 0

                    # 记录soc数量的变化
                    g_soc_count = common_utils.get_round(g_soc_count + buy_soc_count, COUNT_DECIMAL)
                    # 记录wicc数量的变化
                    g_wicc_count = g_wicc_count - change_count
                    minus = calc_by_soc(soc_sell_price) - wicc_buy_price
                    percent = common_utils.get_round(percent, 3)
                    state = "卖wicc买soc"
                    soc_wicc_helper.save_trade_data(get_format_time(), state, soc_sell_price, wicc_buy_price, minus,
                                                    percent, g_soc_count, g_wicc_count)
                    # 更新配置信息
                    save_config()
            # 结束 soc 比 wicc 贵的逻辑
            return

        if wicc_buy_price >= calc_by_soc(soc_sell_price):
            print "wicc贵, 卖wicc买soc流程"

            # =========================  1. 价格变成wicc贵了, soc上还有点位的话这里就卖掉  =========================
            change_count = 0
            end = len(g_config['soc'])
            for i in xrange(0, end):
                if g_config['soc'][i]['state'] == HAS_TRADE_STATE:
                    g_config['soc'][i]['state'] = WICC_TO_SOC_TEMP_STATE
                    # 记录需要转换的数量
                    change_count = change_count + g_config['soc'][i]['trade_count']

            if change_count != 0:
                change_count = common_utils.get_round(change_count, COUNT_DECIMAL)
                buy_soc_count = soc_wicc_helper.sell_wicc_and_buy_soc(change_count)
                print "......卖%s个wicc买%s个soc" % (str(change_count), str(buy_soc_count))
                if buy_soc_count:
                    for i in xrange(0, end):
                        if g_config['soc'][i]['state'] == WICC_TO_SOC_TEMP_STATE:
                            # 重置标识位
                            g_config['soc'][i]['state'] = NORMAL_STATE
                            # 转换数量清零
                            g_config['soc'][i]['trade_count'] = 0
                    # 记录soc数量的变化
                    g_soc_count = common_utils.get_round(g_soc_count + buy_soc_count, COUNT_DECIMAL)
                    # 记录wicc数量的变化
                    g_wicc_count = common_utils.get_round(g_wicc_count - change_count, COUNT_DECIMAL)
                    minus = wicc_to_soc_minus
                    percent = common_utils.get_round(wicc_to_soc_percent, 3)
                    state = "卖wicc买soc"
                    soc_wicc_helper.save_trade_data(get_format_time(), state, soc_sell_price, wicc_buy_price, minus,
                                                    percent, g_soc_count, g_wicc_count)
                    # 更新配置信息
                    save_config()
            # =========================  2. 当前点位下, 比该点位小的点位有没有进行转换(wicc -> soc)  =========================
            # 注意, 虽然是1 - n的点位划分的, 但是索引是从0开始的
            point = int(wicc_to_soc_percent)
            index = point - 1
            # 防止越界
            end = min(point, len(g_config['wicc']))

            has_wicc_to_soc_percent = 0
            need_wicc_to_soc_percent = 0
            for i in xrange(0, end):
                # 比如现在是偏离 6个点, 索引就是0-5, 将里面state  == 0(这部分应该要转为soc的)的值取出来
                if g_config['wicc'][i]['state'] == NORMAL_STATE:
                    # 还需要买的
                    need_wicc_to_soc_percent = need_wicc_to_soc_percent + g_config['wicc'][i]['percent']
                    # 把 标识位改为1(已经转为wicc), 等网络请求结果成功后, 再将原先的soc替换
                    g_config['wicc'][i]['state'] = WICC_TO_SOC_TEMP_STATE
                elif g_config['wicc'][i]['state'] == HAS_TRADE_STATE:
                    # 记录已经买了的百分比
                    has_wicc_to_soc_percent = has_wicc_to_soc_percent + g_config['wicc'][i]['percent']

            # wicc转soc操作
            if need_wicc_to_soc_percent != 0:
                # 需要卖wicc买soc, 计算数量
                wicc_sell_count = float(need_wicc_to_soc_percent) / (100 - has_wicc_to_soc_percent) * g_wicc_count
                wicc_sell_count = common_utils.get_round(wicc_sell_count, COUNT_DECIMAL)

                # 卖wicc买soc
                buy_soc_count = soc_wicc_helper.sell_wicc_and_buy_soc(wicc_sell_count)
                print "......卖%s个wicc买%s个soc" % (str(wicc_sell_count), str(buy_soc_count))

                if buy_soc_count != 0:
                    for i in xrange(0, end):
                        if g_config['wicc'][i]['state'] == WICC_TO_SOC_TEMP_STATE:
                            # 修改标识位为已购买状态
                            g_config['wicc'][i]['state'] = HAS_TRADE_STATE
                            # 计算购买的数量
                            there_count = buy_soc_count * g_config['wicc'][i]['percent'] / need_wicc_to_soc_percent
                            # 记录购买了的数量
                            g_config['wicc'][i]['trade_count'] = common_utils.get_round(there_count, COUNT_DECIMAL)

                    # 记录soc数量的变化
                    g_soc_count = common_utils.get_round(g_soc_count + buy_soc_count, COUNT_DECIMAL)
                    # 记录wicc数量的变化
                    g_wicc_count = common_utils.get_round(g_wicc_count - wicc_sell_count, COUNT_DECIMAL)
                    minus = wicc_to_soc_minus
                    percent = common_utils.get_round(wicc_to_soc_percent, 3)
                    state = "卖wicc买soc"
                    soc_wicc_helper.save_trade_data(get_format_time(), state, soc_sell_price, wicc_buy_price,
                                                    minus, percent, g_soc_count, g_wicc_count)
                    # 更新配置信息
                    save_config()
            # =========================  3. 当前点位下, 比该点位大g_delta_point的点位有没有换回来(soc -> wicc)  =========================
            # 在wicc还是比soc贵的情况下, 如果15个点的时候wicc转soc了, 现在滑到10个点, 那就把15个点转的soc再转回wicc
            # 这里是卖soc买wicc, 但是wicc的价格比soc贵
            percent = common_utils.get_round(
                (wicc_sell_price - calc_by_soc(soc_buy_price)) * 100 / calc_by_soc(soc_buy_price), 4)
            # 当前点位档次(注意, 索引从0开始总会比点位档次小1)
            point = int(percent)
            # 防止越界
            end = len(g_config['wicc'])
            change_count = 0
            # 如果 point + g_delta_point >= end 了的话不会进入循环, change_count 一样等于0
            for i in xrange(point - 1 + g_delta_point, end):
                # 这里有点绕, 假设点位档次14, 索引是13 (point - 1), 应该转换15 (索引  + g_delta_point)以上档次的
                if g_config['wicc'][i]['state'] == HAS_TRADE_STATE:
                    # 将标识位改为 想要转换的标识位
                    g_config['wicc'][i]['state'] = SOC_TO_WICC_TEMP_STATE
                    # 计算之前一共买了多少个soc
                    change_count = change_count + g_config['wicc'][i]['trade_count']

            if change_count != 0:
                # 卖soc买wicc
                buy_wicc_count = soc_wicc_helper.sell_soc_and_buy_wicc(change_count)
                print "......卖%s个soc买%s个wicc" % (str(change_count), str(buy_wicc_count))
                if buy_wicc_count != 0:
                    for i in xrange(point - 1 + g_delta_point, end):
                        if g_config['wicc'][i]['state'] == SOC_TO_WICC_TEMP_STATE:
                            # 重置标识位
                            g_config['wicc'][i]['state'] = NORMAL_STATE
                            # 转换数量清零
                            g_config['wicc'][i]['trade_count'] = 0

                    # 记录soc数量的变化
                    g_soc_count = common_utils.get_round(g_soc_count - change_count, COUNT_DECIMAL)
                    # 记录wicc数量的变化
                    g_wicc_count = common_utils.get_round(g_wicc_count + buy_wicc_count, COUNT_DECIMAL)
                    minus = wicc_sell_price - calc_by_soc(soc_buy_price)
                    percent = common_utils.get_round(percent, 3)
                    state = "卖soc买wicc"
                    soc_wicc_helper.save_trade_data(get_format_time(), state, soc_buy_price, wicc_sell_price, minus,
                                                    percent, g_soc_count, g_wicc_count)
                    # 更新配置信息
                    save_config()
            # 结束 wicc 比 soc 贵的逻辑
            return

    else:
        # 获取到的价格有0出现
        if g_config['is_debug']:
            # 测试环境说明 已经没有数据了
            global g_run
            g_run = False
            print "=======回测结束======="
        else:
            # 正式环境中, 可能由于网络问题, 没有拿到价格, 这里把信息打印出来
            msg = msg
            print msg
Example #8
0
def get_deviation(n, max_index):
    one_deviation = 200.0 / (max_index + max_index * n)
    two_deviation = float(max_index) * one_deviation / (n - max_index + 1)
    return common_utils.get_round(one_deviation,
                                  3), common_utils.get_round(two_deviation, 3)
Example #9
0
def get_soc_wicc_data():
    global soc_count

    soc_buy_price, soc_sell_price = get_huobi_price("socusdt", False)
    wicc_buy_price, wicc_sell_price = get_huobi_price("wiccusdt", False)
    msg = "%s get_soc_wicc_data启动了  --------  " \
          "soc_buy_price = %s" \
          "; soc_sell_price = %s" \
          "; wicc_buy_price = %s" \
          "; wicc_sell_price = %s" \
          % (get_format_time(), str(soc_buy_price), str(soc_sell_price), str(wicc_buy_price), str(wicc_sell_price))

    if soc_buy_price and soc_sell_price and wicc_buy_price and wicc_buy_price:
        # 4个价格不为0, 说明网络获取成功

        # soc价格更高的时候, 找soc的买盘价格(卖soc) - wicc的卖盘价格(买wicc) / wicc的卖盘价格
        soc_expensive_percent = common_utils.get_round(
            float((soc_buy_price * 10 - wicc_sell_price) * 100 /
                  wicc_sell_price), 5)
        # wicc价格更高的时候, 找wicc的买盘价格(卖wicc) - soc的卖盘价格(买soc) / soc的卖盘价格
        wicc_expensive_percent = common_utils.get_round(
            float((wicc_buy_price - soc_sell_price * 10) * 100 /
                  (soc_sell_price * 10)), 5)

        percent = max(soc_expensive_percent, wicc_expensive_percent)
        if percent == wicc_expensive_percent:
            minus = wicc_buy_price - soc_sell_price * 10
        else:
            minus = soc_buy_price * 10 - wicc_sell_price

        msg = msg + "; percent = %s" % (str(percent))
        # 写入数据库
        robot_dao.insert_soc_wicc(get_format_time(), float(soc_buy_price),
                                  float(soc_sell_price), float(wicc_buy_price),
                                  float(wicc_sell_price), float(minus),
                                  percent)
        # (soc和wicc偏差不大的情况下) 平的状态下
        if soc_count == 0:
            if percent >= 15:
                if percent == wicc_expensive_percent:
                    # soc价格低, 买入soc
                    message = "相差为%s, 可以买入soc了 --- %s" % (str(percent),
                                                          get_format_time())
                    wicc_balance = get_coin_balance("wicc")
                    if wicc_balance != 0:
                        sell_wicc_and_buy_soc(wicc_balance, 1)
                    print message
                    print msg
                    return
            if percent >= 5.5:
                if percent != wicc_expensive_percent:
                    # 卖出soc, 买入wicc
                    message = "相差为%s, 可以买入wicc了 --- %s" % (str(percent),
                                                           get_format_time())
                    soc_balance = get_coin_balance("soc")
                    if soc_balance != 0:
                        sell_soc_and_buy_wicc(soc_balance, -1)
                    print message
                    print msg
                    return
        # 持有soc状态下
        if soc_count == 1:
            # 这里重新计算一下平soc的百分比
            sell_soc_percent = common_utils.get_round(
                float((wicc_sell_price - soc_buy_price * 10) * 100 /
                      (soc_buy_price * 10)), 5)
            # 卖soc(看买盘价格), 买wicc(看卖盘价格)
            if wicc_sell_price <= soc_buy_price * 10:
                message = "wicc比soc便宜, 可以平了 --- %s" % get_format_time()
                print message
                soc_balance = get_coin_balance("soc")
                if soc_balance != 0:
                    sell_soc_and_buy_wicc(
                        common_utils.get_round(float(soc_balance * 2 / 3), 2),
                        0)
            elif sell_soc_percent >= 0 and sell_soc_percent <= 0.5:
                message = "相差小于千分之5, 可以平了 --- %s" % get_format_time()
                print message
                soc_balance = get_coin_balance("soc")
                if soc_balance != 0:
                    sell_soc_and_buy_wicc(
                        common_utils.get_round(float(soc_balance * 2 / 3), 2),
                        0)
            print msg
            return
        # 持有wicc状态下
        if soc_count == -1:
            # 这里重新计算一下平wicc的百分比
            wicc_sell_percent = common_utils.get_round(
                float((soc_sell_price * 10 - wicc_buy_price) * 100 /
                      wicc_buy_price), 5)
            # 卖wicc(看买盘价格), 买soc(看卖盘价格)
            if wicc_buy_price >= soc_sell_price * 10:
                message = "wicc比soc贵, 可以平了 --- %s" % get_format_time()
                print message
                wicc_balance = get_coin_balance("wicc")
                if wicc_balance != 0:
                    sell_wicc_and_buy_soc(
                        common_utils.get_round(float(wicc_balance * 1 / 3), 2),
                        0)
            elif wicc_sell_percent >= 0 and wicc_sell_percent <= 0.5:
                message = "相差小于千分之5, 可以平了 --- %s" % get_format_time()
                print message
                wicc_balance = get_coin_balance("wicc")
                if wicc_balance != 0:
                    sell_wicc_and_buy_soc(
                        common_utils.get_round(float(wicc_balance * 1 / 3), 2),
                        0)
            print msg
            return

        # 持有soc的状态
        # if soc_count == 1:
        #     if percent <= 24:
        #         if max_price == wicc_price:
        #             # 还是soc价格低, 卖掉1000 soc
        #             sell_soc_result = sell_soc(10000)
        #             print "sell_soc_result = %s" % (str(sell_soc_result))
        #             if sell_soc_result:
        #                 buy_wicc_result = buy_wicc()
        #                 print "buy_wicc_result = %s" % (str(buy_wicc_result))
        #                 if buy_wicc_result:
        #                     robot_dao.insert_trade_data(get_format_time(), "卖soc买wicc")
        #                     soc_count = -1
        #                 else:
        #                     #  卖成功了却没买成功, 在试一次
        #                     two_result = buy_wicc()
        #                     if two_result:
        #                         soc_count = -1
        #                     else:
        #                         soc_count = -22
        #             message = "相差为%s, 卖soc买wicc --- %s" % (str(percent), get_format_time())
        #         else:
        #             # wicc价格低, 这里做平仓, 然后今晚不操作了
        #             message = "相差为%s, 平仓后不再操作--- %s" % (str(percent), get_format_time())
        #             soc_count = -22
        #             robot_dao.insert_trade_data(get_format_time(), message)
        #         print message
        #         return
        # elif soc_count == -1:
        #     #  已经买了wicc的状态
        #     if percent >= 28.5:
        #         wicc_balance = get_coin_balance("wicc")
        #         print "wicc_balance = %s" % (str(wicc_balance))
        #         if wicc_balance:
        #             sell_wicc_result = sell_wicc(wicc_balance)
        #             print "sell_wicc_result = %s" % (str(sell_wicc_result))
        #             if sell_wicc_result:
        #                 buy_soc_result = buy_soc()
        #                 print "buy_soc_result = %s" % (str(buy_soc_result))
        #                 if buy_soc_result:
        #                     soc_count = 1
        #                     robot_dao.insert_trade_data(get_format_time(), "卖wicc买soc")
        #                 else:
        #                     #  卖wicc成功, 买soc却失败了, 再试一次
        #                     two_result = buy_soc()
        #                     if two_result:
        #                         soc_count = 1
        #                     else:
        #                         soc_count = -22
    print msg
Example #10
0
def get_soc_wicc_data():
    global soc_state
    global g_soc_count
    global g_wicc_count
    global g_max_has_soc_count, g_max_trans_soc_count, g_max_has_wicc_count, g_max_trans_wicc_count

    soc_buy_price, soc_sell_price = get_huobi_price("socusdt", False)
    wicc_buy_price, wicc_sell_price = get_huobi_price("wiccusdt", False)
    msg = "%s 1.1版本的 get_soc_wicc_data启动了  --------  单买单卖" \
          "\n soc_buy_price = %s" \
          "; soc_sell_price = %s" \
          "; wicc_buy_price = %s" \
          "; wicc_sell_price = %s" \
          % (get_format_time(), str(soc_buy_price), str(soc_sell_price), str(wicc_buy_price), str(wicc_sell_price))

    if soc_buy_price and soc_sell_price and wicc_buy_price and wicc_buy_price:
        # 4个价格不为0, 说明网络获取成功
        # soc价格更高的时候, 找soc的买盘价格(卖soc) - wicc的卖盘价格(买wicc) / wicc的卖盘价格
        soc_to_wicc_minus = calc_by_soc(soc_buy_price) - wicc_sell_price
        soc_to_wicc_percent = common_utils.get_round(
            (soc_to_wicc_minus * 100 / wicc_sell_price), 4)

        # wicc价格更高的时候, 找wicc的买盘价格(卖wicc) - soc的卖盘价格(买soc) / soc的卖盘价格
        wicc_to_soc_minus = wicc_buy_price - calc_by_soc(soc_sell_price)
        wicc_to_soc_percent = common_utils.get_round(
            (wicc_to_soc_minus * 100 / calc_by_soc(soc_sell_price)), 4)

        # 计算一下全部转化为soc会有多少
        all_trans_soc_count = wicc_buy_price * g_wicc_count * 0.998 / soc_sell_price * 0.998 + g_soc_count
        # 计算一下全部转化为wicc会有多少
        all_trans_wicc_count = soc_buy_price * g_soc_count * 0.998 / wicc_sell_price * 0.998 + g_wicc_count

        g_max_has_soc_count = max(g_max_has_soc_count, g_soc_count)
        g_max_trans_soc_count = max(g_max_trans_soc_count, all_trans_soc_count)

        g_max_has_wicc_count = max(g_max_has_wicc_count, g_wicc_count)
        g_max_trans_wicc_count = max(g_max_trans_wicc_count,
                                     all_trans_wicc_count)

        per_msg = "\n soc_to_wicc_minus = %s" \
                  "; wicc_to_soc_minus = %s" \
                  "; soc_to_wicc_percent = %s" \
                  "; wicc_to_soc_percent = %s" \
                  "\n all_trans_soc_count = %s" \
                  "; all_trans_wicc_count = %s" \
                  "; soc_count = %s" \
                  "; wicc_count = %s" % (
                      str(soc_to_wicc_minus),
                      str(wicc_to_soc_minus),
                      str(soc_to_wicc_percent),
                      str(wicc_to_soc_percent),
                      str(all_trans_soc_count),
                      str(all_trans_wicc_count),
                      str(g_soc_count),
                      str(g_wicc_count))

        msg = msg + per_msg
        if g_is_debug:
            per_msg = "\n max_has_soc_count = %s" \
                      "; max_trans_soc_count = %s" \
                      "; max_has_wicc_count = %s" \
                      "; max_trans_wicc_count = %s" % (
                          str(g_max_has_soc_count),
                          str(g_max_trans_soc_count),
                          str(g_max_has_wicc_count),
                          str(g_max_trans_wicc_count))
            msg = msg + per_msg

        print msg

        # (soc和wicc偏差不大的情况下) 平的状态下
        if soc_state == 0:
            if wicc_to_soc_percent >= g_delta_point:
                # 卖wicc买soc
                buy_soc_count = soc_wicc_helper.sell_wicc_and_buy_soc(
                    g_wicc_count)
                print "......卖%s个wicc买%s个soc" % (str(g_wicc_count),
                                                 str(buy_soc_count))
                # 记录soc数量的变化
                g_soc_count = common_utils.get_round(
                    g_soc_count + buy_soc_count, COUNT_DECIMAL)
                # 记录wicc数量的变化
                g_wicc_count = 0
                # 持有soc
                soc_state = 1
                minus = wicc_to_soc_minus
                percent = common_utils.get_round(wicc_to_soc_percent, 3)
                state = "卖wicc买soc"
                soc_wicc_helper.save_trade_data(get_format_time(), state,
                                                soc_sell_price, wicc_buy_price,
                                                minus, percent, g_soc_count,
                                                g_wicc_count)
                return
            if soc_to_wicc_percent >= g_delta_point:
                # 卖soc买wicc
                buy_wicc_count = soc_wicc_helper.sell_soc_and_buy_wicc(
                    g_soc_count)
                print "......卖%s个soc买%s个wicc" % (str(g_soc_count),
                                                 str(buy_wicc_count))
                # 记录soc数量的变化
                g_soc_count = 0
                # 记录wicc数量的变化
                g_wicc_count = common_utils.get_round(
                    g_wicc_count + buy_wicc_count, COUNT_DECIMAL)
                # 持有wicc
                soc_state = -1
                minus = soc_to_wicc_minus
                percent = common_utils.get_round(soc_to_wicc_percent, 3)
                state = "卖soc买wicc"
                soc_wicc_helper.save_trade_data(get_format_time(), state,
                                                soc_buy_price, wicc_sell_price,
                                                minus, percent, g_soc_count,
                                                g_wicc_count)
                return

            if soc_state == 1:
                # 持有soc的状态
                if soc_to_wicc_percent > 0:
                    soc_sell_count = common_utils.get_round(
                        g_soc_count * 0.5, COUNT_DECIMAL)
                    # 卖soc买wicc
                    buy_wicc_count = soc_wicc_helper.sell_soc_and_buy_wicc(
                        soc_sell_count)
                    print "......卖%s个soc买%s个wicc" % (str(soc_sell_count),
                                                     str(buy_wicc_count))
                    # 记录soc数量的变化
                    g_soc_count = common_utils.get_round(
                        g_soc_count - soc_sell_count, COUNT_DECIMAL)
                    # 记录wicc数量的变化
                    g_wicc_count = common_utils.get_round(
                        g_wicc_count + buy_wicc_count, COUNT_DECIMAL)
                    #  状态为平
                    soc_state = 0
                    minus = soc_to_wicc_minus
                    percent = common_utils.get_round(soc_to_wicc_percent, 3)
                    state = "卖soc买wicc"
                    soc_wicc_helper.save_trade_data(get_format_time(), state,
                                                    soc_buy_price,
                                                    wicc_sell_price, minus,
                                                    percent, g_soc_count,
                                                    g_wicc_count)
                    return

            if soc_state == -1:
                # 持有wicc的状态
                if wicc_to_soc_percent > 0:
                    wicc_sell_count = common_utils.get_round(
                        g_wicc_count * 0.5, COUNT_DECIMAL)
                    # 卖wicc买soc
                    buy_soc_count = soc_wicc_helper.sell_wicc_and_buy_soc(
                        wicc_sell_count)
                    print "......卖%s个wicc买%s个soc" % (str(wicc_sell_count),
                                                     str(buy_soc_count))
                    # 记录soc数量的变化
                    g_soc_count = common_utils.get_round(
                        g_soc_count + buy_soc_count, COUNT_DECIMAL)
                    # 记录wicc数量的变化
                    g_wicc_count = common_utils.get_round(
                        g_wicc_count - wicc_sell_count, COUNT_DECIMAL)
                    # 状态为平
                    soc_state = 0
                    minus = wicc_to_soc_minus
                    percent = common_utils.get_round(wicc_to_soc_percent, 3)
                    state = "卖wicc买soc"
                    soc_wicc_helper.save_trade_data(get_format_time(), state,
                                                    soc_buy_price,
                                                    wicc_sell_price, minus,
                                                    percent, g_soc_count,
                                                    g_wicc_count)
                    return
    else:
        # TODO 这个代码只做了测试版本用于查看测试数据, 如果测试结果比较理想, 需要实际跑的话要注意修改
        # TODO 然而跑过之后发现效果并不理想, 当然, 这里的写法也没有btc_robot里这么复杂, 但是还是决定用2.0版本了
        # 获取到的价格有0出现
        # 测试环境说明 已经没有数据了
        global g_run
        g_run = False
        print "=======回测结束======="