{"id":17771151,"url":"https://github.com/dehesa/finance-ig","last_synced_at":"2025-07-29T17:06:09.427Z","repository":{"id":60804064,"uuid":"148529622","full_name":"dehesa/finance-ig","owner":"dehesa","description":"Interface to IG public APIs \u0026 Streaming service.","archived":false,"fork":false,"pushed_at":"2024-05-07T19:56:48.000Z","size":45753,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-27T03:26:40.905Z","etag":null,"topics":["broker","broker-api","ig","lightstreamer","swift","trading"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dehesa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"docs/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-12T19:16:09.000Z","updated_at":"2024-05-07T19:56:06.000Z","dependencies_parsed_at":"2024-10-26T21:55:34.502Z","dependency_job_id":"783f7832-f586-44f4-8000-5788a793edab","html_url":"https://github.com/dehesa/finance-ig","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dehesa%2Ffinance-ig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dehesa%2Ffinance-ig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dehesa%2Ffinance-ig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dehesa%2Ffinance-ig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dehesa","download_url":"https://codeload.github.com/dehesa/finance-ig/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243750399,"owners_count":20342051,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["broker","broker-api","ig","lightstreamer","swift","trading"],"created_at":"2024-10-26T21:29:40.015Z","updated_at":"2025-03-15T15:30:24.742Z","avatar_url":"https://github.com/dehesa.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"docs/assets/IG.svg\" alt=\"Framework Logo\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://swift.org/about/#swiftorg-and-open-source\"\u003e\u003cimg src=\"docs/assets/badges/Swift.svg\" alt=\"Swift 5.3\"\u003e\u003c/a\u003e\n    \u003cimg src=\"docs/assets/badges/Apple.svg\" alt=\"macOS 10.15+ - iOS 13+ - tvOS 13+\"\u003e\n    \u003ca href=\"http://doge.mit-license.org\"\u003e\u003cimg src=\"docs/assets/badges/License.svg\" alt=\"MIT License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nThis framework provides:\n\n-   All public [HTTP IG endpoints](https://labs.ig.com/rest-trading-api-reference) (with request, response, and error handling support).\n-   All public [Lightstreamer IG subscriptions](https://labs.ig.com/streaming-api-reference) (with request, response, and error handling support).\n    \u003cbr\u003eThe [Lighstreamer binaries](https://labs.ig.com/lightstreamer-downloads) are packaged with the source code. IG only supports an older Lightstreamer version and this framework provides exactly that version.\n-   Session management helpers (such as OAuth and Certificate token refreshes, etc).\n-   Optional small SQLite database to cache market and price information.\n-   Currency and optional _Money_ types.\n\n# Usage\n\nTo use this library, you need to:\n\n\u003cul\u003e\n\u003cdetails\u003e\u003csummary\u003eAdd \u003ccode\u003eIG\u003c/code\u003e to your project through \u003ca href=\"https://github.com/apple/swift-package-manager/tree/master/Documentation\"\u003eSPM\u003c/a\u003e.\u003c/summary\u003e\u003cp\u003e\n\n```swift\n// swift-tools-version:5.3\nimport PackageDescription\n\nlet package = Package(\n    /* Your package name, supported platforms, and generated products go here */\n    dependencies: [\n        .package(url: \"https://github.com/dehesa/IG.git\", from: \"0.11.2\")\n    ],\n    targets: [\n        .target(name: /* Your target name here */, dependencies: [\"IG\"])\n    ]\n)\n```\n\n\u003c/p\u003e\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eImport \u003ccode\u003eIG\u003c/code\u003e in the file that needs it.\u003c/summary\u003e\u003cp\u003e\n\n```swift\nimport IG\n```\n\n\u003c/p\u003e\u003c/details\u003e\n\u003c/ul\u003e\n\n## API\n\nAll public HTTP endpoints are defined under the `API` reference type. To expose the functionality:\n1. Create an API instance.\n\n    ```swift\n    let api = API()\n    // Optionally you can pass the demo rootURL: API(rootURL: API.demoRootURL)\n    ```\n\n2. Log into an account.\n\n    ```swift\n    let key: API.Key = \"a12345bc67890d12345e6789fg0hi123j4567890\"\n    let user = API.User(name: \"username\", password: \"password\")\n    api.sessions.login(type: .certificate, key: key, user: user)\n    ```\n\n    To generate your own API key, look [here](https://labs.ig.com/gettingstarted).\n\n3. Call a specific endpoint.\n\n    ```swift\n    // As an example, lets get information about the EURUSD forex mini market.\n    api.markets.get(epic: \"CS.D.EURUSD.MINI.IP\")\n    ```\n\nIt 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:\n\n```swift\nlet api = API(rootURL: API.rootURL, credentials: nil)\nlet cancellable = api.sessions.login(type: .certificate, key: key, user: user)\n    .then {\n        api.markets.get(epic: \"CS.D.EURUSD.MINI.IP\")\n    }.flatMap {\n        api.prices.get(epic: $0.instrument.epic, from: Date(timeIntervalSinceNow: -3_600), resolution: .minute)\n    }.sink(receiveCompletion: {\n        guard case .finished = $0 else { return print($0) }\n    }, receiveValue: { (prices) in\n        prices.forEach { print($0) }\n    })\n```\n\nThe 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:\n- OAuth tokens are only valid for 60 seconds, while Certificate tokens usually last for 6 hours.\n- It is not possible to request Lightstreamer credentials with OAuth tokens.\n\nFor those reasons, it is recommended to use to Certificate tokens.\n\n## Streamer\n\nAll public Lightstreamer subscriptions are defined under the `Streamer` reference type. To expose the functionality.\n\n1. Retrieve the streamer credentials and initialize a `Streamer` instance.\n\n    ```swift\n    guard let apiCreds = api.session.credentials else { return }\n    let streamerCreds = try Streamer.Credentials(apiCreds)\n    let streamer = Streamer(rootURL: apiCreds.streamerURL, credentials: streamerCreds)\n    ```\n\n2. Connect the streamer.\n\n    ```swift\n    streamer.sessions.connect()\n    ```\n\n3. Subscribe to any targeted event.\n\n    ```swift\n    streamer.prices.subscribe(epic: \"CS.D.EURUSD.MINI.IP\", interval: .minute, fields: .all)\n    ```\n\n    The returned publisher will forward events till the publisher is cancelled.\n\n\u003e Please be mindful of the [limits enforced by IG](https://labs.ig.com/faq#limits).\n\n## Database\n\nThe 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.\n\n1. Define a database location.\n\n    ```swift\n    let db = try Database(location: .memory)\n    ```\n\n2. Write some API market data.\n\n    ```swift\n    db.markets.update(apiMarket)\n    ```\n\n3. Write some API prices.\n\n    ```swift\n    db.prices.update(apiPrices, epic: \"CS.D.EURUSD.MINI.IP\")\n    ```\n\n## Services\n\nYou can cherry pick which service to use; however, it might be simpler to let the convenience `Services` initialize all subservices for you.\n\n1. Get credentials.\n\n    ```swift\n    let user: API.User = .init(name: \"username\", password: \"password\")\n    let apiKey: API.Key = \"a12345bc67890d12345e6789fg0hi123j4567890\"\n    ```\n\n2. Create a services aggregator.\n\n    ```swift\n    let services = Services.make(key: apiKey, user: user)\n    ```\n\nA `Services` instance has completely functional HTTP, Lightstreamer services, and SQLite database. All these services are initialized and ready to operate.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdehesa%2Ffinance-ig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdehesa%2Ffinance-ig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdehesa%2Ffinance-ig/lists"}