Subversion Repositories Integrator Subversion

Rev

Rev 795 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
795 blopes 1
package br.com.kronus.ibkr.history;
2
 
3
import java.io.BufferedWriter;
4
import java.io.IOException;
5
import java.nio.file.Files;
6
import java.nio.file.Path;
7
import java.time.Instant;
796 blopes 8
import java.time.LocalDateTime;
795 blopes 9
import java.time.ZoneId;
10
import java.time.format.DateTimeFormatter;
11
 
12
import com.ib.client.Contract;
13
 
14
import br.com.kronus.ibkr.api.IbkrClient;
15
 
16
public class IbkrRealtimeBarsService {
17
 
18
    private final IbkrClient ibkr;
19
    private final ZoneId zoneId;
20
    private static final DateTimeFormatter DATA =
21
            DateTimeFormatter.ofPattern("yyyy-MM-dd");
22
    private static final DateTimeFormatter HORA =
23
            DateTimeFormatter.ofPattern("HH:mm:ss");
24
 
25
    public IbkrRealtimeBarsService(IbkrClient ibkr, ZoneId zoneId) {
26
        this.ibkr = ibkr;
27
        this.zoneId = zoneId;
28
    }
29
 
30
    /**
31
     * Inicia o stream de realtime bars (5s) e grava em CSV.
32
     * Cabeçalho: data;hora;open;high;low;close;volume;wap;count
33
     */
34
    public int iniciarStreamCsv(int reqId, Contract contract, Path arquivoCsv) throws IOException {
35
        if (!Files.exists(arquivoCsv.getParent())) {
36
            Files.createDirectories(arquivoCsv.getParent());
37
        }
38
 
39
        boolean novo = Files.notExists(arquivoCsv);
40
        BufferedWriter writer = Files.newBufferedWriter(arquivoCsv,
41
                java.nio.file.StandardOpenOption.CREATE,
42
                java.nio.file.StandardOpenOption.APPEND);
43
 
44
        if (novo) {
45
            writer.write("data;hora;open;high;low;close;volume;wap;count");
46
            writer.newLine();
47
            writer.flush();
48
        }
49
 
50
        ibkr.registrarRealtimeBarListener(reqId, (id, time, open, high, low, close, volume, wap, count) -> {
51
            try {
796 blopes 52
                Instant inst = Instant.ofEpochMilli(time);
795 blopes 53
                String dataStr = DATA.format(inst.atZone(zoneId));
54
                String horaStr = HORA.format(inst.atZone(zoneId));
55
 
56
                String linha = String.join(";",
57
                        dataStr,
58
                        horaStr,
59
                        String.valueOf(open),
60
                        String.valueOf(high),
61
                        String.valueOf(low),
62
                        String.valueOf(close),
63
                        String.valueOf(volume),
64
                        String.valueOf(wap),
65
                        String.valueOf(count)
66
                );
67
                writer.write(linha);
68
                writer.newLine();
69
                writer.flush();
70
            } catch (IOException e) {
71
                e.printStackTrace();
72
            }
73
        });
74
 
75
        ibkr.getClient().reqRealTimeBars(
76
                reqId,
77
                contract,
78
                5,
79
                "TRADES",
80
                true,
81
                null
82
        );
83
 
84
        return reqId;
85
    }
796 blopes 86
 
87
    public static LocalDateTime converterIbkrTime(long timeMillis) {
88
        return Instant.ofEpochMilli(timeMillis)
89
                .atZone(ZoneId.of("America/Sao_Paulo"))
90
                .toLocalDateTime();
91
    }
92
 
795 blopes 93
}