def cal_dcf(self, fcf: Decimal, year: int, date: str) -> Decimal: fcf = decimal_utils.none_to_zero(fcf) if fcf <= 0: return Decimal(0), Decimal(0) inc_map: list = self._get_arr_by_name('inc_rate', date) dr_map: list = self._get_arr_by_name('discount_rate', date) mv = fcf inc_rate = 0.07 discount_rate = 0.1 # 计算10年的 for i in range(0, 10): y = year + i + 1 if y in inc_map: inc_rate = inc_map[y].value if y in dr_map: discount_rate = dr_map[y].value fcf = decimal_utils.div(decimal_utils.mul(fcf, (1 + inc_rate)), (1 + discount_rate)) if fcf < 0: fcf = 0 mv = decimal_utils.add(mv, fcf) inc_rate = inc_map[9999].value discount_rate = dr_map[9999].value mv_forever = decimal_utils.add(mv, decimal_utils.div( decimal_utils.mul(fcf, (1 + inc_rate)), (discount_rate - inc_rate))) return mv, mv_forever
def fetch_daily_basic(self, ts_code: str = None, end_date: str = None, start_date: str = None, trade_date: str = None) -> DataFrame: """ https://tushare.pro/document/2?doc_id=32 :param ts_code: 股票编码 :param end_date: 起始日期 :param start_date: 结束日期 :param trade_date: 交易日期 :return: """ df: DataFrame = self.__pro.daily_basic(ts_code=ts_code, start_date=start_date, end_date=end_date, trade_date=trade_date) if not df.empty: # 按日期升序 df = df.sort_values(by='trade_date') df.loc[:, 'turnover_rate'] = df.apply(lambda row: decimal_utils.none_to_zero(row.turnover_rate), axis=1) df.loc[:, 'volume_ratio'] = df.apply(lambda row: decimal_utils.none_to_zero(row.volume_ratio), axis=1) # 替换掉所有需要是0的之后,取最近的值填充 df = df.ffill() df.loc[:, 'total_share'] = df.apply(lambda row: decimal_utils.mul(row.total_share, 10000, err_default=None), axis=1) df.loc[:, 'float_share'] = df.apply(lambda row: decimal_utils.mul(row.float_share, 10000, err_default=None), axis=1) df.loc[:, 'free_share'] = df.apply(lambda row: decimal_utils.mul(row.free_share, 10000, err_default=None), axis=1) df.loc[:, 'free_share'] = df.apply(lambda row: row.float_share if row.free_share is None or math.isnan(row.free_share) else row.free_share, axis=1) df.loc[:, 'total_mv'] = df.apply(lambda row: decimal_utils.mul(row.total_mv, 10000, err_default=None), axis=1) return df
def fetch_forecast(self, ts_code: str = None, end_date: str = None, start_date: str = None, ann_date: str = None, period: str = None) -> DataFrame: """ https://tushare.pro/document/2?doc_id=45 :param ts_code: 股票编码 :param end_date: 起始日期 :param start_date: 结束日期 :param ann_date: 公告日期 :param period: 报告期 :return: """ df: DataFrame = self.__pro.forecast_vip(ts_code=ts_code, period=period, start_date=start_date, end_date=end_date, ann_date=ann_date) df = df.where(pandas.notnull(df), None) if not df.empty: df.loc[:, 'net_profit_min'] = df.apply(lambda row: decimal_utils.mul(row.net_profit_min, 10000, err_default=None), axis=1) df.loc[:, 'net_profit_max'] = df.apply(lambda row: decimal_utils.mul(row.net_profit_max, 10000, err_default=None), axis=1) df.loc[:, 'last_parent_net'] = df.apply(lambda row: decimal_utils.mul(row.last_parent_net, 10000, err_default=None), axis=1) self.fix_ann_date_with_list_date(df, 'ann_date') df.loc[:, 'mq_ann_date'] = df.apply(lambda row: row.ann_date, axis=1) return df
def extract_from_dividend(result_list: list, store: MqQuarterStore, period_dict: dict, d: TsDividend): # 有可能有非固定分红的派钱 lp: str = date_utils.latest_period_date(d.end_date) update_date: str = date_utils.format_delta(d.imp_ann_date, -1) call_add_nx = partial(add_nx, ts_code=d.ts_code, period=lp, update_date=update_date, report_type=mq_report_type.report, store=store, result_list=result_list, period_dict=period_dict) # 叠加到最近一个季度的分红中 dividend: Decimal = decimal_utils.mul(d.cash_div_tax, d.base_share) if lp != d.end_date: ori_d: MqQuarterMetric = store.find_period_latest( ts_code=d.ts_code, period=lp, update_date=update_date, name=mq_quarter_metric_enum.dividend.name) if ori_d is not None: dividend = dividend + ori_d.value call_add_nx(name='dividend', value=dividend)
def update_adj(self, adj_type: str, latest_adj: str): """ 更新到想要的复权状态 :param adj_type: 复权类型 qfq-前复权 hfq-后复权 其他-不复权 :param latest_adj: 最新复权因子 :return: """ div_factor = 1 if self.adj_type == 'qfq' and not decimal_utils.equals( self.adj, self.latest_adj): div_factor = decimal_utils.div(self.adj, self.latest_adj) elif self.adj_type == 'hfq': div_factor = self.adj mul_factor = 1 if adj_type == 'qfq' and not decimal_utils.equals( self.adj, latest_adj): mul_factor = decimal_utils.div(self.adj, latest_adj) elif adj_type == 'hfq': mul_factor = self.adj factor = 1 if decimal_utils.equals(mul_factor, div_factor) else decimal_utils.div( mul_factor, div_factor) if factor == 1: return self.open = decimal_utils.mul(self.open, factor) self.high = decimal_utils.mul(self.high, factor) self.low = decimal_utils.mul(self.low, factor) self.close = decimal_utils.mul(self.close, factor) self.up_limit = decimal_utils.mul(self.up_limit, factor) self.down_limit = decimal_utils.mul(self.down_limit, factor) self.pre_close = decimal_utils.mul(self.pre_close, factor)
def multiply(i1: MqQuarterMetric, i2: MqQuarterMetric, name: str) -> MqQuarterMetric: if 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.mul(i1.value, i2.value))
def __init__(self, ts_code: str, trade_date: str, is_trade: int, adj_type: str, adj: decimal, latest_adj: decimal, open_price: decimal, high: decimal, low: decimal, close: decimal, up_limit: decimal, down_limit: decimal, pre_close: decimal): factor = 1 if adj_type == 'qfq' and not decimal_utils.equals(adj, latest_adj): factor = decimal_utils.div(adj, latest_adj) elif adj_type == 'hfq': factor = adj self.ts_code = ts_code self.trade_date = trade_date self.is_trade = is_trade self.adj_type = adj_type self.adj = adj self.latest_adj = latest_adj self.open = decimal_utils.mul(open_price, factor) self.high = decimal_utils.mul(high, factor) self.low = decimal_utils.mul(low, factor) self.close = decimal_utils.mul(close, factor) self.up_limit = decimal_utils.mul(up_limit, factor) self.down_limit = decimal_utils.mul(down_limit, factor) self.pre_close = decimal_utils.mul(pre_close, factor)
def cal(daily_store: mq_daily_store.MqDailyStore, quarter_store: mq_quarter_store.MqQuarterStore, ts_code: str, update_date: str) -> MqDailyMetric: daily_find = partial(daily_store.find_date_exact, ts_code=ts_code, update_date=update_date) quarter_find = partial(quarter_store.find_latest, ts_code=ts_code, update_date=update_date) score = -1 period = '00000000' dividend_yields = daily_find( name=mq_daily_metric_enum.dividend_yields.name) risk_point = quarter_find(name=mq_quarter_metric_enum.risk_point.name) revenue_quarter = quarter_find( name=mq_quarter_metric_enum.revenue_quarter.name) dprofit_quarter = quarter_find( name=mq_quarter_metric_enum.dprofit_quarter.name) if dividend_yields is None or \ calculate.gt(risk_point, 0, 'value', True) or \ calculate.lt(dividend_yields, 0.03, 'value', True) or \ not earn_and_dividend_in_year(quarter_store, ts_code, dividend_yields.period, update_date, 5) or \ not earn_in_period(quarter_store, ts_code, dividend_yields.period, update_date, 4) or \ calculate.lt(revenue_quarter, max_desc_yoy, 'yoy', True) or \ calculate.lt(revenue_quarter, dprofit_quarter, 'yoy', True): score = -1 else: period = dividend_yields.period pe = daily_find(name=mq_daily_metric_enum.pe.name) pb = daily_find(name=mq_daily_metric_enum.pb.name) dividend_score = decimal_utils.mul(dividend_yields.value, Decimal(1000)) # * 100 / 10 * 100 pe_score = decimal_utils.valid_score((1 - decimal_utils.div( calculate.get_val(pe, 'value', max_pe), max_pe, err_default=0)) * 100) pb_score = decimal_utils.valid_score((1 - decimal_utils.div( calculate.get_val(pb, 'value', max_pb), max_pb, err_default=0)) * 100) pepb_score = decimal_utils.valid_score((1 - decimal_utils.div( decimal_utils.mul(calculate.get_val(pe, 'value', max_pe), calculate.get_val(pb, 'value', max_pb)), max_pepb)) * 100) profit_yoy_score = history_profit_yoy_score(quarter_store, ts_code, dividend_yields.period, update_date, 5) dividend_yoy_score = history_dividend_yoy_score( quarter_store, ts_code, dividend_yields.period, update_date, 5) if profit_yoy_score == 0 or dividend_yoy_score == 0: score = 0 elif pe_score == 0 and pb_score == 0 and pepb_score == 0: score = 0 else: score = decimal_utils.add( decimal_utils.mul(dividend_score, 0.3), decimal_utils.mul(dividend_yoy_score, 0.2), decimal_utils.mul((pe_score + pb_score + pepb_score), 0.1), decimal_utils.mul(profit_yoy_score, 0.2)) val_score_metric = MqDailyMetric(ts_code=ts_code, report_type=mq_report_type.mq_predict, period=period, update_date=update_date, name=mq_daily_metric_enum.val_score.name, value=decimal_utils.valid_score(score)) return [val_score_metric]