TWS Java API. Как создавать заказ на покупку каждую секунду после получения ставки/цены

#java #tws

Вопрос:

Я пытаюсь выполнять заказы на покупку каждую секунду на основе ставки/запроса. Если заказ не был заполнен, мне нужно отменить его и попытаться снова совершить ПОКУПКУ. Как мне это сделать? Как отмечать ставки и спрашивать каждую секунду и делать заказ на покупку? В настоящее время я могу сделать только один заказ на покупку.

Мой код:

 public class IBApplication implements EWrapper {

private EClientSocket clientSocket;
private EJavaSignal readerSignal;

public static void main(String[] args) throws IOException {
    new MintApplication().connect();
    System.in.read();// press enter to exit
    System.exit(0);

}

private void connect() {
    readerSignal = new EJavaSignal();
    clientSocket = new EClientSocket(this, readerSignal);
    clientSocket.eConnect("127.0.0.1", 7497, 0);
    // Create a reader to consume messages from the TWS. The EReader will consume
    // the incoming messages and put them in a queue
    EReader reader = new EReader(clientSocket, readerSignal);
    reader.start();
    // Once the messages are in the queue, an additional thread can be created to
    // fetch them
    Thread processer = new Thread(() -> {
        while (clientSocket.isConnected()) {
            readerSignal.waitForSignal();
            try {
                reader.processMsgs();
            } catch (IOException ex) {
            }
        }
    });
    processer.setDaemon(true);
    processer.start();
}

public Order orderFill(double lmtPrice, String action, String orderType, String tif, int totalQuantity,
        String account, int clientId) {
    Order order = new Order();
    order.lmtPrice(lmtPrice);
    order.action(action);
    order.orderType(orderType);
    order.tif(tif);
    order.totalQuantity(totalQuantity);
    // order.account(account);
    // order.clientId(clientId);
    return order;
}

public Contract contractFill(String secIdType, String currency, String symbol, String secType, String exchange) {
    Contract contract = new Contract();
    // contract.secIdType(secIdType);
    contract.currency(currency);
    contract.symbol(symbol);
    contract.secType(secType);
    contract.exchange(exchange);
    return contract;
}

@Override
public void error(int id, int errorCode, String errorMsg) {
    System.out.println(errorCode   " "   errorMsg);
}

@Override
public void nextValidId(int i) {
    int orderidE = i;
    
    clientSocket.placeOrder(orderidE  , contractFill("", "USD", "AAPL", "STK", "SMART"),
            orderFill(0, "BUY", "MKT", "GTC", 10, "", 0));
}