Search in sources :

Example 26 with Investment

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

the class EmailingListenerTest method listeners.

@TestFactory
Stream<DynamicNode> listeners() throws MalformedURLException {
    // prepare data
    final Loan loan = Loan.custom().setId(66666).setAmount(100_000).setInterestRate(BigDecimal.TEN).setDatePublished(OffsetDateTime.now().minusMonths(2)).setName("Úvěr").setRegion(Region.JIHOCESKY).setPurpose(Purpose.AUTO_MOTO).setMainIncomeType(MainIncomeType.EMPLOYMENT).setRemainingInvestment(2000).setRating(Rating.AAAAA).setTermInMonths(25).setUrl(new URL("http://www.robozonky.cz")).build();
    final LoanDescriptor loanDescriptor = new LoanDescriptor(loan);
    final RecommendedLoan recommendation = loanDescriptor.recommend(1200, false).get();
    final Investment i = Investment.fresh((MarketplaceLoan) loan, 1000).build();
    // create events for listeners
    return Stream.of(forListener(SupportedListener.INVESTMENT_DELEGATED, new InvestmentDelegatedEvent(recommendation, "random")), forListener(SupportedListener.INVESTMENT_MADE, new InvestmentMadeEvent(i, loan, MAX_PORTFOLIO)), forListener(SupportedListener.INVESTMENT_SOLD, new InvestmentSoldEvent(i, loan, MAX_PORTFOLIO)), forListener(SupportedListener.INVESTMENT_SKIPPED, new InvestmentSkippedEvent(recommendation)), forListener(SupportedListener.INVESTMENT_REJECTED, new InvestmentRejectedEvent(recommendation, "random")), forListener(SupportedListener.LOAN_NO_LONGER_DELINQUENT, new LoanNoLongerDelinquentEvent(i, loan, LocalDate.now(), Collections.singletonList(mockCollectionNoEndDate()))), forListener(SupportedListener.LOAN_DEFAULTED, new LoanDefaultedEvent(i, loan, LocalDate.now(), Collections.singletonList(mockCollectionNoNote()))), forListener(SupportedListener.LOAN_NOW_DELINQUENT, new LoanNowDelinquentEvent(i, loan, LocalDate.now(), Collections.emptyList())), forListener(SupportedListener.LOAN_DELINQUENT_10_PLUS, new LoanDelinquent10DaysOrMoreEvent(i, loan, LocalDate.now().minusDays(11), Collections.emptyList())), forListener(SupportedListener.LOAN_DELINQUENT_30_PLUS, new LoanDelinquent30DaysOrMoreEvent(i, loan, LocalDate.now().minusDays(31), Collections.emptyList())), forListener(SupportedListener.LOAN_DELINQUENT_60_PLUS, new LoanDelinquent60DaysOrMoreEvent(i, loan, LocalDate.now().minusDays(61), Collections.emptyList())), forListener(SupportedListener.LOAN_DELINQUENT_90_PLUS, new LoanDelinquent90DaysOrMoreEvent(i, loan, LocalDate.now().minusDays(91), Collections.emptyList())), forListener(SupportedListener.LOAN_REPAID, new LoanRepaidEvent(i, loan, MAX_PORTFOLIO)), forListener(SupportedListener.BALANCE_ON_TARGET, new ExecutionStartedEvent(Collections.emptyList(), MAX_PORTFOLIO)), forListener(SupportedListener.BALANCE_UNDER_MINIMUM, new ExecutionStartedEvent(Collections.emptyList(), mockPortfolio(0))), forListener(SupportedListener.CRASHED, new RoboZonkyCrashedEvent(ReturnCode.ERROR_UNEXPECTED, new RuntimeException())), forListener(SupportedListener.REMOTE_OPERATION_FAILED, new RemoteOperationFailedEvent(new RuntimeException())), forListener(SupportedListener.DAEMON_FAILED, new RoboZonkyDaemonFailedEvent(new RuntimeException())), forListener(SupportedListener.INITIALIZED, new RoboZonkyInitializedEvent()), forListener(SupportedListener.ENDING, new RoboZonkyEndingEvent()), forListener(SupportedListener.TESTING, new RoboZonkyTestingEvent()), forListener(SupportedListener.UPDATE_DETECTED, new RoboZonkyUpdateDetectedEvent("1.2.3")), forListener(SupportedListener.EXPERIMENTAL_UPDATE_DETECTED, new RoboZonkyExperimentalUpdateDetectedEvent("1.3.0-beta-1")), forListener(SupportedListener.INVESTMENT_PURCHASED, new InvestmentPurchasedEvent(i, loan, MAX_PORTFOLIO)), forListener(SupportedListener.SALE_OFFERED, new SaleOfferedEvent(i, loan)));
}
Also used : RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) InvestmentSoldEvent(com.github.robozonky.api.notifications.InvestmentSoldEvent) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) LoanDefaultedEvent(com.github.robozonky.api.notifications.LoanDefaultedEvent) URL(java.net.URL) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) RoboZonkyEndingEvent(com.github.robozonky.api.notifications.RoboZonkyEndingEvent) RemoteOperationFailedEvent(com.github.robozonky.api.notifications.RemoteOperationFailedEvent) LoanDelinquent10DaysOrMoreEvent(com.github.robozonky.api.notifications.LoanDelinquent10DaysOrMoreEvent) LoanNoLongerDelinquentEvent(com.github.robozonky.api.notifications.LoanNoLongerDelinquentEvent) RoboZonkyDaemonFailedEvent(com.github.robozonky.api.notifications.RoboZonkyDaemonFailedEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) RoboZonkyInitializedEvent(com.github.robozonky.api.notifications.RoboZonkyInitializedEvent) RoboZonkyTestingEvent(com.github.robozonky.api.notifications.RoboZonkyTestingEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) LoanDelinquent30DaysOrMoreEvent(com.github.robozonky.api.notifications.LoanDelinquent30DaysOrMoreEvent) LoanDelinquent60DaysOrMoreEvent(com.github.robozonky.api.notifications.LoanDelinquent60DaysOrMoreEvent) RoboZonkyUpdateDetectedEvent(com.github.robozonky.api.notifications.RoboZonkyUpdateDetectedEvent) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) LoanDelinquent90DaysOrMoreEvent(com.github.robozonky.api.notifications.LoanDelinquent90DaysOrMoreEvent) InvestmentPurchasedEvent(com.github.robozonky.api.notifications.InvestmentPurchasedEvent) LoanNowDelinquentEvent(com.github.robozonky.api.notifications.LoanNowDelinquentEvent) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) RoboZonkyCrashedEvent(com.github.robozonky.api.notifications.RoboZonkyCrashedEvent) LoanRepaidEvent(com.github.robozonky.api.notifications.LoanRepaidEvent) RoboZonkyExperimentalUpdateDetectedEvent(com.github.robozonky.api.notifications.RoboZonkyExperimentalUpdateDetectedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) TestFactory(org.junit.jupiter.api.TestFactory)

