Exemplo n.º 1
0
def calc_and_save_stock_bias_quantile():
    observation_period = 250
    win_lens = [20, 60, 120]

    session = DBSession()

    sec_codes = get_sse50_stk_codes()

    for sec_code in sec_codes:
        bias_df = get_bias_eod(sec_code, is_index=False)
        bias_df = bias_df.iloc[-observation_period:].copy()  # 观察期内所有bias。 注意,sort会改变原来的值
        last_bias_quantile_dict = dict()
        last_trade_date = bias_df.index[-1]
        for win_len in win_lens:
            bias_df['value_%d' % win_len] = bias_df['value_%d' % win_len].abs()  # bias全部转成 非负数
            last_bias = bias_df['value_%d' % win_len][-1]
            bias_arr = bias_df['value_%d' % win_len].values
            bias_arr.sort()  # 注意,会改变bias_arr本来的内容
            if np.isnan(last_bias):
                raise ValueError('最新数据是nan,检查数据')
            last_bias_idx = np.argwhere(bias_arr == last_bias)[0][0]
            last_bias_quantile = last_bias_idx / observation_period
            last_bias_quantile_dict[win_len] = last_bias_quantile  # 分位数

        new_obj = AShareBiasQuantile()
        new_obj.sec_code = sec_code
        new_obj.trade_date = last_trade_date
        new_obj.value_20 = trans_number_to_float(last_bias_quantile_dict[20])
        new_obj.value_60 = trans_number_to_float(last_bias_quantile_dict[60])
        new_obj.value_120 = trans_number_to_float(last_bias_quantile_dict[120])
        new_obj.observation_period = observation_period
        session.add(new_obj)

    session.commit()
    session.close()
Exemplo n.º 2
0
def calc_and_save_rolling_alpha(daily_alpha_df):
    # 注意:这里修改窗口长度
    alpha_rolling_unit_value_df_20 = (1 + daily_alpha_df).rolling(20).apply(
        calc_prod, raw=True)
    alpha_rolling_unit_value_df_60 = (1 + daily_alpha_df).rolling(60).apply(
        calc_prod, raw=True)
    # save_df_into_db(alpha_rolling_unit_value_df, AShareAlpha)

    df_20 = alpha_rolling_unit_value_df_20
    mat_20 = df_20.values
    mat_60 = alpha_rolling_unit_value_df_60.values
    trade_dates = df_20.index
    sec_codes = df_20.columns
    session = DBSession()

    for i in range(len(trade_dates)):
        for j in range(len(sec_codes)):
            trade_date = trade_dates[i]
            sec_code = sec_codes[j]

            new_obj = AShareAlpha()
            new_obj.sec_code = sec_code
            new_obj.trade_date = trade_date
            new_obj.value_20 = trans_number_to_float(mat_20[i, j])
            new_obj.value_60 = trans_number_to_float(mat_60[i, j])

            session.add(new_obj)

    session.commit()
    session.close()
Exemplo n.º 3
0
def calc_and_save_alpha_quantile():
    observation_period = 250
    win_len = 20

    session = DBSession()

    sec_codes = get_sse50_stk_codes()

    for sec_code in sec_codes:
        value_df = get_a_share_alpha(sec_code)
        value_df = value_df.iloc[-observation_period:].copy()  # 注意,sort会改变原来的值
        last_trade_date = value_df.index[-1]
        last_value = value_df['value_%d' % win_len][-1]
        value_arr = value_df['value_%d' % win_len].values
        value_arr = np.array([x for x in value_arr if x > 1e-6])
        value_arr.sort()  # 注意,会改变bias_arr本来的内容
        if np.isnan(last_value):
            raise ValueError('最新数据是nan,检查数据')
        last_value_idx = np.argwhere(value_arr == last_value)[0][0]
        last_value_quantile = last_value_idx / observation_period

        new_obj = AShareAlphaQuantile()
        new_obj.sec_code = sec_code
        new_obj.trade_date = last_trade_date
        new_obj.value_20 = trans_number_to_float(last_value_quantile)
        new_obj.observation_period = observation_period
        session.add(new_obj)

    session.commit()
    session.close()
Exemplo n.º 4
0
def calc_and_save_stock_pb_quantile():
    observation_period = 750

    session = DBSession()

    sec_codes = get_sse50_stk_codes()

    for sec_code in sec_codes:
        value_df = get_a_share_fin_pit(sec_code)
        value_df = value_df.iloc[-observation_period:].copy()  # sort会改变原来的值
        last_trade_date = value_df.index[-1]
        last_value = value_df['pb'][-1]
        value_arr = value_df['pb'].values
        value_arr.sort()  # 注意,sort会改变bias_arr本来的内容
        if np.isnan(last_value):
            raise ValueError('最新数据是nan,检查数据')
        last_value_idx = np.argwhere(value_arr == last_value)[0][0]
        last_value_quantile = last_value_idx / observation_period

        new_obj = ASharePbQuantile()
        new_obj.sec_code = sec_code
        new_obj.trade_date = last_trade_date
        new_obj.val = trans_number_to_float(last_value_quantile)
        new_obj.observation_period = observation_period
        session.add(new_obj)
    session.commit()
    session.close()
Exemplo n.º 5
0
def init_a_share_eod_prices():
    session = DBSession()

    years = [2016, 2017, 2018, 2019]
    close_dfs = []
    pct_chg_dfs = []
    for year in years:
        close_df = pd.read_csv('csv/{year}_close.csv'.format(year=year),
                               index_col='Unnamed: 0')
        close_dfs.append(close_df)
        pct_chg_df = pd.read_csv('csv/{year}_pct_chg.csv'.format(year=year),
                                 index_col='Unnamed: 0')
        pct_chg_dfs.append(pct_chg_df)

    close_df = pd.concat(close_dfs)
    pct_chg_df = pd.concat(pct_chg_dfs)

    adjfactor_df = pd.read_csv('csv/cpnt_adjfactor.csv', index_col='DateTime')
    sec_codes = close_df.columns
    close_df.index = pd.to_datetime(close_df.index)
    trade_dates = close_df.index.date
    close_mat = close_df.values
    pct_chg_mat = pct_chg_df.values
    adjfactor_mat = adjfactor_df.values

    for i in range(len(trade_dates)):
        for j in range(len(sec_codes)):
            trade_date = trade_dates[i]
            sec_code = sec_codes[j]

            new_obj = AShareEodPrice()
            new_obj.sec_code = sec_code
            new_obj.trade_date = trade_date
            new_obj.close = trans_number_to_float(close_mat[i, j])
            new_obj.pct_chg = trans_number_to_float(pct_chg_mat[i, j])
            new_obj.adjfactor = trans_number_to_float(adjfactor_mat[i, j])

            session.add(new_obj)

    session.commit()
    session.close()
Exemplo n.º 6
0
def init_a_share_fin_pit():
    session = DBSession()

    years = [2016, 2017, 2018, 2019]
    for year in years:

        fa_bps_df = pd.read_csv('csv/{year}_fa_bps.csv'.format(year=year),
                                index_col='Unnamed: 0')
        pe_ttm_df = pd.read_csv('csv/{year}_pe_ttm.csv'.format(year=year),
                                index_col='Unnamed: 0')
        close_df = pd.read_csv('csv/{year}_close.csv'.format(year=year),
                               index_col='Unnamed: 0')
        pb_df = close_df / fa_bps_df

        sec_codes = close_df.columns
        close_df.index = pd.to_datetime(close_df.index)
        trade_dates = close_df.index.date
        pb_mat = pb_df.values
        pe_ttm_mat = pe_ttm_df.values
        fa_bps_mat = fa_bps_df.values

        for i in range(len(trade_dates)):
            for j in range(len(sec_codes)):
                trade_date = trade_dates[i]
                sec_code = sec_codes[j]

                new_obj = AShareFinPit()
                new_obj.sec_code = sec_code
                new_obj.trade_date = trade_date
                new_obj.fa_bps = trans_number_to_float(fa_bps_mat[i, j])
                new_obj.pe_ttm = trans_number_to_float(pe_ttm_mat[i, j])
                new_obj.pb = trans_number_to_float(pb_mat[i, j])

                session.add(new_obj)

        session.commit()
        session.close()
Exemplo n.º 7
0
def save_df_into_db(df, clazz):
    mat = df.values
    trade_dates = df.index
    sec_codes = df.columns
    session = DBSession()

    for i in range(len(trade_dates)):
        for j in range(len(sec_codes)):
            trade_date = trade_dates[i]
            sec_code = sec_codes[j]

            new_obj = clazz()
            new_obj.sec_code = sec_code
            new_obj.trade_date = trade_date
            new_obj.value_20 = trans_number_to_float(mat[i, j])

            session.add(new_obj)

    session.commit()
    session.close()
Exemplo n.º 8
0
def save_indicators(indicators):
    session = DBSession()
    new_obj = AShareTalib()
    new_obj.sec_code = indicators['sec_code']
    new_obj.trade_date = indicators['trade_date']
    new_obj.freq = 'D'

    # ma, ema
    new_obj.ma5 = trans_number_to_float(indicators['ma'][0])
    new_obj.ma10 = trans_number_to_float(indicators['ma'][1])
    new_obj.ma20 = trans_number_to_float(indicators['ma'][2])
    new_obj.ma60 = trans_number_to_float(indicators['ma'][3])
    new_obj.ma120 = trans_number_to_float(indicators['ma'][4])

    new_obj.ema5 = trans_number_to_float(indicators['ema'][0])
    new_obj.ema10 = trans_number_to_float(indicators['ema'][1])
    new_obj.ema20 = trans_number_to_float(indicators['ema'][2])
    new_obj.ema60 = trans_number_to_float(indicators['ema'][3])
    new_obj.ema120 = trans_number_to_float(indicators['ema'][4])

    # macd, rsi, stoch, adx, cci, stochrsi, uo, roc, sar
    new_obj.macd = trans_number_to_float(indicators['macd'])
    new_obj.rsi = trans_number_to_float(indicators['rsi'])
    new_obj.cci = trans_number_to_float(indicators['cci'])
    new_obj.adx = trans_number_to_float(indicators['adx'])
    new_obj.stochrsi = trans_number_to_float(indicators['stochrsi'])
    new_obj.stoch = trans_number_to_float(indicators['stoch'])
    new_obj.uo = trans_number_to_float(indicators['uo'])
    new_obj.roc = trans_number_to_float(indicators['roc'])
    new_obj.sar = trans_number_to_float(indicators['sar'])

    session.add(new_obj)
    session.commit()
    session.close()