Rev 767 | Rev 782 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 767 | Rev 779 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | package br.com.sl.core; |
1 | package br.com.sl.core; |
| 2 | 2 | ||
| 3 | import java.io.FileInputStream; |
3 | import java.io.FileInputStream; |
| 4 | import java.io.IOException; |
4 | import java.io.IOException; |
| 5 | import java.math.BigDecimal; |
5 | import java.math.BigDecimal; |
| - | 6 | import java.nio.file.Files; |
|
| 6 | import java.nio.file.Path; |
7 | import java.nio.file.Path; |
| 7 | import java.time.LocalDate; |
8 | import java.time.LocalDate; |
| 8 | import java.time.LocalDateTime; |
9 | import java.time.LocalDateTime; |
| 9 | import java.time.LocalTime; |
10 | import java.time.LocalTime; |
| 10 | import java.time.format.DateTimeFormatter; |
11 | import java.time.format.DateTimeFormatter; |
| 11 | import java.util.ArrayList; |
12 | import java.util.ArrayList; |
| 12 | import java.util.Collections; |
13 | import java.util.Collections; |
| 13 | import java.util.List; |
14 | import java.util.List; |
| 14 | import java.util.Locale; |
15 | import java.util.Locale; |
| 15 | import java.util.Map; |
16 | import java.util.Map; |
| - | 17 | import java.util.stream.Stream; |
|
| 16 | 18 | ||
| 17 | import org.apache.poi.EncryptedDocumentException; |
19 | import org.apache.poi.EncryptedDocumentException; |
| 18 | import org.apache.poi.ss.usermodel.Cell; |
20 | import org.apache.poi.ss.usermodel.Cell; |
| 19 | import org.apache.poi.ss.usermodel.Row; |
21 | import org.apache.poi.ss.usermodel.Row; |
| 20 | import org.apache.poi.ss.usermodel.Sheet; |
22 | import org.apache.poi.ss.usermodel.Sheet; |
| Line 36... | Line 38... | ||
| 36 | private final int colAsset = 0; |
38 | private final int colAsset = 0; |
| 37 | private final int colDate = 1; |
39 | private final int colDate = 1; |
| 38 | private final int colTime = 2; |
40 | private final int colTime = 2; |
| 39 | private final int colLast = 3; |
41 | private final int colLast = 3; |
| 40 | 42 | ||
| 41 | // formatos da data e hora, conforme Excel
|
- | |
| 42 | private final DateTimeFormatter fmtData = DateTimeFormatter.ofPattern("dd/MM/yyyy"); |
- | |
| 43 | private final DateTimeFormatter fmtHora = DateTimeFormatter.ofPattern("HH:mm:ss"); |
- | |
| 44 | - | ||
| 45 | private static final DateTimeFormatter STRING_DATA_FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); |
- | |
| 46 | private static final DateTimeFormatter STRING_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); |
- | |
| - | 43 | private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); |
|
| - | 44 | private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); |
|
| 47 | 45 | ||
| 48 | public ExcelProfitDataProvider(Path excelFile, String sheetName) { |
46 | public ExcelProfitDataProvider(Path excelFile, String sheetName) { |
| 49 | this.excelFile = excelFile; |
47 | this.excelFile = excelFile; |
| 50 | this.sheetName = sheetName; |
48 | this.sheetName = sheetName; |
| 51 | }
|
49 | }
|
| Line 55... | Line 53... | ||
| 55 | // TODO Auto-generated method stub
|
53 | // TODO Auto-generated method stub
|
| 56 | return null; |
54 | return null; |
| 57 | }
|
55 | }
|
| 58 | 56 | ||
| 59 | /**
|
57 | /**
|
| 60 | * Lê o arquivo Excel dentro do resources.
|
- | |
| 61 | * Exemplo de uso:
|
- | |
| 62 | * lerCandles("/dados/Dados Trade 20251117.xlsx");
|
- | |
| 63 | * @throws IOException
|
- | |
| - | 58 | * Lê um ou vários arquivos:
|
|
| - | 59 | * - Se excelFile for um arquivo: lê apenas esse arquivo (.xlsx, .xls ou .csv)
|
|
| - | 60 | * - Se excelFile for uma pasta: lê todos os .xlsx, .xls e .csv da pasta.
|
|
| 64 | */
|
61 | */
|
| 65 | public List<Candle> lerCandles() throws IOException { |
62 | public List<Candle> lerCandles() throws IOException { |
| 66 | List<Candle> candles = new ArrayList<>(); |
63 | List<Candle> candles = new ArrayList<>(); |
| 67 | 64 | ||
| 68 | try (FileInputStream fis = new FileInputStream(excelFile.toFile()); Workbook workbook = new XSSFWorkbook(fis)) { |
- | |
| 69 | int numberOfSheets = workbook.getNumberOfSheets(); |
- | |
| - | 65 | if (Files.isDirectory(excelFile)) { |
|
| - | 66 | // Percorre todos os arquivos da pasta
|
|
| - | 67 | try (Stream<Path> stream = Files.list(excelFile)) { |
|
| - | 68 | stream |
|
| - | 69 | .filter(Files::isRegularFile) |
|
| - | 70 | .filter(p -> { |
|
| - | 71 | String name = p.getFileName().toString().toLowerCase(Locale.ROOT); |
|
| - | 72 | return name.endsWith(".xlsm") || name.endsWith(".xlsx") || name.endsWith(".xls") || name.endsWith(".csv"); |
|
| - | 73 | }) |
|
| - | 74 | .forEach(path -> { |
|
| - | 75 | try { |
|
| - | 76 | lerCandlesDeArquivo(path, candles); |
|
| - | 77 | } catch (IOException e) { |
|
| - | 78 | // aqui você pode trocar por log
|
|
| - | 79 | e.printStackTrace(); |
|
| - | 80 | }
|
|
| - | 81 | }); |
|
| - | 82 | }
|
|
| - | 83 | } else { |
|
| - | 84 | // Apenas um arquivo
|
|
| - | 85 | lerCandlesDeArquivo(excelFile, candles); |
|
| - | 86 | }
|
|
| 70 | 87 | ||
| - | 88 | return adicionarContadores(candles); |
|
| - | 89 | }
|
|
| - | 90 | ||
| - | 91 | /**
|
|
| - | 92 | * Decide se o arquivo é Excel ou CSV e delega para o método correto.
|
|
| - | 93 | */
|
|
| - | 94 | private void lerCandlesDeArquivo(Path arquivo, List<Candle> candles) throws IOException { |
|
| - | 95 | String name = arquivo.getFileName().toString().toLowerCase(Locale.ROOT); |
|
| - | 96 | ||
| - | 97 | if (name.endsWith(".xlsm") || name.endsWith(".xlsx") || name.endsWith(".xls")) { |
|
| - | 98 | lerCandlesDeArquivoExcel(arquivo, candles); |
|
| - | 99 | } else if (name.endsWith(".csv")) { |
|
| - | 100 | // lerCandlesDeArquivoCsv(arquivo, candles);
|
|
| - | 101 | } else { |
|
| - | 102 | // Tipo não suportado, ignora ou loga
|
|
| - | 103 | }
|
|
| - | 104 | }
|
|
| - | 105 | ||
| - | 106 | /**
|
|
| - | 107 | * Lê os candles de UM arquivo Excel e adiciona na lista passada.
|
|
| - | 108 | * (É basicamente o seu código original, só movido para cá)
|
|
| - | 109 | */
|
|
| - | 110 | private void lerCandlesDeArquivoExcel(Path arquivoExcel, List<Candle> candles) throws IOException { |
|
| - | 111 | try (FileInputStream fis = new FileInputStream(arquivoExcel.toFile()); |
|
| - | 112 | Workbook workbook = new XSSFWorkbook(fis)) { |
|
| - | 113 | ||
| - | 114 | int numberOfSheets = workbook.getNumberOfSheets(); |
|
| - | 115 | ||
| - | 116 | // LER APENAS OS ATIVOS
|
|
| - | 117 | List<String> ativos = new ArrayList<String>(); |
|
| 71 | for (int i = 0; i < numberOfSheets; i++) { |
118 | for (int i = 0; i < numberOfSheets; i++) { |
| 72 | Sheet sheet = workbook.getSheetAt(i); |
119 | Sheet sheet = workbook.getSheetAt(i); |
| 73 | if (sheet.getSheetName().equals(sheetName)) { |
120 | if (sheet.getSheetName().equals(sheetName)) { |
| 74 | boolean firstRow = true; |
121 | boolean firstRow = true; |
| 75 | 122 | ||
| Line 86... | Line 133... | ||
| 86 | // 4 = Abertura
|
133 | // 4 = Abertura
|
| 87 | // 5 = Máxima
|
134 | // 5 = Máxima
|
| 88 | // 6 = Mínima
|
135 | // 6 = Mínima
|
| 89 | // 7 = Fechamento
|
136 | // 7 = Fechamento
|
| 90 | 137 | ||
| 91 | Cell contadorCell = row.getCell(0); |
- | |
| 92 | Cell ativoCell = row.getCell(1); |
- | |
| 93 | Cell dataCell = row.getCell(2); |
- | |
| 94 | Cell horaCell = row.getCell(3); |
- | |
| 95 | Cell aberturaCell = row.getCell(4); |
- | |
| 96 | Cell maximaCell = row.getCell(5); |
- | |
| 97 | Cell minimaCell = row.getCell(6); |
- | |
| 98 | Cell fechamentoCell = row.getCell(7); |
- | |
| 99 | Cell finalizadoCell = row.getCell(8); |
- | |
| 100 | - | ||
| 101 | if (!ExcelDataUtils.isNumeric(aberturaCell) || !ExcelDataUtils.isNumeric(maximaCell) |
- | |
| 102 | || !ExcelDataUtils.isNumeric(minimaCell) || !ExcelDataUtils.isNumeric(fechamentoCell)) { |
- | |
| 103 | continue; |
- | |
| 104 | }
|
- | |
| 105 | - | ||
| 106 | Integer contador = BigDecimal.valueOf(contadorCell.getNumericCellValue()).intValue(); |
- | |
| 107 | String ativoDescricao = ativoCell.getStringCellValue(); |
- | |
| 108 | - | ||
| 109 | LocalDate data = ExcelDataUtils.lerData(dataCell); |
- | |
| 110 | LocalTime hora = ExcelDataUtils.lerHora(horaCell); |
- | |
| 111 | LocalDateTime dataHora = LocalDateTime.of(data, hora); |
- | |
| 112 | - | ||
| 113 | BigDecimal abertura = BigDecimal.valueOf(aberturaCell.getNumericCellValue()); |
- | |
| 114 | BigDecimal topo = BigDecimal.valueOf(maximaCell.getNumericCellValue()); |
- | |
| 115 | BigDecimal fundo = BigDecimal.valueOf(minimaCell.getNumericCellValue()); |
- | |
| 116 | BigDecimal fechamento = BigDecimal.valueOf(fechamentoCell.getNumericCellValue()); |
- | |
| 117 | - | ||
| 118 | String finalizado = finalizadoCell.getStringCellValue(); |
- | |
| 119 | if (finalizado.equals("S")) { |
- | |
| 120 | Candle candle = new Candle(contador, ativoDescricao, dataHora, abertura, topo, fundo, fechamento, TipoPeriodoCandle.M1.getValor()); |
- | |
| 121 | candles.add(candle); |
- | |
| - | 138 | String ativoCell = row.getCell(1).getStringCellValue(); |
|
| - | 139 | if (!ativos.contains(ativoCell)) { |
|
| - | 140 | ativos.add(ativoCell); |
|
| 122 | }
|
141 | }
|
| 123 | }
|
142 | }
|
| 124 | }
|
143 | }
|
| - | 144 | }
|
|
| - | 145 | ||
| - | 146 | for (String ativo : ativos) { |
|
| - | 147 | for (int i = 0; i < numberOfSheets; i++) { |
|
| - | 148 | Sheet sheet = workbook.getSheetAt(i); |
|
| - | 149 | if (sheet.getSheetName().equals(sheetName)) { |
|
| - | 150 | boolean firstRow = true; |
|
| - | 151 | ||
| - | 152 | for (Row row : sheet) { |
|
| - | 153 | if (firstRow) { |
|
| - | 154 | firstRow = false; // pula o cabeçalho |
|
| - | 155 | continue; |
|
| - | 156 | }
|
|
| - | 157 | ||
| - | 158 | // 0 = Contador
|
|
| - | 159 | // 1 = Ativo
|
|
| - | 160 | // 2 = Dia
|
|
| - | 161 | // 3 = Hora
|
|
| - | 162 | // 4 = Abertura
|
|
| - | 163 | // 5 = Máxima
|
|
| - | 164 | // 6 = Mínima
|
|
| - | 165 | // 7 = Fechamento
|
|
| - | 166 | ||
| - | 167 | Cell contadorCell = row.getCell(0); |
|
| - | 168 | Cell ativoCell = row.getCell(1); |
|
| - | 169 | Cell dataCell = row.getCell(2); |
|
| - | 170 | Cell horaCell = row.getCell(3); |
|
| - | 171 | Cell aberturaCell = row.getCell(4); |
|
| - | 172 | Cell maximaCell = row.getCell(5); |
|
| - | 173 | Cell minimaCell = row.getCell(6); |
|
| - | 174 | Cell fechamentoCell = row.getCell(7); |
|
| - | 175 | Cell finalizadoCell = row.getCell(8); |
|
| - | 176 | ||
| - | 177 | String ativoDescricao = ativoCell.getStringCellValue(); |
|
| - | 178 | ||
| - | 179 | if (!ExcelDataUtils.isNumeric(aberturaCell) || !ExcelDataUtils.isNumeric(maximaCell) |
|
| - | 180 | || !ExcelDataUtils.isNumeric(minimaCell) || !ExcelDataUtils.isNumeric(fechamentoCell) |
|
| - | 181 | || !ativo.equals(ativoDescricao)) { |
|
| - | 182 | continue; |
|
| - | 183 | }
|
|
| - | 184 | ||
| - | 185 | Integer contador = BigDecimal.valueOf(contadorCell.getNumericCellValue()).intValue(); |
|
| - | 186 | ||
| - | 187 | LocalDate data = ExcelDataUtils.lerData(dataCell); |
|
| - | 188 | LocalTime hora = ExcelDataUtils.lerHora(horaCell); |
|
| - | 189 | LocalDateTime dataHora = LocalDateTime.of(data, hora); |
|
| - | 190 | ||
| - | 191 | BigDecimal abertura = BigDecimal.valueOf(aberturaCell.getNumericCellValue()); |
|
| - | 192 | BigDecimal topo = BigDecimal.valueOf(maximaCell.getNumericCellValue()); |
|
| - | 193 | BigDecimal fundo = BigDecimal.valueOf(minimaCell.getNumericCellValue()); |
|
| - | 194 | BigDecimal fechamento = BigDecimal.valueOf(fechamentoCell.getNumericCellValue()); |
|
| - | 195 | ||
| - | 196 | String finalizado = finalizadoCell.getStringCellValue(); |
|
| - | 197 | if (finalizado.equals("S")) { |
|
| - | 198 | Candle candle = new Candle(contador, ativoDescricao, dataHora, abertura, topo, fundo, fechamento, TipoPeriodoCandle.M1.getValor()); |
|
| - | 199 | candles.add(candle); |
|
| - | 200 | }
|
|
| - | 201 | }
|
|
| - | 202 | }
|
|
| - | 203 | }
|
|
| 125 | }
|
204 | }
|
| 126 | } catch (EncryptedDocumentException e) { |
205 | } catch (EncryptedDocumentException e) { |
| 127 | // TODO Auto-generated catch block
|
- | |
| 128 | e.printStackTrace(); |
- | |
| 129 | }
|
- | |
| 130 | return candles; |
- | |
| - | 206 | e.printStackTrace(); |
|
| - | 207 | }
|
|
| 131 | }
|
208 | }
|
| 132 | 209 | ||
| - | 210 | /**
|
|
| - | 211 | * Lê os candles de UM arquivo CSV e adiciona na lista passada.
|
|
| - | 212 | * Espera layout:
|
|
| - | 213 | * 0 = Ativo
|
|
| - | 214 | * 1 = Dia (dd/MM/yyyy)
|
|
| - | 215 | * 2 = Hora (HH:mm:ss)
|
|
| - | 216 | * 3 = Abertura
|
|
| - | 217 | * 4 = Máxima
|
|
| - | 218 | * 5 = Mínima
|
|
| - | 219 | * 6 = Fechamento
|
|
| - | 220 | */
|
|
| - | 221 | private void lerCandlesDeArquivoCsv(Path arquivoCsv, List<Candle> candles) throws IOException {} |
|
| - | 222 | ||
| - | 223 | private boolean isNumericString(String value) { |
|
| - | 224 | if (value == null) return false; |
|
| - | 225 | String normalized = value.trim().replace(".", "").replace(",", "."); |
|
| - | 226 | if (normalized.isEmpty()) return false; |
|
| - | 227 | try { |
|
| - | 228 | new BigDecimal(normalized); |
|
| - | 229 | return true; |
|
| - | 230 | } catch (NumberFormatException e) { |
|
| - | 231 | return false; |
|
| - | 232 | }
|
|
| - | 233 | }
|
|
| - | 234 | ||
| 133 | public static List<Candle> inverterLista(List<Candle> candles) { |
235 | public static List<Candle> inverterLista(List<Candle> candles) { |
| 134 | List<Candle> invertida = new ArrayList<>(candles); |
236 | List<Candle> invertida = new ArrayList<>(candles); |
| 135 | Collections.reverse(invertida); |
237 | Collections.reverse(invertida); |
| 136 | return invertida; |
238 | return invertida; |
| 137 | }
|
239 | }
|
| 138 | 240 | ||
| - | 241 | // Contador separado para cada ativo
|
|
| 139 | public static List<Candle> adicionarContadores(List<Candle> candles) { |
242 | public static List<Candle> adicionarContadores(List<Candle> candles) { |
| 140 | Integer contador = 1; |
- | |
| 141 | List<Candle> comContadores = new ArrayList<>(); |
- | |
| - | 243 | Map<String, Integer> contadorPorAtivo = new java.util.HashMap<>(); |
|
| - | 244 | List<Candle> comContadores = new ArrayList<>(); |
|
| - | 245 | ||
| 142 | for (Candle candle : candles) { |
246 | for (Candle candle : candles) { |
| 143 | candle.setContadorCandle(contador); |
- | |
| 144 | comContadores.add(candle); |
- | |
| 145 | contador++;
|
- | |
| - | 247 | String ativo = candle.getNomeAtivo(); |
|
| - | 248 | ||
| - | 249 | Integer contadorAtual = contadorPorAtivo.get(ativo); |
|
| - | 250 | if (contadorAtual == null) { |
|
| - | 251 | contadorAtual = 0; |
|
| - | 252 | }
|
|
| - | 253 | ||
| - | 254 | contadorAtual++;
|
|
| - | 255 | contadorPorAtivo.put(ativo, contadorAtual); |
|
| - | 256 | candle.setContadorCandle(contadorAtual); |
|
| - | 257 | comContadores.add(candle); |
|
| 146 | }
|
258 | }
|
| 147 | return comContadores; |
259 | return comContadores; |
| 148 | }
|
260 | }
|
| 149 | 261 | ||
| 150 | /**
|
262 | /**
|