Example 27 with Investment

use of com.github.robozonky.api.remote.entities.sanitized.Investment 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 28 with Investment

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

the class Selling method processInvestment.

private Optional<Investment> processInvestment(final Zonky zonky, final RecommendedInvestment r) {
    Events.fire(new SaleRequestedEvent(r));
    final Investment i = r.descriptor().item();
    if (isDryRun) {
        LOGGER.debug("Not sending sell request for loan #{} due to dry run.", i.getLoanId());
    } else {
        LOGGER.debug("Sending sell request for loan #{}.", i.getLoanId());
        zonky.sell(i);
        LOGGER.trace("Request over.");
    }
    Investment.putOnSmp(i);
    Events.fire(new SaleOfferedEvent(i, r.descriptor().related()));
    return Optional.of(i);
}
Also used : SaleRequestedEvent(com.github.robozonky.api.notifications.SaleRequestedEvent) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) RecommendedInvestment(com.github.robozonky.api.strategies.RecommendedInvestment) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment)

Example 29 with Investment

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

the class Selling method sell.

private void sell(final Portfolio portfolio, final SellStrategy strategy, final Authenticated auth) {
    final PortfolioOverview overview = portfolio.calculateOverview();
    final Set<InvestmentDescriptor> eligible = portfolio.getActiveForSecondaryMarketplace().parallel().map(i -> getDescriptor(i, auth)).collect(Collectors.toSet());
    Events.fire(new SellingStartedEvent(eligible, overview));
    final Collection<Investment> investmentsSold = strategy.recommend(eligible, overview).peek(r -> Events.fire(new SaleRecommendedEvent(r))).map(r -> auth.call(zonky -> processInvestment(zonky, r))).flatMap(o -> o.map(Stream::of).orElse(Stream.empty())).collect(Collectors.toSet());
    Events.fire(new SellingCompletedEvent(investmentsSold, portfolio.calculateOverview()));
}
Also used : SaleRecommendedEvent(com.github.robozonky.api.notifications.SaleRecommendedEvent) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Zonky(com.github.robozonky.common.remote.Zonky) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) SaleRequestedEvent(com.github.robozonky.api.notifications.SaleRequestedEvent) InvestmentDescriptor(com.github.robozonky.api.strategies.InvestmentDescriptor) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) RecommendedInvestment(com.github.robozonky.api.strategies.RecommendedInvestment) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) Stream(java.util.stream.Stream) 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) SaleOfferedEvent(com.github.robozonky.api.notifications.SaleOfferedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) Optional(java.util.Optional) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment) SellStrategy(com.github.robozonky.api.strategies.SellStrategy) SellingCompletedEvent(com.github.robozonky.api.notifications.SellingCompletedEvent) SellingStartedEvent(com.github.robozonky.api.notifications.SellingStartedEvent) SaleRecommendedEvent(com.github.robozonky.api.notifications.SaleRecommendedEvent) InvestmentDescriptor(com.github.robozonky.api.strategies.InvestmentDescriptor) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) RecommendedInvestment(com.github.robozonky.api.strategies.RecommendedInvestment) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) RawInvestment(com.github.robozonky.api.remote.entities.RawInvestment)

