def on_finish_entity(self, entity):
        super().on_finish_entity(entity)

        if not self.fetch_jq_timestamp:
            return

        # fill the timestamp for report published date
        the_data_list = get_data(
            data_schema=self.data_schema,
            provider=self.provider,
            entity_id=entity.id,
            order=self.data_schema.timestamp.asc(),
            return_type='domain',
            session=self.session,
            filters=[
                self.data_schema.timestamp == self.data_schema.report_date,
                self.data_schema.timestamp >= to_pd_timestamp('2005-01-01')
            ])
        if the_data_list:
            if self.data_schema == FinanceFactor:
                for the_data in the_data_list:
                    self.fill_timestamp_with_jq(entity, the_data)
            else:
                df = FinanceFactor.query_data(
                    entity_id=entity.id,
                    columns=[
                        FinanceFactor.timestamp, FinanceFactor.report_date,
                        FinanceFactor.id
                    ],
                    filters=[
                        FinanceFactor.timestamp != FinanceFactor.report_date,
                        FinanceFactor.timestamp >=
                        to_pd_timestamp('2005-01-01'),
                        FinanceFactor.report_date >=
                        the_data_list[0].report_date,
                        FinanceFactor.report_date <=
                        the_data_list[-1].report_date,
                    ])

                if pd_is_not_null(df):
                    index_df(df, index='report_date', time_field='report_date')

                for the_data in the_data_list:
                    if (df is not None) and (
                            not df.empty) and the_data.report_date in df.index:
                        the_data.timestamp = df.at[the_data.report_date,
                                                   'timestamp']
                        self.logger.info(
                            'db fill {} {} timestamp:{} for report_date:{}'.
                            format(self.data_schema, entity.id,
                                   the_data.timestamp, the_data.report_date))
                        self.session.commit()
                    else:
                        # self.logger.info(
                        #     'waiting jq fill {} {} timestamp:{} for report_date:{}'.format(self.data_schema,
                        #                                                                    security_item.id,
                        #                                                                    the_data.timestamp,
                        #                                                                    the_data.report_date))

                        self.fill_timestamp_with_jq(entity, the_data)
示例#2
0
def risky_company(region: Region,
                  the_date=None,
                  income_yoy=-0.1,
                  profit_yoy=-0.1,
                  entity_ids=None):
    if not the_date:
        the_date = to_pd_timestamp(now_time_str(region))

    codes = []
    start_timestamp = to_pd_timestamp(the_date) - datetime.timedelta(130)
    # 营收降,利润降,流动比率低,速动比率低
    finance_filter = or_(FinanceFactor.op_income_growth_yoy < income_yoy,
                         FinanceFactor.net_profit_growth_yoy <= profit_yoy,
                         FinanceFactor.current_ratio < 0.7,
                         FinanceFactor.quick_ratio < 0.5)
    df = FinanceFactor.query_data(region=region,
                                  entity_ids=entity_ids,
                                  start_timestamp=start_timestamp,
                                  filters=[finance_filter],
                                  columns=['code'])
    if pd_is_not_null(df):
        codes = codes + df.code.tolist()

    # 高应收,高存货,高商誉
    balance_filter = (BalanceSheet.accounts_receivable + BalanceSheet.inventories + BalanceSheet.goodwill) \
                     > BalanceSheet.total_equity
    df = BalanceSheet.query_data(region=region,
                                 entity_ids=entity_ids,
                                 start_timestamp=start_timestamp,
                                 filters=[balance_filter],
                                 columns=['code'])
    if pd_is_not_null(df):
        codes = codes + df.code.tolist()

    # 应收>利润*1/2
    df1 = BalanceSheet.query_data(
        region=region,
        entity_ids=entity_ids,
        start_timestamp=start_timestamp,
        columns=[BalanceSheet.code, BalanceSheet.accounts_receivable])
    if pd_is_not_null(df1):
        df1.drop_duplicates(subset='code', keep='last', inplace=True)
        df1 = df1.set_index('code', drop=True).sort_index()

    df2 = IncomeStatement.query_data(
        region=region,
        entity_ids=entity_ids,
        start_timestamp=start_timestamp,
        columns=[IncomeStatement.code, IncomeStatement.net_profit])
    if pd_is_not_null(df2):
        df2.drop_duplicates(subset='code', keep='last', inplace=True)
        df2 = df2.set_index('code', drop=True).sort_index()

    if pd_is_not_null(df1) and pd_is_not_null(df2):
        codes = codes + df1[df1.accounts_receivable > df2.net_profit /
                            2].index.tolist()

    return list(set(codes))
