Search in sources :

Example 1 with BacktestingCandle

use of tech.cassandre.trading.bot.domain.BacktestingCandle in project cassandre-trading-bot by cassandre-tech.

the class TickerFluxMock method marketService.

@Bean
@Primary
public MarketService marketService() {
    // Removes everything from table.
    backtestingCandleRepository.deleteAllInBatch();
    // Creates the mock.
    marketServiceBacktestingImplementation = new MarketServiceBacktestingImplementation(orderFlux, tradeFlux, orderRepository, tradeRepository, backtestingCandleRepository);
    // Getting the list of files to import and insert them in database.
    logger.info("Importing candles for backtesting...");
    Set<CurrencyPairDTO> currencyPairUsed = new HashSet<>();
    getCandlesFilesToLoad().stream().filter(resource -> resource.getFilename() != null).peek(resource -> logger.info("Importing {}...", resource.getFilename())).forEach(resource -> {
        try {
            // Insert the tickers in database.
            AtomicLong sequence = new AtomicLong(1);
            new CsvToBeanBuilder<ImportedCandle>(Files.newBufferedReader(resource.getFile().toPath())).withType(ImportedCandle.class).withIgnoreLeadingWhiteSpace(true).build().parse().forEach(importedCandle -> {
                logger.debug("Importing candle {}", importedCandle);
                BacktestingCandle candle = backtestingTickerMapper.mapToBacktestingCandle(importedCandle);
                // Specific fields in Backtesting candle.
                BacktestingCandleId id = new BacktestingCandleId();
                id.setTestSessionId(marketServiceBacktestingImplementation.getTestSessionId());
                id.setResponseSequenceId(sequence.getAndIncrement());
                id.setCurrencyPair(importedCandle.getCurrencyPair());
                candle.setId(id);
                // Save in database.
                backtestingCandleRepository.save(candle);
                // We build a list of currency pairs listed in files.
                currencyPairUsed.add(id.getCurrencyPairDTO());
            });
        } catch (IOException e) {
            logger.error("Impossible to load candles for backtesting: {}", e.getMessage());
        }
    });
    // Setting the flux size of each currency pair.
    currencyPairUsed.forEach(currencyPairDTO -> marketServiceBacktestingImplementation.getFluxSize().put(currencyPairDTO, backtestingCandleRepository.findByIdCurrencyPair(currencyPairDTO.toString()).size()));
    return marketServiceBacktestingImplementation;
}
Also used : Primary(org.springframework.context.annotation.Primary) Arrays(java.util.Arrays) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LoggerFactory(org.slf4j.LoggerFactory) TestConfiguration(org.springframework.boot.test.context.TestConfiguration) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) HashSet(java.util.HashSet) TradeFlux(tech.cassandre.trading.bot.batch.TradeFlux) CsvToBeanBuilder(com.opencsv.bean.CsvToBeanBuilder) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) BacktestingTickerMapper(tech.cassandre.trading.bot.util.mapper.BacktestingTickerMapper) Mappers(org.mapstruct.factory.Mappers) OrderFlux(tech.cassandre.trading.bot.batch.OrderFlux) BacktestingCandleRepository(tech.cassandre.trading.bot.repository.BacktestingCandleRepository) MarketServiceBacktestingImplementation(tech.cassandre.trading.bot.service.MarketServiceBacktestingImplementation) Resource(org.springframework.core.io.Resource) Logger(org.slf4j.Logger) BacktestingCandleId(tech.cassandre.trading.bot.domain.BacktestingCandleId) Files(java.nio.file.Files) Set(java.util.Set) TickerFlux(tech.cassandre.trading.bot.batch.TickerFlux) IOException(java.io.IOException) ApplicationContext(org.springframework.context.ApplicationContext) ImportedCandle(tech.cassandre.trading.bot.domain.ImportedCandle) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) OrderRepository(tech.cassandre.trading.bot.repository.OrderRepository) MarketService(tech.cassandre.trading.bot.service.MarketService) Bean(org.springframework.context.annotation.Bean) TradeRepository(tech.cassandre.trading.bot.repository.TradeRepository) BacktestingCandle(tech.cassandre.trading.bot.domain.BacktestingCandle) Collections(java.util.Collections) AtomicLong(java.util.concurrent.atomic.AtomicLong) ImportedCandle(tech.cassandre.trading.bot.domain.ImportedCandle) CurrencyPairDTO(tech.cassandre.trading.bot.dto.util.CurrencyPairDTO) MarketServiceBacktestingImplementation(tech.cassandre.trading.bot.service.MarketServiceBacktestingImplementation) IOException(java.io.IOException) BacktestingCandle(tech.cassandre.trading.bot.domain.BacktestingCandle) BacktestingCandleId(tech.cassandre.trading.bot.domain.BacktestingCandleId) HashSet(java.util.HashSet) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Aggregations

CsvToBeanBuilder (com.opencsv.bean.CsvToBeanBuilder)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Mappers (org.mapstruct.factory.Mappers)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 TestConfiguration (org.springframework.boot.test.context.TestConfiguration)1 ApplicationContext (org.springframework.context.ApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1 Primary (org.springframework.context.annotation.Primary)1 Resource (org.springframework.core.io.Resource)1 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)1 OrderFlux (tech.cassandre.trading.bot.batch.OrderFlux)1