https://github.com/dehesa/finance-ig
Interface to IG public APIs & Streaming service.
https://github.com/dehesa/finance-ig
broker broker-api ig lightstreamer swift trading
Last synced: 11 months ago
JSON representation
Interface to IG public APIs & Streaming service.
- Host: GitHub
- URL: https://github.com/dehesa/finance-ig
- Owner: dehesa
- License: mit
- Created: 2018-09-12T19:16:09.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2024-05-07T19:56:48.000Z (about 2 years ago)
- Last Synced: 2025-02-27T03:26:40.905Z (over 1 year ago)
- Topics: broker, broker-api, ig, lightstreamer, swift, trading
- Language: Swift
- Homepage:
- Size: 43.6 MB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: docs/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
This framework provides:
- All public [HTTP IG endpoints](https://labs.ig.com/rest-trading-api-reference) (with request, response, and error handling support).
- All public [Lightstreamer IG subscriptions](https://labs.ig.com/streaming-api-reference) (with request, response, and error handling support).
The [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.
- 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
// 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.
```swift
import IG
```
## API
All public HTTP endpoints are defined under the `API` reference type. To expose the functionality:
1. Create an API instance.
```swift
let api = API()
// Optionally you can pass the demo rootURL: API(rootURL: API.demoRootURL)
```
2. Log into an account.
```swift
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](https://labs.ig.com/gettingstarted).
3. Call a specific endpoint.
```swift
// 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:
```swift
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.
```swift
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.
```swift
streamer.sessions.connect()
```
3. Subscribe to any targeted event.
```swift
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](https://labs.ig.com/faq#limits).
## 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.
```swift
let db = try Database(location: .memory)
```
2. Write some API market data.
```swift
db.markets.update(apiMarket)
```
3. Write some API prices.
```swift
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.
```swift
let user: API.User = .init(name: "username", password: "password")
let apiKey: API.Key = "a12345bc67890d12345e6789fg0hi123j4567890"
```
2. Create a services aggregator.
```swift
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.