└── demo ├── LoggerImplementation.java ├── EntrySignalA.java ├── ConnectionHandlerImplementation.java ├── TopMktDataHandlerImplementation.java ├── Demo.java └── OrderHandler.java /demo/LoggerImplementation.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import ib.controller.ApiConnection; 4 | 5 | public class LoggerImplementation implements ApiConnection.ILogger { 6 | 7 | @Override 8 | public void log(String valueOf) { 9 | //Do something 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /demo/EntrySignalA.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import ib.controller.Types; 4 | 5 | import java.util.ArrayList; 6 | 7 | public class EntrySignalA { 8 | 9 | private ArrayList prices; 10 | private OrderHandler orderHandler; 11 | 12 | public EntrySignalA(ArrayList prices){ 13 | this.prices = prices; 14 | this.orderHandler = new OrderHandler(); 15 | if(prices.size()>1) { 16 | if (prices.get(0) - prices.get(1) > 3) { 17 | orderHandler.placeBracketOrder(1000, Types.Action.BUY, 1, 1, 1, .5); 18 | } 19 | if(prices.get(0) - prices.get(1) < -3) { 20 | orderHandler.placeBracketOrder(1000, Types.Action.BUY, 1, 1, 1, .5); 21 | } 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /demo/ConnectionHandlerImplementation.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import ib.controller.ApiController; 4 | 5 | import java.util.ArrayList; 6 | 7 | public class ConnectionHandlerImplementation implements ApiController.IConnectionHandler { 8 | @Override 9 | public void connected() { 10 | //Do something when connected 11 | System.out.println("Connected"); 12 | } 13 | 14 | @Override 15 | public void disconnected() { 16 | //Do something when disconnected 17 | } 18 | 19 | @Override 20 | public void accountList(ArrayList list) { 21 | //Do something with the account list 22 | } 23 | 24 | @Override 25 | public void error(Exception e) { 26 | //Do something on error 27 | } 28 | 29 | @Override 30 | public void message(int id, int errorCode, String errorMsg) { 31 | //Do something with server messages 32 | } 33 | 34 | @Override 35 | public void show(String string) { 36 | //Do something with paramter 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /demo/TopMktDataHandlerImplementation.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import ib.controller.ApiController; 4 | import ib.controller.NewTickType; 5 | import ib.controller.Types; 6 | 7 | import java.util.ArrayList; 8 | 9 | public class TopMktDataHandlerImplementation implements ApiController.ITopMktDataHandler { 10 | 11 | ArrayList prices = new ArrayList<>(); 12 | 13 | @Override 14 | public void tickPrice(NewTickType tickType, double price, int canAutoExecute) { 15 | //Do something with the price response 16 | if(tickType.equals(NewTickType.LAST)) { 17 | prices.add(0, price); 18 | System.out.println("Current Price: "+price); 19 | new EntrySignalA(prices); 20 | } 21 | } 22 | 23 | @Override 24 | public void tickSize(NewTickType tickType, int size) { 25 | //Do something with the volume response 26 | } 27 | 28 | @Override 29 | public void tickString(NewTickType tickType, String value) { 30 | //Do something with a specific tickType 31 | } 32 | 33 | @Override 34 | public void tickSnapshotEnd() { 35 | //Do something on the end of the snapshot 36 | } 37 | 38 | @Override 39 | public void marketDataType(Types.MktDataType marketDataType) { 40 | //Do something with the type of market data 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /demo/Demo.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import ib.controller.ApiController; 4 | import ib.controller.NewContract; 5 | import ib.controller.Types; 6 | 7 | public class Demo { 8 | 9 | //We will need an instance of our connection handler implementation 10 | static LoggerImplementation inLogger = new LoggerImplementation(); 11 | static LoggerImplementation outLogger = new LoggerImplementation(); 12 | static ConnectionHandlerImplementation connectionHandler = new ConnectionHandlerImplementation(); 13 | static TopMktDataHandlerImplementation topMktDataHandlerImplementation = new TopMktDataHandlerImplementation(); 14 | static ApiController apiController = new ApiController(connectionHandler, inLogger, outLogger); 15 | static OrderHandler orderHandler = new OrderHandler(); 16 | 17 | public static void main(String _[]){ 18 | apiController.connect("localhost", 7497, 0); 19 | apiController.reqTopMktData(initializeContract(), "", false, topMktDataHandlerImplementation); 20 | } 21 | 22 | static NewContract initializeContract(){ 23 | NewContract nq = new NewContract(); 24 | nq.localSymbol("NQU8"); 25 | nq.secType(Types.SecType.FUT); 26 | nq.exchange("GLOBEX"); 27 | nq.symbol("NQ"); 28 | nq.currency("USD"); 29 | nq.multiplier("20"); 30 | return nq; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /demo/OrderHandler.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import ib.controller.*; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class OrderHandler implements ApiController.ILiveOrderHandler, ApiController.IOrderHandler { 9 | 10 | //This is taken straight from the API Documentation, with some minor modifications 11 | private static List BracketOrder(int parentOrderId, Types.Action action, int quantity, double limitPrice, double takeProfitLimitPrice, double stopLossPrice) { 12 | //This will be our main or "parent" order 13 | NewOrder parent = new NewOrder(); 14 | parent.orderId(parentOrderId); 15 | parent.action(action); 16 | parent.orderType(OrderType.LMT); 17 | parent.totalQuantity(quantity); 18 | parent.lmtPrice(limitPrice); 19 | //The parent and children orders will need this attribute set to false to prevent accidental executions. 20 | //The LAST CHILD will have it set to true. 21 | parent.transmit(false); 22 | 23 | NewOrder takeProfit = new NewOrder(); 24 | takeProfit.orderId(parent.orderId() + 1); 25 | takeProfit.action(action.equals(Types.Action.BUY) ? Types.Action.SELL : Types.Action.BUY); 26 | takeProfit.orderType(OrderType.LMT); 27 | takeProfit.totalQuantity(quantity); 28 | takeProfit.lmtPrice(takeProfitLimitPrice); 29 | takeProfit.parentId(parentOrderId); 30 | takeProfit.transmit(false); 31 | 32 | NewOrder stopLoss = new NewOrder(); 33 | stopLoss.orderId(parent.orderId() + 2); 34 | stopLoss.action(action.equals(Types.Action.BUY) ? Types.Action.SELL : Types.Action.BUY); 35 | stopLoss.orderType(OrderType.STP); 36 | //Stop trigger price 37 | stopLoss.auxPrice(stopLossPrice); 38 | stopLoss.totalQuantity(quantity); 39 | stopLoss.parentId(parentOrderId); 40 | //In this case, the low side order will be the last child being sent. Therefore, it needs to set this attribute to true 41 | //to activate all its predecessors 42 | stopLoss.transmit(true); 43 | 44 | List bracketOrder = new ArrayList<>(); 45 | bracketOrder.add(parent); 46 | bracketOrder.add(takeProfit); 47 | bracketOrder.add(stopLoss); 48 | 49 | return bracketOrder; 50 | } 51 | 52 | static NewContract initializeContract(){ 53 | NewContract nq = new NewContract(); 54 | nq.localSymbol("NQU8"); 55 | nq.secType(Types.SecType.FUT); 56 | nq.exchange("GLOBEX"); 57 | nq.symbol("NQ"); 58 | nq.currency("USD"); 59 | nq.multiplier("20"); 60 | return nq; 61 | } 62 | 63 | public void placeBracketOrder(int parentOrderId, Types.Action action, int quantity, double limitPrice, double takeProfitLimitPrice, double stopLossPrice){ 64 | List bracketOrder = BracketOrder(parentOrderId,action,quantity,limitPrice,takeProfitLimitPrice,stopLossPrice); 65 | for(NewOrder o : bracketOrder) { 66 | Demo.apiController.placeOrModifyOrder(initializeContract(), o,this); 67 | } 68 | } 69 | 70 | @Override 71 | public void orderState(NewOrderState orderState) { 72 | 73 | } 74 | 75 | @Override 76 | public void orderStatus(OrderStatus status, int filled, int remaining, double avgFillPrice, long permId, int parentId, double lastFillPrice, int clientId, String whyHeld) { 77 | 78 | } 79 | 80 | @Override 81 | public void handle(int errorCode, String errorMsg) { 82 | 83 | } 84 | 85 | @Override 86 | public void openOrder(NewContract contract, NewOrder order, NewOrderState orderState) { 87 | 88 | } 89 | 90 | @Override 91 | public void openOrderEnd() { 92 | 93 | } 94 | 95 | @Override 96 | public void orderStatus(int orderId, OrderStatus status, int filled, int remaining, double avgFillPrice, long permId, int parentId, double lastFillPrice, int clientId, String whyHeld) { 97 | 98 | } 99 | 100 | @Override 101 | public void handle(int orderId, int errorCode, String errorMsg) { 102 | 103 | } 104 | } 105 | --------------------------------------------------------------------------------