Giter Club home page Giter Club logo

ibx's Introduction

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.

Installation

From OPAM

$ opam install ibx

From Source

$ make
$ make install

Usage

Documentation

The still incomplete API-documentation of this distribution can be built with make doc. It can also be found online.

Prerequisites

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.

Examples

This simple program will connect to the IB Gateway running on localhost and retrieve the closing price for a given stock symbol:

:::ocaml
open Core
open Async
open Ibx

let host = "localhost"
let port = 4001

let () =
  Command.async
    ~summary:"Show the closing price of the given stock symbol"
    Command.Spec.(
      empty
      +> flag "-currency" ~doc:" currency of the stock price"
        (optional_with_default `USD (Arg_type.create Currency.of_string))
      +> anon ("STOCK-SYMBOL" %: Arg_type.create Symbol.of_string)
    )
    (fun currency symbol () ->
      Tws.with_client ~host ~port ~on_handler_error:(`Call (fun e ->
        eprintf "[Error] Failed to retrieve closing price for %s: %s\n%!"
          (Symbol.to_string symbol) (Error.to_string_hum e);
      ))
      (fun tws ->
        Tws.latest_close_exn tws ~contract:(Contract.stock symbol ~currency)
        >>= fun close ->
        printf "[Info] Closing price for %s is %4.2f %s\n"
          (Symbol.to_string symbol) (Close.price close |> Price.to_float)
          (Currency.to_string currency);
        return ()
      )
    )
  |> Command.run

Assuming the above program is stored in the file show_close.ml, you can simply build it by running

$ ocamlbuild -use-ocamlfind -tag thread -pkg ibx show_close.native

and then use it to get the closing price of a stock (e.g. Apple Inc.) as follows

$ ./show_close.native AAPL

For more complex examples please refer to the examples-directory of this distribution. For instance, you can build the plot_history example by typing

$ ocamlbuild -use-ocamlfind -tag thread -pkg ibx,gnuplot plot_history.native

and then run

$ ./plot_history.native -sma 100 AAPL

to plot a candle stick chart of Apple prices with a simple 100-day moving average

AAPL SMA

Moreover, to plot 1 minute of intraday trade and quote (TAQ) data, type

$ ocamlbuild -use-ocamlfind -tag thread -pkg ibx,gnuplot plot_taq_data.native
$ ./plot_taq_data.native AAPL

which creates a plot similar to this image

AAPL TAQ

NOTE: This TAQ data plot does not reflect the true market history, since Interactive Brokers accumulates market data in time increments of 300 milliseconds.

More Examples

For even more examples, please check out the IBX command line tools.

Contact Information

In case of bugs, feature requests and similar, please contact:

ibx's People

Contributors

xvilka avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.