Rev 764 | Rev 771 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 761 | blopes | 1 | package br.com.sl.core; |
| 2 | |||
| 767 | blopes | 3 | import java.io.FileInputStream; |
| 761 | blopes | 4 | import java.io.IOException; |
| 5 | import java.io.InputStream; |
||
| 6 | import java.math.BigDecimal; |
||
| 767 | blopes | 7 | import java.nio.file.Path; |
| 761 | blopes | 8 | import java.text.SimpleDateFormat; |
| 767 | blopes | 9 | import java.time.LocalDate; |
| 761 | blopes | 10 | import java.time.LocalDateTime; |
| 767 | blopes | 11 | import java.time.LocalTime; |
| 761 | blopes | 12 | import java.time.ZoneId; |
| 13 | import java.time.format.DateTimeFormatter; |
||
| 14 | import java.util.ArrayList; |
||
| 15 | import java.util.Collections; |
||
| 16 | import java.util.Date; |
||
| 17 | import java.util.List; |
||
| 18 | import java.util.Locale; |
||
| 19 | import java.util.Map; |
||
| 20 | |||
| 21 | import org.apache.poi.EncryptedDocumentException; |
||
| 22 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
||
| 23 | import org.apache.poi.ss.usermodel.Cell; |
||
| 24 | import org.apache.poi.ss.usermodel.CellType; |
||
| 25 | import org.apache.poi.ss.usermodel.DateUtil; |
||
| 26 | import org.apache.poi.ss.usermodel.Row; |
||
| 27 | import org.apache.poi.ss.usermodel.Sheet; |
||
| 28 | import org.apache.poi.ss.usermodel.Workbook; |
||
| 29 | import org.apache.poi.ss.usermodel.WorkbookFactory; |
||
| 767 | blopes | 30 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 761 | blopes | 31 | |
| 32 | import br.com.kronus.core.Timeframe; |
||
| 33 | import br.com.sl.domain.dto.robo.ProfitTick; |
||
| 34 | import br.com.sl.domain.model.Candle; |
||
| 35 | import br.com.sl.domain.model.tipos.TipoPeriodo; |
||
| 36 | import br.com.sl.domain.model.tipos.TipoPeriodoCandle; |
||
| 767 | blopes | 37 | import br.com.sl.shared.ExcelDataUtils; |
| 761 | blopes | 38 | |
| 39 | public class ExcelProfitHistoricoDataProvider implements ProfitDataProvider { |
||
| 40 | |||
| 767 | blopes | 41 | private final Path excelFile; |
| 761 | blopes | 42 | private final String sheetName; |
| 43 | |||
| 44 | private static final DateTimeFormatter STRING_DATA_FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); |
||
| 45 | private static final DateTimeFormatter STRING_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); |
||
| 46 | |||
| 767 | blopes | 47 | public ExcelProfitHistoricoDataProvider(Path excelFile, String sheetName) { |
| 48 | this.excelFile = excelFile; |
||
| 761 | blopes | 49 | this.sheetName = sheetName; |
| 50 | } |
||
| 51 | |||
| 52 | @Override |
||
| 53 | public Map<String, ProfitTick> readCurrentTicks() { |
||
| 54 | // TODO Auto-generated method stub |
||
| 55 | return null; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Lê o arquivo Excel dentro do resources. |
||
| 60 | * Exemplo de uso: |
||
| 61 | * lerCandles("/dados/Dados Trade 20251117.xlsx"); |
||
| 62 | * @throws IOException |
||
| 63 | */ |
||
| 64 | public List<Candle> lerCandles() throws IOException { |
||
| 65 | List<Candle> candles = new ArrayList<>(); |
||
| 66 | |||
| 767 | blopes | 67 | try (FileInputStream fis = new FileInputStream(excelFile.toFile()); Workbook workbook = new XSSFWorkbook(fis)) { |
| 761 | blopes | 68 | int numberOfSheets = workbook.getNumberOfSheets(); |
| 69 | |||
| 70 | for (int i = 0; i < numberOfSheets; i++) { |
||
| 71 | Sheet sheet = workbook.getSheetAt(i); |
||
| 767 | blopes | 72 | if (sheet.getSheetName().equals(sheetName)) { |
| 73 | |||
| 74 | for (Row row : sheet) { |
||
| 75 | |||
| 76 | // 0 = Ativo |
||
| 77 | // 1 = Dia |
||
| 78 | // 2 = Hora |
||
| 79 | // 3 = Abertura |
||
| 80 | // 4 = Máxima |
||
| 81 | // 5 = Mínima |
||
| 82 | // 6 = Fechamento |
||
| 83 | // 7 = Volume |
||
| 84 | // 8 = ... |
||
| 85 | |||
| 86 | Cell ativoCell = row.getCell(0); |
||
| 87 | Cell dataCell = row.getCell(1); |
||
| 88 | Cell horaCell = row.getCell(2); |
||
| 89 | Cell aberturaCell = row.getCell(3); |
||
| 90 | Cell maximaCell = row.getCell(4); |
||
| 91 | Cell minimaCell = row.getCell(5); |
||
| 92 | Cell fechamentoCell = row.getCell(6); |
||
| 93 | |||
| 94 | if (!ExcelDataUtils.isNumeric(aberturaCell) || !ExcelDataUtils.isNumeric(maximaCell) |
||
| 95 | || !ExcelDataUtils.isNumeric(minimaCell) || !ExcelDataUtils.isNumeric(fechamentoCell)) { |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | |||
| 99 | LocalDate data = ExcelDataUtils.lerData(dataCell); |
||
| 100 | LocalTime hora = ExcelDataUtils.lerHora(horaCell); |
||
| 101 | hora = hora.plusMinutes(6).plusSeconds(28); |
||
| 102 | LocalDateTime dataHora = LocalDateTime.of(data, hora); |
||
| 103 | |||
| 104 | String ativoDescricao = ativoCell.getStringCellValue(); |
||
| 105 | BigDecimal abertura = BigDecimal.valueOf(aberturaCell.getNumericCellValue()); |
||
| 106 | BigDecimal topo = BigDecimal.valueOf(maximaCell.getNumericCellValue()); |
||
| 107 | BigDecimal fundo = BigDecimal.valueOf(minimaCell.getNumericCellValue()); |
||
| 108 | BigDecimal fechamento = BigDecimal.valueOf(fechamentoCell.getNumericCellValue()); |
||
| 109 | |||
| 110 | Candle candle = new Candle(null, ativoDescricao, dataHora, abertura, topo, fundo, fechamento, TipoPeriodoCandle.M1.getValor()); |
||
| 111 | candles.add(candle); |
||
| 112 | } |
||
| 761 | blopes | 113 | } |
| 114 | } |
||
| 115 | } catch (EncryptedDocumentException e) { |
||
| 116 | // TODO Auto-generated catch block |
||
| 117 | e.printStackTrace(); |
||
| 767 | blopes | 118 | } |
| 761 | blopes | 119 | return adicionarContadores(inverterLista(candles)); |
| 120 | } |
||
| 121 | |||
| 122 | public static List<Candle> inverterLista(List<Candle> candles) { |
||
| 123 | List<Candle> invertida = new ArrayList<>(candles); |
||
| 124 | Collections.reverse(invertida); |
||
| 125 | return invertida; |
||
| 126 | } |
||
| 127 | |||
| 128 | public static List<Candle> adicionarContadores(List<Candle> candles) { |
||
| 129 | Integer contador = 1; |
||
| 130 | List<Candle> comContadores = new ArrayList<>(); |
||
| 131 | for (Candle candle : candles) { |
||
| 132 | candle.setContadorCandle(contador); |
||
| 133 | comContadores.add(candle); |
||
| 134 | contador++; |
||
| 135 | } |
||
| 136 | return comContadores; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * 1 minuto = 1, 5 minutos = 2, 15 minutos = 3, 1 dia = 4 |
||
| 141 | */ |
||
| 142 | private Timeframe resolveTipoTemporizador(String sheetName) { |
||
| 143 | if (sheetName == null) return null; |
||
| 144 | |||
| 145 | String name = sheetName.toUpperCase(Locale.ROOT); |
||
| 146 | |||
| 147 | if (name.startsWith("1 MIN")) return Timeframe.M1; |
||
| 148 | if (name.startsWith("5 MIN")) return Timeframe.M5; |
||
| 149 | if (name.startsWith("15 MIN")) return Timeframe.M15; |
||
| 150 | if (name.startsWith("1 DIA")) return Timeframe.D1; |
||
| 151 | |||
| 152 | return null; |
||
| 153 | } |
||
| 154 | |||
| 155 | } |