Quickstart

Installation

Installing with pip:

$ pip install trading-ig

Installing with all optional dependencies:

$ pip install "trading-ig[pandas, munch, tenacity]"

Configuration

Config file

Make a copy of the config template module (trading_ig_config.default.py), and rename trading_ig_config.py. Edit the new file, replacing the variables with your own:

class config(object):
    username = "your_username"
    password = "your_password"
    api_key = "your_api_key"
    acc_type = "DEMO"
    acc_number = "your_account_number"

Environment variables

If any exceptions are raised loading the config, IGService will attempt to find your authentication details from environment variables

$ export IG_SERVICE_USERNAME="your_username"
$ export IG_SERVICE_PASSWORD="your_password"
$ export IG_SERVICE_API_KEY="your_api_key"
$ export IG_SERVICE_ACC_NUMBER="your_account_number"
$ export IG_SERVICE_ACC_TYPE="your_acc_type"

Connection

>>> from trading_ig.rest import IGService
>>> from trading_ig.config import config
>>> ig_service = IGService(config.username, config.password, config.api_key, config.acc_number)
>>> ig = ig_service.create_session()
>>> ig

Using the REST API

Searching for a market

>>> results = ig_service.search_markets("gold")
>>> results

Get info about a market

>>> market = ig_service.fetch_market_by_epic('CS.D.USCGC.TODAY.IP')
>>> market

Getting historic prices

>>> result = ig_service.fetch_historical_prices_by_epic(epic='CS.D.USCGC.TODAY.IP')
>>> result['prices']

Opening a position

>>> resp = ig_service.create_open_position(
        currency_code='GBP',
        direction='BUY',
        epic='CS.D.USCGC.TODAY.IP',
        order_type='MARKET',
        expiry='DFB',
        force_open='false',
        guaranteed_stop='false',
        size=0.5, level=None,
        limit_distance=None,
        limit_level=None,
        quote_id=None,
        stop_level=None,
        stop_distance=None,
        trailing_stop=None,
        trailing_stop_increment=None)
>>> resp

Getting account activity

>>> from_date = datetime(2021, 1, 1)
>>> activities = ig_service.fetch_account_activity(from_date=from_date)
>>> activities

Using the Streaming API

Assuming config as above

>>> from trading_ig import IGService, IGStreamService
>>> from trading_ig.config import config
>>> from lightstreamer.client import Subscription, SubscriptionListener, ItemUpdate
>>> class PriceListener(SubscriptionListener):
>>>   def onItemUpdate(self, update: ItemUpdate):
>>>     logger.info(f"{update.getItemName()} Bid: {update.getValue('BIDPRICE1')}, Offer: {update.getValue('ASKPRICE1')}")
>>> ig_service = IGService(config.username, config.password, config.api_key, config.acc_type, acc_number=config.acc_number)
>>> ig_stream_service = IGStreamService(ig_service)
>>> ig_stream_service.create_session()
>>> sub = Subscription(mode="MERGE", items=[f"PRICE:{config.acc_number}:CS.D.GBPUSD.TODAY.IP"], fields=["BIDPRICE1", "ASKPRICE1"])
>>> sub.setDataAdapter("Pricing")
>>> sub.addListener(PriceListener())
>>> ig_stream_service.subscribe(sub)
>>> ig_stream_service.disconnect()

Using the Streamer classes to connect with the Streaming API

See the streamer samples: