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_pcf_ratio_3(self): stockcode = '000002.XSHE' quarter_metrics = _get_quarter_metrics(stockcode) year_quarters_map = strategy_quarter_index_on_year(quarter_metrics) stockcode_without_suffix = stockcode.split(".")[0] market_caps = _day_metrics(stockcode_without_suffix) four_straight_quarter_cash_flow = _four_straight_quarter( quarter_metrics, 'cash') four_straight_quarter_increment_cash_flow = _four_straight_increment_quarter( four_straight_quarter_cash_flow, 'cash') pcf_ratio_3_dict = { 'TRD_DATE': [], 'pcf_ratio_3_recal': [], 'TCAP_1': [] } print(four_straight_quarter_cash_flow) print(four_straight_quarter_increment_cash_flow) for index, record in enumerate(market_caps): recal_four_straight_indicator( record, four_straight_quarter_increment_cash_flow, year_quarters_map, 'pcf_ratio_3', ['cash', 'cash_equivalent']) pcf_ratio_3_dict['TRD_DATE'].append(record['tradedate']) pcf_ratio_3_dict['pcf_ratio_3_recal'].append(record['pcf_ratio_3']) pcf_ratio_3_dict['TCAP_1'].append(record['market_cap']) calc_df = DataFrame(pcf_ratio_3_dict) calc_df.to_csv( '~/Desktop/eod_indicator_identify/000002_pcf_ratio_3_identify.csv')
def test_pb_ratio(self): stockcode = '000001.XSHE' quarter_metrics = _get_quarter_metrics(stockcode) year_quarters_map = strategy_quarter_index_on_year(quarter_metrics) stockcode_without_suffix = stockcode.split(".")[0] market_caps = _day_metrics(stockcode_without_suffix) pb_ratio_dict = {'TRD_DATE': [], 'pb_ratio_recal': [], 'pb_ratio_manual': [], 'TCAP_1': [], 'isdifference': []} manual_df = read_csv('~/Desktop/recal_manual/000001_pb_ratio.csv') for index, record in enumerate(market_caps): recal_latest_quarter_indicator(record, quarter_metrics, year_quarters_map, 'pb_ratio', ['current_assets']) pb_ratio_manual = float(manual_df['pb_ratio'][index]) if math.isnan(pb_ratio_manual): pb_ratio_manual = None else: pb_ratio_manual = Decimal(manual_df['pb_ratio'][index]).quantize(Decimal('0.0000')) if record['pb_ratio'] == pb_ratio_manual: record['isdifference'] = "" else: record['isdifference'] = "******" pb_ratio_dict['TRD_DATE'].append(record['tradedate']) pb_ratio_dict['pb_ratio_recal'].append(record['pb_ratio']) pb_ratio_dict['pb_ratio_manual'].append(pb_ratio_manual) pb_ratio_dict['TCAP_1'].append(record['market_cap']) pb_ratio_dict['isdifference'].append(record['isdifference']) calc_df = DataFrame(pb_ratio_dict) calc_df.to_csv('~/Desktop/eod_indicator_identify/000001_pb_ratio_identify.csv') self.assertEqual(manual_df['pb_ratio'].equals(calc_df['pb_ratio_recal']), True)
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_day_metrics(order_book_id): return _day_metrics(order_book_id)