Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 795 | blopes | 1 | package br.com.kronus.ibkr.futures; |
| 2 | |||
| 3 | import java.math.BigDecimal; |
||
| 4 | import java.time.Duration; |
||
| 5 | import java.util.Map; |
||
| 6 | import java.util.concurrent.CompletableFuture; |
||
| 7 | import java.util.concurrent.ConcurrentHashMap; |
||
| 8 | import java.util.concurrent.TimeUnit; |
||
| 9 | |||
| 10 | import com.ib.client.Contract; |
||
| 11 | import com.ib.client.Decimal; |
||
| 12 | import com.ib.client.Order; |
||
| 13 | |||
| 14 | import br.com.kronus.core.StatusOrdemFuturos; |
||
| 15 | import br.com.kronus.core.trade.ServicoOrdensFuturos; |
||
| 16 | import br.com.kronus.ibkr.api.IbkrClient; |
||
| 17 | import br.com.kronus.ibkr.api.IbkrMapper; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Implementação de ordens usando IBKR, mantendo a assinatura da Binance. |
||
| 21 | */ |
||
| 22 | public class ServicoOrdensFuturosIbkr implements ServicoOrdensFuturos { |
||
| 23 | |||
| 24 | private final IbkrClient ibkr; |
||
| 25 | private final Map<String, Integer> mapaClientOrder = new ConcurrentHashMap<>(); |
||
| 26 | |||
| 27 | public ServicoOrdensFuturosIbkr(IbkrClient ibkr) { |
||
| 28 | super(); |
||
| 29 | this.ibkr = ibkr; |
||
| 30 | } |
||
| 31 | |||
| 32 | @Override |
||
| 33 | public StatusOrdemFuturos novaOrdemLimit( |
||
| 34 | String symbol, |
||
| 35 | String side, |
||
| 36 | BigDecimal quantity, |
||
| 37 | BigDecimal price, |
||
| 38 | String clientOrderId |
||
| 39 | ) throws Exception { |
||
| 40 | |||
| 41 | int orderId = ibkr.getNextOrderIdBlocking(Duration.ofSeconds(5)); |
||
| 42 | |||
| 43 | Contract contract = IbkrMapper.contratoFuturoIndice(symbol); |
||
| 44 | |||
| 45 | Order order = new Order(); |
||
| 46 | order.action(side); |
||
| 47 | order.orderType("LMT"); |
||
| 48 | order.totalQuantity(Decimal.get(quantity)); |
||
| 49 | order.lmtPrice(price.doubleValue()); |
||
| 50 | order.tif("GTC"); |
||
| 51 | order.orderRef(clientOrderId); |
||
| 52 | |||
| 53 | mapaClientOrder.put(clientOrderId, orderId); |
||
| 54 | |||
| 55 | CompletableFuture<StatusOrdemFuturos> future = ibkr.registrarFutureOrdem(orderId); |
||
| 56 | |||
| 57 | System.out.println("[IBKR-ORDEM] Enviando ordem: id=" + orderId + |
||
| 58 | ", symbol=" + contract.symbol() + |
||
| 59 | ", localSymbol=" + contract.localSymbol() + |
||
| 60 | ", secType=" + contract.getSecType() + |
||
| 61 | ", exchange=" + contract.exchange() + |
||
| 62 | ", primaryExch=" + contract.primaryExch() + |
||
| 63 | ", currency=" + contract.currency()); |
||
| 64 | |||
| 65 | |||
| 66 | ibkr.getClient().placeOrder(orderId, contract, order); |
||
| 67 | |||
| 68 | StatusOrdemFuturos status = future.get(10, TimeUnit.SECONDS); |
||
| 69 | status.setClientOrderId(clientOrderId); |
||
| 70 | status.setSymbol(symbol); |
||
| 71 | return status; |
||
| 72 | } |
||
| 73 | |||
| 74 | @Override |
||
| 75 | public StatusOrdemFuturos consultarStatusOrdem(String symbol, String origClientOrderId) throws Exception { |
||
| 76 | Integer orderId = mapaClientOrder.get(origClientOrderId); |
||
| 77 | if (orderId == null) { |
||
| 78 | throw new IllegalArgumentException("Não há orderId mapeado para clientOrderId=" + origClientOrderId); |
||
| 79 | } |
||
| 80 | |||
| 81 | CompletableFuture<StatusOrdemFuturos> future = ibkr.registrarFutureOrdem(orderId); |
||
| 82 | |||
| 83 | ibkr.getClient().reqOpenOrders(); |
||
| 84 | |||
| 85 | StatusOrdemFuturos status = future.get(5, TimeUnit.SECONDS); |
||
| 86 | status.setClientOrderId(origClientOrderId); |
||
| 87 | status.setSymbol(symbol); |
||
| 88 | return status; |
||
| 89 | } |
||
| 90 | } |