Subversion Repositories Integrator Subversion

Rev

Rev 783 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 783 Rev 785
Line 1... Line 1...
1
package br.com.sl.core;
1
package br.com.sl.core;
2
2
-
 
3
import java.io.BufferedReader;
3
import java.io.FileInputStream;
4
import java.io.FileInputStream;
4
import java.io.IOException;
5
import java.io.IOException;
5
import java.math.BigDecimal;
6
import java.math.BigDecimal;
-
 
7
import java.nio.charset.StandardCharsets;
6
import java.nio.file.Files;
8
import java.nio.file.Files;
7
import java.nio.file.Path;
9
import java.nio.file.Path;
8
import java.time.LocalDate;
10
import java.time.LocalDate;
9
import java.time.LocalDateTime;
11
import java.time.LocalDateTime;
10
import java.time.LocalTime;
12
import java.time.LocalTime;
Line 27... Line 29...
27
import br.com.ec.core.util.VerificadorUtil;
29
import br.com.ec.core.util.VerificadorUtil;
28
import br.com.kronus.core.Timeframe;
30
import br.com.kronus.core.Timeframe;
29
import br.com.sl.domain.dto.robo.ProfitTick;
31
import br.com.sl.domain.dto.robo.ProfitTick;
30
import br.com.sl.domain.model.Candle;
32
import br.com.sl.domain.model.Candle;
31
import br.com.sl.domain.model.tipos.TipoPeriodoCandle;
33
import br.com.sl.domain.model.tipos.TipoPeriodoCandle;
-
 