示例#3
0
    def filter_selector_long_targets(self, timestamp, selector: TargetSelector, long_targets: List[str]) -> List[str]:
        # 选择器选出的个股,再做进一步处理
        if selector.level == IntervalLevel.LEVEL_1DAY:
            if not long_targets:
                return None

            # 选择营收有增长的,并排序
            start_timestamp = timestamp - datetime.timedelta(100)
            df = FinanceFactor.query_data(entity_ids=long_targets, order=FinanceFactor.op_income_growth_yoy.desc(),
                                          filters=[FinanceFactor.op_income_growth_yoy > 0],
                                          start_timestamp=start_timestamp,
                                          end_timestamp=timestamp,
                                          columns=[FinanceFactor.entity_id, FinanceFactor.op_income_growth_yoy])
            if pd_is_not_null(df):
                return df['entity_id'].iloc[:10].tolist()
            return None
        return long_targets
示例#4
0
def test_000778_finance_factor():
    correct_timestamps = [
        '2018-09-30', '2018-06-30', '2018-03-31', '2017-12-31', '2017-09-30',
        '2017-06-30', '2017-03-31', '2016-12-31', '2016-09-30', '2016-06-30',
        '2016-03-31', '2015-12-31', '2015-09-30', '2015-06-30', '2015-03-31',
        '2014-12-31', '2014-09-30', '2014-06-30', '2014-03-31', '2013-12-31',
        '2013-09-30', '2013-06-30', '2013-03-31', '2012-12-31', '2012-09-30',
        '2012-06-30', '2012-03-31', '2011-12-31', '2011-09-30', '2011-06-30',
        '2011-03-31', '2010-12-31', '2010-09-30', '2010-06-30', '2010-03-31',
        '2009-12-31', '2009-09-30', '2009-06-30', '2009-03-31', '2008-12-31',
        '2008-09-30', '2008-06-30', '2008-03-31', '2007-12-31', '2007-09-30',
        '2007-06-30', '2007-03-31', '2006-12-31', '2006-09-30', '2006-06-30',
        '2006-03-31', '2005-12-31', '2005-09-30', '2005-06-30', '2005-03-31',
        '2004-12-31', '2004-09-30', '2004-06-30', '2004-03-31', '2003-12-31',
        '2003-09-30', '2003-06-30', '2003-03-31', '2002-12-31', '2002-09-30',
        '2002-06-30', '2002-03-31', '2001-12-31', '2001-06-30', '2000-12-31',
        '2000-06-30', '1999-12-31', '1999-06-30', '1998-12-31', '1998-06-30',
        '1997-12-31', '1997-06-30', '1996-12-31', '1995-12-31', '1994-12-31'
    ]
    result = FinanceFactor.query_data(session=session,
                                      provider='eastmoney',
                                      return_type='domain',
                                      codes=['000778'],
                                      end_timestamp='2018-12-30',
                                      order=FinanceFactor.report_date.desc(),
                                      time_field='report_date')
    assert len(correct_timestamps) == len(result)
    timestamps = [to_time_str(item.report_date) for item in result]
    assert set(correct_timestamps) == set(timestamps)
    latest: FinanceFactor = result[0]

    assert latest.basic_eps == 0.4537
    assert latest.diluted_eps == 0.4537
    assert latest.bps == 5.0919
    assert latest.capital_reserve_ps == 2.1769
    assert latest.undistributed_profit_ps == 1.8132
    assert latest.op_cash_flow_ps == 1.0148
    assert latest.total_op_income == 31710000000
    assert latest.gross_profit == 5491000000
    assert latest.net_profit == 1811000000
    assert latest.deducted_net_profit == 1897000000
    assert latest.total_op_income_growth_yoy == -0.1024
    assert latest.inc_net_profit_shareholders_yoy == 1.2404
    assert latest.deducted_net_profit_growth_yoy == 1.4813
    assert latest.op_income_growth_qoq == 0.0408
    assert latest.net_profit_growth_qoq == 0.2143
    assert latest.deducted_net_profit_growth_qoq == 0.2955

    assert latest.roe == 0.0882
    assert latest.rota == 0.0376
    assert latest.gross_profit_margin == 0.1731
    assert latest.net_margin == 0.0591

    assert latest.advance_receipts_per_op_income == 0.1
    assert latest.sales_net_cash_flow_per_op_income == 0.88
    assert latest.op_net_cash_flow_per_op_income == 0.13
    assert latest.actual_tax_rate == 0.2362

    assert latest.current_ratio == 1
    assert latest.quick_ratio == 0.84
    assert latest.cash_flow_ratio == 0.17
    assert latest.debt_asset_ratio == 0.5766
    assert latest.em == 2.36
    assert latest.equity_ratio == 1.43

    assert latest.total_assets_turnover_days == 423.91
    assert latest.inventory_turnover_days == 32.88
    assert latest.receivables_turnover_days == 52.23
    assert latest.total_assets_turnover == 0.64
    assert latest.inventory_turnover == 8.21
    assert latest.receivables_turnover == 5.17
