Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 778 | blopes | 1 | package br.com.robo.sim; |
| 2 | |||
| 3 | import java.io.IOException; |
||
| 4 | import java.io.InputStream; |
||
| 5 | import java.text.SimpleDateFormat; |
||
| 6 | import java.time.LocalDateTime; |
||
| 7 | import java.time.ZoneId; |
||
| 8 | import java.time.format.DateTimeFormatter; |
||
| 9 | import java.util.ArrayList; |
||
| 10 | import java.util.Collections; |
||
| 11 | import java.util.Date; |
||
| 12 | import java.util.List; |
||
| 13 | import java.util.Locale; |
||
| 14 | |||
| 15 | import org.apache.poi.EncryptedDocumentException; |
||
| 16 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
||
| 17 | import org.apache.poi.ss.usermodel.Cell; |
||
| 18 | import org.apache.poi.ss.usermodel.CellType; |
||
| 19 | import org.apache.poi.ss.usermodel.DateUtil; |
||
| 20 | import org.apache.poi.ss.usermodel.Row; |
||
| 21 | import org.apache.poi.ss.usermodel.Sheet; |
||
| 22 | import org.apache.poi.ss.usermodel.Workbook; |
||
| 23 | import org.apache.poi.ss.usermodel.WorkbookFactory; |
||
| 24 | |||
| 25 | import br.com.ec.core.util.VerificadorUtil; |
||
| 26 | import br.com.kronus.core.Candle; |
||
| 27 | import br.com.kronus.core.Timeframe; |
||
| 28 | |||
| 29 | public class CandleExcelReader { |
||
| 30 | |||
| 31 | private static final DateTimeFormatter STRING_DATA_FORMATTER = |
||
| 32 | DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); |
||
| 33 | |||
| 34 | private static final DateTimeFormatter STRING_DATE_TIME_FORMATTER = |
||
| 35 | DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Lê o arquivo Excel dentro do resources. |
||
| 39 | * Exemplo de uso: |
||
| 40 | * lerCandles("/dados/Dados Trade 20251117.xlsx"); |
||
| 41 | */ |
||
| 42 | public List<Candle> lerCandles(String resourcePath) throws IOException { |
||
| 43 | List<Candle> candles = new ArrayList<>(); |
||
| 44 | |||
| 45 | // Arquivo dentro de src/main/resources |
||
| 46 | InputStream is = getClass().getResourceAsStream(resourcePath); |
||
| 47 | if (is == null) { |
||
| 48 | throw new IOException("Arquivo não encontrado no resources: " + resourcePath); |
||
| 49 | } |
||
| 50 | |||
| 51 | try (Workbook workbook = WorkbookFactory.create(is)) { |
||
| 52 | int numberOfSheets = workbook.getNumberOfSheets(); |
||
| 53 | |||
| 54 | for (int i = 0; i < numberOfSheets; i++) { |
||
| 55 | Sheet sheet = workbook.getSheetAt(i); |
||
| 56 | String sheetName = sheet.getSheetName(); |
||
| 57 | boolean firstRow = true; |
||
| 58 | |||
| 59 | for (Row row : sheet) { |
||
| 60 | if (firstRow) { |
||
| 61 | firstRow = false; // pula o cabeçalho |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | |||
| 65 | // 0 = Ativo |
||
| 66 | // 1 = Dia |
||
| 67 | // 2 = Hora |
||
| 68 | // 3 = Abertura |
||
| 69 | // 4 = Máxima |
||
| 70 | // 5 = Mínima |
||
| 71 | // 6 = Fechamento |
||
| 72 | // 7 = Volume |
||
| 73 | // 8 = ... |
||
| 74 | |||
| 75 | Cell ativoCell = row.getCell(0); |
||
| 76 | Cell diaCell = row.getCell(1); |
||
| 77 | Cell horaCell = row.getCell(2); |
||
| 78 | Cell aberturaCell = row.getCell(3); |
||
| 79 | Cell maximaCell = row.getCell(4); |
||
| 80 | Cell minimaCell = row.getCell(5); |
||
| 81 | Cell fechamentoCell = row.getCell(6); |
||
| 82 | |||
| 83 | if (!isNumeric(aberturaCell) || !isNumeric(maximaCell) |
||
| 84 | || !isNumeric(minimaCell) || !isNumeric(fechamentoCell)) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | Date data = diaCell.getDateCellValue(); |
||
| 89 | String dia = new SimpleDateFormat("dd/MM/yyyy").format(data); |
||
| 90 | String hora = horaCell.getStringCellValue(); |
||
| 91 | // String hora = Double.toString(horaCell.getNumericCellValue()); |
||
| 92 | |||
| 93 | // Date dataHora = horaCell.getDateCellValue(); |
||
| 94 | // String hora = new SimpleDateFormat("hh:MM:ss").format(dataHora); |
||
| 95 | // String hora = String.valueOf(horaCell.getNumericCellValue()); |
||
| 96 | |||
| 97 | LocalDateTime time = getLocalDateTime(dia + " " + hora, STRING_DATA_FORMATTER); |
||
| 98 | |||
| 99 | double abertura = aberturaCell.getNumericCellValue(); |
||
| 100 | double topo = maximaCell.getNumericCellValue(); |
||
| 101 | double fundo = minimaCell.getNumericCellValue(); |
||
| 102 | double fechamento = fechamentoCell.getNumericCellValue(); |
||
| 103 | |||
| 104 | Candle candle = new Candle(time, abertura, topo, fundo, fechamento, Timeframe.M1); |
||
| 105 | candles.add(candle); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } catch (EncryptedDocumentException e) { |
||
| 109 | // TODO Auto-generated catch block |
||
| 110 | e.printStackTrace(); |
||
| 111 | } catch (InvalidFormatException e) { |
||
| 112 | // TODO Auto-generated catch block |
||
| 113 | e.printStackTrace(); |
||
| 114 | } |
||
| 115 | |||
| 116 | return adicionarContadores(inverterLista(candles)); |
||
| 117 | } |
||
| 118 | |||
| 119 | public static List<Candle> inverterLista(List<Candle> candles) { |
||
| 120 | List<Candle> invertida = new ArrayList<>(candles); |
||
| 121 | Collections.reverse(invertida); |
||
| 122 | return invertida; |
||
| 123 | } |
||
| 124 | |||
| 125 | public static List<Candle> adicionarContadores(List<Candle> candles) { |
||
| 126 | Integer contador = 1; |
||
| 127 | List<Candle> comContadores = new ArrayList<>(); |
||
| 128 | for (Candle candle : candles) { |
||
| 129 | candle.setContador(contador); |
||
| 130 | comContadores.add(candle); |
||
| 131 | contador++; |
||
| 132 | } |
||
| 133 | return comContadores; |
||
| 134 | } |
||
| 135 | |||
| 136 | public List<Candle> lerCandles(String resourcePath, Timeframe selecionarTipoTemporizador) throws IOException { |
||
| 137 | List<Candle> candles = new ArrayList<>(); |
||
| 138 | |||
| 139 | // Arquivo dentro de src/main/resources |
||
| 140 | InputStream is = getClass().getResourceAsStream(resourcePath); |
||
| 141 | if (is == null) { |
||
| 142 | throw new IOException("Arquivo não encontrado no resources: " + resourcePath); |
||
| 143 | } |
||
| 144 | |||
| 145 | try (Workbook workbook = WorkbookFactory.create(is)) { |
||
| 146 | |||
| 147 | int numberOfSheets = workbook.getNumberOfSheets(); |
||
| 148 | |||
| 149 | for (int i = 0; i < numberOfSheets; i++) { |
||
| 150 | |||
| 151 | Sheet sheet = workbook.getSheetAt(i); |
||
| 152 | String sheetName = sheet.getSheetName(); |
||
| 153 | |||
| 154 | Timeframe tipoTemporizador = resolveTipoTemporizador(sheetName); |
||
| 155 | if (tipoTemporizador == selecionarTipoTemporizador) { |
||
| 156 | boolean firstRow = true; |
||
| 157 | |||
| 158 | for (Row row : sheet) { |
||
| 159 | if (firstRow) { |
||
| 160 | firstRow = false; // pula o cabeçalho |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | |||
| 164 | // 0 = Data (data+hora) |
||
| 165 | // 1 = Abertura |
||
| 166 | // 2 = Máxima |
||
| 167 | // 3 = Mínima |
||
| 168 | // 4 = Fechamento |
||
| 169 | |||
| 170 | Cell dataCell = row.getCell(0); |
||
| 171 | Cell aberturaCell = row.getCell(1); |
||
| 172 | Cell maximaCell = row.getCell(2); |
||
| 173 | Cell minimaCell = row.getCell(3); |
||
| 174 | Cell fechamentoCell = row.getCell(4); |
||
| 175 | |||
| 176 | if (!isNumeric(aberturaCell) || !isNumeric(maximaCell) |
||
| 177 | || !isNumeric(minimaCell) || !isNumeric(fechamentoCell)) { |
||
| 178 | // linha vazia ou inválida |
||
| 179 | continue; |
||
| 180 | } |
||
| 181 | |||
| 182 | LocalDateTime time = getLocalDateTime(dataCell, STRING_DATE_TIME_FORMATTER); |
||
| 183 | if (time == null) { |
||
| 184 | // se não conseguir converter a data/hora, pode pular ou manter null |
||
| 185 | // aqui vou pular para evitar candle "incompleto" |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | double abertura = aberturaCell.getNumericCellValue(); |
||
| 190 | double topo = maximaCell.getNumericCellValue(); |
||
| 191 | double fundo = minimaCell.getNumericCellValue(); |
||
| 192 | double fechamento = fechamentoCell.getNumericCellValue(); |
||
| 193 | |||
| 194 | Candle candle = new Candle(time, abertura, topo, fundo, fechamento, tipoTemporizador); |
||
| 195 | candles.add(candle); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } catch (EncryptedDocumentException e) { |
||
| 200 | // TODO Auto-generated catch block |
||
| 201 | e.printStackTrace(); |
||
| 202 | } catch (InvalidFormatException e) { |
||
| 203 | // TODO Auto-generated catch block |
||
| 204 | e.printStackTrace(); |
||
| 205 | } |
||
| 206 | |||
| 207 | return candles; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * 1 minuto = 1, 5 minutos = 2, 15 minutos = 3, 1 dia = 4 |
||
| 212 | */ |
||
| 213 | private Timeframe resolveTipoTemporizador(String sheetName) { |
||
| 214 | if (sheetName == null) return null; |
||
| 215 | |||
| 216 | String name = sheetName.toUpperCase(Locale.ROOT); |
||
| 217 | |||
| 218 | if (name.startsWith("1 MIN")) return Timeframe.M1; |
||
| 219 | if (name.startsWith("5 MIN")) return Timeframe.M5; |
||
| 220 | if (name.startsWith("15 MIN")) return Timeframe.M15; |
||
| 221 | if (name.startsWith("1 DIA")) return Timeframe.D1; |
||
| 222 | |||
| 223 | return null; |
||
| 224 | } |
||
| 225 | |||
| 226 | private boolean isNumeric(Cell cell) { |
||
| 227 | if (cell == null) return false; |
||
| 228 | |||
| 229 | if (cell.getCellType() == CellType.NUMERIC.getCode()) { |
||
| 230 | return true; |
||
| 231 | } |
||
| 232 | |||
| 233 | if (cell.getCellType() == CellType.STRING.getCode()) { |
||
| 234 | try { |
||
| 235 | Double.parseDouble(cell.getStringCellValue().replace(",", ".")); |
||
| 236 | return true; |
||
| 237 | } catch (NumberFormatException e) { |
||
| 238 | return false; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | return false; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Converte a célula de data/hora do Excel para LocalDateTime. |
||
| 247 | */ |
||
| 248 | private LocalDateTime getLocalDateTime(Cell cell, DateTimeFormatter formatacaoDataTime) { |
||
| 249 | if (cell == null) { |
||
| 250 | return null; |
||
| 251 | } |
||
| 252 | |||
| 253 | // Caso seja data/hora numérica do Excel |
||
| 254 | if (cell.getCellType() == CellType.NUMERIC.getCode() && DateUtil.isCellDateFormatted(cell)) { |
||
| 255 | Date date = cell.getDateCellValue(); // disponível em todas as versões |
||
| 256 | if (date == null) { |
||
| 257 | return null; |
||
| 258 | } |
||
| 259 | return date.toInstant() |
||
| 260 | .atZone(ZoneId.systemDefault()) |
||
| 261 | .toLocalDateTime(); |
||
| 262 | } |
||
| 263 | |||
| 264 | // Caso venha como TEXT (por exemplo num CSV importado) |
||
| 265 | if (cell.getCellType() == CellType.STRING.getCode()) { |
||
| 266 | String text = cell.getStringCellValue(); |
||
| 267 | if (text == null || text.trim().isEmpty()) { |
||
| 268 | return null; |
||
| 269 | } |
||
| 270 | text = text.trim(); |
||
| 271 | try { |
||
| 272 | // Ajuste o pattern se seu Excel estiver em outro formato |
||
| 273 | return LocalDateTime.parse(text, formatacaoDataTime); |
||
| 274 | } catch (Exception e) { |
||
| 275 | return null; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | return null; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Converte a célula de data/hora do Excel para LocalDateTime. |
||
| 283 | */ |
||
| 284 | private LocalDateTime getLocalDateTime(String cell, DateTimeFormatter formatacaoDataTime) { |
||
| 285 | if (cell == null || cell.trim().isEmpty()) { |
||
| 286 | return null; |
||
| 287 | } |
||
| 288 | cell = cell.trim(); |
||
| 289 | try { |
||
| 290 | return LocalDateTime.parse(cell, formatacaoDataTime); |
||
| 291 | } catch (Exception e) { |
||
| 292 | return null; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | } |