IBX - OCaml implementation of the Interactive Brokers TWS API
IBX is a pure OCaml implementation of the Interactive Brokers Trader Workstation API (TWS API) built on top of Jane Street's Core and Async library.
DISCLAIMER: This software is not approved by Interactive Brokers or any of its affiliates. It comes with absolutely no warranty and the use of this software for actual trading is at your own risk.
$ opam install ibx
$ make $ make install
The still incomplete API-documentation of this distribution can be built with
It can also be found online.
Before you start, please install the Interactive Brokers Trader Workstation TWS or its low-resource alternative IB Gateway and make sure that the software allows for incoming API connections. How this is done for TWS can be watched here. For more detailed information please refer to the Interactive Brokers API Reference Guide.
NOTE: You can use the username edemo with password demouser to log into the demo account.
This simple program will connect to the IB Gateway running on localhost and retrieve the last price for a given stock symbol:
open Core.Std open Async.Std open Ibx.Std let host = "localhost" let port = 4001 let print_last_price enable_logging symbol = Tws.with_client ~enable_logging ~host ~port ~on_handler_error:(`Call (fun e -> eprintf "[Error] Failed to retrieve last price for %s:\n" symbol; prerr_endline (Error.to_string_hum e); shutdown 1 )) (fun tws -> let stock = Contract.stock ~currency:`USD (Symbol.of_string symbol) in Tws.trade_snapshot_exn tws ~contract:stock >>= fun snapshot -> let price = Price.to_float (Trade_snapshot.last_price snapshot) in printf "[Info] Last price for %s was %4.2f USD\n" symbol price; return () ) let command = Command.async_basic ~summary:"Retrieve last stock price" Command.Spec.( empty +> flag "-enable-logging" no_arg ~doc:" enable logging" +> anon ("STOCK-SYMBOL" %: string) ) (fun enable_logging symbol () -> if enable_logging then Log.Global.set_level `Debug; print_last_price enable_logging symbol ) let () = Command.run command
Assuming the above program is stored in the file
you can simply build it by running
$ ocamlbuild -use-ocamlfind -tag thread -pkg ibx last_price.native
and then use it to get the last price of a stock (e.g. Apple Inc.) as follows:
$ ./last_price.native AAPL
$ ./last_price.native -enable-logging AAPL 2> ibx.log
For more complex examples please refer to the
examples-directory of this
distribution. You can build the examples by typing
$ ./configure --enable-examples $ make
in the top-level directory of this distribution. If the compilation of the examples was successful, you can type, for instance,
$ ./plot_hist_bars.native AAPL
in order to plot a candle stick chart of the Apple stock price with simple moving averages. Your plot should look similar to this image:
The next command accumulates 30 seconds of trade and quote (TAQ) data of Google Inc.
$ ./plot_taq_data.native GOOG
and generates a plot which should look similar to this image:
However, please note that these TAQ data plots do not reflect the true market history, since Interactive Brokers accumulates market data in time increments of 300 milliseconds; see here for more information.
In case of bugs, feature requests and similar, please contact:
- Oliver Gu email@example.com