34
import br.com.sl.domain.util.BigDecimalUtils;
32
import br.com.sl.shared.ExcelDataUtils;
35
import br.com.sl.shared.ExcelDataUtils;
33
36
34
public class ExcelProfitDataProvider implements ProfitDataProvider {
37
public class ExcelProfitDataProvider implements ProfitDataProvider {
35
38
36
        private final Path excelFile;
39
        private final Path excelFile;
Line 40... Line 43...
40
    private final int colAsset = 0;
43
    private final int colAsset = 0;
41
    private final int colDate  = 1;
44
    private final int colDate  = 1;
42
    private final int colTime  = 2;
45
    private final int colTime  = 2;
43
    private final int colLast  = 3;
46
    private final int colLast  = 3;
44
47
-
 
48
    private static final DateTimeFormatter DATE_FORMAT_CSV = DateTimeFormatter.ofPattern("dd/MM/yyyy");
-
 
49
    private static final DateTimeFormatter TIME_FORMAT_CSV = DateTimeFormatter.ofPattern("HH:mm:ss");
-
 
50
   
45
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
51
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
46
    private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
52
    private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
47
53
48
    public ExcelProfitDataProvider(Path excelFile, String sheetName) {
54
    public ExcelProfitDataProvider(Path excelFile, String sheetName) {
49
        this.excelFile = excelFile;
55
        this.excelFile = excelFile;
Line 91... Line 97...
91
        String name = arquivo.getFileName().toString().toLowerCase(Locale.ROOT);
97
        String name = arquivo.getFileName().toString().toLowerCase(Locale.ROOT);
92
98
93
        if (name.endsWith(".xlsm") || name.endsWith(".xlsx") || name.endsWith(".xls")) {
99
        if (name.endsWith(".xlsm") || name.endsWith(".xlsx") || name.endsWith(".xls")) {
94
            lerCandlesDeArquivoExcel(arquivo, candles);
100
            lerCandlesDeArquivoExcel(arquivo, candles);
95
        } else if (name.endsWith(".csv")) {
101
        } else if (name.endsWith(".csv")) {
96
//            lerCandlesDeArquivoCsv(arquivo, candles);
-
 
-
 
102
            lerCandlesDeArquivoCsv(arquivo, candles);
97
        } else {
103
        } else {
98
            // Tipo não suportado, ignora ou loga
104
            // Tipo não suportado, ignora ou loga
99
        }
105
        }
100
    }
106
    }
101
107
Line 210... Line 216...
210
     * 3 = Abertura
216
     * 3 = Abertura
211
     * 4 = Máxima
217
     * 4 = Máxima
212
     * 5 = Mínima
218
     * 5 = Mínima
213
     * 6 = Fechamento
219
     * 6 = Fechamento
214
     */
220
     */
215
    private void lerCandlesDeArquivoCsv(Path arquivoCsv, List<Candle> candles) throws IOException {}
-
 
-
 
221
    private void lerCandlesDeArquivoCsv(Path arquivoCsv, List<Candle> candles) throws IOException {
-
 
222
        try (BufferedReader br = Files.newBufferedReader(arquivoCsv, StandardCharsets.UTF_8)) {
-
 
223
            String line;
-
 
224
            while ((line = br.readLine()) != null) {
-
 
225
-
 
226
                // Substitui line.isBlank() por trim().isEmpty()
-
 
227
                String trimmed = line.trim();
-
 
228
                if (trimmed.isEmpty()) {
-
 
229
                    continue;
-
 
230
                }
-
 
231
-
 
232
                // Ajuste aqui se o separador do seu CSV for vírgula
-
 
233
                String[] parts = trimmed.split(";", -1);
-
 
234
                if (parts.length < 7) {
-
 
235
                    continue;
-
 
236
                }
-
 
237
               
-
 
238
                String ativoDescricao = "";
-
 
239
                String dataStr        = "";
-
 
240
                String horaStr        = "";
-
 
241
                String aberturaStr    = "";
-
 
242
                String maximaStr      = "";
-
 
243
                String minimaStr      = "";
-
 
244
                String fechamentoStr  = "";
-
 
245
               
-
 
246
                ativoDescricao = parts[0].trim();
-
 
247
                dataStr        = parts[1].trim();
-
 
248
                if (parts.length >= 9) {
-
 
249
                        horaStr        = parts[2].trim();
-
 
250
                        aberturaStr    = parts[3].trim();
-
 
251
                        maximaStr      = parts[4].trim();
-
 
252
                        minimaStr      = parts[5].trim();
-
 
253
                        fechamentoStr  = parts[6].trim();
-
 
254
                } else {
-
 
255
                        horaStr        = "18:00:00";
-
 
256
                        aberturaStr    = parts[2].trim();
-
 
257
                        maximaStr      = parts[3].trim();
-
 
258
                        minimaStr      = parts[4].trim();
-
 
259
                        fechamentoStr  = parts[5].trim();
-
 
260
                }
-
 
261
-
 
262
                // Ignora header, caso exista
-
 
263
                if (ativoDescricao.equalsIgnoreCase("ativo")) {
-
 
264
                    continue;
-
 
265
                }
-
 
266
-
 
267
                if (!isNumericString(aberturaStr) ||
-
 
268
                    !isNumericString(maximaStr)   ||
-
 
269
                    !isNumericString(minimaStr)   ||
-
 
270
                    !isNumericString(fechamentoStr)) {
-
 
271
                    continue;
-
 
272
                }
-
 
273
-
 
274
                LocalDate data = LocalDate.parse(dataStr, DATE_FORMAT_CSV);
-
 
275
                LocalTime hora = LocalTime.parse(horaStr, TIME_FORMAT_CSV);
-
 
276
                LocalDateTime dataHora = LocalDateTime.of(data, hora);
-
 
277
-
 
278
                BigDecimal abertura   = BigDecimalUtils.converterStringEmBigDecimal(aberturaStr);
-
 
279
                BigDecimal topo       = BigDecimalUtils.converterStringEmBigDecimal(maximaStr);
-
 
280
                BigDecimal fundo      = BigDecimalUtils.converterStringEmBigDecimal(minimaStr);
-
 
281
                BigDecimal fechamento = BigDecimalUtils.converterStringEmBigDecimal(fechamentoStr);
-
 
282
-
 
283
                Candle candle = new Candle(
-
 
284
                        null,
-
 
285
                        ativoDescricao,
-
 
286
                        dataHora,
-
 
287
                        abertura,
-
 
288
                        topo,
-
 
289
                        fundo,
-
 
290
                        fechamento,
-
 
291
                        TipoPeriodoCandle.M1.getValor()
-
 
292
                );
-
 
293
                candles.add(candle);
-
 
294
            }
-
 
295
        }
-
 
296
   
-
 
297
       
-
 
298
    }
216
   
299
   
217
    private boolean isNumericString(String value) {
300
    private boolean isNumericString(String value) {
218
        if (value == null) return false;
301
        if (value == null) return false;
219
        String normalized = value.trim().replace(".", "").replace(",", ".");
302
        String normalized = value.trim().replace(".", "").replace(",", ".");
220
        if (normalized.isEmpty()) return false;
303
        if (normalized.isEmpty()) return false;