Search in sources :

Example 6 with MarketplaceFilter

use of com.github.robozonky.strategy.natural.conditions.MarketplaceFilter in project robozonky by RoboZonky.

the class ParsedStrategyTest method matchesAlsoSellFilter.

@Test
void matchesAlsoSellFilter() {
    final MarketplaceFilter accepting = MarketplaceFilter.of(MarketplaceFilterCondition.alwaysAccepting());
    final Collection<MarketplaceFilter> filters = Collections.singleton(accepting);
    final DefaultValues v = new DefaultValues(DefaultPortfolio.PROGRESSIVE);
    final FilterSupplier s = new FilterSupplier(v, Collections.emptySet(), Collections.emptySet(), filters);
    final ParsedStrategy ps = new ParsedStrategy(v, Collections.emptyList(), Collections.emptyMap(), s);
    final Loan l = mockLoan(200_000);
    final LoanDescriptor ld = new LoanDescriptor(l);
    assertThat(ps.getApplicableLoans(Collections.singleton(ld))).isEmpty();
    final Participation p = mock(Participation.class);
    final ParticipationDescriptor pd = new ParticipationDescriptor(p, l);
    assertThat(ps.getApplicableParticipations(Collections.singleton(pd))).isEmpty();
}
Also used : Participation(com.github.robozonky.api.remote.entities.Participation) Loan(com.github.robozonky.api.remote.entities.sanitized.Loan) MarketplaceLoan(com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan) MarketplaceFilter(com.github.robozonky.strategy.natural.conditions.MarketplaceFilter) ParticipationDescriptor(com.github.robozonky.api.strategies.ParticipationDescriptor) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Test(org.junit.jupiter.api.Test)

Example 7 with MarketplaceFilter

use of com.github.robozonky.strategy.natural.conditions.MarketplaceFilter in project robozonky by RoboZonky.

the class SimpleStrategyService method parseOrThrow.

