def _cal_pe_ratio_1_by_manual(order_book_id): ret = [] raw_day_reports = _day_metrics(order_book_id) raw_quarter_reports = _quarter_metrics(order_book_id) enddate_report_map = _enddate_reports_map(raw_quarter_reports) day_df = DataFrame(raw_day_reports).fillna(numpy.nan) market_caps = day_df.market_cap.values tradedates = day_df.tradedate.values for i in range(len(tradedates)): t = int(to_datetime(tradedates[i]).strftime('%Y%m%d')) ret_value = {'tradedate': t} latest_enddates = _latest_enddates(t) report = None for latest_enddate in latest_enddates: latest_report = enddate_report_map.get(latest_enddate) if latest_report is None: continue ann_date = latest_report.get('announce_date') if t >= ann_date: report = latest_report ret_value['announce_date'] = ann_date ret_value['end_date'] = latest_enddate break if report is not None: nppc_value = report.get('net_profit_parent_company') if nppc_value is not None and nppc_value != 0: market_cap = market_caps[i] ret_value['pe_ratio_1'] = round(market_cap / nppc_value, 4) ret_value['net_profit_parent_company'] = nppc_value ret.append(ret_value) return ret
def _cal_ev_by_manual(order_book_id): ret = [] raw_day_reports = _day_metrics(order_book_id) raw_quarter_reports = _quarter_metrics(order_book_id) enddate_report_map = _enddate_reports_map(raw_quarter_reports) day_df = DataFrame(raw_day_reports).fillna(numpy.nan) tradedates = day_df.tradedate.values val_stk_rights = day_df.val_of_stk_right.values for i in range(len(tradedates)): t = int(to_datetime(tradedates[i]).strftime('%Y%m%d')) ret_value = {'tradedate': t} latest_enddates = _latest_enddates(t) report = None for latest_enddate in latest_enddates: latest_report = enddate_report_map.get(latest_enddate) if latest_report is None: continue ann_date = latest_report.get('announce_date') if t >= ann_date: report = latest_report ret_value['announce_date'] = ann_date ret_value['end_date'] = latest_enddate break ret_value['ev'] = 0 val_stk_value = val_stk_rights[i] if val_stk_value is not None: ret_value['ev'] += val_stk_value if report is not None: debt = report.get('interest_bearing_debt') if debt is not None: ret_value['ev'] += debt ret_value['interest_bearing_debt'] = debt ret.append(ret_value) return ret
def test_four_straight_quarter(self): quarter_metrics = _quarter_metrics('000001.XSHE') calc_four_straight_quarter = _four_straight_quarter(quarter_metrics, 'net_profit') calc_df = DataFrame(calc_four_straight_quarter) calc_df = calc_df[calc_df['announce_date'] <= 20160812] calc_df['four_straight_net_profit'] = calc_df.apply( lambda row: float(row['net_profit']), axis=1) manual_df = read_csv('recal_by_manual/four_straight_net_profit.csv') manual_df = manual_df[manual_df['announce_date'] <= 20160812] self.assertEqual(manual_df['four_straight_net_profit'].equals( calc_df['four_straight_net_profit']), True)
def _cal_peg_ratio_by_manual(order_book_id): ret = [] raw_quarter_reports = _quarter_metrics(order_book_id) enddate_report_map = _enddate_reports_map(raw_quarter_reports) pe_ratio_2_by_manual = _cal_four_latest_by_manual(order_book_id, 'net_profit_parent_company', 'pe_ratio_2') for i in range(len(pe_ratio_2_by_manual)): tradedate = pe_ratio_2_by_manual[i]['tradedate'] ret_value = {'tradedate': tradedate} pe_ratio_2_report = pe_ratio_2_by_manual[i] last_year = int(tradedate / 10000) - 1 latest_annual_report = enddate_report_map.get(int(str(last_year) + QUARTER_ENDDATE_MAP[4])) four_latest_report = pe_ratio_2_by_manual[i] pe_ratio_2_value = pe_ratio_2_report.get('pe_ratio_2') latest_annual_value = latest_annual_report.get('net_profit_parent_company') if latest_annual_report is not None else 0 four_latest_value = four_latest_report.get('net_profit_parent_company') if None not in (pe_ratio_2_value, latest_annual_value, four_latest_value) and latest_annual_value != 0: return_inc_g = (four_latest_value - latest_annual_value) / latest_annual_value * 100 if return_inc_g != 0: ret_value['peg_ratio'] = round(pe_ratio_2_value / return_inc_g, 4) ret.append(ret_value) return ret
def _cal_four_latest_by_manual(order_book_id, metric_name, recal_metric_name): ret = [] raw_day_reports = _day_metrics(order_book_id) raw_quarter_reports = _quarter_metrics(order_book_id) four_latest_reports = _four_latest_quarter(raw_quarter_reports, metric_name) enddate_four_latest_map = _enddate_reports_map(four_latest_reports) day_df = DataFrame(raw_day_reports).fillna(numpy.nan) market_caps = day_df.market_cap.values tradedates = day_df.tradedate.values for i in range(len(tradedates)): t = int(to_datetime(tradedates[i]).strftime('%Y%m%d')) ret_value = {'tradedate': t} latest_enddates = _latest_enddates(t) report = None for latest_enddate in latest_enddates: latest_report = enddate_four_latest_map.get(latest_enddate) if latest_report is None: continue ann_date = latest_report.get('announce_date') if t >= ann_date: report = latest_report ret_value['announce_date'] = ann_date ret_value['end_date'] = latest_enddate break if report is None: ret.append(ret_value) continue metric_value = report.get(metric_name) if metric_value is None or metric_value == 0: ret.append(ret_value) continue ret_value[metric_name] = metric_value ret_value[recal_metric_name] = round(market_caps[i] / metric_value, 4) ret.append(ret_value) return ret
def _get_quarter_metrics(order_book_id): return _quarter_metrics(order_book_id)