0

I have written some Java code (using the interactive brokers API) to poll for futures pricing every 50ms and when it's in a situation I like, it will purchase or sell X contracts, etc.

Timer t = new Timer( );
t.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
      // get pricing, look for situational stuff
    }
}, 0, 50);

However, I'm lost as to how to write the code to properly keep the program always running in the background. If it matters, I'm developing this on OS X and production environment can be either ubuntu or centOS

  • 1
    Timer() is the same as Timer(false)// isDaemon , I think you should ask how to stop the program. – brian Dec 18 '16 at 21:18

1 Answers1

0

A java program will run until the last non daemon thread stops or you call one of the stop methods (like System.exit(0)). Since your timer is non daemon, it will run forever. If you have a window, then that also has a thread.

Since you've presumably connected to IB, EReader creates a non-daemon thread to read from the socket. You have to disconnect in order to stop this thread.

A better choice would be to subscribe to market data and do something when new data is received. Note that 50ms is too fast to ask for data since IB only updates a few times per second anyway.

brian
  • 10,619
  • 4
  • 21
  • 79
  • Which method in this API would help me subscribe to this data? Are there sample code I could look at to see the proper way to do this? [API link](https://www.interactivebrokers.com/en/software/api/apiguide/java/java_eclientsocket_methods.htm) –  Dec 18 '16 at 21:28
  • This is the only SO answer I've found for java-http://stackoverflow.com/a/30178619/2855515 You can read the API manual, you can look at some sample code in the API. There is a yahoo group at https://groups.yahoo.com/neo/groups/TWSAPI/conversations/messages that you can join. There is a tutorial at http://holowczak.com/ib-api-tutorials-by-programming-language/ – brian Dec 18 '16 at 21:31
  • You can also try some code and if it doesn't work, ask a new question. – brian Dec 18 '16 at 21:38