private static InvestmentStrategy parseOrThrow(final String strategy) {
    final ImmutableConfiguration config = ImmutableConfiguration.from(strategy);
    // set default values
    final DefaultValues d = new DefaultValues(DefaultPortfolio.EMPTY);
    d.setMinimumBalance(SimpleStrategyService.getMinimumBalance(config));
    d.setTargetPortfolioSize(SimpleStrategyService.getTargetPortfolioSize(config));
    d.setInvestmentShare(new DefaultInvestmentShare(SimpleStrategyService.getInvestmentShare(config)));
    final LoanRatingEnumeratedCondition c = new LoanRatingEnumeratedCondition();
    Stream.of(Rating.values()).filter(r -> StrategyFileProperty.REQUIRE_CONFIRMATION.getValue(r, config::getBoolean)).forEach(c::add);
    d.setConfirmationCondition(c);
    // assemble strategy
    final Map<Rating, InvestmentSize> investmentSizes = Stream.of(Rating.values()).collect(Collectors.toMap(Function.identity(), r -> {
        final int min = StrategyFileProperty.MINIMUM_LOAN_AMOUNT.getValue(r, config::getInt);
        final int max = StrategyFileProperty.MAXIMUM_LOAN_AMOUNT.getValue(r, config::getInt);
        return new InvestmentSize(min, max);
    }));
    final Collection<PortfolioShare> portfolio = Stream.of(Rating.values()).map(r -> {
        final int min = SimpleStrategyService.getShare(config, StrategyFileProperty.TARGET_SHARE, r);
        final int max = SimpleStrategyService.getShare(config, StrategyFileProperty.MAXIMUM_SHARE, r);
        return new PortfolioShare(r, min, max);
    }).collect(Collectors.toList());
    // filter loans with less than minimum term
    final Stream<Optional<MarketplaceFilter>> s1 = Stream.of(Rating.values()).map(r -> {
        final int min = StrategyFileProperty.MINIMUM_TERM.getValue(r, config::getInt);
        if (min < 2) {
            // there will be no loan with term smaller than 1
            return Optional.empty();
        }
        final MarketplaceFilterCondition f = new LoanTermCondition(0, min - 1);
        return Optional.of(getRatingFilter(r, f));
    });
    // filter loans with more than maximum term
    final Stream<Optional<MarketplaceFilter>> s2 = Stream.of(Rating.values()).map(r -> {
        final int max = StrategyFileProperty.MAXIMUM_TERM.getValue(r, config::getInt);
        if (max < 1) {
            return Optional.empty();
        }
        final MarketplaceFilterCondition f = new LoanTermCondition(max + 1);
        return Optional.of(getRatingFilter(r, f));
    });
    // filter loans with ask for less than minimum
    final Stream<Optional<MarketplaceFilter>> s3 = Stream.of(Rating.values()).map(r -> {
        final int min = StrategyFileProperty.MINIMUM_ASK.getValue(r, config::getInt);
        if (min < 2) {
            // amount smaller than 1 is 0, no need to have that filter
            return Optional.empty();
        }
        final MarketplaceFilterCondition f = new LoanAmountCondition(0, min - 1);
        return Optional.of(getRatingFilter(r, f));
    });
    // filter loans with ask more than maximum
    final Stream<Optional<MarketplaceFilter>> s4 = Stream.of(Rating.values()).map(r -> {
        final int max = StrategyFileProperty.MAXIMUM_ASK.getValue(r, config::getInt);
        if (max < 1) {
            return Optional.empty();
        }
        final MarketplaceFilterCondition f = new LoanAmountCondition(max + 1);
        return Optional.of(getRatingFilter(r, f));
    });
    // put all filters together
    final Collection<MarketplaceFilter> filters = Stream.of(s1, s2, s3, s4).flatMap(Function.identity()).flatMap(s -> s.map(Stream::of).orElse(Stream.empty())).collect(Collectors.toList());
    // and create the strategy
    final ParsedStrategy p = new ParsedStrategy(d, portfolio, investmentSizes, new FilterSupplier(d, filters));
    LOGGER.debug("Converted strategy: {}.", p);
    final InvestmentStrategy result = new NaturalLanguageInvestmentStrategy(p);
    return new SimpleStrategyService.ExclusivelyPrimaryMarketplaceInvestmentStrategy(result);
}
Also used : RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Arrays(java.util.Arrays) InvestmentStrategy(com.github.robozonky.api.strategies.InvestmentStrategy) LoggerFactory(org.slf4j.LoggerFactory) PurchaseStrategy(com.github.robozonky.api.strategies.PurchaseStrategy) ParsedStrategy(com.github.robozonky.strategy.natural.ParsedStrategy) Function(java.util.function.Function) DefaultValues(com.github.robozonky.strategy.natural.DefaultValues) BigDecimal(java.math.BigDecimal) LoanAmountCondition(com.github.robozonky.strategy.natural.conditions.LoanAmountCondition) LoanTermCondition(com.github.robozonky.strategy.natural.conditions.LoanTermCondition) MarketplaceFilter(com.github.robozonky.strategy.natural.conditions.MarketplaceFilter) LoanRatingEnumeratedCondition(com.github.robozonky.strategy.natural.conditions.LoanRatingEnumeratedCondition) Map(java.util.Map) DefaultPortfolio(com.github.robozonky.strategy.natural.DefaultPortfolio) FilterSupplier(com.github.robozonky.strategy.natural.FilterSupplier) Rating(com.github.robozonky.api.remote.enums.Rating) NaturalLanguageInvestmentStrategy(com.github.robozonky.strategy.natural.NaturalLanguageInvestmentStrategy) DefaultInvestmentShare(com.github.robozonky.strategy.natural.DefaultInvestmentShare) LoanDescriptor(com.github.robozonky.api.strategies.LoanDescriptor) Logger(org.slf4j.Logger) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) InvestmentSize(com.github.robozonky.strategy.natural.InvestmentSize) Restrictions(com.github.robozonky.api.remote.entities.Restrictions) StrategyService(com.github.robozonky.api.strategies.StrategyService) Stream(java.util.stream.Stream) PortfolioOverview(com.github.robozonky.api.strategies.PortfolioOverview) Optional(java.util.Optional) MarketplaceFilterCondition(com.github.robozonky.strategy.natural.conditions.MarketplaceFilterCondition) PortfolioShare(com.github.robozonky.strategy.natural.PortfolioShare) Collections(java.util.Collections) SellStrategy(com.github.robozonky.api.strategies.SellStrategy) MarketplaceFilter(com.github.robozonky.strategy.natural.conditions.MarketplaceFilter) Rating(com.github.robozonky.api.remote.enums.Rating) InvestmentSize(com.github.robozonky.strategy.natural.InvestmentSize) DefaultValues(com.github.robozonky.strategy.natural.DefaultValues) FilterSupplier(com.github.robozonky.strategy.natural.FilterSupplier) LoanAmountCondition(com.github.robozonky.strategy.natural.conditions.LoanAmountCondition) Optional(java.util.Optional) LoanRatingEnumeratedCondition(com.github.robozonky.strategy.natural.conditions.LoanRatingEnumeratedCondition) ParsedStrategy(com.github.robozonky.strategy.natural.ParsedStrategy) PortfolioShare(com.github.robozonky.strategy.natural.PortfolioShare) LoanTermCondition(com.github.robozonky.strategy.natural.conditions.LoanTermCondition) DefaultInvestmentShare(com.github.robozonky.strategy.natural.DefaultInvestmentShare) MarketplaceFilterCondition(com.github.robozonky.strategy.natural.conditions.MarketplaceFilterCondition) InvestmentStrategy(com.github.robozonky.api.strategies.InvestmentStrategy) NaturalLanguageInvestmentStrategy(com.github.robozonky.strategy.natural.NaturalLanguageInvestmentStrategy) NaturalLanguageInvestmentStrategy(com.github.robozonky.strategy.natural.NaturalLanguageInvestmentStrategy)

Example 8 with MarketplaceFilter

use of com.github.robozonky.strategy.natural.conditions.MarketplaceFilter in project robozonky by RoboZonky.

the class SimpleStrategyService method getRatingFilter.

private static MarketplaceFilter getRatingFilter(final Rating r, final MarketplaceFilterCondition condition) {
    final LoanRatingEnumeratedCondition c = new LoanRatingEnumeratedCondition();
    c.add(Collections.singleton(r));
    final MarketplaceFilter f = new MarketplaceFilter();
    f.when(Arrays.asList(c, condition));
    return f;
}
Also used : MarketplaceFilter(com.github.robozonky.strategy.natural.conditions.MarketplaceFilter) LoanRatingEnumeratedCondition(com.github.robozonky.strategy.natural.conditions.LoanRatingEnumeratedCondition)

Aggregations

MarketplaceFilter (com.github.robozonky.strategy.natural.conditions.MarketplaceFilter)8 Test (org.junit.jupiter.api.Test)5 LoanDescriptor (com.github.robozonky.api.strategies.LoanDescriptor)4 PortfolioOverview (com.github.robozonky.api.strategies.PortfolioOverview)4 Restrictions (com.github.robozonky.api.remote.entities.Restrictions)3 Loan (com.github.robozonky.api.remote.entities.sanitized.Loan)2 MarketplaceLoan (com.github.robozonky.api.remote.entities.sanitized.MarketplaceLoan)2 InvestmentStrategy (com.github.robozonky.api.strategies.InvestmentStrategy)2 PurchaseStrategy (com.github.robozonky.api.strategies.PurchaseStrategy)2 RecommendedLoan (com.github.robozonky.api.strategies.RecommendedLoan)2 SellStrategy (com.github.robozonky.api.strategies.SellStrategy)2 LoanRatingEnumeratedCondition (com.github.robozonky.strategy.natural.conditions.LoanRatingEnumeratedCondition)2 LoanTermCondition (com.github.robozonky.strategy.natural.conditions.LoanTermCondition)2 MarketplaceFilterCondition (com.github.robozonky.strategy.natural.conditions.MarketplaceFilterCondition)2 Participation (com.github.robozonky.api.remote.entities.Participation)1 Rating (com.github.robozonky.api.remote.enums.Rating)1 ParticipationDescriptor (com.github.robozonky.api.strategies.ParticipationDescriptor)1 RecommendedInvestment (com.github.robozonky.api.strategies.RecommendedInvestment)1 RecommendedParticipation (com.github.robozonky.api.strategies.RecommendedParticipation)1 StrategyService (com.github.robozonky.api.strategies.StrategyService)1