Example #1
0
def update_factor_in_category(con, category, factors, cur_date,
                              code_date_series):
    # 解析
    basic_data_info, factor_list = resolve_factors(factors)
    print(basic_data_info)

    # 获取该因子类中因子字段及它们的排列情况
    fields = si.get_table_fields(con, category)
    print(fields)

    codes = si.get_all_codes(con)
    data_list = []  # 存储所有因子数据
    for code in codes:
        # 根据收集的信息从数据库中去获取基础数据
        for table in basic_data_info:
            dict = {}
            for field in basic_data_info[table]:
                dict[field] = si.get_data_series(con, table, field, code,
                                                 code_date_series[code])
            # 更新基础数据
            basic_data_info[table] = dict

        code_list = [code, cur_date]
        row = {}
        # 计算因子,通过反射找到方法并传入参数计算
        # 注意之后要进行数据对齐,为了避免不同时间窗口下计算的因子数据结果长度不同,只取最后一天数据
        for factor in factor_list:
            func = getattr(module_dict[category], factor["name"])
            param_list = []  # 存放参数列表
            # 获取参数
            for table in factor["params0"]:
                for field in factor["params0"][table]:
                    param_list.append(basic_data_info[table][field])
            for val in factor["params1"]:
                param_list.append(val)

            ret_data = func(param_list)  # 调用函数计算因子
            if len(ret_data) > 0:
                data = ret_data[len(ret_data) - 1]  # 只取最后一天(当日的数据)
                row[factor["field"]] = float("%.5f" % data)

        if row:  # 字典对象不为空
            # 重新排序使得结果与表中字段顺序一致
            for field in fields:
                code_list.append(row[field])
            print(code_list)
            data_list.append(code_list)  # 添加该股票下的因子数据

    si.insert_factor_data(con, category, data_list)  # 批量更新当天的因子数据
Example #2
0
def add_factor(con, category, factor, code_date_series):
    # 解析因子
    basic_data_info, factor_dict, factor_filed = resolve_factor(factor)
    print(basic_data_info)

    # 增加字段
    si.add_factor_column(con, category, factor_filed)

    codes = si.get_all_codes(con)
    values = []
    for code in codes:
        # 根据收集的信息从数据库中去获取基础数据
        for table in basic_data_info:
            dict = {}
            for field in basic_data_info[table]:
                dict[field] = si.get_data_series(con, table, field, code,
                                                 code_date_series[code])
            # 更新基础数据
            basic_data_info[table] = dict

        func = getattr(module_dict[category],
                       factor_dict["name"])  # 通过反射拿到相应的函数对象
        param_list = []
        # 向参数列表中计算因子要用到的基本数据
        for table in factor_dict["params0"]:
            for field in factor_dict["params0"][table]:
                param_list.append(basic_data_info[table][field])
        # 向参数列表中增加常数参数
        for val in factor_dict["params1"]:
            param_list.append(val)
        ret_data = func(param_list)  # 计算因子
        values += si.get_column_tuple_list(code, code_date_series[code],
                                           ret_data)  # 增加该股票下的因子数据

    # 更新数据
    si.update_factor_column(con, category, factor_filed, values)
