Subversion Repositories Integrator Subversion

Rev

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.math.BigDecimal;
4
import java.time.LocalDateTime;
5
import java.time.ZoneId;
6
import java.time.ZonedDateTime;
7
import java.util.Date;
8
 
9
import br.com.kronus.binance.futures.model.CandleKLinha;
10
import br.com.kronus.core.Candle;
11
 
12
public final class ConversorCandle {
13
 
14
    private ConversorCandle() {}
15
 
16
    // =========================================================
17
    //   CandleKLinha  ->  Candle (modelo Kronus)
18
    // =========================================================
19
    public static Candle candleKLinhaParaCandle(CandleKLinha k, String ativo, String periodo, ZoneId zona) {
20
        if (k == null) return null;
21
 
22
        // Usa o método já existente no KLinha:
23
        ZonedDateTime abertura = k.getHorarioAberturaZoned(zona);
24
 
25
        Candle c = new Candle();
26
        c.setNomeAtivo(ativo);
27
        c.setPeriodo(periodo);
28
 
29
        // DATA / HORA no padrão da classe Candle
30
        Date dataHora = Date.from(abertura.toInstant());
31
        c.setDataHora(dataHora);
32
        c.setDataHoraTime(abertura.toLocalDateTime());
33
 
34
        // OHLCV
35
        c.setAbertura(k.getAbertura());
36
        c.setMaxima(k.getMaxima());
37
        c.setMinima(k.getMinima());
38
        c.setFechamento(k.getFechamento());
39
        c.setVolume(k.getVolume());
40
 
41
        return c;
42
    }
43
 
44
    // =========================================================
45
    //   Candle  ->  CandleKLinha (modelo Binance Futures)
46
    //   AGORA APENAS COM SETTERS  (SEM CONSTRUTOR)
47
    // =========================================================
48
    public static CandleKLinha candleParaCandleKLinha(Candle c, ZoneId zona) {
49
        if (c == null) return null;
50
 
51
        // Recupera horário do Candle do Kronus
52
        LocalDateTime ldt;
53
 
54
        if (c.getDataHora() != null) {
55
            ldt = c.getDataHora().toInstant().atZone(zona).toLocalDateTime();
56
        } else if (c.getDataHoraTime() != null) {
57
            ldt = c.getDataHoraTime();
58
        } else {
59
            throw new IllegalArgumentException("Candle sem dataHora nem dataHoraTime definido.");
60
        }
61
 
62
        ZonedDateTime abertura = ldt.atZone(zona);
63
 
64
        long openTime  = abertura.toInstant().toEpochMilli();
65
        long closeTime = abertura.plusMinutes(1).toInstant().toEpochMilli();
66
 
67
        BigDecimal vol = c.getVolume() != null ? c.getVolume() : BigDecimal.ZERO;
68
 
69
        CandleKLinha k = new CandleKLinha();
70
 
71
        // ===== AJUSTAR AQUI OS SETTERS CONFORME SUA CLASSE KLinha =====
72
        // nomes mais comuns:
73
        k.setHorarioAbertura(openTime);        // ou k.setOpenTime(...)
74
        k.setHorarioFechamento(closeTime);     // ou k.setCloseTime(...)
75
 
76
        k.setAbertura(c.getAbertura());
77
        k.setMaxima(c.getMaxima());
78
        k.setMinima(c.getMinima());
79
        k.setFechamento(c.getFechamento());
80
 
81
        // Volume é opcional
82
        k.setVolume(vol);
83
        // ===============================================================
84
 
85
        return k;
86
    }
87
}