Subversion Repositories Integrator Subversion

Rev

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

Rev Author Line No. Line
760 blopes 1
package br.com.sl.core;
2
 
3
import java.io.FileInputStream;
4
import java.io.IOException;
5
import java.math.BigDecimal;
6
import java.nio.file.Path;
7
import java.time.LocalDate;
8
import java.time.LocalDateTime;
9
import java.time.LocalTime;
10
import java.time.format.DateTimeFormatter;
764 blopes 11
import java.util.ArrayList;
12
import java.util.Collections;
761 blopes 13
import java.util.List;
764 blopes 14
import java.util.Locale;
760 blopes 15
import java.util.Map;
16
 
764 blopes 17
import org.apache.poi.EncryptedDocumentException;
760 blopes 18
import org.apache.poi.ss.usermodel.Cell;
19
import org.apache.poi.ss.usermodel.Row;
20
import org.apache.poi.ss.usermodel.Sheet;
21
import org.apache.poi.ss.usermodel.Workbook;
22
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
23
 
764 blopes 24
import br.com.kronus.core.Timeframe;
760 blopes 25
import br.com.sl.domain.dto.robo.ProfitTick;
761 blopes 26
import br.com.sl.domain.model.Candle;
764 blopes 27
import br.com.sl.domain.model.tipos.TipoPeriodoCandle;
767 blopes 28
import br.com.sl.shared.ExcelDataUtils;
760 blopes 29
 
30
public class ExcelProfitDataProvider implements ProfitDataProvider {
31
 
764 blopes 32
        private final Path excelFile;
760 blopes 33
    private final String sheetName;
34
 
35
    // índices de coluna (0 = A, 1 = B, etc.)
36
    private final int colAsset = 0;
37
    private final int colDate  = 1;
38
    private final int colTime  = 2;
39
    private final int colLast  = 3;
40
 
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");
764 blopes 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");
760 blopes 47
 
48
    public ExcelProfitDataProvider(Path excelFile, String sheetName) {
49
        this.excelFile = excelFile;
50
        this.sheetName = sheetName;
51
    }
764 blopes 52
 
760 blopes 53
    @Override
54
    public Map<String, ProfitTick> readCurrentTicks() {
764 blopes 55
        // TODO Auto-generated method stub
56
        return null;
57
    }
58
 
59
    /**
60
     * Lê o arquivo Excel dentro do resources.
61
     * Exemplo de uso:
62
     *    lerCandles("/dados/Dados Trade 20251117.xlsx");
63
     * @throws IOException
64
     */
65
    public List<Candle> lerCandles() throws IOException {
66
        List<Candle> candles = new ArrayList<>();
760 blopes 67
 
68
        try (FileInputStream fis = new FileInputStream(excelFile.toFile()); Workbook workbook = new XSSFWorkbook(fis)) {
764 blopes 69
            int numberOfSheets = workbook.getNumberOfSheets();
760 blopes 70
 
764 blopes 71
            for (int i = 0; i < numberOfSheets; i++) {
72
                Sheet sheet = workbook.getSheetAt(i);
73
                if (sheet.getSheetName().equals(sheetName)) {
74
                        boolean firstRow = true;
75
 
76
                        for (Row row : sheet) {
77
                            if (firstRow) {
78
                                firstRow = false; // pula o cabeçalho
79
                                continue;
80
                            }
81
 
82
                            // 0 = Contador
767 blopes 83
                            // 1 = Ativo
84
                            // 2 = Dia
85
                            // 3 = Hora
86
                            // 4 = Abertura
87
                            // 5 = Máxima
88
                            // 6 = Mínima
89
                            // 7 = Fechamento
764 blopes 90
 
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
 
767 blopes 101
                            if (!ExcelDataUtils.isNumeric(aberturaCell) || !ExcelDataUtils.isNumeric(maximaCell)
102
                                        || !ExcelDataUtils.isNumeric(minimaCell) || !ExcelDataUtils.isNumeric(fechamentoCell)) {
764 blopes 103
                                continue;
104
                            }
105
 
106
                            Integer contador = BigDecimal.valueOf(contadorCell.getNumericCellValue()).intValue();
107
                            String ativoDescricao = ativoCell.getStringCellValue();
108
 
767 blopes 109
                            LocalDate data = ExcelDataUtils.lerData(dataCell);
110
                            LocalTime hora = ExcelDataUtils.lerHora(horaCell);
764 blopes 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);
122
                            }
123
                        }
760 blopes 124
                }
125
            }
764 blopes 126
        } catch (EncryptedDocumentException e) {
127
                        // TODO Auto-generated catch block
128
                        e.printStackTrace();
129
                }
130
        return candles;
131
    }
132
 
133
    public static List<Candle> inverterLista(List<Candle> candles) {
134
        List<Candle> invertida = new ArrayList<>(candles);
135
        Collections.reverse(invertida);
136
        return invertida;
137
    }
760 blopes 138
 
764 blopes 139
    public static List<Candle> adicionarContadores(List<Candle> candles) {
140
        Integer contador = 1;
141
        List<Candle> comContadores = new ArrayList<>();
142
        for (Candle candle : candles) {
143
                candle.setContadorCandle(contador);
144
                comContadores.add(candle);
145
                contador++;
760 blopes 146
        }
764 blopes 147
        return comContadores;
760 blopes 148
    }
764 blopes 149
 
150
    /**
151
     * 1 minuto = 1, 5 minutos = 2, 15 minutos = 3, 1 dia = 4
152
     */
153
    private Timeframe resolveTipoTemporizador(String sheetName) {
154
        if (sheetName == null) return null;
155
 
156
        String name = sheetName.toUpperCase(Locale.ROOT);
157
 
158
        if (name.startsWith("1 MIN"))  return Timeframe.M1;
159
        if (name.startsWith("5 MIN"))  return Timeframe.M5;
160
        if (name.startsWith("15 MIN")) return Timeframe.M15;
161
        if (name.startsWith("1 DIA"))  return Timeframe.D1;
162
 
163
        return null;
164
    }
165
 
760 blopes 166
}