Example 30 with Investment

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

the class Session method invest.

/**
 * Request {@link ControlApi} to invest in a given loan, leveraging the {@link ConfirmationProvider}.
 * @param recommendation Loan to invest into.
 * @return True if investment successful. The investment is reflected in {@link #getResult()}.
 */
public boolean invest(final RecommendedLoan recommendation) {
    final LoanDescriptor loan = recommendation.descriptor();
    final int loanId = loan.item().getId();
    if (portfolioOverview.getCzkAvailable() < recommendation.amount().intValue()) {
        // should not be allowed by the calling code
        return false;
    }
    Events.fire(new InvestmentRequestedEvent(recommendation));
    final boolean seenBefore = state.getSeenLoans().stream().anyMatch(l -> isSameLoan(l, loanId));
    final ZonkyResponse response = investor.invest(recommendation, seenBefore);
    Session.LOGGER.debug("Response for loan {}: {}.", loanId, response);
    final String providerId = investor.getConfirmationProviderId().orElse("-");
    switch(response.getType()) {
        case REJECTED:
            return investor.getConfirmationProviderId().map(c -> {
                Events.fire(new InvestmentRejectedEvent(recommendation, providerId));
                // rejected through a confirmation provider => forget
                discard(loan);
                return false;
            }).orElseGet(() -> {
                // rejected due to no confirmation provider => make available for direct investment later
                Events.fire(new InvestmentSkippedEvent(recommendation));
                Session.LOGGER.debug("Loan #{} protected by CAPTCHA, will check back later.", loanId);
                skip(loan);
                return false;
            });
        case DELEGATED:
            final Event e = new InvestmentDelegatedEvent(recommendation, providerId);
            Events.fire(e);
            if (recommendation.isConfirmationRequired()) {
                // confirmation required, delegation successful => forget
                discard(loan);
            } else {
                // confirmation not required, delegation successful => make available for direct investment later
                skip(loan);
            }
            return false;
        case INVESTED:
            final int confirmedAmount = response.getConfirmedAmount().getAsInt();
            final Investment i = Investment.fresh(recommendation.descriptor().item(), confirmedAmount);
            markSuccessfulInvestment(i);
            Events.fire(new InvestmentMadeEvent(i, loan.item(), portfolioOverview));
            return true;
        case // still protected by CAPTCHA
        SEEN_BEFORE:
            return false;
        default:
            throw new IllegalStateException("Not possible.");
    }
}
Also used : RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) TransactionCategory(com.github.robozonky.api.remote.enums.TransactionCategory) LoggerFactory(org.slf4j.LoggerFactory) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) ControlApi(com.github.robozonky.api.remote.ControlApi) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) BlockedAmount(com.github.robozonky.api.remote.entities.BlockedAmount) Events(com.github.robozonky.app.Events) Authenticated(com.github.robozonky.app.authentication.Authenticated) Defaults(com.github.robozonky.internal.api.Defaults) ConfirmationProvider(com.github.robozonky.api.confirmations.ConfirmationProvider) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Logger(org.slf4j.Logger) Collection(java.util.Collection) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) Collectors(java.util.stream.Collectors) Event(com.github.robozonky.api.notifications.Event) List(java.util.List) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) Portfolio(com.github.robozonky.app.portfolio.Portfolio) Collections(java.util.Collections) LoanRecommendedEvent(com.github.robozonky.api.notifications.LoanRecommendedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) ExecutionCompletedEvent(com.github.robozonky.api.notifications.ExecutionCompletedEvent) InvestmentDelegatedEvent(com.github.robozonky.api.notifications.InvestmentDelegatedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) ExecutionStartedEvent(com.github.robozonky.api.notifications.ExecutionStartedEvent) Event(com.github.robozonky.api.notifications.Event) InvestmentRequestedEvent(com.github.robozonky.api.notifications.InvestmentRequestedEvent) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) LoanRecommendedEvent(com.github.robozonky.api.notifications.LoanRecommendedEvent) InvestmentSkippedEvent(com.github.robozonky.api.notifications.InvestmentSkippedEvent) InvestmentRejectedEvent(com.github.robozonky.api.notifications.InvestmentRejectedEvent) Investment(com.github.robozonky.api.remote.entities.sanitized.Investment) InvestmentMadeEvent(com.github.robozonky.api.notifications.InvestmentMadeEvent)

Aggregations

Investment (com.github.robozonky.api.remote.entities.sanitized.Investment)52 Test (org.junit.jupiter.api.Test)34 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)24 AbstractZonkyLeveragingTest (com.github.robozonky.app.AbstractZonkyLeveragingTest)20 BigDecimal (java.math.BigDecimal)20 Event (com.github.robozonky.api.notifications.Event)13 PortfolioOverview (com.github.robozonky.api.strategies.PortfolioOverview)13 Authenticated (com.github.robozonky.app.authentication.Authenticated)13 Zonky (com.github.robozonky.common.remote.Zonky)13 Collection (java.util.Collection)13 Assertions (org.assertj.core.api.Assertions)13 SoftAssertions (org.assertj.core.api.SoftAssertions)13 Mockito (org.mockito.Mockito)13 Collections (java.util.Collections)12 Stream (java.util.stream.Stream)11 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)10 Portfolio (com.github.robozonky.app.portfolio.Portfolio)9 Participation (com.github.robozonky.api.remote.entities.Participation)8 MarketplaceLoan (com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan)8 ExecutionCompletedEvent (com.github.robozonky.api.notifications.ExecutionCompletedEvent)7