def extract_from_cash_flow(result_list: list, store: MqQuarterStore,
                           period_dict: dict, cf: TsCashFlow):
    update_date = date_utils.format_delta(cf.mq_ann_date, -1)
    ts_code = cf.ts_code
    period = cf.end_date
    call_find = partial(store.find_period_exact,
                        ts_code=ts_code,
                        period=period,
                        update_date=update_date)
    call_add_nx = partial(add_nx,
                          ts_code=ts_code,
                          period=period,
                          update_date=update_date,
                          report_type=mq_report_type.report if cf.report_type
                          == '1' else mq_report_type.report_adjust,
                          store=store,
                          result_list=result_list,
                          period_dict=period_dict)

    for i in mq_quarter_metric_enum.extract_from_cf_list:
        if i.is_cf_h and date_utils.get_quarter_num(cf.end_date) == 3:
            q2: MqManualMetric = store.find_period_latest(
                ts_code=cf.ts_code,
                name=i.name,
                period=date_utils.period_delta(cf.end_date, -1),
                update_date=update_date)
            if q2 is None:
                log.error('Cant find q2 in cash flow %s %s %s' %
                          (cf.ts_code, cf.end_date, cf.mq_ann_date))
                call_add_nx(name=i.name, value=getattr(cf, i.from_name))
            else:
                call_add_nx(name=i.name, value=q2.value)
        else:
            call_add_nx(name=i.name, value=getattr(cf, i.from_name))
def cal_quarter_ltm(result_list: list, store: MqQuarterStore,
                    period_o: PeriodObj, ts_code: str, update_date: str):
    period = period_o.end_date
    quarter_num = date_utils.get_quarter_num(period)
    report_type = period_o.report_type
    call_find_now = partial(store.find_period_exact,
                            ts_code=ts_code,
                            update_date=update_date)
    call_find_pre = partial(store.find_period_latest,
                            ts_code=ts_code,
                            update_date=update_date)
    call_add = partial(common_add, result_list=result_list, store=store)

    for k in mq_quarter_metric_enum.cal_quarter_list:
        name = k.name
        from_name = k.from_name
        i1 = call_find_now(period=period, name=from_name)
        i2 = call_find_pre(period=date_utils.period_delta(period, -1),
                           name=from_name)
        quarter = quarter_cal_utils.cal_quarter(name, i1, i2)
        if quarter is None:
            common_log_err(ts_code, period, report_type, update_date, name)
        else:
            call_add(to_add=quarter)

    for k in mq_quarter_metric_enum.cal_ltm_list:
        name = k.name
        from_name = k.from_name
        i1 = call_find_now(period=period, name=from_name)
        i2 = call_find_pre(period=date_utils.period_delta(period, -1),
                           name=from_name)
        i3 = call_find_pre(period=date_utils.period_delta(period, -2),
                           name=from_name)
        i4 = call_find_pre(period=date_utils.period_delta(period, -3),
                           name=from_name)

        ltm: MqQuarterMetric = quarter_cal_utils.cal_ltm_with_quarter(
            name, i1, i2, i3, i4)
        if ltm is None and quarter_num == 4:
            q_m: MqQuarterMetricEnum = mq_quarter_metric_enum.find_by_name(
                from_name)
            ltm = quarter_cal_utils.add_up(
                name, [call_find_now(period=period, name=q_m.from_name)])

        if ltm is None:
            common_log_err(ts_code, period, report_type, update_date, name)
        else:
            call_add(to_add=ltm)
Exemple #3
0
def cal_ltm_with_period(name: str, current: MqQuarterMetric,
                        last_year_q4: MqQuarterMetric,
                        last_year: MqQuarterMetric) -> MqQuarterMetric:
    '''
    用季度去计算LTM
    '''
    if current is None:
        return None

    if date_utils.get_quarter_num(current.period) == 4:
        return add_up(name, [current])
    elif last_year_q4 is None or last_year is None:
        return None
    else:
        return add_up(
            name, [current, sub_from('_', [last_year_q4, last_year])])
Exemple #4
0
def cal_quarter(name: str, i1: MqQuarterMetric,
                i2: MqQuarterMetric) -> MqQuarterMetric:
    if i1 is not None and date_utils.get_quarter_num(i1.period) == 1:
        return MqQuarterMetric(ts_code=i1.ts_code,
                               report_type=i1.report_type,
                               period=i1.period,
                               update_date=i1.update_date,
                               name=name,
                               value=i1.value)
    elif i1 is None or i2 is None:
        return None
    return MqQuarterMetric(ts_code=i1.ts_code,
                           report_type=i1.report_type | i2.report_type,
                           period=i1.period,
                           update_date=i1.update_date,
                           name=name,
                           value=decimal_utils.sub(i1.value, i2.value))
Exemple #5
0
def earn_and_dividend_in_year(quarter_store: mq_quarter_store.MqQuarterStore,
                              ts_code: str, report_period: str,
                              update_date: str, year: int) -> bool:
    quarter_num = get_quarter_num(report_period)
    period = report_period if quarter_num == 4 else period_delta(
        report_period, -quarter_num)
    period = period_delta(period, 4)
    for i in range(year):
        period = period_delta(period, -4)
        dprofit_ltm = quarter_store.find_period_latest(
            ts_code, mq_quarter_metric_enum.dprofit_ltm.name, period,
            update_date)
        dividend_ltm = quarter_store.find_period_latest(
            ts_code, mq_quarter_metric_enum.dividend_ltm.name, period,
            update_date)
        if calculate.lt(dprofit_ltm, zero, 'value', True):
            return False
        if calculate.lt(dividend_ltm, zero, 'value', True):
            return False
    return True
Exemple #6
0
def history_dividend_yoy_score(quarter_store: mq_quarter_store.MqQuarterStore,
                               ts_code: str, report_period: str,
                               update_date: str, year: int) -> bool:
    quarter_num = get_quarter_num(report_period)
    period = report_period if quarter_num == 4 else period_delta(
        report_period, -quarter_num)
    period = period_delta(period, 4)
    yoy_list = []
    for i in range(year):
        period = period_delta(period, -4)
        dividend_ltm = quarter_store.find_period_latest(
            ts_code, mq_quarter_metric_enum.dividend_ltm.name, period,
            update_date)
        yoy_list.append(calculate.get_val(dividend_ltm, 'yoy', zero))

    result = 0
    score_per_one = 100 / year
    for yoy in yoy_list:
        if yoy > 0:
            result += score_per_one
        elif yoy < 0:
            result -= score_per_one / 2
    return decimal_utils.valid_score(result)
Exemple #7
0
def cal_ltm_with_quarter(name: str, i1: MqQuarterMetric, i2: MqQuarterMetric,
                         i3: MqQuarterMetric, i4) -> MqQuarterMetric:
    '''
    用4个单季去计算LTM
    '''
    if i1 is not None and date_utils.get_quarter_num(i1.period) == 4:
        return MqQuarterMetric(ts_code=i1.ts_code,
                               report_type=i1.report_type,
                               period=i1.period,
                               update_date=i1.update_date,
                               name=name,
                               value=i1.value)
    elif i1 is None or i2 is None or i3 is None or i4 is None:
        return None
    else:
        return MqQuarterMetric(ts_code=i1.ts_code,
                               report_type=i1.report_type | i2.report_type
                               | i3.report_type | i4.report_type,
                               period=i1.period,
                               update_date=i1.update_date,
                               name=name,
                               value=decimal_utils.add(i1.value, i2.value,
                                                       i3.value, i4.value))