trading with Python and IG markets API
Project description
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:
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:
from datetime import datetime, timedelta
import requests_cache
session = requests_cache.CachedSession(cache_name='cache', backend='sqlite', expire_after=timedelta(hours=1))
# set expire_after=None if you don't want cache expiration
# set expire_after=0 if you don't want to cache queries
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for trading_ig-0.0.5-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b09bf93baf67a87ea6abfd0aec5fcc283d171833429eaf266b7b632c10dbe44 |
|
MD5 | 49904233caafeed9f9cd1102c3f23fb0 |
|
BLAKE2b-256 | 15a9089dd3fcd4e12b5c1e538656b25764ed1cc344dc7886f02416d357a0188d |