Code Monkey home page Code Monkey logo

ibclient's Introduction

Scala IBClient

Interactive brokers Scala client.

This code wraps the java IBClient in asynchronous (futures) and reactive (RxScala) API.

All the wrapped functions from the java client return Futures, making the scala IBClient a standalone class that plays nicely with concurrent code.

The Java IBClient is copyright interactive brokers and subject to it's license, see https://www.interactivebrokers.com/en/index.php?f=5041 and http://interactivebrokers.github.io/

Build standalone jar

You can use a fat jar to run the commandline client by doing

sbt assembly

Which can be run with:

java -jar target/scala-2.12/ibclient-assembly-0.2.2-SNAPSHOT.jar

Or a binary executable with sbt pack, and run with target/pack/bin/main

Connect to IB

To connect to ib you can use IBC, I would recommend my docker version of IBC:

check out here how to use it:

https://github.com/larroy/IBC/blob/docker/docker/README.md

Examples

The client connects asynchronously so to wait until it's connected we can do the following: For ilustration purposes we will block on the futures to make the API syncrhonous:

import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
def block[A](f: Future[A]): A = {
    Await.result(f, Duration.Inf)
}

import com.larroy.ibclient.{IBClient}
val ibc = new IBClient("localhost", 7496, 3)
block(ibc.connect())
ibc.isConnected

Using the IBClient class is simple, historical data works out of the box and does throttling automatically:



import com.larroy.ibclient.contract.{CashContract, FutureContract, GenericContract, StockContract}
val subscription = ibc.marketData(new StockContract("SPY"))
observableBar.subscribe({bar=>println(s"got bar ${bar}")}
import scala.concurrent.duration._
import org.joda.time._
import com.ib.client.Types.DurationUnit
import com.ib.client.Types.WhatToShow

val startDate = new DateTime(2015, 4, 10, 15, 0).toDate

val endDate = new DateTime(2015, 4, 13, 15, 0).toDate
val h = block(ibc.historicalData(new StockContract("SPY"), endDate, 20, DurationUnit.DAY, BarSize._15_mins, WhatToShow.MIDPOINT, false))

val h = block(ibclient.historicalData(new StockContract("SPY"), endDate, 20, 
    DurationUnit.DAY, BarSize._15_mins, WhatToShow.MIDPOINT, false))

Or with the 'easy historical data' facility:

val eh = block(ibc.easyHistoricalData(new StockContract("SPY"),startDate,endDate,BarSize._1_day, WhatToShow.TRADES))
val res2 = Await.result(ibclient.historicalData(new CashContract("EUR","EUR.USD"), endDate2, 120, DurationUnit.SECOND, BarSize._1_min, WhatToShow.MIDPOINT, false), scala.concurrent.duration.Duration.Inf)
import java.text.SimpleDateFormat
import java.util.Date
import com.larroy.ibclient.contract.{CashContract, StockContract}
import com.ib.client.Types.{BarSize, DurationUnit, WhatToShow, SecType}
import scala.concurrent.duration._
import scala.concurrent.Await
import com.larroy.ibclient._
import com.typesafe.config.ConfigFactory
val cfg = ConfigFactory.load().getConfig("ibclient.test")
Await.result(ibclient.connect(), Duration.Inf)

Subscribe to market data:

val subscription = ibclient.realtimeBars(new CashContract("EUR","EUR.USD"), WhatToShow.MIDPOINT)
subscription.observableBar.subscribe({bar=>println(s"got bar ${bar}")}, {error โ‡’ throw (error)})

Get contract details

import scala.concurrent.Await
import scala.concurrent.duration.Duration
import com.larroy.ibclient.contract.{CashContract, FutureContract, GenericContract, StockContract}
import com.larroy.ibclient.{IBClient}
val ibc = new IBClient("localhost", 7496, 3)
Await.result(ibc.connect(), Duration.Inf)
val c = new FutureContract("CL", "", "NYMEX")
val cd = Await.result(ibc.contractDetails(c), Duration.Inf)

Or with the commandline client:

history -c CL -t FUT -e NYMEX -x USD  -y 20150421 -a "20150128 15:00:00" -z "20150415 15:00:00"  -b
_1_min -o cl.csv 
history -c VIX -e CBOE -t IND -a "20161001 00:00:00" -o vix.csv -b _1_day
java -jar target/scala-2.11/ibclient-assembly-0.2.2-SNAPSHOT.jar history -c VIX -e CBOE -t IND -a
"20161115 00:00:00" -z "201118 00:00:00" -o vix.csv -b _1_day

ibclient's People

Contributors

larroy avatar

Stargazers

Suminda Sirinath Salpitikorala Dharmasena avatar Justin Zoeller avatar Stefan G. avatar  avatar odenzo avatar George Watkins avatar  avatar Eric Simon avatar Giovanni Bignardi avatar Tharsis Souza avatar

Watchers

Alexander Poluektov avatar James Cloos avatar  avatar  avatar  avatar  avatar

ibclient's Issues

Update IB API to a more recent version

It appears that project's version of theTWS api dates back to 2013, judging by the copyright notices. Most things still work, but it would be nice to have more recent features. For example the newer API's have ownership reports in the fundamental data.

I might give it shot myself, but it would be nice to hear if the project is still active.

Very nice project ๐Ÿ‘

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.