Пример #1
0
def get_trading_time(target_list: 'list',
                     frequency: 'str',
                     fre_num: 'int',
                     begin_date,
                     end_date=''):
    if isinstance(target_list, str):
        target_list = [target_list]

    new_targets = todictmc(target_list)
    new_begin_date = convert_str_or_datetime_to_int_date(begin_date)
    if isinstance(end_date, str) and end_date.strip() == '':
        new_end_date = to_int_now_date('%Y%m%d')
    else:
        new_end_date = convert_str_or_datetime_to_int_date(end_date)

    if frequency in ('day', 'week', 'month') and fre_num != 1:
        raise ValueError(text.ERROR_INPUT_FREQUENCY_FREQNUM)

    if 0 < new_end_date < new_begin_date:
        raise ValueError(text.ERROR_INPUT_BEGIN_GT_ENDDATE)

    t, d = _history.get_trading_time(new_targets, frequency, new_begin_date,
                                     new_end_date, fre_num)

    if gv.freuency_to_int(frequency) >= gv.KFreq_Day:
        d = np.arange(0, t.size, 1, dtype=np.int)
    else:
        d -= 1

    return history_cvt.convert_trading_time_to_df(t, d)
Пример #2
0
def get_factor_by_code(factor_list: 'list', target: 'str', begin_date,
                       end_date):
    if isinstance(factor_list, str):
        factor_list = [factor_list]

    targets = check_target(todictmc([target]))
    new_begin_date = convert_str_or_datetime_to_int_date(begin_date)
    new_end_date = convert_str_or_datetime_to_int_date(end_date)
    if begin_date != 0 and new_begin_date > new_end_date:
        raise ValueError(text.ERROR_INPUT_BEGIN_GT_ENDDATE)
    check_begin_end_date(new_begin_date, new_end_date)
    factor_dict = _factors.get_factor_by_code(factor_list, targets,
                                              new_begin_date, new_end_date)
    factor_df = factor_cvt.convert_factor_by_code_to_df(
        factor_dict) if factor_dict is not None else None
    return factor_df
Пример #3
0
def get_factor_by_factor(factor: 'str', target_list: 'list', begin_date,
                         end_date):
    if isinstance(target_list, str):
        target_list = [target_list]

    targets = check_target(todictmc(target_list))
    new_begin_date = convert_str_or_datetime_to_int_date(begin_date)
    new_end_date = convert_str_or_datetime_to_int_date(end_date)
    if begin_date != 0 and new_begin_date > new_end_date:
        raise ValueError(text.ERROR_INPUT_BEGIN_GT_ENDDATE)
    check_begin_end_date(new_begin_date, new_end_date)
    factor_dict = _factors.get_factor_by_factor(factor, targets,
                                                new_begin_date, new_end_date)
    result_df = factor_cvt.convert_factor_by_factor_to_df(
        factor_dict,
        todotmc(targets)) if factor_dict['date'].size > 0 else None
    return result_df
Пример #4
0
def get_kdata_n(target_list: 'list',
                frequency: 'str',
                fre_num: 'int',
                n: 'int',
                end_date: 'str',
                fq=0,
                fill_up=False,
                df=False,
                sort_by_date=False):
    if isinstance(target_list, str):
        target_list = [target_list]

    if len(target_list) < 1:
        raise ValueError(
            text.ERROR_INPUT_EMPTY_PARAM.format(PARAMNAME='target_list'))

    if isinstance(end_date, str) and end_date.strip() == '':
        end_date = to_int_now_date('%Y%m%d')
    else:
        end_date = convert_str_or_datetime_to_int_date(end_date)

    new_targets = todictmc(target_list)
    fq = convert_internal_fq_to_atcore(fq)

    if frequency in ('day', 'week', 'month') and fre_num != 1:
        raise ValueError(text.ERROR_INPUT_FREQUENCY_FREQNUM)

    results = [None] * len(new_targets)
    for idx, target in enumerate(new_targets):
        kdata = _history.get_k_data_n([target], frequency, fre_num, n,
                                      end_date, fill_up, fq)
        results[idx] = history_cvt.convert_k_data_to_df(
            kdata[0], target_list[idx],
            cnt.env.get_target_type(todotmc(target)))

    if df:
        data_df = pd.concat(results, ignore_index=True)  # type: pd.DataFrame
        if sort_by_date:
            data_df = data_df.sort_values(
                'time', ascending=True,
                na_position='first')  # type: pd.DataFrame
            data_df.index = range(data_df.shape[0])
        return data_df
    else:
        return {target: results[idx] for idx, target in enumerate(target_list)}
Пример #5
0
def get_kdata(target_list: 'list',
              frequency: 'str',
              fre_num: 'int',
              begin_date,
              end_date,
              fq=enums.FQ_NA,
              fill_up=False,
              df=False,
              sort_by_date=False):
    if isinstance(target_list, str):
        target_list = [target_list]

    if len(target_list) < 1:
        raise ValueError(
            text.ERROR_INPUT_EMPTY_PARAM.format(PARAMNAME='target_list'))

    begin_date = convert_str_or_datetime_to_int_date(begin_date)
    end_date = convert_str_or_datetime_to_int_date(end_date)
    new_targets = todictmc(target_list)
    fq = convert_internal_fq_to_atcore(fq)

    if frequency in ('day', 'week', 'month') and fre_num != 1:
        raise ValueError(text.ERROR_INPUT_FREQUENCY_FREQNUM)

    if 0 < end_date < begin_date:
        raise ValueError(text.ERROR_INPUT_BEGIN_GT_ENDDATE)

    results = [None] * len(new_targets)
    data_list = _history.get_k_data(new_targets, frequency, fre_num,
                                    begin_date, end_date, fill_up, fq)
    for idx, data in enumerate(data_list):
        results[idx] = history_cvt.convert_k_data_to_df(
            data, target_list[idx],
            cnt.env.get_target_type(todotmc(new_targets[idx])))

    if df:
        data_df = pd.concat(results, ignore_index=True)  # type: pd.DataFrame
        if sort_by_date:
            data_df = data_df.sort_values(
                'time', ascending=True,
                na_position='first')  # type: pd.DataFrame
            data_df.index = range(data_df.shape[0])
        return data_df
    else:
        return {target: results[idx] for idx, target in enumerate(target_list)}
Пример #6
0
def get_factor_by_day(factor_list: 'list', target_list: 'list', date=''):
    if isinstance(factor_list, str):
        factor_list = [factor_list]

    if isinstance(target_list, str):
        target_list = [target_list]

    targets = check_target(todictmc(target_list))
    if date == '':
        new_date = to_int_now_date()
    else:
        new_date = convert_str_or_datetime_to_int_date(date)
    check_begin_end_date(new_date, new_date)
    factor_dict = _factors.get_factor_by_day(factor_list, targets, new_date)
    factor_df = factor_cvt.convert_factor_by_day_to_df(
        factor_dict) if factor_dict is not None else None

    return factor_df
Пример #7
0
def get_main_contract(main_code: 'str', begin_date, end_date):
    begin_date = convert_str_or_datetime_to_int_date(begin_date)
    if isinstance(end_date, str) and end_date.strip() == '':
        end_date = to_int_now_date('%Y%m%d')
    else:
        end_date = convert_str_or_datetime_to_int_date(end_date)
    new_targets = todictmc(main_code)

    if begin_date > end_date:
        raise ValueError(text.ERROR_INPUT_BEGIN_GT_ENDDATE)

    ls = _history.get_main_contract(new_targets['Market'], new_targets['Code'],
                                    begin_date, end_date)
    ls = [
        t for t in ls
        if str.lower(t['TargetIns']) != str.lower(new_targets['Code'])
    ]

    return history_cvt.convert_main_contract_to_df(ls, new_targets['Market'])
Пример #8
0
def get_history_instruments(target_list: 'list',
                            begin_date,
                            end_date='',
                            df=False,
                            sort_by_date=True):
    if isinstance(target_list, str):
        target_list = [target_list]

    targets = check_target(todictmc(target_list))
    new_begin_date = convert_str_or_datetime_to_int_date(begin_date)
    new_end_date = to_int_now_date(
    ) if end_date == '' else convert_str_or_datetime_to_int_date(end_date)
    if begin_date != 0 and new_begin_date > new_end_date:
        raise ValueError(text.ERROR_INPUT_BEGIN_GT_ENDDATE)

    data_matrix = _history.get_history_instruments(targets, new_begin_date,
                                                   new_end_date)

    return history_cvt.convert_history_instrument_matrix_to_output(
        target_list, data_matrix, df, sort_by_date)