예제 #1
0
def net_credit_sales(stock: str, date: datetime = datetime.now(), lookback_period: timedelta = timedelta(days=0),
                     period: str = ''):
    return fi.net_sales(stock=stock, date=date, lookback_period=lookback_period, period=period) \
           - fi.net_accounts_receivable(stock=stock, date=date, lookback_period=lookback_period, period=period)
예제 #2
0
def gross_profit(stock: str, date: datetime = datetime.now(), lookback_period: timedelta = timedelta(days=0),
                 period: str = ''):
    return fi.net_sales(stock=stock, date=date, lookback_period=lookback_period, period=period) \
           - fi.cost_of_goods_services(stock=stock, date=date, lookback_period=lookback_period, period=period)
예제 #3
0
def beneish_m_score(stock: str,
                    date: datetime = datetime.now(),
                    lookback_period: timedelta = timedelta(days=0),
                    period: str = 'TTM',
                    describe=False):
    current_net_accounts_receivable = fi.net_accounts_receivable(
        stock=stock, date=date, lookback_period=lookback_period, period=period)
    current_net_sales = fi.net_sales(stock=stock,
                                     date=date,
                                     lookback_period=lookback_period,
                                     period=period)
    current_receivables_to_sales = current_net_accounts_receivable / current_net_sales

    previous_net_accounts_receivable = fi.net_accounts_receivable(
        stock=stock,
        date=date - timedelta(days=365 if period == 'FY' else 90),
        lookback_period=lookback_period,
        period=period)
    previous_net_sales = fi.net_sales(
        stock=stock,
        date=date - timedelta(days=365 if period == 'FY' else 90),
        lookback_period=lookback_period,
        period=period)
    previous_receivables_to_sales = previous_net_accounts_receivable / previous_net_sales

    DSRI = current_receivables_to_sales / previous_receivables_to_sales

    previous_gross_profit_margin = ratios.gross_profit_margin(
        stock=stock,
        date=date - timedelta(days=365 if period == 'FY' else 90),
        lookback_period=lookback_period,
        period=period)
    current_gross_profit_margin = ratios.gross_profit_margin(
        stock=stock, date=date, lookback_period=lookback_period, period=period)

    GMI = previous_gross_profit_margin / current_gross_profit_margin

    current_asset_quality = 1 - (fi.total_current_assets(
        stock=stock, date=date, lookback_period=lookback_period, period=period
    ) + fi.net_property_plant_equipment(
        stock=stock, date=date, lookback_period=lookback_period, period=period
    ) + fi.current_marketable_securities(
        stock=stock, date=date, lookback_period=lookback_period,
        period=period))
    previous_asset_quality = 1 - (fi.total_current_assets(
        stock=stock,
        date=date - timedelta(days=365 if period == 'FY' else 90),
        lookback_period=lookback_period,
        period=period) + fi.net_property_plant_equipment(
            stock=stock,
            date=date - timedelta(days=365 if period == 'FY' else 90),
            lookback_period=lookback_period,
            period=period) + fi.current_marketable_securities(
                stock=stock,
                date=date - timedelta(days=365 if period == 'FY' else 90),
                lookback_period=lookback_period,
                period=period))
    AQI = current_asset_quality / previous_asset_quality

    SGI = fi.net_sales(stock=stock, date=date, lookback_period=lookback_period, period=period) \
          / fi.net_sales(stock=stock, date=date - timedelta(days=365 if period == 'FY' else 90),
                         lookback_period=lookback_period, period=period)

    current_depreciation = fi.accumulated_depreciation_amortization(stock=stock, date=date,
                                                                    lookback_period=lookback_period, period=period) \
                           / (fi.net_property_plant_equipment(stock=stock, date=date, lookback_period=lookback_period,
                                                              period=period)
                              + fi.accumulated_depreciation_amortization(stock=stock, date=date,
                                                                         lookback_period=lookback_period,
                                                                         period=period))
    previous_depreciation = fi.accumulated_depreciation_amortization(stock=stock,
                                                                     date=date - timedelta(
                                                                         days=365 if period == 'FY' else 90),
                                                                     lookback_period=lookback_period, period=period) \
                            / (fi.net_property_plant_equipment(stock=stock,
                                                               date=date - timedelta(
                                                                   days=365 if period == 'FY' else 90),
                                                               lookback_period=lookback_period, period=period)
                               + fi.accumulated_depreciation_amortization(stock=stock, date=date - timedelta(
                days=365 if period == 'FY' else 90), lookback_period=lookback_period, period=period))

    DEPI = previous_depreciation / current_depreciation

    SGAI = (fi.selling_general_administrative(stock=stock, date=date, lookback_period=lookback_period, period=period)
            / fi.net_sales(stock=stock, date=date, lookback_period=lookback_period, period=period)) \
           / (fi.selling_general_administrative(stock=stock, date=date - timedelta(days=365 if period == 'FY' else 90),
                                                lookback_period=lookback_period, period=period)
              / fi.net_sales(stock=stock, date=date - timedelta(days=365 if period == 'FY' else 90),
                             lookback_period=lookback_period, period=period))

    previous_leverage = (fi.total_current_liabilities(stock=stock,
                                                      date=date - timedelta(days=365 if period == 'FY' else 90),
                                                      lookback_period=lookback_period, period=period) +
                         fi.total_long_term_debt(stock=stock, date=date - timedelta(days=365 if period == 'FY' else 90),
                                                 lookback_period=lookback_period, period=period)) \
                        / fi.total_assets(stock=stock, date=date - timedelta(days=365 if period == 'FY' else 90),
                                          lookback_period=lookback_period, period=period)
    current_leverage = (fi.total_current_liabilities(stock=stock, date=date, lookback_period=lookback_period,
                                                     period=period)
                        + fi.total_long_term_debt(stock=stock, date=date, lookback_period=lookback_period,
                                                  period=period)) \
                       / fi.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    LVGI = current_leverage / previous_leverage

    TATA = (fi.operating_income(stock=stock, date=date, lookback_period=lookback_period, period=period)
            - fi.cash_flow_operating_activities(stock=stock, date=date, lookback_period=lookback_period, period=period)) \
           / fi.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)

    if not describe:
        return -4.84 + 0.92 * DSRI + 0.528 * GMI + 0.404 * AQI + 0.892 * SGI + 0.115 * DEPI - 0.172 * SGAI + 4.679 * TATA - 0.327 * LVGI
    else:
        return {'Inputs': {}}
