Beispiel #1
0
def handle_friday(next_date: Datum, period: str, mult: int, start_date: Datum):
    """ Extracted the calculation for when the next_day is Friday """
    assert isinstance(next_date, Datum)
    assert isinstance(start_date, Datum)

    # Starting from line 220.
    tmp_sat = next_date.clone()
    tmp_sat.add_days(1)

    tmp_sun = next_date.clone()
    tmp_sun.add_days(2)

    if period == RecurrencePeriod.END_OF_MONTH.value:
        if (next_date.is_end_of_month() or tmp_sat.is_end_of_month()
                or tmp_sun.is_end_of_month()):
            next_date.add_months(1)
        else:
            next_date.add_months(mult - 1)
    else:
        if tmp_sat.get_day_name() == start_date.get_day_name():
            next_date.add_days(1)
            next_date.add_months(mult)
        elif tmp_sun.get_day_name() == start_date.get_day_name():
            next_date.add_days(2)
            next_date.add_months(mult)
        elif next_date.get_day() >= start_date.get_day():
            next_date.add_months(mult)
        elif next_date.is_end_of_month():
            next_date.add_months(mult)
        elif tmp_sat.is_end_of_month():
            next_date.add_days(1)
            next_date.add_months(mult)
        elif tmp_sun.is_end_of_month():
            next_date.add_days(2)
            next_date.add_months(mult)
        else:
            # /* one fewer month fwd because of the occurrence in this month */
            next_date.subtract_months(1)

    return next_date
def get_stock_model_from(book: Book, commodity: Commodity,
                         as_of_date: date) -> StockViewModel:
    """ Parses stock/commodity and returns the model for display """
    from decimal import Decimal
    from pydatum import Datum

    svc = SecurityAggregate(book, commodity)
    model = StockViewModel()

    model.exchange = commodity.namespace
    model.symbol = commodity.mnemonic

    model.shares_num = svc.get_num_shares_on(as_of_date)
    # Ignore 0-balance
    if model.shares_num == 0:
        return None

    model.avg_price = svc.get_avg_price()

    # Last price
    price_svc = PricesAggregate(book)
    # last_price: Price = price_svc.get_price_as_of(commodity, as_of_date)
    last_price: PriceModel = price_svc.get_latest_price(commodity)
    if last_price is not None:
        model.price = last_price.value
    else:
        model.price = Decimal(0)

    # currency
    if model.price:
        model.currency = last_price.currency

    # Cost
    model.cost = model.shares_num * model.avg_price

    # Balance (current value)
    if model.shares_num > 0 and model.price:
        model.balance = model.shares_num * model.price
    else:
        model.balance = Decimal(0)

    # Gain/Loss
    model.gain_loss = model.balance - model.cost

    # Gain/loss percentage
    gain_loss_perc = 0
    if model.cost:
        gain_loss_perc = abs(model.gain_loss) * 100 / model.cost
        if model.gain_loss < 0:
            gain_loss_perc *= -1
    model.gain_loss_perc = gain_loss_perc

    # Income
    income = symbol_dividends.get_dividend_sum_for_symbol(book, model.symbol)
    model.income = float(income)

    #income_12m = symbol_dividends.
    start = Datum()
    start.subtract_months(12)
    end = Datum()
    model.income_last_12m = svc.get_income_in_period(start, end)
    if model.balance > 0 and model.income_last_12m > 0:
        model.income_last_12m_perc = model.income_last_12m * 100 / model.balance
    else:
        model.income_last_12m_perc = Decimal(0)

    return model
Beispiel #3
0
    def run(self, symbol: str) -> SecurityDetailsViewModel:
        """ Loads the model for security details """
        from pydatum import Datum

        svc = self._svc
        sec_agg = svc.securities.get_aggregate_for_symbol(symbol)

        model = SecurityDetailsViewModel()

        model.symbol = sec_agg.security.namespace + ":" + sec_agg.security.mnemonic
        model.security = sec_agg.security

        # Quantity
        model.quantity = sec_agg.get_quantity()
        model.value = sec_agg.get_value()
        currency = sec_agg.get_currency()
        if currency:
            assert isinstance(currency, str)
            model.currency = currency
        model.price = sec_agg.get_last_available_price()

        model.average_price = sec_agg.get_avg_price()
        # Here we take only the amount paid for the remaining stock.
        model.total_paid = sec_agg.get_total_paid_for_remaining_stock()

        # Profit/loss
        model.profit_loss = model.value - model.total_paid
        if model.total_paid:
            model.profit_loss_perc = abs(
                model.profit_loss) * 100 / model.total_paid
        else:
            model.profit_loss_perc = 0
        if abs(model.value) < abs(model.total_paid):
            model.profit_loss_perc *= -1

        # Income
        model.income = sec_agg.get_income_total()
        if model.total_paid:
            model.income_perc = model.income * 100 / model.total_paid
        else:
            model.income_perc = 0
        # income in the last 12 months
        start = Datum()
        start.subtract_months(12)
        end = Datum()
        model.income_last_12m = sec_agg.get_income_in_period(start, end)
        if model.total_paid == 0:
            model.income_perc_last_12m = 0
        else:
            model.income_perc_last_12m = model.income_last_12m * 100 / model.total_paid

        # Return of Capital
        roc = sec_agg.get_return_of_capital()
        model.return_of_capital = roc

        # total return
        model.total_return = model.profit_loss + model.income
        if model.total_paid:
            model.total_return_perc = model.total_return * 100 / model.total_paid
        else:
            model.total_return_perc = 0

        # load all holding accounts
        model.accounts = sec_agg.accounts
        # Income accounts
        model.income_accounts = sec_agg.get_income_accounts()

        # Load asset classes to which this security belongs.
        # todo load asset allocation, find the parents for this symbol
        # svc.asset_allocation.load_config_only(svc.currencies.default_currency)
        # stocks = svc.asset_allocation.get_stock(model.symbol)
        #
        # for stock in stocks:
        #     model.asset_classes.append(stock.asset_class)
        from asset_allocation import AppAggregate
        aa = AppAggregate()
        aa.open_session()
        aa.get_asset_classes_for_security(None, model.symbol)

        return model