예제 #1
0
def invested_capital(stock: str, date: datetime = datetime.now(), lookback_period: timedelta = timedelta(days=0),
                     period: str = '', operating_approach=True):
    if operating_approach:
        return net_working_capital(stock=stock, date=date, lookback_period=lookback_period, period=period,
                                   exclude_current_portion_cash_and_debt=True) \
               + fi.net_property_plant_equipment(stock=stock, date=date, lookback_period=lookback_period, period=period) \
               + fi.total_intangible_assets(stock=stock, date=date, lookback_period=lookback_period, period=period)
    else:  # investing approach
        return fi.current_total_liabilities(stock=stock, date=date, lookback_period=lookback_period, period=period) \
               + fi.long_term_debt_excluding_current_portion(stock, date, lookback_period=timedelta(days=0),
                                                             period=period) \
               + fi.total_shareholders_equity(stock=stock, date=date, lookback_period=lookback_period, period=period) \
               + fi.cash_flow_financing_activities(stock=stock, date=date, lookback_period=lookback_period,
                                                   period=period) \
               + fi.cash_flow_investing_activities(stock=stock, date=date, lookback_period=lookback_period,
                                                   period=period)
예제 #2
0
def weighted_average_cost_of_capital(
        stock,
        date=datetime.now(),
        lookback_period: timedelta = timedelta(days=0),
        lookback_lookback_period: timedelta = timedelta(days=365 * 5),
        period: str = 'FY',
        beta_period='Monthly',
        benchmark: str = '^GSPC'):
    from_date = date - lookback_period - lookback_lookback_period
    to_date = date - lookback_period

    dictio = {
        'Common Equity':
        (cost_of_equity_capm(stock=stock,
                             from_date=from_date,
                             to_date=to_date,
                             beta_period=beta_period,
                             benchmark=benchmark),
         fi.total_shareholders_equity(stock=stock,
                                      date=date,
                                      lookback_period=lookback_period,
                                      period=period)),
        'Preferred Equity':
        (cost_of_preferred_stock(stock=stock,
                                 date=date,
                                 lookback_period=lookback_period,
                                 period=period),
         fi.preferred_stock_value(stock=stock,
                                  date=date,
                                  lookback_period=lookback_period,
                                  period=period)),
        'Debt': (cost_of_debt(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))
    }

    capitals = [np.nan_to_num(v[1]) for k, v in dictio.items()]
    weights = [part / sum(capitals) for part in capitals]
    costs = [np.nan_to_num(v[0]) for k, v in dictio.items()]
    return np.sum([weight * cost for weight, cost in zip(weights, costs)])
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'