Rev 796 | 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.util.List; |
||
| 8 | import java.util.concurrent.CompletableFuture; |
||
| 9 | import java.util.concurrent.TimeUnit; |
||
| 10 | |||
| 11 | import com.ib.client.Bar; |
||
| 12 | import com.ib.client.Contract; |
||
| 13 | import com.ib.client.TagValue; |
||
| 14 | |||
| 15 | import br.com.kronus.ibkr.api.IbkrClient; |
||
| 16 | |||
| 17 | public class IbkrHistoricalDataService { |
||
| 18 | |||
| 19 | private final IbkrClient ibkr; |
||
| 20 | |||
| 21 | public IbkrHistoricalDataService(IbkrClient ibkr) { |
||
| 22 | this.ibkr = ibkr; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Solicita histórico para IBKR e grava tudo em CSV. |
||
| 27 | * |
||
| 28 | * @param contract contrato IBKR |
||
| 29 | * @param endDateTime "" para agora ou "yyyyMMdd HH:mm:ss" |
||
| 30 | * @param durationStr "2 D", "5 D", "1 W", "1 M", etc. |
||
| 31 | * @param barSizeSetting "1 min", "5 mins", "15 mins", "1 day", etc. |
||
| 32 | * @param whatToShow "TRADES", "MIDPOINT", etc. |
||
| 33 | */ |
||
| 34 | public void baixarHistoricoParaCsv( |
||
| 35 | int reqId, |
||
| 36 | Contract contract, |
||
| 37 | String endDateTime, |
||
| 38 | String durationStr, |
||
| 39 | String barSizeSetting, |
||
| 40 | String whatToShow, |
||
| 41 | Path arquivoCsv |
||
| 42 | ) throws Exception { |
||
| 43 | |||
| 44 | if (!Files.exists(arquivoCsv.getParent())) { |
||
| 45 | Files.createDirectories(arquivoCsv.getParent()); |
||
| 46 | } |
||
| 47 | |||
| 48 | try (BufferedWriter writer = Files.newBufferedWriter(arquivoCsv, |
||
| 49 | java.nio.file.StandardOpenOption.CREATE, |
||
| 50 | java.nio.file.StandardOpenOption.TRUNCATE_EXISTING)) { |
||
| 51 | |||
| 52 | writer.write("data;hora;open;high;low;close;volume;wap;count"); |
||
| 53 | writer.newLine(); |
||
| 54 | |||
| 55 | CompletableFuture<List<Bar>> fut = ibkr.prepararFutureHistorico(reqId); |
||
| 56 | |||
| 57 | ibkr.getClient().reqHistoricalData( |
||
| 58 | reqId, |
||
| 59 | contract, |
||
| 60 | endDateTime, |
||
| 61 | durationStr, |
||
| 62 | barSizeSetting, |
||
| 63 | whatToShow, |
||
| 64 | 1, |
||
| 65 | 2, |
||
| 66 | false, |
||
| 67 | (List<TagValue>) null |
||
| 68 | ); |
||
| 69 | |||
| 70 | List<Bar> bars = fut.get(60, TimeUnit.SECONDS); |
||
| 71 | |||
| 72 | for (Bar b : bars) { |
||
| 73 | String timeStr = b.time(); // ex: 20250125 10:05:00 |
||
| 74 | String dataStr = timeStr.substring(0, 8); |
||
| 75 | String horaStr = timeStr.substring(9); |
||
| 76 | |||
| 77 | String yyyy = dataStr.substring(0, 4); |
||
| 78 | String mm = dataStr.substring(4, 6); |
||
| 79 | String dd = dataStr.substring(6, 8); |
||
| 80 | String dataFormatada = yyyy + "-" + mm + "-" + dd; |
||
| 81 | |||
| 82 | String linha = String.join(";", |
||
| 83 | dataFormatada, |
||
| 84 | horaStr, |
||
| 85 | String.valueOf(b.open()), |
||
| 86 | String.valueOf(b.high()), |
||
| 87 | String.valueOf(b.low()), |
||
| 88 | String.valueOf(b.close()), |
||
| 89 | String.valueOf(b.volume()), |
||
| 90 | String.valueOf(b.wap()), |
||
| 91 | String.valueOf(b.count()) |
||
| 92 | ); |
||
| 93 | writer.write(linha); |
||
| 94 | writer.newLine(); |
||
| 95 | } |
||
| 96 | writer.flush(); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |