/finance-ig

Interface to IG public APIs & Streaming service.

Primary LanguageSwiftMIT LicenseMIT

Framework Logo

Swift 5.3 macOS 10.15+ - iOS 13+ - tvOS 13+ MIT License

This framework provides:

  • All public HTTP IG endpoints (with request, response, and error handling support).
  • All public Lightstreamer IG subscriptions (with request, response, and error handling support).
    The Lighstreamer binaries are packaged with the source code. IG only supports an older Lightstreamer version and this framework provides exactly that version.
  • Session management helpers (such as OAuth and Certificate token refreshes, etc).
  • Optional small SQLite database to cache market and price information.
  • Currency and optional Money types.

Usage

To use this library, you need to:

    Add IG to your project through SPM.

    // swift-tools-version:5.3
    import PackageDescription
    
    let package = Package(
        /* Your package name, supported platforms, and generated products go here */
        dependencies: [
            .package(url: "https://github.com/dehesa/IG.git", from: "0.11.2")
        ],
        targets: [
            .target(name: /* Your target name here */, dependencies: ["IG"])
        ]
    )

    Import IG in the file that needs it.

    import IG

API

All public HTTP endpoints are defined under the API reference type. To expose the functionality:

  1. Create an API instance.

    let api = API()
    // Optionally you can pass the demo rootURL: API(rootURL: API.demoRootURL)
  2. Log into an account.

    let key: API.Key = "a12345bc67890d12345e6789fg0hi123j4567890"
    let user = API.User(name: "username", password: "password")
    api.sessions.login(type: .certificate, key: key, user: user)

    To generate your own API key, look here.

  3. Call a specific endpoint.

    // As an example, lets get information about the EURUSD forex mini market.
    api.markets.get(epic: "CS.D.EURUSD.MINI.IP")

It is worth noticing that all the endpoints are asynchronous (they must call the server and receive a response). That is why this framework relies heavily in Combine and most functions return a Publisher type that can be chained with further endpoints. For example:

let api = API(rootURL: API.rootURL, credentials: nil)
let cancellable = api.sessions.login(type: .certificate, key: key, user: user)
    .then {
        api.markets.get(epic: "CS.D.EURUSD.MINI.IP")
    }.flatMap {
        api.prices.get(epic: $0.instrument.epic, from: Date(timeIntervalSinceNow: -3_600), resolution: .minute)
    }.sink(receiveCompletion: {
        guard case .finished = $0 else { return print($0) }
    }, receiveValue: { (prices) in
        prices.forEach { print($0) }
    })

The login process only needs to be called once, since the temporary token is stored within the api object. Make sure you keep the API instance around while you are using API functionality. IG permits the usage of OAuth or Certificate tokens. Although both work with any API endpoint, there are some differences:

  • OAuth tokens are only valid for 60 seconds, while Certificate tokens usually last for 6 hours.
  • It is not possible to request Lightstreamer credentials with OAuth tokens.

For those reasons, it is recommended to use to Certificate tokens.

Streamer

All public Lightstreamer subscriptions are defined under the Streamer reference type. To expose the functionality.

  1. Retrieve the streamer credentials and initialize a Streamer instance.

    guard let apiCreds = api.session.credentials else { return }
    let streamerCreds = try Streamer.Credentials(apiCreds)
    let streamer = Streamer(rootURL: apiCreds.streamerURL, credentials: streamerCreds)
  2. Connect the streamer.

    streamer.sessions.connect()
  3. Subscribe to any targeted event.

    streamer.prices.subscribe(epic: "CS.D.EURUSD.MINI.IP", interval: .minute, fields: .all)

    The returned publisher will forward events till the publisher is cancelled.

Please be mindful of the limits enforced by IG.

Database

The library provides the option to create a SQLite database to cache market information and/or price data. This is a work in progress and it currently only support forex markets and price resolutions of one minute.

  1. Define a database location.

    let db = try Database(location: .memory)
  2. Write some API market data.

    db.markets.update(apiMarket)
  3. Write some API prices.

    db.prices.update(apiPrices, epic: "CS.D.EURUSD.MINI.IP")

Services

You can cherry pick which service to use; however, it might be simpler to let the convenience Services initialize all subservices for you.

  1. Get credentials.

    let user: API.User = .init(name: "username", password: "password")
    let apiKey: API.Key = "a12345bc67890d12345e6789fg0hi123j4567890"
  2. Create a services aggregator.

    let services = Services.make(key: apiKey, user: user)

A Services instance has completely functional HTTP, Lightstreamer services, and SQLite database. All these services are initialized and ready to operate.