Subversion Repositories Integrator Subversion

Rev

Rev 764 | Rev 779 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

package br.com.sl.core;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import br.com.kronus.core.Timeframe;
import br.com.sl.domain.dto.robo.ProfitTick;
import br.com.sl.domain.model.Candle;
import br.com.sl.domain.model.tipos.TipoPeriodoCandle;
import br.com.sl.shared.ExcelDataUtils;

public class ExcelProfitDataProvider implements ProfitDataProvider {

        private final Path excelFile;
    private final String sheetName;

    // índices de coluna (0 = A, 1 = B, etc.)
    private final int colAsset = 0;
    private final int colDate  = 1;
    private final int colTime  = 2;
    private final int colLast  = 3;

    // formatos da data e hora, conforme Excel
    private final DateTimeFormatter fmtData = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    private final DateTimeFormatter fmtHora = DateTimeFormatter.ofPattern("HH:mm:ss");
   
    private static final DateTimeFormatter STRING_DATA_FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
    private static final DateTimeFormatter STRING_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");

    public ExcelProfitDataProvider(Path excelFile, String sheetName) {
        this.excelFile = excelFile;
        this.sheetName = sheetName;
    }
   
    @Override
    public Map<String, ProfitTick> readCurrentTicks() {
        // TODO Auto-generated method stub
        return null;
    }
   
    /**
     * Lê o arquivo Excel dentro do resources.
     * Exemplo de uso:
     *    lerCandles("/dados/Dados Trade 20251117.xlsx");
     * @throws IOException
     */

    public List<Candle> lerCandles() throws IOException {
        List<Candle> candles = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(excelFile.toFile()); Workbook workbook = new XSSFWorkbook(fis)) {
            int numberOfSheets = workbook.getNumberOfSheets();

            for (int i = 0; i < numberOfSheets; i++) {
                Sheet sheet = workbook.getSheetAt(i);
                if (sheet.getSheetName().equals(sheetName)) {
                        boolean firstRow = true;
       
                        for (Row row : sheet) {
                            if (firstRow) {
                                firstRow = false; // pula o cabeçalho
                                continue;
                            }
                           
                            // 0 = Contador
                            // 1 = Ativo
                            // 2 = Dia
                            // 3 = Hora
                            // 4 = Abertura
                            // 5 = Máxima
                            // 6 = Mínima
                            // 7 = Fechamento
       
                            Cell contadorCell   = row.getCell(0);
                            Cell ativoCell      = row.getCell(1);
                            Cell dataCell       = row.getCell(2);
                            Cell horaCell       = row.getCell(3);
                            Cell aberturaCell   = row.getCell(4);
                            Cell maximaCell     = row.getCell(5);
                            Cell minimaCell     = row.getCell(6);
                            Cell fechamentoCell = row.getCell(7);
                            Cell finalizadoCell = row.getCell(8);
       
                            if (!ExcelDataUtils.isNumeric(aberturaCell) || !ExcelDataUtils.isNumeric(maximaCell)
                                        || !ExcelDataUtils.isNumeric(minimaCell) || !ExcelDataUtils.isNumeric(fechamentoCell)) {
                                continue;
                            }
                           
                            Integer contador = BigDecimal.valueOf(contadorCell.getNumericCellValue()).intValue();
                            String ativoDescricao = ativoCell.getStringCellValue();
                           
                            LocalDate data = ExcelDataUtils.lerData(dataCell);
                            LocalTime hora = ExcelDataUtils.lerHora(horaCell);
                            LocalDateTime dataHora = LocalDateTime.of(data, hora);
       
                            BigDecimal abertura = BigDecimal.valueOf(aberturaCell.getNumericCellValue());
                            BigDecimal topo = BigDecimal.valueOf(maximaCell.getNumericCellValue());
                            BigDecimal fundo = BigDecimal.valueOf(minimaCell.getNumericCellValue());
                            BigDecimal fechamento = BigDecimal.valueOf(fechamentoCell.getNumericCellValue());
                           
                            String finalizado = finalizadoCell.getStringCellValue();
                            if (finalizado.equals("S")) {
                                Candle candle = new Candle(contador, ativoDescricao, dataHora, abertura, topo, fundo, fechamento, TipoPeriodoCandle.M1.getValor());
                                candles.add(candle);
                            }
                        }
                }
            }
        } catch (EncryptedDocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        return candles;
    }
   
    public static List<Candle> inverterLista(List<Candle> candles) {
        List<Candle> invertida = new ArrayList<>(candles);
        Collections.reverse(invertida);
        return invertida;
    }

    public static List<Candle> adicionarContadores(List<Candle> candles) {
        Integer contador = 1;
        List<Candle> comContadores = new ArrayList<>();
        for (Candle candle : candles) {
                candle.setContadorCandle(contador);
                comContadores.add(candle);
                contador++;
        }
        return comContadores;
    }
   
    /**
     * 1 minuto = 1, 5 minutos = 2, 15 minutos = 3, 1 dia = 4
     */

    private Timeframe resolveTipoTemporizador(String sheetName) {
        if (sheetName == null) return null;

        String name = sheetName.toUpperCase(Locale.ROOT);

        if (name.startsWith("1 MIN"))  return Timeframe.M1;
        if (name.startsWith("5 MIN"))  return Timeframe.M5;
        if (name.startsWith("15 MIN")) return Timeframe.M15;
        if (name.startsWith("1 DIA"))  return Timeframe.D1;

        return null;
    }

}