Example #3
0
def update_emotion_factor(code_date_serise):
    con = pymysql.connect(host="localhost",
                          user="******",
                          passwd="mysql",
                          db="stockvision")
    table_name = "emotion_factor"
    pool = si.get_all_codes(con)

    logvol = Factor("logvol", [])
    logvol_10 = Factor("logvol_10", [])
    logvol_15 = Factor("logvol_15", [])
    logvol_20 = Factor("logvol_20", [])
    vol_change_rate = Factor("vol_change_rate", [])
    vol_change_rate_5 = Factor("vol_change_rate_5", [])
    vol_change_rate_10 = Factor("vol_change_rate_10", [])
    vol_change_rate_20 = Factor("vol_change_rate_20", [])
    vol_relative_ratio_0 = Factor("vol_relative_ratio_0", [])
    vol_relative_ratio_1 = Factor("vol_relative_ratio_1", [])
    ar = Factor("ar", [])
    ar_14 = Factor("ar_14", [])
    br = Factor("br", [])
    br_14 = Factor("br_14", [])
    obv_0 = Factor("obv_0", [])
    obv_1 = Factor("obv_1", [])
    vma = Factor("vma", [])
    pvt_0 = Factor("pvt_0", [])
    pvt_1 = Factor("pvt_1", [])
    mfi = Factor("mfi", [])

    # 更新因子数据
    for code in pool:
        # 获取基础数据
        vol_series = si.get_data_series(con, "daily", "vol", code,
                                        code_date_serise[code])
        open_series = si.get_data_series(con, "daily", "open", code,
                                         code_date_serise[code])
        high_series = si.get_data_series(con, "daily", "high", code,
                                         code_date_serise[code])
        low_series = si.get_data_series(con, "daily", "high", code,
                                        code_date_serise[code])
        close_series = si.get_data_series(con, "daily", "close", code,
                                          code_date_serise[code])

        # 避免时间序列不够长无法进行计算/避免输入空值
        if len(vol_series) > 0:
            vol_change_rate_list = cal_vol_change_rate_n(vol_series, 1)
            vol_change_rate.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         vol_change_rate_list))

            obv_0_list = cal_obv(close_series, vol_series, 0)
            obv_0.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         obv_0_list))
            obv_1_list = cal_obv(close_series, vol_series, 1)
            obv_1.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         obv_1_list))

            pvt_0_list = cal_pvt(close_series, vol_series, 0)
            pvt_0.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         pvt_0_list))
            pvt_1_list = cal_pvt(close_series, vol_series, 1)
            pvt_1.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         pvt_1_list))

            vma_list = cal_vma_n(vol_series, 6)
            vma.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         vma_list))

        if len(vol_series) >= 5:
            # 得到因子
            logvol_list = cal_logvol_n(vol_series, 5)
            vol_change_rate_5_list = cal_vol_change_rate_n(vol_series, 5)
            vol_relative_ratio_0_list = cal_vol_relative_ratio(
                vol_series, 5, 5, 0)
            vol_relative_ratio_1_list = cal_vol_relative_ratio(
                vol_series, 5, 5, 1)

            logvol.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         logvol_list))
            vol_change_rate_5.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         vol_change_rate_5_list))
            vol_relative_ratio_0.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         vol_relative_ratio_0_list))
            vol_relative_ratio_1.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         vol_relative_ratio_1_list))

            mfi_list = cal_mfi(high_series, low_series, close_series,
                               vol_series, 5)
            mfi.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         mfi_list))

        if len(vol_series) >= 10:
            logvol_10_list = cal_logvol_n(vol_series, 10)
            vol_change_rate_10_list = cal_vol_change_rate_n(vol_series, 10)

            logvol_10.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         logvol_10_list))
            vol_change_rate_10.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         vol_change_rate_10_list))

        if len(vol_series) >= 14:
            ar_14_list = cal_ar_n(open_series, high_series, low_series, 14)
            br_14_list = cal_br_n(close_series, high_series, low_series, 14)

            ar_14.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         ar_14_list))
            br_14.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         br_14_list))

        if len(vol_series) >= 15:
            logvol_15_list = cal_logvol_n(vol_series, 15)

            logvol_15.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         logvol_15_list))

        if len(vol_series) >= 20:
            logvol_20_list = cal_logvol_n(vol_series, 20)
            vol_change_rate_20_list = cal_vol_change_rate_n(vol_series, 20)

            logvol_20.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         logvol_20_list))
            vol_change_rate_20.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         vol_change_rate_20_list))

        if len(vol_series) >= 26:
            ar_list = cal_ar_n(open_series, high_series, low_series, 26)
            br_list = cal_br_n(close_series, high_series, low_series, 26)

            ar.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         ar_list))
            br.append_value(
                si.get_column_tuple_list(code, code_date_serise[code],
                                         br_list))

    # 更新数据库中因子
    si.update_factor_column(con, table_name, logvol.get_name(),
                            logvol.get_value())
    si.update_factor_column(con, table_name, logvol_10.get_name(),
                            logvol_10.get_value())
    si.update_factor_column(con, table_name, logvol_15.get_name(),
                            logvol_15.get_value())
    si.update_factor_column(con, table_name, logvol_20.get_name(),
                            logvol_20.get_value())
    si.update_factor_column(con, table_name, vol_change_rate.get_name(),
                            vol_change_rate.get_value())
    si.update_factor_column(con, table_name, vol_change_rate_5.get_name(),
                            vol_change_rate_5.get_value())
    si.update_factor_column(con, table_name, vol_change_rate_10.get_name(),
                            vol_change_rate_10.get_value())
    si.update_factor_column(con, table_name, vol_change_rate_20.get_name(),
                            vol_change_rate_20.get_value())
    si.update_factor_column(con, table_name, ar.get_name(), ar.get_value())
    si.update_factor_column(con, table_name, br.get_name(), br.get_value())
    si.update_factor_column(con, table_name, ar_14.get_name(),
                            ar_14.get_value())
    si.update_factor_column(con, table_name, br_14.get_name(),
                            br_14.get_value())
    si.update_factor_column(con, table_name, vol_relative_ratio_0.get_name(),
                            vol_relative_ratio_0.get_value())
    si.update_factor_column(con, table_name, vol_relative_ratio_1.get_name(),
                            vol_relative_ratio_1.get_value())
    si.update_factor_column(con, table_name, obv_0.get_name(),
                            obv_0.get_value())
    si.update_factor_column(con, table_name, obv_1.get_name(),
                            obv_1.get_value())
    si.update_factor_column(con, table_name, vma.get_name(), vma.get_value())
    si.update_factor_column(con, table_name, pvt_0.get_name(),
                            pvt_0.get_value())
    si.update_factor_column(con, table_name, pvt_1.get_name(),
                            pvt_1.get_value())
    si.update_factor_column(con, table_name, mfi.get_name(), mfi.get_value())