def altman_z_score(stock: str,
                   date: datetime = datetime.now(),
                   lookback_period: timedelta = timedelta(days=0),
                   period: str = 'TTM'):
    A = metrics.net_working_capital(stock=stock, date=date, lookback_period=lookback_period, period=period) \
        / financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    B = financials.retained_earnings(stock=stock, date=date, lookback_period=lookback_period, period=period) \
        / financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    C = metrics.earnings_before_interest_and_taxes(stock=stock, date=date, lookback_period=lookback_period,
                                                   period=period) \
        / financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    D = financials.total_shareholders_equity(stock=stock, date=date, lookback_period=lookback_period, period=period) \
        / financials.total_liabilities(stock=stock, date=date, lookback_period=lookback_period, period=period)
    E = financials.net_sales(stock=stock, date=date, lookback_period=lookback_period, period=period) \
        / financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)

    with open(
            os.path.join(config.ROOT_DIR, config.DATA_DIR_NAME,
                         config.MARKET_TICKERS_DIR_NAME, "nasdaq_df.pickle"),
            "rb") as f:
        nasdaq_tickers = pickle.load(f).index

    # for private manufacturing companies
    if stock not in nasdaq_tickers and 'Manufacturing' not in metrics.get_stock_sector(
            stock):
        z_plus_score = 0.717 * A + 0.847 * B + 3.107 * C + 0.420 * D + 0.998 * E
        if z_plus_score > 2.9:
            return z_plus_score, 'safe zone'
        elif z_plus_score < 1.23:
            return z_plus_score, 'distress zone'
        else:
            return z_plus_score, 'grey zone'

    # for foreign firms (i.e. all but US and Canada) and for non-manufacturing firms, both public and private
    elif ('Other Countries' in metrics.get_stock_location(stock)) \
            or ('Manufacturing' not in metrics.get_stock_sector(stock)):

        if 'Other Countries' in metrics.get_stock_location(stock):
            z_plus_plus_score = 3.25 + 6.56 * A + 3.26 * B + 6.72 * C + 1.05 * D

        else:
            z_plus_plus_score = 6.56 * A + 3.26 * B + 6.72 * C + 1.05 * D

        if z_plus_plus_score > 2.6:
            return z_plus_plus_score, 'safe zone'
        elif z_plus_plus_score < 1.1:
            return z_plus_plus_score, 'distress zone'
        else:
            return z_plus_plus_score, 'grey zone'

    else:  # for public manufacturing firms, original score
        D = metrics.market_capitalization(
            stock=stock,
            date=date,
            lookback_period=lookback_period,
            period=period) / financials.total_liabilities(
                stock=stock,
                date=date,
                lookback_period=lookback_period,
                period=period)
        z_score = 1.2 * A + 1.4 * B + 3.3 * C + 0.6 * D + 1.0 * E
        if z_score > 2.99:
            return z_score, 'safe zone'
        elif z_score < 1.81:
            return z_score, 'distress zone'
        else:
            return z_score, 'grey zone'
