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