Trading bot construction (IB)

Discussion in 'Automated Trading' started by doli, Jan 9, 2007.

  1. doli


    I have an interest in trading bots, because I've had some success trading ES manually; however, I have a day job, which prevents me from trading ES during regular trading hours. I trade through Interactive Brokers, so this journal will be IB specific, although I suppose that if any trading algorithms are discovered they may be useful with other brokerages. My purpose in writing the journal is to share what I'll learn about the IB API, in addition to placing my thoughts in writing, which I find causes me to think things through more thouroughly than when I work off the top of my head. Of course, if anyone has questions and/or comments, they will be most welcome, because those can often lead to a different perspective.

    The first major goal is to make a random trading bot.
    A random trading bot will, at a random time, toss a coin and enter a buy or sell order. You may well ask, "Why a random trading bot?" The answer is that the results produced by that bot will provide something to measure the results of more sophisticated bots against. Of course, the bot will papertrade; it isn't until and unless we build a bot that can successfully papertrade that we would consider letting it go with real money.

    The bot will trade as though it has an account worth 15K. That amount lets it place an order, then exit whenever three ES points are gained or lost. I suppose there should be maximum daily, weekly and monthly drawdown limits, too.

    As I said, I've got a day job. I'll only post something when I've got something new to add, but I'll get an email whenever someone replies and will try to get back to you ASAP. If you want to follow what's going on here, the best thing to do is subscribe to the journal, if that's possible. Initially I'll be posting some background info about the the IB API, so that everyone starts on the same page.
  2. doli


    One of the 1st questions to come up is "What infrastructure to use?" I have an x86 desktop box that runs Solaris or Windows, along with a laptop that runs OS X. On the x86 box I use Solaris, primarily.
    TWS and its API runs on all of them, and I don't want to be tied to a Windows only API (IB offers a C++ API for Windows, but I suspect that it would have to be hacked quite a bit to remove the Windows-isms before it could be built on another platform). Aside from the Java API from IB, I found a C API
    (, which builds and appears to work on Solaris; however, using that might require a lot of catch-up work whenever IB makes a change to the API. So, I've decided to work with IB's Java API. Out of the box it has a GUI, which isn't particularly useful to a trading bot, but is useful for finding out how to get market data and how to submit an order. After verifying the steps involved, I'll post something about downloading the IB java API, starting it, requesting market data, and entering an order.
  3. doli


    The Java API can be downloaded from
    Either the mac or unix file can be downloaded, they are probably the same, and I would be surprised if that same file can't be used on Windows (correct me if I'm wrong). Its name is twsapi_unixmac.jar.

    To start, decide where to put the API. Wherever the next command is run, the API will be dumped into a directory named 'IBJts'. I suggest making a new directory somewhere named, say, 'api', then, after moving the downloaded file into 'api', it can be unpacked where it was moved by running this command, in the 'api' directory:
    jar xf twsapi_unixmac.jar
    Then change into the 'IBJts' directory that was created when the 'jar' command was run. In the 'IBJts' directory there is '.txt' file, a 'java' directory, and a file named 'jtsclient.jar'.

    Next, I'll start the API, get some market data, and place two orders with the GUI. Understanding how to do that will help, because it will provide some clues about where to look in the Java code to find the code that does those things.
  4. Bowgett


    I found it very easy to do it in C++ using Windows API. I even did auto login so my bot wakes computer up, logs in to IB and starts trading/monitoring. All while I am sleeping :)
  5. You can do the auto login bit in pure Java if you want to. Works on all platforms.
  6. JB3


    Interesting thread. I've dreamed of doing something like this, but I don't have the technical know how. What is your background? Must be like a programmer right?
  7. Good luck with the thread and bot construction Doli.

    You may or may not be aware of the following resources that may assist your endeavors:

    - The Yahoo! TWSAPI Group:

    There are also numerous pieces of sample code, some out-dated:

    You'll need to sign-in to Yahoo! and sign-up to the group to access the files etc.

    - The Interactive Brokers Forums:

    More specifically, the TWS API forum.

    - Existing IB-specific Java bots

    JSystemTrader by nonlinear/Eugene:

    Discussions are found on the IB forums mentioned above.

    Frostengine's bot:

    I'm not sure if it has been released yet.

    Lastly, you are encouraged to participate in the thread I have going here:

    It is a bit larger in scope and ambition than perhaps what you have in mind but you may get some ideas.

    I have been away for a few weeks so will be continuing the thread discussion shortly.

    Once again, good luck.
  8. Does anyone have any opinions of the FIX protocol for building a trading bot on IB? Is it worth the extra time / money to implement? Is it still possible to be logged into TWS while its running to overview all trades? Is the execution speed, data feed compression, # of feeds etc better than the regular API?
  9. doli


    I did try to run the unixmac jar on windows and it does work.

    jb3: yes, I do programming. Java is new to me, so it is also an opportunity to learn something about it.

    tradermojo: thanks for the links. I have looked at the yahoo twsapi group and did find a thread about OCA/bracket orders, which will be useful. I'll check out the other links, too. At first that group seemed really chaotic, then I found the "group by topic" button, which helped a lot.

    walterjennings: using FIX directly may be faster, but there are several hops involved in getting an order filled:

    customer -> IB
    IB -> exchange
    exchange -> IB
    IB -> customer

    each of those may be tens of milliseconds, so optimizing one of them may not pay off if the other hops take longer.
  10. true but every bit helps right? esp if you make your money scalping.
    #10     Jan 10, 2007