def altman_z_score(stock, date, lookback_period: timedelta = timedelta(days=0), period: str = 'TTM'):
    """

    :param stock:
    :param date:
    :param lookback_period:
    :param period:
    :return:

    * For **private manufacturing companies**, a Z-score > 2.9 indicates *safe zone*, while < 1.23 indicates *distress zone*,
    and what's in between is the *grey zone*.

    For foreign firms (i.e. all but US and Canada) and for non-manufacturing firms (both public and private),
    a Z-score

    +------------------------+------------+----------+----------+
    |                        | Safe Zone   | Grey Zone | Distress Zone|
    +========================+============+==========+==========+
    | Public, Manufacturing |   > 2.99     |            |  < 1.81        |
    +-------------------------+-------------+---------+----------|
    | Private, Manufacturing   | column 2   | column 3 | column 4 |
    +------------------------+------------+----------+----------+
    | Non-Manufacturing, Foreign |            |           |         |
    +------------------------+------------+----------+----------+

    """
    A = metrics.net_working_capital(stock=stock, date=date, lookback_period=lookback_period, period=period) \
        / financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    B = financials.retained_earnings(stock=stock, date=date, lookback_period=lookback_period, period=period) \
        / financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    C = metrics.earnings_before_interest_and_taxes(stock=stock, date=date, lookback_period=lookback_period,
                                                   period=period) \
        / financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    D = financials.total_shareholders_equity(stock=stock, date=date, lookback_period=lookback_period, period=period) \
        / financials.total_liabilities(stock=stock, date=date, lookback_period=lookback_period, period=period)
    E = financials.net_sales(stock=stock, date=date, lookback_period=lookback_period, period=period) \
        / financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)

    public_tickers = object_model.Company.objects(exchange__in=['NASDAQ', 'NYSE', 'AMEX']).values_list('name')
    manufacturing_tickers = object_model.Company.objects(sic_sector='MANUFACTURING').values_list('name')
    # for private manufacturing companies
    if stock not in public_tickers and 'Manufacturing' not in metrics.get_stock_sector(stock):
        z_plus_score = 0.717 * A + 0.847 * B + 3.107 * C + 0.420 * D + 0.998 * E
        if z_plus_score > 2.9:
            return z_plus_score, 'safe zone'
        elif z_plus_score < 1.23:
            return z_plus_score, 'distress zone'
        else:
            return z_plus_score, 'grey zone'

    # for foreign firms (i.e. all but US and Canada) and for non-manufacturing firms, both public and private
    elif ('Other Countries' in metrics.get_stock_location(stock)) \
            or ('Manufacturing' not in metrics.get_stock_sector(stock)):

        if 'Other Countries' in metrics.get_stock_location(stock):
            z_plus_plus_score = 3.25 + 6.56 * A + 3.26 * B + 6.72 * C + 1.05 * D

        else:
            z_plus_plus_score = 6.56 * A + 3.26 * B + 6.72 * C + 1.05 * D

        if z_plus_plus_score > 2.6:
            return z_plus_plus_score, 'safe zone'
        elif z_plus_plus_score < 1.1:
            return z_plus_plus_score, 'distress zone'
        else:
            return z_plus_plus_score, 'grey zone'

    else:  # for public manufacturing firms, original score
        D = metrics.market_capitalization(stock=stock, date=date, lookback_period=lookback_period,
                                          period=period) / financials.total_liabilities(stock=stock, date=date,
                                                                                        lookback_period=lookback_period,
                                                                                        period=period)
        z_score = 1.2 * A + 1.4 * B + 3.3 * C + 0.6 * D + 1.0 * E
        if z_score > 2.99:
            return z_score, 'safe zone'
        elif z_score < 1.81:
            return z_score, 'distress zone'
        else:
            return z_score, 'grey zone'