示例#5
0
def test_000001_finance_factor():
    correct_timestamps = [
        '2018-09-30', '2018-06-30', '2018-03-31', '2017-12-31', '2017-09-30',
        '2017-06-30', '2017-03-31', '2016-12-31', '2016-09-30', '2016-06-30',
        '2016-03-31', '2015-12-31', '2015-09-30', '2015-06-30', '2015-03-31',
        '2014-12-31', '2014-09-30', '2014-06-30', '2014-03-31', '2013-12-31',
        '2013-09-30', '2013-06-30', '2013-03-31', '2012-12-31', '2012-09-30',
        '2012-06-30', '2012-03-31', '2011-12-31', '2011-09-30', '2011-06-30',
        '2011-03-31', '2010-12-31', '2010-09-30', '2010-06-30', '2010-03-31',
        '2009-12-31', '2009-09-30', '2009-06-30', '2009-03-31', '2008-12-31',
        '2008-09-30', '2008-06-30', '2008-03-31', '2007-12-31', '2007-09-30',
        '2007-06-30', '2007-03-31', '2006-12-31', '2006-09-30', '2006-06-30',
        '2006-03-31', '2005-12-31', '2005-09-30', '2005-06-30', '2005-03-31',
        '2004-12-31', '2004-09-30', '2004-06-30', '2004-03-31', '2003-12-31',
        '2003-09-30', '2003-06-30', '2003-03-31', '2002-12-31', '2002-09-30',
        '2002-06-30', '2002-03-31', '2001-12-31', '2001-09-30', '2001-06-30',
        '2001-03-31', '2000-12-31', '2000-06-30', '1999-12-31', '1999-06-30',
        '1998-12-31', '1998-06-30', '1997-12-31', '1997-06-30', '1996-12-31',
        '1996-06-30', '1995-12-31', '1995-06-30', '1994-12-31', '1994-06-30',
        '1993-12-31', '1993-06-30', '1992-12-31', '1991-12-31', '1990-12-31',
        '1989-12-31'
    ]
    result = FinanceFactor.query_data(session=session,
                                      provider='eastmoney',
                                      return_type='domain',
                                      codes=['000001'],
                                      end_timestamp='2018-12-30',
                                      order=FinanceFactor.report_date.desc(),
                                      time_field='report_date')
    assert len(correct_timestamps) == len(result)
    timestamps = [to_time_str(item.report_date) for item in result]
    assert set(correct_timestamps) == set(timestamps)
    latest: FinanceFactor = result[0]
    assert latest.basic_eps == 1.14
    assert latest.deducted_eps == 1.13
    assert latest.diluted_eps == 1.14
    assert latest.bps == 12.538
    assert latest.capital_reserve_ps == 3.2886
    assert latest.undistributed_profit_ps == 5.3566
    assert latest.op_cash_flow_ps == -0.6587

    assert latest.total_op_income == 86660000000
    assert latest.net_profit == 20460000000
    assert latest.deducted_net_profit == 20350000000
    assert latest.total_op_income_growth_yoy == 0.0856
    assert latest.inc_net_profit_shareholders_yoy == 0.068
    assert latest.deducted_inc_net_profit_shareholders_yoy == 0.0636
    assert latest.op_income_growth_qoq == 0.0336
    assert latest.net_profit_growth_qoq == 0.0202
    assert latest.deducted_net_profit_growth_qoq == 0.0168

    assert latest.roe == 0.0948
    assert latest.deducted_roe == 0.0943
    assert latest.rota == 0.0062
    assert latest.net_margin == 0.2360

    assert latest.debt_asset_ratio == 0.9298
    assert latest.em == 14.25
    assert latest.equity_ratio == 13.25

    assert latest.fi_total_deposit == 2130000000000
    assert latest.fi_total_loan == 1920000000000
    assert latest.fi_loan_deposit_ratio == 0.9004
    assert latest.fi_npl_ratio == 0.0168
    assert latest.fi_npl_provision_coverage == 1.6914
示例#6
0
                            # arrowcolor='#030813',
                            ax=-10,
                            ay=-30,
                            bordercolor='#c7c7c7',
                            borderwidth=1,
                            bgcolor=color,
                            opacity=0.8))
        return annotations
    return None


if __name__ == '__main__':
    # from zvt.factors.pattern import ZenFactor
    #
    # data_reader1 = ZenFactor(codes=['000338'], level='1d')
    # data_reader2 = ZenFactor(codes=['000338'], level='1wk')
    # print(data_reader2.data_df)
    #
    # stacked = StackedDrawer(data_reader1.drawer(), data_reader2.drawer()).draw_kline()
    df = Stock1dHfqKdata.query_data(code='000338',
                                    start_timestamp='2015-01-01')
    sub_df = FinanceFactor.query_data(code='000338',
                                      start_timestamp='2015-01-01',
                                      columns=[
                                          FinanceFactor.roe,
                                          FinanceFactor.entity_id,
                                          FinanceFactor.timestamp
                                      ])

    Drawer(main_df=df, sub_df_list=[sub_df]).draw_kline(show=True)