Market orders go through just fine, but when I place a limit order the TWS doesn't repsond. The relevant code is below...am I just forgetting some silly little thing??
public void run() {
try {
connectToTWS();
Contract contract = createContract(symbol, "STK", "SMART", "USD");
Order order = createOrder(action, shares, "LMT");
if (action.equals("BUY")) {
order.m_lmtPrice = price*(1+dev);
}
if (action.equals("SELL")) {
order.m_lmtPrice = price*(1-dev);
}
nextIDupdated = false;
int waitCount = 0;
eClientSocket.reqIds(1);
while (!nextIDupdated && waitCount < MAX_WAIT_COUNT) {
sleep(WAIT_TIME); // Pause for 1 second
waitCount++;
}
eClientSocket.placeOrder(nextID, contract, order);
} catch (Throwable t) {
System.out.println(t.getMessage());
} finally {
disconnectFromTWS();
}
}
protected Order createOrder(String action, int quantity, String orderType) {
Order order = new Order();
order.m_action = action;
order.m_totalQuantity = quantity;
order.m_orderType = orderType;
order.m_overridePercentageConstraints= true;
return order;
}
protected Contract createContract(String symbol, String securityType, String exchange, String currency) {
return createContract(symbol, securityType, exchange, currency, null, null, 0.0);
}
protected Contract createContract(String symbol, String securityType, String exchange, String currency, String expiry, String right, double strike) {
Contract contract = new Contract();
contract.m_symbol = symbol;
contract.m_secType = securityType;
contract.m_exchange = exchange;
contract.m_currency = currency;
if (expiry != null) {
contract.m_expiry = expiry;
}
if (strike != 0.0) {
contract.m_strike = strike;
}
if (right != null) {
contract.m_right = right;
}
return contract;
}
public void run() {
try {
connectToTWS();
Contract contract = createContract(symbol, "STK", "SMART", "USD");
Order order = createOrder(action, shares, "LMT");
if (action.equals("BUY")) {
order.m_lmtPrice = price*(1+dev);
}
if (action.equals("SELL")) {
order.m_lmtPrice = price*(1-dev);
}
nextIDupdated = false;
int waitCount = 0;
eClientSocket.reqIds(1);
while (!nextIDupdated && waitCount < MAX_WAIT_COUNT) {
sleep(WAIT_TIME); // Pause for 1 second
waitCount++;
}
eClientSocket.placeOrder(nextID, contract, order);
} catch (Throwable t) {
System.out.println(t.getMessage());
} finally {
disconnectFromTWS();
}
}
protected Order createOrder(String action, int quantity, String orderType) {
Order order = new Order();
order.m_action = action;
order.m_totalQuantity = quantity;
order.m_orderType = orderType;
order.m_overridePercentageConstraints= true;
return order;
}
protected Contract createContract(String symbol, String securityType, String exchange, String currency) {
return createContract(symbol, securityType, exchange, currency, null, null, 0.0);
}
protected Contract createContract(String symbol, String securityType, String exchange, String currency, String expiry, String right, double strike) {
Contract contract = new Contract();
contract.m_symbol = symbol;
contract.m_secType = securityType;
contract.m_exchange = exchange;
contract.m_currency = currency;
if (expiry != null) {
contract.m_expiry = expiry;
}
if (strike != 0.0) {
contract.m_strike = strike;
}
if (right != null) {
contract.m_right = right;
}
return contract;
}