Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 795 | blopes | 1 | package br.com.kronus.binance.futures; |
| 2 | |||
| 3 | import java.io.IOException; |
||
| 4 | import java.math.BigDecimal; |
||
| 5 | import java.nio.charset.StandardCharsets; |
||
| 6 | import java.nio.file.Files; |
||
| 7 | import java.nio.file.Path; |
||
| 8 | import java.nio.file.StandardOpenOption; |
||
| 9 | import java.text.DecimalFormat; |
||
| 10 | import java.text.DecimalFormatSymbols; |
||
| 11 | import java.time.ZoneId; |
||
| 12 | import java.time.format.DateTimeFormatter; |
||
| 13 | import java.util.Locale; |
||
| 14 | |||
| 15 | import br.com.kronus.binance.futures.model.CandleKLinha; |
||
| 16 | |||
| 17 | public class EscritorCsvCandle { |
||
| 18 | |||
| 19 | private final Path diretorioSaida; |
||
| 20 | private final ZoneId zoneId; |
||
| 21 | private final DateTimeFormatter formatoData; |
||
| 22 | private final DateTimeFormatter formatoHora; |
||
| 23 | private final DecimalFormat formatoDecimal; |
||
| 24 | |||
| 25 | public EscritorCsvCandle(Path diretorioSaida, ZoneId zoneId) { |
||
| 26 | this.diretorioSaida = diretorioSaida; |
||
| 27 | this.zoneId = zoneId; |
||
| 28 | |||
| 29 | this.formatoData = DateTimeFormatter.ofPattern("dd/MM/yyyy"); |
||
| 30 | this.formatoHora = DateTimeFormatter.ofPattern("HH:mm:ss"); |
||
| 31 | |||
| 32 | DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale("pt", "BR")); |
||
| 33 | symbols.setDecimalSeparator(','); |
||
| 34 | symbols.setGroupingSeparator('.'); |
||
| 35 | |||
| 36 | this.formatoDecimal = new DecimalFormat("#0.00", symbols); |
||
| 37 | this.formatoDecimal.setGroupingUsed(false); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Grava uma linha no CSV no formato: |
||
| 42 | * SYMBOL;dd/MM/yyyy;HH:mm:ss;open;high;low;close;volume;numberOfTrades |
||
| 43 | */ |
||
| 44 | public void escreverCandle(CandleKLinha candle) { |
||
| 45 | try { |
||
| 46 | Files.createDirectories(diretorioSaida); |
||
| 47 | |||
| 48 | // um arquivo por símbolo+intervalo, ex: BTCUSDT_1m.csv |
||
| 49 | String nomeArquivo = candle.getSimbolo() + "_" + candle.getIntervalo() + ".csv"; |
||
| 50 | Path arquivo = diretorioSaida.resolve(nomeArquivo); |
||
| 51 | |||
| 52 | var aberturaZoned = candle.getHorarioAberturaZoned(zoneId); |
||
| 53 | String data = aberturaZoned.toLocalDate().format(formatoData); |
||
| 54 | String hora = aberturaZoned.toLocalTime().format(formatoHora); |
||
| 55 | |||
| 56 | String linha = new StringBuilder() |
||
| 57 | .append(candle.getSimbolo()).append(';') |
||
| 58 | .append(data).append(';') |
||
| 59 | .append(hora).append(';') |
||
| 60 | .append(formatar(candle.getAbertura())).append(';') |
||
| 61 | .append(formatar(candle.getMaxima())).append(';') |
||
| 62 | .append(formatar(candle.getMinima())).append(';') |
||
| 63 | .append(formatar(candle.getFechamento())).append(';') |
||
| 64 | .append(formatar(candle.getVolume())).append(';') |
||
| 65 | .append(candle.getQuantidadeNegocios()) |
||
| 66 | .append(System.lineSeparator()) |
||
| 67 | .toString(); |
||
| 68 | |||
| 69 | Files.write(arquivo, |
||
| 70 | linha.getBytes(StandardCharsets.UTF_8), |
||
| 71 | StandardOpenOption.CREATE, |
||
| 72 | StandardOpenOption.APPEND); |
||
| 73 | |||
| 74 | } catch (IOException e) { |
||
| 75 | throw new RuntimeException("Erro ao escrever CSV de candle", e); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | public void escreverCandleHistorico(CandleKLinha candle) { |
||
| 80 | escreverCandle(candle); // usa o mesmo padrão do WebSocket |
||
| 81 | } |
||
| 82 | |||
| 83 | private String formatar(BigDecimal valor) { |
||
| 84 | return formatoDecimal.format(valor); |
||
| 85 | } |
||
| 86 | } |