Skip to main content
Latest Version Supported Python versions Download format License Development Status https://sourcegraph.com/api/repos/github.com/femtotrader/ig-markets-rest-api-python-library/.badges/status.png https://badges.gitter.im/Join%20Chat.svg https://travis-ci.org/femtotrader/ig-markets-rest-api-python-library.svg?branch=master

IG Markets REST API - Python Library

A lightweight Python library that can be used to connect to the IG Markets REST API with a LIVE or DEMO account.

You can use the IG Markets HTTP / REST API to submit trade orders, open positions, close positions and view market sentiment. IG Markets provide Retail Spread Betting and CFD accounts for trading Equities, Forex, Commodities, Indices and much more.

Full details about the API along with information about how to open an account with IG can be found at the link below:

http://labs.ig.com/

How To Use The Library

Using this library to connect to the IG Markets API is extremely easy. All you need to do is import the IGService class, create an instance, and call the methods you wish to use. There is a method for each endpoint exposed by their API. The code sample below shows you how to connect to the API, switch to a secondary account and retrieve all open positions for the active account.

Note: The secure session with IG is established when you create an instance of the IGService class.

from trading_ig import IGService
from trading_ig_config import config

ig_service = IGService(config.username, config.password, config.api_key, config.acc_type)
ig_service.create_session()

account_info = ig_service.switch_account(config.acc_number, False) # not necessary
print(account_info)

open_positions = ig_service.fetch_open_positions()
print("open_positions:\n%s" % open_positions)

print("")

epic = 'CS.D.EURUSD.MINI.IP'
resolution = 'D'
num_points = 10
response = ig_service.fetch_historical_prices_by_epic_and_num_points(epic, resolution, num_points)
df_ask = response['prices']['ask']
print("ask prices:\n%s" % df_ask)

with trading_ig_config.py

class config(object):
    username = "YOUR_USERNAME"
    password = "YOUR_PASSWORD"
    api_key = "YOUR_API_KEY"
    acc_type = "DEMO" # LIVE / DEMO
    acc_number = "ABC123"

Config can also be set as environment variable

export IG_SERVICE_USERNAME="..."
export IG_SERVICE_PASSWORD="..."
export IG_SERVICE_API_KEY="..."
export IG_SERVICE_ACC_TYPE="DEMO" # LIVE / DEMO
export IG_SERVICE_ACC_NUMBER="..."

and we can get it using

from trading_ig import ConfigEnvVar
config = ConfigEnvVar("IG_SERVICE")

it should display:

open_positions:
Empty DataFrame
Columns: []
Index: []

ask prices:
                        Open     High      Low    Close
DateTime
2014:11:18-00:00:00  1.24510  1.25465  1.24442  1.25330
2014:11:19-00:00:00  1.25332  1.26013  1.25127  1.25461
2014:11:20-00:00:00  1.25463  1.25760  1.25048  1.25427
2014:11:21-00:00:00  1.25428  1.25689  1.23755  1.23924
2014:11:23-00:00:00  1.23640  1.23770  1.23607  1.23725
2014:11:24-00:00:00  1.23864  1.24453  1.23830  1.24390
2014:11:25-00:00:00  1.24389  1.24877  1.24026  1.24743
2014:11:26-00:00:00  1.24744  1.25322  1.24443  1.25077
2014:11:27-00:00:00  1.25078  1.25244  1.24569  1.24599
2014:11:28-00:00:00  1.24598  1.24909  1.24269  1.24505

Many IGService methods return Python Pandas DataFrame, Series or Panel.

Cache queries requests-cache

Set CachedSession using:

import requests_cache
session = requests_cache.CachedSession(cache_name='cache', backend='sqlite', expire_after=60*60)

CachedSession can be applied globally on IGService

ig_service = IGService(config.username, config.password, config.api_key, config.acc_type, session)
ig_service.create_session()

or just for a given method (like fetching prices)

epic = 'CS.D.EURUSD.MINI.IP'
resolution = 'D'
start_date = '2014-12-15'
end_date = '2014-12-20'
response = ig_service.fetch_historical_prices_by_epic_and_date_range(epic, resolution, start_date, end_date, session)

Install

From Python package index

$ pip install trading_ig

From source

Get latest version using Git

$ git clone https://github.com/femtotrader/ig-markets-rest-api-python-library.git
$ cd ig-markets-rest-api-python-library
$ python setup.py install

Release files for trading-ig 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trading-ig 0.0.3
File Size Uploaded
trading_ig-0.0.3.tar.gz 14.1 kB Details

Release files / trading_ig-0.0.3.tar.gz

Download URL trading_ig-0.0.3.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
68f8da7e93d84930c713ebde41dfebbe49fd8f45e265c56b8bbd2c3b71e8f872
BLAKE2b-256 checksum
How to use checksums
d831fcc59116c496adcabc65dc544b485fc278a74552cd6f5998de0cf05f9be6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.0.25

2 release files

0.0.24

2 release files

0.0.20

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.15

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

This release

0.0.3 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page