Beispiel #1
0
def debt(
    stock: str,
    date: datetime = datetime.now(),
    lookback_period: timedelta = timedelta(days=0),
    period: str = '',
    only_interest_expense=False,  # any interest-bearing liability to qualify
    all_liabilities=False,  # including accounts payable and deferred income
    long_term_debt=True,  # and its associated currently due portion (measures capital structure)
    exclude_current_portion_long_term_debt=False  # if true then also above should be true
):
    if long_term_debt:
        if not exclude_current_portion_long_term_debt:
            return fi.total_long_term_debt(stock=stock,
                                           date=date,
                                           lookback_period=lookback_period,
                                           period=period)
        else:
            return fi.long_term_debt_excluding_current_portion(
                stock=stock,
                date=date,
                lookback_period=lookback_period,
                period=period)

    if all_liabilities:
        return fi.total_liabilities(stock=stock,
                                    date=date,
                                    lookback_period=lookback_period,
                                    period=period)

    if only_interest_expense:
        return fi.interest_expense(stock=stock,
                                   date=date,
                                   lookback_period=lookback_period,
                                   period=period)
def ohlson_o_score(stock: str, date: datetime = datetime.now(), lookback_period: timedelta = timedelta(days=0),
                   period: str = 'TTM'):
    TA = financials.total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    GNP = macroeconomic_analysis.gross_national_product_price_index(date)
    TL = financials.total_liabilities(stock=stock, date=date, lookback_period=lookback_period, period=period)
    WC = metrics.net_working_capital(stock=stock, date=date, lookback_period=lookback_period, period=period)
    CL = financials.total_current_liabilities(stock=stock, date=date, lookback_period=lookback_period, period=period)
    CA = financials.total_current_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    X = 1 if TL > TA else 0
    NI = financials.net_income(stock=stock, date=date, lookback_period=lookback_period, period=period)
    NI_prev = financials.net_income(stock=stock, date=date, lookback_period=lookback_period, period=period)
    FFO = financials.cash_flow_operating_activities(stock=stock, date=date, lookback_period=lookback_period,
                                                    period=period)
    Y = 1 if (NI < 0 and NI_prev < 0) else 0
    return -1.32 - 0.407 * np.log(TA / GNP) + 6.03 * (TL / TA) - 1.43 * (WC / TA) + 0.0757 * (CL / CA) - 1.72 * X \
           - 2.37 * (NI / TA) - 1.83 * (FFO / TL) + 0.285 * Y - 0.521 * ((NI - NI_prev) / (abs(NI) + abs(NI_prev)))
def net_current_asset_value(stock: str, date: datetime = datetime.now(), lookback_period: timedelta = timedelta(days=0),
                            period: str = ''):
    return fi.current_total_assets(stock=stock, date=date, lookback_period=lookback_period, period=period) \
           - fi.total_liabilities(stock=stock, date=date, lookback_period=lookback_period, period=period)
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'