Giter Club home page Giter Club logo

ibkit's Introduction

IBKit - Interactive Brokers API swiftified

License Swift Xcode 13.0+ macOS 12+ SPM

IBKit is open source Interactive Brokers API library written in Swift. It allows you to automate trades, retrieve market data and monitor positions and orders. Offering both live and paper trading capabilities, it can be used as an algo trading platform or as an backtesting datasource. In latter case you need to write your own portfolio accounting and performance measurement methods.

The software is not an official API and is not affiliated or related with Interactive Brokers.

Conformance

The current version should conform with IB API version 10.18. It is the aim to update the software with to MacOS major releases and include also better bites from TWS API.

Current status

  • Common features seems to work fine but complex orders may generate errors.
  • Some tick events (like dividends) needs better implementation as separate event.
  • FA Accounts are not currently sypported

Requirements

Setting up IB Gateway

  • Launch IB Gateway, select IB Api as interface and use your live or paper trading credentials to log in.
  • To prevent IB Gateway/TWS to close application on your specified time, you need to select “Restart” option instead of “Auto logoff” under Configuration>Lock and Exit menu. IB maintains its servers on Saturday early mornings (GMT) and the service is disrupted for a few hours. They also restart their servers on each morning so your gateway or workstation will be disconnected for a minute or two.
  • Pay attention to your Master Client ID, host and port as you will need them when initiating new client. You find these values under Configuration>Settings>API>Settings menu.
  • Select UTC format for sending instrument-specific attributes
  • If you want to submit orders, you shall also uncheck Read Only API from Configuration>Settings>API>Settings menu.

Getting Started

Create a new Xcode project and navigate to File > Swift Packages > Add Package Dependency. Enter the url https://github.com/stensoosaar/IBKit and tap Next. Choose the main branch, and on the next screen, check off the packages as needed.

How to use?

While the api calls closely resemble IB's own API, the responses are provided by using dedicated data publishers for market, account and system events.

	var subscriptions: [AnyCancellable] = []

	client.marketEventFeed
		.sink { event in 
			handle event
		}
		.store(in: &subscriptions)

	do {
		let requestID = client.getNextID()
		let lookback = IBDuration.lookback(30, unit: .day)
		try client.requestPriceHistory(requestID, contract: 	contract, barSize: IBBarSize.day, barSource: IBBarSource.bidAsk, lookback: lookback)
	} catch {
		print(error.localizedDescription)
	}

Most of IB market data messages are stripped down from context (e.g. contract symbol, bar size, bar source) and if you are using multiple contracts and / or multiple timeframes you should store your request first and map the incoming messages to stored request parameters by using requestID.

See also included playgrounds

ibkit's People

Contributors

stensoosaar avatar mikecsh 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.