Example #4
0
def update_motive_factors(code_date_series):
    con = pymysql.connect(host="localhost", user="******", passwd="mysql", db="stockvision")
    table_name = "motive_factor"
    pool = si.get_all_codes(con)

    macd = Factor("macd", [])
    cci = Factor("cci", [])
    roc = Factor("roc", [])
    trix = Factor("trix", [])
    rsi = Factor("rsi", [])
    dpo = Factor("dpo", [])
    ma_close_dis = Factor("ma_close_dis", [])
    ma_ma_dis = Factor("ma_ma_dis", [])
    bias = Factor("bias", [])
    for code in pool:
        close_series = si.get_data_series(con, "daily", "close", code, code_date_series[code])
        high_series = si.get_data_series(con, "daily", "high", code, code_date_series[code])
        low_series = si.get_data_series(con, "daily", "low", code, code_date_series[code])

        # 保证输入不为空
        if len(close_series) > 0:
            macd_list = cal_macd(close_series)
            print(macd_list, end=',')
            print(code + " " + macd.get_name())
            macd.append_value(si.get_column_tuple_list(code, code_date_series[code], macd_list))

        if len(close_series) >= 5:
            ma_close_dis_list = cal_ma_close_dis(close_series, 5)
            print(macd_list, end=',')
            print(code + " " + ma_close_dis.get_name())
            ma_close_dis.append_value(si.get_column_tuple_list(code, code_date_series[code], ma_close_dis_list))

        if len(close_series) >= 6:
            bias_list = cal_bias_n(close_series, 6)
            bias.append_value(si.get_column_tuple_list(code, code_date_series[code], bias_list))

        if len(close_series) >= 10:
            # dpo_list = cal_dpo(close_series, 10)
            # dpo.append_value(si.get_column_tuple_list(code, code_date_series[code], dpo_list))

            ma_ma_dis_list = cal_ma_ma_dis(close_series, 5, 10)
            ma_ma_dis.append_value(si.get_column_tuple_list(code, code_date_series[code], ma_ma_dis_list))

        if len(close_series) >= 12:
            trix_list = cal_trix(close_series, 12)
            trix.append_value(si.get_column_tuple_list(code, code_date_series[code], trix_list))

            roc_list = cal_roc_n(close_series, 12)
            roc.append_value(si.get_column_tuple_list(code, code_date_series[code], roc_list))

        if len(close_series) >= 14:
            rsi_list = cal_rsi(close_series, 14)
            rsi.append_value(si.get_column_tuple_list(code, code_date_series[code], rsi_list))

            cci_list = cal_cci_n(close_series, high_series, low_series, 14)
            cci.append_value(si.get_column_tuple_list(code, code_date_series[code], cci_list))

    si.update_factor_column(con, table_name, macd.get_name(), macd.get_value())
    si.update_factor_column(con, table_name, cci.get_name(), cci.get_value())
    si.update_factor_column(con, table_name, roc.get_name(), roc.get_value())
    si.update_factor_column(con, table_name, trix.get_name(), trix.get_value())
    si.update_factor_column(con, table_name, rsi.get_name(), rsi.get_value())
    # si.update_factor_column(con, table_name, dpo.get_name(), dpo.get_value())
    si.update_factor_column(con, table_name, ma_close_dis.get_name(), ma_close_dis.get_value())
    si.update_factor_column(con, table_name, ma_ma_dis.get_name(), ma_ma_dis.get_value())
    si.update_factor_column(con, table_name, bias.get_name(), bias.get_value())
