protected void connect() { client.eConnect(ip_add, return_port, return_clientId); try { while (!client.isConnected()) ; q.offer( "Connected to TWS server version " + client.serverVersion() + " at " + client.TwsConnectionTime()); } catch (Exception e) { q.offer("EXCEPTION while connecting: " + e.getMessage()); } }
public DataSet request_histData(Company company, int long_short_volatility) { Contract contract = company.create_contract(); Vector<TagValue> mkt_data_options = new Vector<TagValue>(); sem_oid.acquireUninterruptibly(); OpenRequest or = new OpenRequest(company, long_short_volatility); open_request_map.put(next_orderId, or); this.date = Calendar.getInstance().getTime(); if (long_short_volatility == 0) client.reqHistoricalData( next_orderId, contract, df_ib.format(this.date), "1800 S", "30 mins", "TRADES", 1, 1, mkt_data_options); else if (long_short_volatility == 1) client.reqHistoricalData( next_orderId, contract, df_ib.format(this.date), parent.data_period, parent.data_granularity, "TRADES", 1, 1, mkt_data_options); else client.reqHistoricalData( next_orderId, contract, df_ib.format(calendar.getTime()), "1 D", "1 hour", "TRADES", 1, 1, mkt_data_options); next_orderId++; sem_oid.release(); return null; }
public void place_order(Company company, int quantity, String buy_sell, byte user_system) { Contract contract = company.create_contract(); Order order = company.create_order(buy_sell, quantity); sem_oid.acquireUninterruptibly(); OpenOrder oo = new OpenOrder(order, company, user_system); open_order_map.put(next_orderId, oo); client.placeOrder(next_orderId, contract, order); String message = df_user.format(Calendar.getInstance().getTime()) + buy_sell + " ORDER placed: " + company.name() + "x" + quantity; message = (user_system == 0) ? message + " BY USER" : message; next_orderId++; sem_oid.release(); q.offer(message); }
protected void cancel_mktData(Company company) { int oid = 0; client.cancelMktData(oid); // req_id_mapping.remove(company); System.out.println("MktDataCanceled for oid " + oid); }
protected void request_mktData(Company company) { Contract contract = company.create_contract(); Vector<TagValue> mkt_data_options = new Vector<TagValue>(); sem_oid.acquireUninterruptibly(); OpenRequest or = new OpenRequest(company, -1); open_request_map.put(next_orderId, or); client.reqMktData(next_orderId, contract, null, true, mkt_data_options); next_orderId++; sem_oid.release(); }
protected boolean check_connection() { return client.isConnected(); }
protected void startConnection() { if (!connected) { connected = true; clientSocket.eConnect(connection.getHost(), connection.getPort(), connection.getClientId()); } }
public void disconnect() { if (clientSocket.isConnected()) { clientSocket.eDisconnect(); connected = false; } }