Swiftpack.co - Package - dehesa/IG

IG Framework + Swift

Swift 5.1 platforms Xcode 11 License

This framework provides:

  • Access to IG's HTTP APIs.
  • Access to IG's Lightstreamer service.
    The Lighstreamer binaries are packaged with the source code. IG only supports an older Lightstreamer version and this framework provides exactly that version. To know more, check labs.ig.com.
  • Endpoints & Lighstreamer events cache.
    Implement through SQLite.
  • Session management helpers. Such as OAuth and certificate token refreshes, etc.


The IG framework can be used to interface with IG's APIs, Lightstreamer's "real-time" events, and cache temporary data.

  • Access all IG's endpoints through the API instance.
    All endpoints listed in IG's API reference are supported (browse API.swift file to check them out).
    These endpoints offer compile-time interfaces for Swift and use Standard or Foundation types (e.g. Date).

    let api = API(credentials: nil, targetQueue: nil)
    api.login(type: .certificate, key: "a12345bc67890d12345e6789fg0hi123j4567890", user: .init("username", "password")).expectsCompletion()
    let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
    api.price.get(epic: "CS.D.EURUSD.MINI.IP", from: yesterday, resolution: .minute).sink { (prices, allowance) in
  • Establish "real-time" connections through the Streamer instance.
    All subscriptions listed in IG's Streaming API reference are supported (browse Streamer.swift file to check them out).
    As with the HTTP service, this framework offer compile-time interfaces for Swift and use Standard and/or Foundation types.

    let streamer = Streamer(rootURL: "...", credentials: .init(identifier: "ABC12", password: "..."), targetQueue: nil)
    streamer.price.subscribe(epic: "CS.D.EURUSD.MINI.IP", interval: .second, fields: [.date, . volume, .openBid, .closeBid]).sink {

    Please be mindful of the limits enforced by IG.

  • Create an in-memory or file database with the DB instance.
    The database is work in progress; currently only supporting price resolutions of one minute.

Although you can cherry pick which service to use, it might be simpler to let the convenience Services to initialize all subservices for you. To log in you need:

  • an API key.
    You can get one from someone that has an IG application, or you can generate your own; e.g. a12345bc67890d12345e6789fg0hi123j4567890.
  • Information for the user you will be logged in as.
    You can log in with your actual credentials.
    let user: API.User = .init(name: "username", password: "password")
    let apiKey: API.Key = "a12345bc67890d12345e6789fg0hi123j4567890"
    var services = Services.make(key: apiKey, user: user).sink(...)
    Optionally you can log in with an OAuth token or Certificate token.
    let oauthAccess = "toa7770m-1915-83u4-q665-80g574lm7659"
    let oauthRefresh = "rho2072f-4006-17t8-n417-42j560hw5130"
    let apiKey = "a12345bc67890d12345e6789fg0hi123j4567890"
    let token: API.Credentials.Token = .init(.oauth(access: oauthAccess, refresh: oauthRefresh, scope: "profile", type: "Bearer"), .expiresIn: 60))
    var services = Services.make(key: apiKey, token: token).sink(...)

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

services.api.transactions.get(from: .yesterday: to: Date()).sink { (transactions) in
    print("Between yesterday and today, there were \(transactions.count) transactions")

    for transaction in transactions {

services.streamer.markets.subscribe(to: "CS.D.EURUSD.MINI.IP", fields: [.bid, .offer, .date]).startWithValues {
    print("Date: \($0.date!)")
    print("Offer: \($0.price.offer!)")
    print("Bid: \($0.price.bid!)")


The following is a list of 1st party and 3rd party library/framework dependencies:

  • Foundation.
  • Combine (iOS 13.1+ & macOS 10.15).
  • Conbini
  • SQLite.
  • Lightstreamer binaries (prepackage within the framework under /Frameworks).

I would love to provide the framework through SPM, but SPM doesn't currently support prebuilt binaries; they are, however, on the "pitch" phase.


Visual roadmap about the Framework's future


Stars: 2
Help us keep the lights on


Used By

Total: 0


0.7.0 - Nov 6, 2019

This release contains:

  • Full IG API support.
  • Support for all Lightstreamer subscriptions.
  • Combine publishers to store price data points in the SQLite database.
  • Optional small Command-Line utility to keep running in the background to store the price data points of some targeted forex markets.

0.6.0 - Oct 11, 2019

First public release for the Swift+IG interface. There is support for:

  • API endpoints.
  • Lightstreamer "real-time" events.
  • SQLite data cache.
  • Tests and convenience/optional wrappers.

There is sadly no SPM support yet since it is not possible to include pre-built binaries.