Beispiel #1
0
 def fetch_cash_flow(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=44
     :param ts_code: 股票编码
     :param end_date: 起始日期
     :param start_date: 结束日期
     :param ann_date: 公告日期
     :param period: 报告期
     :return:
     """
     df1 = self.__pro.cashflow_vip(report_type=1, ts_code=ts_code, period=period,
                                   start_date=start_date, end_date=end_date, ann_date=ann_date)
     df2 = self.__pro.cashflow_vip(report_type=4, ts_code=ts_code, period=period,
                                   start_date=start_date, end_date=end_date, ann_date=ann_date)
     df = df1.append(df2)
     if not df.empty:
         self.fix_ann_date_with_list_date(df, 'ann_date')
         self.fix_ann_date_with_list_date(df, 'f_ann_date')
         df.loc[:, 'mq_ann_date'] = df.apply(lambda row: mini(row.ann_date, row.f_ann_date), axis=1)
         df.loc[:, 'prov_depr_assets'] = df.apply(lambda row: decimal_utils.none_to_zero(row.prov_depr_assets),
                                                  axis=1)
         df.loc[:, 'depr_fa_coga_dpba'] = df.apply(lambda row: decimal_utils.none_to_zero(row.depr_fa_coga_dpba),
                                                   axis=1)
         df.loc[:, 'amort_intang_assets'] = df.apply(lambda row: decimal_utils.none_to_zero(row.amort_intang_assets),
                                                     axis=1)
         df.loc[:, 'lt_amort_deferred_exp'] = df.apply(
             lambda row: decimal_utils.none_to_zero(row.lt_amort_deferred_exp), axis=1)
         df.loc[:, 'loss_scr_fa'] = df.apply(lambda row: decimal_utils.none_to_zero(row.loss_scr_fa), axis=1)
     return df
Beispiel #2
0
 def fetch_income(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=33
     :param ts_code: 股票编码
     :param end_date: 起始日期
     :param start_date: 结束日期
     :param ann_date: 公告日期
     :param period: 报告期
     :return:
     """
     qpm = 30
     if not self.__rt.get('fetch_income', seconds_per_minute / qpm, qpm, 2):
         raise Exception('fetch_income %d per minute, waiting' % qpm)
     df1: DataFrame = self.__pro.income_vip(report_type=1, ts_code=ts_code, period=period,
                                            start_date=start_date, end_date=end_date, ann_date=ann_date)
     df2: DataFrame = self.__pro.income_vip(report_type=4, ts_code=ts_code, period=period,
                                            start_date=start_date, end_date=end_date, ann_date=ann_date)
     df: DataFrame = df1.append(df2)
     if not df.empty:
         self.fix_ann_date_with_list_date(df, 'ann_date')
         self.fix_ann_date_with_list_date(df, 'f_ann_date')
         df.loc[:, 'mq_ann_date'] = df.apply(lambda row: mini(row.ann_date, row.f_ann_date), axis=1)
         # 资产减值损失 - 2019Q2开始计入其他收益,应该为负数,不可转回。2019Q2之前为成本,所以需要取反
         df.loc[:, 'assets_impair_loss'] = df.apply(lambda row: decimal_utils.none_to_zero(row.assets_impair_loss),
                                                    axis=1)
         df.loc[:, 'assets_impair_loss'] = df.apply(
             lambda row: decimal_utils.negative(row.assets_impair_loss)
             if row.end_date < '20190630' or (row.end_date >= '20190630' and row.assets_impair_loss > 0)
             else row.assets_impair_loss,
             axis=1)
     return df
Beispiel #3
0
def fetch_period_report(ts_code: dict(type=str, help='股票编码'),
                        to_date: dict(type=str, help='数据要获取到哪天'),
                        to_do: dict(type=bool, help='是否要进行此次获取') = True):
    """
    按股票拉取季报相关的数据
    任何一步失败都可以退出
    """
    if not to_do:
        return False, None
    r, d1 = common_fetch_data(ts_code,
                              'fetch_income',
                              TsIncome,
                              TsIncome.ann_date,
                              TsIncome.ts_code,
                              to_date=to_date,
                              to_do=True,
                              page_size=days_of_year100)
    r, d2 = common_fetch_data(ts_code,
                              'fetch_balance_sheet',
                              TsBalanceSheet,
                              TsBalanceSheet.ann_date,
                              TsBalanceSheet.ts_code,
                              to_date=to_date,
                              to_do=r,
                              page_size=days_of_year100)
    r, d3 = common_fetch_data(ts_code,
                              'fetch_cash_flow',
                              TsCashFlow,
                              TsCashFlow.ann_date,
                              TsCashFlow.ts_code,
                              to_date=to_date,
                              to_do=r,
                              page_size=days_of_year100)
    r, d4 = common_fetch_data(ts_code,
                              'fetch_fina_indicator',
                              TsFinaIndicator,
                              TsFinaIndicator.ann_date,
                              TsFinaIndicator.ts_code,
                              to_date=to_date,
                              to_do=r,
                              page_size=days_of_year100)
    report_from_date = compare_utils.mini(d1, d2, d3, d4)
    return r, trade_date_service.get_previous_trade_date(
        report_from_date) if report_from_date is not None else None
Beispiel #4
0
 def fetch_balance_sheet(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=36
     :param ts_code: 股票编码
     :param end_date: 起始日期
     :param start_date: 结束日期
     :param ann_date: 公告日期
     :param period: 报告期
     :return:
     """
     df1 = self.__pro.balancesheet_vip(report_type=1, ts_code=ts_code, period=period,
                                       start_date=start_date, end_date=end_date, ann_date=ann_date)
     df2 = self.__pro.balancesheet_vip(report_type=4, ts_code=ts_code, period=period,
                                       start_date=start_date, end_date=end_date, ann_date=ann_date)
     df = df1.append(df2)
     if not df.empty:
         self.fix_ann_date_with_list_date(df, 'ann_date')
         self.fix_ann_date_with_list_date(df, 'f_ann_date')
         df.loc[:, 'mq_ann_date'] = df.apply(lambda row: mini(row.ann_date, row.f_ann_date), axis=1)
         df.loc[:, 'notes_receiv'] = df.apply(lambda row: decimal_utils.none_to_zero(row.notes_receiv), axis=1)
         df.loc[:, 'accounts_receiv'] = df.apply(lambda row: decimal_utils.none_to_zero(row.accounts_receiv), axis=1)
         df.loc[:, 'lt_rec'] = df.apply(lambda row: decimal_utils.none_to_zero(row.lt_rec), axis=1)
         df.loc[:, 'oth_receiv'] = df.apply(lambda row: decimal_utils.none_to_zero(row.oth_receiv), axis=1)
         df.loc[:, 'div_receiv'] = df.apply(lambda row: decimal_utils.none_to_zero(row.div_receiv), axis=1)
         df.loc[:, 'int_receiv'] = df.apply(lambda row: decimal_utils.none_to_zero(row.int_receiv), axis=1)
         df.loc[:, 'notes_payable'] = df.apply(lambda row: decimal_utils.none_to_zero(row.notes_payable), axis=1)
         df.loc[:, 'acct_payable'] = df.apply(lambda row: decimal_utils.none_to_zero(row.acct_payable), axis=1)
         df.loc[:, 'total_nca'] = df.apply(lambda row: decimal_utils.none_to_zero(row.total_nca), axis=1)
         df.loc[:, 'fa_avail_for_sale'] = df.apply(lambda row: decimal_utils.none_to_zero(row.fa_avail_for_sale),
                                                   axis=1)
         df.loc[:, 'total_cur_liab'] = df.apply(lambda row: decimal_utils.none_to_zero(row.total_cur_liab), axis=1)
         df.loc[:, 'total_cur_assets'] = df.apply(lambda row: decimal_utils.none_to_zero(row.total_cur_assets),
                                                  axis=1)
         df.loc[:, 'lt_borr'] = df.apply(lambda row: decimal_utils.none_to_zero(row.lt_borr), axis=1)
         df.loc[:, 'st_borr'] = df.apply(lambda row: decimal_utils.none_to_zero(row.st_borr), axis=1)
         df.loc[:, 'money_cap'] = df.apply(lambda row: decimal_utils.none_to_zero(row.money_cap), axis=1)
         df.loc[:, 'oth_cur_assets'] = df.apply(lambda row: decimal_utils.none_to_zero(row.oth_cur_assets), axis=1)
         # 待摊费用(新会计准则取消) -> 长期待摊费用
         df.loc[:, 'lt_amor_exp'] = df.apply(lambda row: decimal_utils.add(row.amor_exp, row.lt_amor_exp), axis=1)
     return df
Beispiel #5
0
def valid_score(score, s=0, e=100):
    return mini(maxi(score, s), e)
Beispiel #6
0
def run(ts_code: str, to_date: str = date_utils.get_current_dt()):
    """
    按股票拉取所有需要的数据
    任何一步失败都可以退出
    :param ts_code: 股票编码
    :param to_date: 数据要获取到哪天
    """
    if to_date is None:
        to_date = date_utils.get_current_dt()
    # https://tushare.pro/document/2?doc_id=32 每日指标
    r, d1 = common_fetch_data(ts_code,
                              'fetch_daily_basic',
                              TsDailyBasic,
                              TsDailyBasic.trade_date,
                              TsDailyBasic.ts_code,
                              to_date=to_date,
                              to_do=True)
    # https://tushare.pro/document/2?doc_id=27 日线行情
    r, d2 = common_fetch_data(ts_code,
                              'fetch_daily_bar',
                              TsDailyTradeInfo,
                              TsDailyTradeInfo.trade_date,
                              TsDailyTradeInfo.ts_code,
                              to_date=to_date,
                              to_do=r)
    # https://tushare.pro/document/2?doc_id=28 复权因子
    r, d3 = common_fetch_data(ts_code,
                              'fetch_adj_factor',
                              TsAdjFactor,
                              TsAdjFactor.trade_date,
                              TsAdjFactor.ts_code,
                              to_date=to_date,
                              to_do=r)
    # 季报等
    r, d4 = fetch_period_report(ts_code, to_date, to_do=r)

    # https://tushare.pro/document/2?doc_id=45 预报,数量少,直接全拉
    r, d5 = common_fetch_data(ts_code,
                              'fetch_forecast',
                              TsForecast,
                              TsForecast.ann_date,
                              TsForecast.ts_code,
                              to_date=to_date,
                              to_do=r,
                              page_size=days_of_year100)
    d5 = trade_date_service.get_previous_trade_date(
        d5) if d5 is not None else None

    # https://tushare.pro/document/2?doc_id=46 快报,数量少,直接全拉
    r, d6 = common_fetch_data(ts_code,
                              'fetch_express',
                              TsExpress,
                              TsExpress.ann_date,
                              TsExpress.ts_code,
                              to_date=to_date,
                              to_do=r,
                              page_size=days_of_year100)
    d6 = trade_date_service.get_previous_trade_date(
        d6) if d6 is not None else None

    # https://tushare.pro/document/2?doc_id=183
    r, d7 = common_fetch_data(ts_code,
                              'fetch_stk_limit',
                              TsStkLimit,
                              TsStkLimit.trade_date,
                              TsStkLimit.ts_code,
                              to_date=to_date,
                              to_do=r)
    return r, compare_utils.mini(d1, d2, d3, d4, d5, d6, d7)