Search in sources :

Example 1 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor 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 2 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.

the class Session method purchase.

public static Collection<Investment> purchase(final Portfolio portfolio, final Authenticated auth, final Stream<ParticipationDescriptor> items, final RestrictedPurchaseStrategy strategy, final boolean dryRun) {
    final Session session = new Session(portfolio, items, auth, dryRun);
    final Collection<ParticipationDescriptor> c = session.getAvailable();
    if (c.isEmpty()) {
        return Collections.emptyList();
    }
    Events.fire(new PurchasingStartedEvent(c, session.portfolioOverview));
    session.purchase(strategy);
    final Collection<Investment> result = session.getResult();
    Events.fire(new PurchasingCompletedEvent(result, session.portfolioOverview));
    return Collections.unmodifiableCollection(result);
}
Also used : PurchasingStartedEvent(com.github.robozonky.api.notifications.PurchasingStartedEvent) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) PurchasingCompletedEvent(com.github.robozonky.api.notifications.PurchasingCompletedEvent)

Example 3 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.

the class ParsedStrategyTest method exitButNoSelloff.

@Test
void exitButNoSelloff() {
    final DefaultPortfolio portfolio = DefaultPortfolio.EMPTY;
    final DefaultValues values = new DefaultValues(portfolio);
    // exit active, no sell-off yet
    values.setExitProperties(new ExitProperties(LocalDate.now().plusMonths(6)));
    final ParsedStrategy strategy = new ParsedStrategy(values, Collections.emptyList(), Collections.emptyMap(), new FilterSupplier(values, Collections.emptySet(), Collections.emptySet(), Collections.emptySet()));
    // no loan or participation should be bought; every investment should be sold
    final Loan loanUnder = mockLoan(1000);
    final Loan loanOver = Loan.custom().setId(2).setAmount(2000).setTermInMonths(84).build();
    final LoanDescriptor ldOver = new LoanDescriptor(loanOver);
    final LoanDescriptor ldUnder = new LoanDescriptor(loanUnder);
    final ParticipationDescriptor pdOver = mockParticipationDescriptor(loanOver);
    final ParticipationDescriptor pdUnder = mockParticipationDescriptor(loanUnder);
    final Investment iUnder = Investment.fresh((MarketplaceLoan) loanUnder, 200);
    final InvestmentDescriptor idUnder = new InvestmentDescriptor(iUnder, loanUnder);
    final Investment iOver = Investment.fresh((MarketplaceLoan) loanOver, 200);
    final InvestmentDescriptor idOver = new InvestmentDescriptor(iOver, loanOver);
    assertSoftly(softly -> {
        softly.assertThat(strategy.getApplicableLoans(Arrays.asList(ldOver, ldUnder))).containsOnly(ldUnder);
        softly.assertThat(strategy.getApplicableParticipations(Arrays.asList(pdOver, pdUnder))).containsOnly(pdUnder);
        softly.assertThat(strategy.getApplicableInvestments(Arrays.asList(idOver, idUnder))).isEmpty();
    });
}
Also used : Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) InvestmentDescriptor(com.github.robozonky.api.strategies.InvestmentDescriptor) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Test(org.junit.jupiter.api.Test)

Example 4 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.

the class ParsedStrategyTest method mockParticipationDescriptor.

private static ParticipationDescriptor mockParticipationDescriptor(final Loan loan) {
    final Participation p = mock(Participation.class);
    doReturn(loan.getTermInMonths()).when(p).getRemainingInstalmentCount();
    return new ParticipationDescriptor(p, loan);
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor)

Example 5 with ParticipationDescriptor

use of com.github.robozonky.api.strategies.ParticipationDescriptor in project robozonky by RoboZonky.

the class SessionTest method properReal.

@Test
void properReal() {
    final Loan l = Loan.custom().setId(1).setAmount(200).setRating(Rating.D).setRemainingInvestment(200).setMyInvestment(mockMyInvestment()).build();
    final Participation p = mock(Participation.class);
    when(p.getLoanId()).thenReturn(l.getId());
    when(p.getRemainingPrincipal()).thenReturn(BigDecimal.valueOf(200));
    final PurchaseStrategy s = mock(PurchaseStrategy.class);
    when(s.recommend(any(), any(), any())).thenAnswer(i -> {
        final Collection<ParticipationDescriptor> participations = i.getArgument(0);
        return participations.stream().map(ParticipationDescriptor::recommend).flatMap(o -> o.map(Stream::of).orElse(Stream.empty()));
    });
    final Zonky z = mockZonky(BigDecimal.valueOf(100_000));
    when(z.getLoan(eq(l.getId()))).thenReturn(l);
    final Authenticated auth = mockAuthentication(z);
    final Portfolio portfolio = spy(Portfolio.create(z, mockBalance(z)));
    final ParticipationDescriptor pd = new ParticipationDescriptor(p, l);
    final Collection<Investment> i = Session.purchase(portfolio, auth, Stream.of(pd), new RestrictedPurchaseStrategy(s, new Restrictions()), false);
    assertThat(i).hasSize(1);
    assertThat(this.getNewEvents()).hasSize(5);
    verify(z).purchase(eq(p));
    verify(portfolio).newBlockedAmount(eq(auth), argThat((a) -> a.getLoanId() == l.getId()));
}
Also used : SoftAssertions(org.assertj.core.api.SoftAssertions) Collection(java.util.Collection) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) Wallet(com.github.robozonky.api.remote.entities.Wallet) Zonky(com.github.robozonky.common.remote.Zonky) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Event(com.github.robozonky.api.notifications.Event) Test(org.junit.jupiter.api.Test) BigDecimal(java.math.BigDecimal) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PurchaseRequestedEvent(com.github.robozonky.api.notifications.PurchaseRequestedEvent) Authenticated(com.github.robozonky.app.authentication.Authenticated) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Condition(org.assertj.core.api.Condition) Rating(com.github.robozonky.api.remote.enums.Rating) Assertions(org.assertj.core.api.Assertions) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Participation(com.github.robozonky.api.remote.entities.Participation) Participation(com.github.robozonky.api.remote.entities.Participation) Authenticated(com.github.robozonky.app.authentication.Authenticated) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Zonky(com.github.robozonky.common.remote.Zonky) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) Stream(java.util.stream.Stream) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Test(org.junit.jupiter.api.Test) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest)

Aggregations

ParticipationDescriptor (com.github.robozonky.api.strategies.ParticipationDescriptor)10 Participation (com.github.robozonky.api.remote.entities.Participation)7 Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)7 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)7 Test (org.junit.jupiter.api.Test)7 PurchaseStrategy (com.github.robozonky.api.strategies.PurchaseStrategy)5 Restrictions (com.github.robozonky.api.remote.entities.Restrictions)4 Authenticated (com.github.robozonky.app.authentication.Authenticated)4 Portfolio (com.github.robozonky.app.portfolio.Portfolio)4 Collection (java.util.Collection)4 Stream (java.util.stream.Stream)4 Event (com.github.robozonky.api.notifications.Event)3 PurchaseRequestedEvent (com.github.robozonky.api.notifications.PurchaseRequestedEvent)3 Wallet (com.github.robozonky.api.remote.entities.Wallet)3 MarketplaceLoan (com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan)3 Rating (com.github.robozonky.api.remote.enums.Rating)3 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)3 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)3 Zonky (com.github.robozonky.common.remote.Zonky)3 BigDecimal (java.math.BigDecimal)3