Example #5
0
def update_fluctuate_factor(code_date_series):
    con = pymysql.connect(host="localhost", user="******", passwd="mysql", db="stockvision")
    table_name = "fluctuate_factor"
    pool = si.get_all_codes(con)

    amplitude = Factor("amplitude", [])
    amplitude_10 = Factor("amplitude_10", [])
    price_efficiency = Factor("price_efficiency", [])
    price_efficiency_10 = Factor("price_efficiency_10", [])
    price_center = Factor("price_center", [])
    avg_daily_amplitude = Factor("avg_daily_amplitude", [])
    atr = Factor("atr", [])
    boll_upperband = Factor("boll_upperband", [])
    boll_middleband = Factor("boll_middleband", [])
    boll_lowerband = Factor("boll_lowerband", [])

    # 更新因子数据
    for code in pool:
        # 获取基础数据
        close_series = si.get_data_series(con, "daily", "close", code, code_date_series[code])
        high_series = si.get_data_series(con, "daily", "high", code, code_date_series[code])
        low_series = si.get_data_series(con, "daily", "low", code, code_date_series[code])

        # 避免时间序列不够长无法进行计算
        if len(close_series) >= 5:
            # 得到因子
            amplitude_list = cal_amplitude_n(close_series, high_series, low_series, 5)
            amplitude.append_value(si.get_column_tuple_list(code, code_date_series[code], amplitude_list))

            price_efficiency_list = cal_price_efficiency_n(close_series, 5)
            price_efficiency.append_value(si.get_column_tuple_list(code, code_date_series[code], price_efficiency_list))

            price_center_list = cal_price_center_n_ma(close_series, 3, 5)
            price_center.append_value(si.get_column_tuple_list(code, code_date_series[code], price_center_list))

            avg_daily_amplitude_list = cal_avg_daily_amplitude_n(close_series, high_series, low_series, 5)
            avg_daily_amplitude.append_value(
                si.get_column_tuple_list(code, code_date_series[code], avg_daily_amplitude_list))

            atr_list = cal_atr(high_series, low_series, close_series, 5)
            atr.append_value(si.get_column_tuple_list(code, code_date_series[code], atr_list))

        if len(close_series) >= 10:
            amplitude_10_list = cal_amplitude_n(close_series, high_series, low_series, 10)
            amplitude_10.append_value(si.get_column_tuple_list(code, code_date_series[code], amplitude_10_list))

            price_efficiency_10_list = cal_price_efficiency_n(close_series, 10)
            price_efficiency_10.append_value(
                si.get_column_tuple_list(code, code_date_series[code], price_efficiency_10_list))

        if len(close_series) >= 20:
            boll_upperband_list, boll_middleband_list, boll_lowerband_list = cal_boll(close_series, 20)
            boll_upperband.append_value(si.get_column_tuple_list(code, code_date_series[code], boll_upperband_list))
            boll_middleband.append_value(si.get_column_tuple_list(code, code_date_series[code], boll_middleband_list))
            boll_lowerband.append_value(si.get_column_tuple_list(code, code_date_series[code], boll_lowerband_list))

    # 更新数据库中因子
    si.update_factor_column(con, table_name, amplitude.get_name(), amplitude.get_value())
    si.update_factor_column(con, table_name, amplitude_10.get_name(), amplitude_10.get_value())
    si.update_factor_column(con, table_name, price_efficiency.get_name(), price_efficiency.get_value())
    si.update_factor_column(con, table_name, price_efficiency_10.get_name(), price_efficiency_10.get_value())
    si.update_factor_column(con, table_name, price_center.get_name(), price_center.get_value())
    si.update_factor_column(con, table_name, avg_daily_amplitude.get_name(), avg_daily_amplitude.get_value())
    si.update_factor_column(con, table_name, atr.get_name(), atr.get_value())
    si.update_factor_column(con, table_name, boll_upperband.get_name(), boll_upperband.get_value())
    si.update_factor_column(con, table_name, boll_middleband.get_name(), boll_middleband.get_value())
    si.update_factor_column(con, table_name, boll_lowerband.get_name(), boll_lowerband.get_value())