Search in sources :

Example 21 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class InvestingDaemon method execute.

@Override
protected void execute(final Portfolio portfolio, final Authenticated authenticated) {
    // don't query anything unless we have enough money to invest
    final int balance = portfolio.getRemoteBalance().get().intValue();
    final int minimum = Defaults.MINIMUM_INVESTMENT_IN_CZK;
    if (balance < minimum) {
        LOGGER.debug("Asleep as there is not enough available balance. ({} < {})", balance, minimum);
        return;
    }
    // query marketplace for investment opportunities
    final Collection<LoanDescriptor> loans = authenticated.call(zonky -> zonky.getAvailableLoans(SELECT)).parallel().filter(// re-investing would fail
    l -> !l.getMyInvestment().isPresent()).map(l -> {
        /*
                     * Loan is first retrieved from the authenticated API. This way, we get all available
                     * information, such as borrower nicknames from other loans made by the same person.
                     */
        final Loan complete = authenticated.call(zonky -> LoanCache.INSTANCE.getLoan(l.getId(), zonky));
        /*
                     * We update the loan within the cache with latest information from the marketplace. This is
                     * done so that we don't cache stale loan information, such as how much of the loan is remaining
                     * to be invested.
                     */
        Loan.updateFromMarketplace(complete, l);
        return complete;
    }).map(LoanDescriptor::new).collect(Collectors.toList());
    investing.apply(portfolio, loans);
}
Also used : InvestmentStrategy(com.github.robozonky.api.strategies.InvestmentStrategy) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Collection(java.util.Collection) Investing(com.github.robozonky.app.investing.Investing) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) Select(com.github.robozonky.common.remote.Select) Consumer(java.util.function.Consumer) Investor(com.github.robozonky.app.investing.Investor) LoanCache(com.github.robozonky.app.util.LoanCache) Authenticated(com.github.robozonky.app.authentication.Authenticated) Duration(java.time.Duration) Optional(java.util.Optional) Defaults(com.github.robozonky.internal.api.Defaults) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor)

Example 22 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class Repayments method accept.

@Override
public void accept(final Portfolio portfolio, final Authenticated authenticated) {
    final PortfolioOverview portfolioOverview = portfolio.calculateOverview();
    final Collection<Integer> active = getActiveLastTime();
    // detect and process loans that have been fully repaid, comparing to the last time active loans were checked
    final Stream<Investment> repaid = portfolio.getActiveWithPaymentStatus(PaymentStatuses.of(PaymentStatus.PAID));
    repaid.filter(i -> active.contains(i.getLoanId())).peek(i -> {
        final Loan l = authenticated.call(zonky -> LoanCache.INSTANCE.getLoan(i, zonky));
        final Event e = new LoanRepaidEvent(i, l, portfolioOverview);
        Events.fire(e);
    }).forEach(i -> active.remove(i.getLoanId()));
    // add all active loans to date
    portfolio.getActiveWithPaymentStatus(PaymentStatus.getActive()).forEach(i -> active.add(i.getLoanId()));
    // store for future reference
    setActive(active);
}
Also used : Collection(java.util.Collection) Set(java.util.Set) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) Collectors(java.util.stream.Collectors) PaymentStatus(com.github.robozonky.api.remote.enums.PaymentStatus) Event(com.github.robozonky.api.notifications.Event) Stream(java.util.stream.Stream) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) LoanCache(com.github.robozonky.app.util.LoanCache) Events(com.github.robozonky.app.Events) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) State(com.github.robozonky.internal.api.State) PaymentStatuses(com.github.robozonky.api.remote.enums.PaymentStatuses) Collections(java.util.Collections) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) Event(com.github.robozonky.api.notifications.Event) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment)

Example 23 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class Purchasing method execute.

@Override
protected Collection<Investment> execute(final Portfolio portfolio, final PurchaseStrategy strategy, final Collection<Participation> marketplace) {
    final Stream<ParticipationDescriptor> participations = marketplace.parallelStream().map(p -> toDescriptor(p, auth)).filter(d -> {
        // never re-purchase what was once sold
        final Loan l = d.related();
        final boolean wasSoldBefore = portfolio.wasOnceSold(l);
        if (wasSoldBefore) {
            LOGGER.debug("Ignoring loan #{} as the user had already sold it before.", l.getId());
        }
        return !wasSoldBefore;
    });
    final RestrictedPurchaseStrategy s = new RestrictedPurchaseStrategy(strategy, auth.getRestrictions());
    return Session.purchase(portfolio, auth, participations, s, dryRun);
}
Also used : Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Stream(java.util.stream.Stream) LoanCache(com.github.robozonky.app.util.LoanCache) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Optional(java.util.Optional) Portfolio(com.github.robozonky.app.portfolio.Portfolio) StrategyExecutor(com.github.robozonky.app.util.StrategyExecutor) Participation(com.github.robozonky.api.remote.entities.Participation) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor)

Example 24 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class Session method purchase.

public boolean purchase(final RecommendedParticipation recommendation) {
    Events.fire(new PurchaseRequestedEvent(recommendation));
    final Participation participation = recommendation.descriptor().item();
    final Loan loan = recommendation.descriptor().related();
    final boolean purchased = isDryRun || actualPurchase(participation);
    if (purchased) {
        final Investment i = Investment.fresh(participation, loan, recommendation.amount());
        markSuccessfulPurchase(i);
        Events.fire(new InvestmentPurchasedEvent(i, loan, portfolioOverview));
    }
    return purchased;
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) RecommendedParticipation(com.github.robozonky.api.strategies.RecommendedParticipation) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment)

Example 25 with Loan

use of com.github.robozonky.api.remote.entities.sanitized.Loan in project robozonky by RoboZonky.

the class LoanCache method getLoan.

public Loan getLoan(final Investment investment, final Zonky api) {
    final Loan loan = getLoan(investment.getLoanId(), api);
    /*
         * investment may have been created without access to the loan. now that we have the loan, we can update the
         * investment, filling any missing information.
         */
    Investment.fillFrom(investment, loan);
    return loan;
}
Also used : Loan(com.github.robozonky.api.remote.entities.sanitized.Loan)

Aggregations

Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)53 Test (org.junit.jupiter.api.Test)46 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)25 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)20 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)19 MarketplaceLoan (com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan)14 Zonky (com.github.robozonky.common.remote.Zonky)14 SoftAssertions (org.assertj.core.api.SoftAssertions)14 PortfolioOverview (com.github.robozonky.api.strategies.PortfolioOverview)13 Assertions (org.assertj.core.api.Assertions)13 Mockito (org.mockito.Mockito)13 Event (com.github.robozonky.api.notifications.Event)12 Portfolio (com.github.robozonky.app.portfolio.Portfolio)12 Authenticated (com.github.robozonky.app.authentication.Authenticated)11 Collection (java.util.Collection)11 Collections (java.util.Collections)11 Stream (java.util.stream.Stream)10 Optional (java.util.Optional)9 InvestmentPurchasedEvent (com.github.robozonky.api.notifications.InvestmentPurchasedEvent)8 Participation (com.github.robozonky.api.remote.entities.Participation)8