Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 783 | blopes | 1 | package br.com.sl.core; |
| 2 | |||
| 3 | import java.io.BufferedReader; |
||
| 4 | import java.io.InputStream; |
||
| 5 | import java.math.BigDecimal; |
||
| 6 | import java.time.LocalDate; |
||
| 7 | import java.time.LocalDateTime; |
||
| 8 | import java.time.LocalTime; |
||
| 9 | import java.time.format.DateTimeFormatter; |
||
| 10 | import java.util.ArrayList; |
||
| 11 | import java.util.Collections; |
||
| 12 | import java.util.List; |
||
| 13 | import java.util.Map; |
||
| 14 | |||
| 15 | import br.com.sl.domain.model.Candle; |
||
| 16 | import br.com.sl.domain.model.tipos.TipoPeriodoCandle; |
||
| 17 | import br.com.sl.domain.util.BigDecimalUtils; |
||
| 18 | |||
| 19 | public class ProfitDataProviderUtil { |
||
| 20 | |||
| 21 | private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd/MM/yyyy"); |
||
| 22 | private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss"); |
||
| 23 | |||
| 24 | public ProfitDataProviderUtil() {} |
||
| 25 | |||
| 26 | public static List<Candle> lerCandlesDeArquivo(BufferedReader reader, String nomeArquivo) throws Exception { |
||
| 27 | if (nomeArquivo.toLowerCase().endsWith(".csv")) { |
||
| 28 | return lerCsv(reader); |
||
| 29 | } else if (nomeArquivo.toLowerCase().endsWith(".xlsm") || nomeArquivo.toLowerCase().endsWith(".xlsx") || nomeArquivo.toLowerCase().endsWith(".xls")) { |
||
| 30 | return null; |
||
| 31 | // return lerExcel(reader); |
||
| 32 | } else { |
||
| 33 | throw new IllegalArgumentException("Formato não suportado: " + nomeArquivo); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | private static List<Candle> lerCsv(BufferedReader reader) throws Exception { |
||
| 38 | List<Candle> candles = new ArrayList<>(); |
||
| 39 | String linha; |
||
| 40 | while ((linha = reader.readLine()) != null) { |
||
| 41 | |||
| 42 | // Substitui line.isBlank() por trim().isEmpty() |
||
| 43 | String trimmed = linha.trim(); |
||
| 44 | if (trimmed.isEmpty()) { |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | // Ajuste aqui se o separador do seu CSV for vírgula |
||
| 49 | String[] parts = trimmed.split(";", -1); |
||
| 50 | if (parts.length < 7) { |
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | |||
| 54 | String ativoDescricao = ""; |
||
| 55 | String dataStr = ""; |
||
| 56 | String horaStr = ""; |
||
| 57 | String aberturaStr = ""; |
||
| 58 | String maximaStr = ""; |
||
| 59 | String minimaStr = ""; |
||
| 60 | String fechamentoStr = ""; |
||
| 61 | |||
| 62 | ativoDescricao = parts[0].trim(); |
||
| 63 | dataStr = parts[1].trim(); |
||
| 64 | if (parts.length >= 9) { |
||
| 65 | horaStr = parts[2].trim(); |
||
| 66 | aberturaStr = parts[3].trim(); |
||
| 67 | maximaStr = parts[4].trim(); |
||
| 68 | minimaStr = parts[5].trim(); |
||
| 69 | fechamentoStr = parts[6].trim(); |
||
| 70 | } else { |
||
| 71 | horaStr = "18:00:00"; |
||
| 72 | aberturaStr = parts[2].trim(); |
||
| 73 | maximaStr = parts[3].trim(); |
||
| 74 | minimaStr = parts[4].trim(); |
||
| 75 | fechamentoStr = parts[5].trim(); |
||
| 76 | } |
||
| 77 | |||
| 78 | // Ignora header, caso exista |
||
| 79 | if (ativoDescricao.equalsIgnoreCase("ativo")) { |
||
| 80 | continue; |
||
| 81 | } |
||
| 82 | |||
| 83 | if (!isNumericString(aberturaStr) || |
||
| 84 | !isNumericString(maximaStr) || |
||
| 85 | !isNumericString(minimaStr) || |
||
| 86 | !isNumericString(fechamentoStr)) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | LocalDate data = LocalDate.parse(dataStr, DATE_FORMAT); |
||
| 91 | LocalTime hora = LocalTime.parse(horaStr, TIME_FORMAT); |
||
| 92 | LocalDateTime dataHora = LocalDateTime.of(data, hora); |
||
| 93 | |||
| 94 | BigDecimal abertura = BigDecimalUtils.converterStringEmBigDecimal(aberturaStr); |
||
| 95 | BigDecimal topo = BigDecimalUtils.converterStringEmBigDecimal(maximaStr); |
||
| 96 | BigDecimal fundo = BigDecimalUtils.converterStringEmBigDecimal(minimaStr); |
||
| 97 | BigDecimal fechamento = BigDecimalUtils.converterStringEmBigDecimal(fechamentoStr); |
||
| 98 | |||
| 99 | Candle candle = new Candle( |
||
| 100 | null, |
||
| 101 | ativoDescricao, |
||
| 102 | dataHora, |
||
| 103 | abertura, |
||
| 104 | topo, |
||
| 105 | fundo, |
||
| 106 | fechamento, |
||
| 107 | TipoPeriodoCandle.M1.getValor() |
||
| 108 | ); |
||
| 109 | candles.add(candle); |
||
| 110 | } |
||
| 111 | |||
| 112 | return adicionarContadores(inverterLista(candles)); |
||
| 113 | } |
||
| 114 | |||
| 115 | private static boolean isNumericString(String value) { |
||
| 116 | if (value == null) return false; |
||
| 117 | String normalized = value.trim().replace(".", "").replace(",", "."); |
||
| 118 | if (normalized.isEmpty()) return false; |
||
| 119 | try { |
||
| 120 | new BigDecimal(normalized); |
||
| 121 | return true; |
||
| 122 | } catch (NumberFormatException e) { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // Contador separado para cada ativo |
||
| 128 | public static List<Candle> adicionarContadores(List<Candle> candles) { |
||
| 129 | Map<String, Integer> contadorPorAtivo = new java.util.HashMap<>(); |
||
| 130 | |||
| 131 | List<Candle> comContadores = new ArrayList<>(); |
||
| 132 | |||
| 133 | for (Candle candle : candles) { |
||
| 134 | |||
| 135 | // Ajuste aqui o getter conforme estiver na sua classe Candle |
||
| 136 | // Ex.: getAtivo(), getAtivoDescricao(), getTicker()... |
||
| 137 | String ativo = candle.getNomeAtivo(); |
||
| 138 | |||
| 139 | Integer contadorAtual = contadorPorAtivo.get(ativo); |
||
| 140 | if (contadorAtual == null) { |
||
| 141 | contadorAtual = 0; |
||
| 142 | } |
||
| 143 | |||
| 144 | contadorAtual++; |
||
| 145 | contadorPorAtivo.put(ativo, contadorAtual); |
||
| 146 | |||
| 147 | candle.setContadorCandle(contadorAtual); |
||
| 148 | comContadores.add(candle); |
||
| 149 | } |
||
| 150 | |||
| 151 | return comContadores; |
||
| 152 | } |
||
| 153 | |||
| 154 | public static List<Candle> inverterLista(List<Candle> candles) { |
||
| 155 | List<Candle> invertida = new ArrayList<>(candles); |
||
| 156 | Collections.reverse(invertida); |
||
| 157 | return invertida; |
||
| 158 | } |
||
| 159 | |||
| 160 | private static List<Candle> lerExcel(InputStream is) throws Exception { |
||
| 161 | /* |
||
| 162 | List<Candle> candles = new ArrayList<>(); |
||
| 163 | |||
| 164 | Workbook wb = WorkbookFactory.create(is); |
||
| 165 | |||
| 166 | for (int s = 0; s < wb.getNumberOfSheets(); s++) { |
||
| 167 | Sheet planilha = wb.getSheetAt(s); |
||
| 168 | |||
| 169 | boolean primeira = true; |
||
| 170 | |||
| 171 | for (Row row : planilha) { |
||
| 172 | if (row == null) continue; |
||
| 173 | |||
| 174 | if (primeira) { |
||
| 175 | primeira = false; |
||
| 176 | continue; |
||
| 177 | } |
||
| 178 | |||
| 179 | int cols = row.getLastCellNum(); |
||
| 180 | String[] col = new String[cols]; |
||
| 181 | |||
| 182 | for (int i = 0; i < cols; i++) { |
||
| 183 | Cell cell = row.getCell(i); |
||
| 184 | col[i] = (cell == null ? "" : formatarCelula(cell)); |
||
| 185 | } |
||
| 186 | |||
| 187 | Candle c = converterLinhaEmCandle(col); |
||
| 188 | if (c != null) candles.add(c); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | return candles; |
||
| 193 | */ |
||
| 194 | return null; |
||
| 195 | } |
||
| 196 | |||
| 197 | } |