示例#1
0
def get_closing_price(storage, month):
    ref_month = get_reference_date(storage) - relativedelta(months=month)

    path = get_path_to_historical_prices(storage, ref_month)
    content = storage.storage_repository.load(path)

    if content:
        history = csv.DictReader(content.splitlines(), delimiter=';')

        last_price = "0"

        first_day_next_month = (ref_month +
                                relativedelta(months=1)).replace(day=1)

        for day in history:
            if "Datum" not in day:
                continue
            date_str = day["Datum"].strip()
            if date_str == "":
                continue
            if datetime.strptime(date_str, "%d.%m.%Y") >= first_day_next_month:
                break
            if day["Schluss"]:
                last_price = day["Schluss"]

        return asFloat(last_price)

    return 0
示例#2
0
def read_price_from_csv(filename: str, storage, date_for_price):
    content = storage.storage_repository.load(filename)

    if content:
        history = csv.DictReader(content.splitlines(), delimiter=';')
        last_price = None

        for day in history:
            if day["Datum"].strip() == "":
                continue

            date = datetime.strptime(day["Datum"].strip(), "%d.%m.%Y")

            if date > date_for_price and last_price is not None:
                break

            if day["Schluss"]:
                last_price = day["Schluss"]

        if last_price is None:
            return 0

        return asFloat(last_price)
    else:
        return 0
示例#3
0
def get_market_capitalization(fundamentals, last_year, last_cross_year):
    market_capitalization = asFloat(
        get_for_year(
            fundamentals["Marktkapitalisierung"]
            ["Marktkapitalisierung in Mio. EUR"],
            [last_year, last_cross_year]))
    if market_capitalization > 0:
        market_capitalization = market_capitalization * 1000000

    return market_capitalization
示例#4
0
def calc_per_5_years(pers, ref_year):
    counter = 0
    per_sum = 0.0
    for key in pers.keys():
        if key <= ref_year and pers[key].strip() != "-":
            counter += 1
            per_sum += asFloat(pers[key])

    if counter == 0:
        return 0

    return per_sum / counter
示例#5
0
def calc_per_5_years(fundamentals, col_names: []):
    pers = fundamentals["Gewinn"]["KGV"]

    ref_year = find_existing_column(pers, col_names)

    if ref_year is None:
        return 0

    counter = 0
    per_sum = 0.0
    for key in pers.keys():
        if key <= ref_year and pers[key].strip() != "-":
            counter += 1
            per_sum += asFloat(pers[key])

    if counter == 0:
        return 0

    return per_sum / counter
示例#6
0
def scrap(stock: Stock, stock_storage: StockStorage, util: OnVistaDateUtil = OnVistaDateUtil()):
    with open(stock_storage.getStoragePath("profil", "html"), mode="r") as f:
        soup = BeautifulSoup(f, 'html.parser')

        currencies = scrap_currencies(soup)

        price = scrap_price(soup)
        fundamentals = scrap_fundamentals(soup)
        company_details = scrap_company_details(soup)

    with open(stock_storage.getStoragePath("bilanz_guv", "html"), mode="r") as f:
        soup = BeautifulSoup(f, 'html.parser')

        bilanz_guv = scrap_bilanz_guv(soup)

    with open(stock_storage.getStoragePath("schaetzungen", "html"), mode="r") as f:
        soup = BeautifulSoup(f, 'html.parser')

        schaetzung = scrap_schaetzung(soup)

    with open(stock_storage.getStoragePath("analysen", "html"), mode="r") as f:
        soup = BeautifulSoup(f, 'html.parser')

        stock.ratings = scrap_analysen(soup)

    last_year = util.get_last_year()
    current_year = util.get_current_year(estimated=False)
    next_year = util.get_next_year(estimated=False)

    stock.price = asFloat(price)

    stock.field = company_details["Branchen"]

    GuV = findIn(bilanz_guv, "GuV")
    gewinn = asFloat(GuV["Ergebnis nach Steuer"][last_year])
    ebit = asFloat(GuV["Ergebnis vor Steuern"][last_year])
    erloes = asFloat(GuV["Umsatzerlöse"][last_year])

    bilanz = findIn(bilanz_guv, "Bilanz")
    eigenkapital = asFloat(bilanz["Eigenkapital"][last_year])

    stock.roi = gewinn / eigenkapital * 100
    stock.ebit_margin = ebit / erloes * 100

    unternehmenskennzahlen = findIn(bilanz_guv, "Unternehmenskennzahlen")
    stock.equity_ratio = asFloat(unternehmenskennzahlen["Eigenkapitalquote in %"][last_year])

    stock.per = asFloat(schaetzung["KGV"][current_year])

    hist_pers = unternehmenskennzahlen["KGV (Jahresendkurs)"]

    per_5_years = stock.per
    number_of_year = 1

    for year in list(hist_pers.keys())[-4:]:
        if hist_pers[year] != "-":
            per_5_years += asFloat(hist_pers[year])
            number_of_year += 1

    stock.per_5_years = (per_5_years / number_of_year)

    eps_row_name = "Ergebnis/Aktie (reported)" if ("Ergebnis/Aktie (reported)" in schaetzung) else "Ergebnis/Aktie"
    stock.eps_current_year = asFloat(schaetzung[eps_row_name][current_year])
    stock.eps_next_year = asFloat(schaetzung[eps_row_name][next_year])

    stock.per_fallback = stock.price / stock.eps_current_year if stock.eps_current_year != 0 else 0

    stock.market_capitalization = asFloat(fundamentals["Marktkapitalisierung in Mrd. EUR"]) * 1000000000

    stock_price_today = 0
    stock_price_6month = 0
    stock_price_1year = 0

    stock.history = History(stock_price_today, stock_price_6month, stock_price_1year)

    stock.monthClosings = MonthClosings()

    stock.historical_eps_current_year = 0
    stock.historical_eps_date = 0
    stock.historical_eps_next_year = 0

    stock.reaction_to_quarterly_numbers = ReactionToQuarterlyNumbers(0, 0, 0, 0, "")

    return stock
示例#7
0
def scrap(stock: Stock,
          stock_storage: StockStorage,
          util: OnVistaDateUtil = OnVistaDateUtil()):
    path = stock_storage.getStoragePath("fundamental", "html")
    content = stock_storage.storage_repository.load(path)

    if content:
        soup = BeautifulSoup(content, 'html.parser')

        stock.symbol = scrap_symbol(soup)
        fundamentals = scrap_fundamentals(soup)

        last_year_est = util.get_last_year(estimated=True)
        last_cross_year_est = util.get_last_cross_year(estimated=True)

        fallback_to_last_year_values = last_year_est in fundamentals[
            "Rentabilität"][
                "Eigenkapitalrendite"] or last_cross_year_est in fundamentals[
                    "Rentabilität"]["Eigenkapitalrendite"]

        if fallback_to_last_year_values:
            last_year = util.get_last_year(min_years=2)
            last_cross_year = util.get_last_cross_year(min_years=2)
            current_year = util.get_last_year(estimated=True)
            current_cross_year = util.get_last_cross_year()
            current_cross_year_est = util.get_last_cross_year(estimated=True)
            next_year = util.get_current_year()
            next_cross_year = util.get_current_cross_year()
        else:
            last_year = util.get_last_year()
            last_cross_year = util.get_last_cross_year()
            current_year = util.get_current_year()
            current_cross_year = util.get_current_cross_year(estimated=False)
            current_cross_year_est = util.get_current_cross_year()
            next_year = util.get_next_year()
            next_cross_year = util.get_next_cross_year()

        stock.price = asFloat(
            soup.find("ul", {
                "class": "KURSDATEN"
            }).find("li").find("span").get_text().strip())

        stock.roi = asFloat(
            get_for_year(fundamentals["Rentabilität"]["Eigenkapitalrendite"],
                         [last_year, last_cross_year]))
        stock.ebit_margin = asFloat(
            get_for_year(fundamentals["Rentabilität"]["EBIT-Marge"],
                         [last_year, last_cross_year]))

        stock.equity_ratio = asFloat(
            get_for_year(fundamentals["Bilanz"]["Eigenkapitalquote"],
                         [last_year, last_cross_year]))

        stock.per_5_years = calc_per_5_years(
            fundamentals,
            [current_year, current_cross_year_est, current_cross_year])

        stock.per = asFloat(
            get_for_year(
                fundamentals["Gewinn"]["KGV"],
                [current_year, current_cross_year_est, current_cross_year]))

        date = stock_storage.indexStorage.date

        if sameDay(date, datetime.now()):
            date = date - relativedelta(days=1)

        stock_price_today = get_latest_price(stock_storage, date)

        stock_price_6month = get_historical_price(
            stock_storage, (date - relativedelta(months=6)))
        stock_price_1year = get_historical_price(
            stock_storage, (date - relativedelta(months=12)))

        stock.history = History(stock_price_today, stock_price_6month,
                                stock_price_1year)

        stock.monthClosings = get_month_closings(stock_storage)

        stock.eps_current_year = asFloat(
            get_for_year(
                fundamentals["Gewinn"]["Gewinn pro Aktie in EUR"],
                [current_year, current_cross_year_est, current_cross_year]))

        stock.per_fallback = stock.price / stock.eps_current_year if stock.eps_current_year != 0 else 0

        stock.eps_next_year = asFloat(
            get_for_year(fundamentals["Gewinn"]["Gewinn pro Aktie in EUR"],
                         [next_year, next_cross_year]))

        stock.market_capitalization = get_market_capitalization(
            fundamentals, last_year, last_cross_year)

    stock = scrap_ratings(stock, stock_storage)

    add_historical_eps(stock, stock_storage)

    add_reaction_to_quarterly_numbers(stock, stock_storage)

    return stock