Code Monkey home page Code Monkey logo

finance-ig's Introduction

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.

finance-ig's People

Contributors

dehesa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

finance-ig's Issues

Checksum of the binary target

Hello Marcos,

thank you very much for this sample project, it's being really helpful.

I have one question: at Lightstreamer we now have a build producing XCframeworks for our supported platforms, to enable use from from SPM. The problem is: we need to publish their checksum to be specified as binary targets. Computing the checksum requires a root package manifest, or SPM raises an error. Obj-C frameworks don't require any such manifest, so it's not actually clear to which manifest it is referring when SPM raises this error.

Any advice? How did you compute the checksum for your sample Lightstreamer XCframework?

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.