Skip to main content

No project description provided

Project description

IG Trading Library

A comprehensive Python library for interfacing with the IG Trading platform. This library provides a straightforward and Pythonic way to interact with the IG Trading API, enabling automation of trading tasks, account management, position management, and order handling.

Prerequisites

  • Python 3.13+
  • make (for running tests)

Installation

pip install ig-trading-lib

Quick Start

from ig_trading_lib.authentication import AuthenticationService
from ig_trading_lib.authentication.cache import InMemoryCache
from ig_trading_lib.trading import CreatePosition, PositionService, IGClient

# Initialize authentication
auth_service = AuthenticationService(
    api_key="your_api_key",
    account_identifier="your_account_identifier", 
    account_password="your_account_password",
    base_url="https://demo-api.ig.com",
    cache=InMemoryCache()
)

# Authenticate
auth_response = auth_service.authenticate()

# Create client and position service
client = IGClient(base_url="https://demo-api.ig.com", api_key="your_api_key", tokens=auth_response.tokens)
position_service = PositionService(client)

# Create a position
position = CreatePosition(
    currencyCode="USD",
    direction="BUY",
    epic="CS.D.GBPUSD.TODAY.IP",
    orderType="MARKET",
    size=1
)

deal_reference = position_service.create_position(position)

Features

Authentication Module

  • AuthenticationService: Handles authentication with the IG REST API
  • Token Management: Automatic token caching and refresh
  • Multiple Cache Options: In-memory and encrypted file-based caching
  • Account Information: Retrieves account details and financial information

Trading Module

Positions Service

  • Create Positions: Open new trading positions with various order types (MARKET, LIMIT, QUOTE)
  • Get Positions: Retrieve all open positions or specific positions by deal ID
  • Update Positions: Modify existing positions (stop levels, limit levels, trailing stops)
  • Close Positions: Close existing positions with flexible order options
  • Risk Management: Support for guaranteed stops, trailing stops, and stop/limit orders

Orders Service

  • Working Orders: Create, retrieve, and delete working orders
  • Order Types: Support for LIMIT and STOP orders
  • Time in Force: GOOD_TILL_CANCELLED and GOOD_TILL_DATE options
  • Order Management: Full CRUD operations for working orders

Data Models

  • Type Safety: Full Pydantic model validation
  • Rich Validation: Comprehensive field validation and constraints
  • Serialization: Automatic JSON serialization/deserialization
  • Error Handling: Detailed error messages and validation feedback

Modules

Authentication (ig_trading_lib.authentication)

  • AuthenticationService: Main authentication service
  • AuthenticationCacheABC: Abstract cache interface
  • DurableCache: File-based caching with optional encryption
  • InMemoryCache: In-memory token caching

Trading (ig_trading_lib.trading)

  • PositionService: Position management operations
  • OrderService: Working order management operations

Models (via ig_trading_lib.trading facade)

  • CreatePosition: Position creation model
  • ClosePosition: Position closing model
  • UpdatePosition: Position update model
  • OpenPosition: Open position data model
  • Market: Market information model

Orders Models (via ig_trading_lib.trading facade)

  • CreateWorkingOrder: Working order creation model
  • WorkingOrder: Working order data model
  • MarketData: Market data for orders

Running Tests

The project includes comprehensive unit and integration tests:

# Run all tests
make test

# Run unit tests only
pytest tests/unit

# Run integration tests (requires API credentials)
pytest tests/integration

API Reference

Authentication Service

from ig_trading_lib.authentication import AuthenticationService
from ig_trading_lib.authentication.cache import DurableCache, InMemoryCache

# Basic usage
auth_service = AuthenticationService(
    api_key="your_api_key",
    account_identifier="your_account_identifier",
    account_password="your_account_password", 
    base_url="https://demo-api.ig.com"  # or https://api.ig.com for live
)

# With caching
auth_service = AuthenticationService(
    api_key="your_api_key",
    account_identifier="your_account_identifier",
    account_password="your_account_password",
    base_url="https://demo-api.ig.com",
    cache=DurableCache("tokens.json", encryption_key=b"your_key")  # Optional
)

auth_response = auth_service.authenticate()

Position Service

from ig_trading_lib.trading import PositionService, CreatePosition, IGClient

client = IGClient(base_url="https://demo-api.ig.com", api_key="your_api_key", tokens=auth_response.tokens)
position_service = PositionService(client)

# Create a market position
position = CreatePosition(
    currencyCode="USD",
    direction="BUY", 
    epic="CS.D.GBPUSD.TODAY.IP",
    orderType="MARKET",
    size=1
)
deal_ref = position_service.create_position(position)

# Get all open positions
positions = position_service.get_open_positions()

# Get specific position
position = position_service.get_open_position_by_deal_id("deal_id")

# Close position
from ig_trading_lib.trading.positions import ClosePosition
close_pos = ClosePosition.from_create(position)
position_service.close_position(close_pos)

Order Service

from ig_trading_lib.trading import OrderService, CreateWorkingOrder, IGClient

client = IGClient(base_url="https://demo-api.ig.com", api_key="your_api_key", tokens=auth_response.tokens)
order_service = OrderService(client)

# Create a working order
order = CreateWorkingOrder(
    currencyCode="USD",
    direction="BUY",
    epic="CS.D.GBPUSD.TODAY.IP", 
    level=1.2500,
    size=1,
    type="LIMIT"
)
deal_ref = order_service.create_order(order)

# Get all working orders
orders = order_service.get_orders()

# Delete working order
order_service.delete_order("deal_id")

Environment Variables

For testing and development, you can set these environment variables:

export IG_API_KEY="your_api_key"
export IG_ACCOUNT_IDENTIFIER="your_account_identifier" 
export IG_ACCOUNT_PASSWORD="your_account_password"
export IG_BASE_URL="https://demo-api.ig.com"  # or https://api.ig.com for live

Contributing

Contributions are welcome! Please feel free to fork the repository and create a pull request.

Development Setup

# Clone the repository
git clone <repository-url>
cd ig-trading-lib

# Install dependencies
poetry install

# Run tests
make test

# Run linting
make lint

License

This project is licensed under the MIT License.

Contact

If you have any questions, feel free to reach out to me on GitHub.

Disclaimer

This library is not affiliated with, authorized, endorsed, or in any way officially connected with IG Markets Ltd. Use at your own risk.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ig_trading_lib-2.1.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ig_trading_lib-2.1.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file ig_trading_lib-2.1.0.tar.gz.

File metadata

  • Download URL: ig_trading_lib-2.1.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for ig_trading_lib-2.1.0.tar.gz
Algorithm Hash digest
SHA256 268de4b743a05bcec81f75b0865950ebf776428c627794054558702e4ed571ef
MD5 2e868d448dbb9998691f0b561298cd78
BLAKE2b-256 f38250035ca5dcc838c90762e3d4e73b2e07d6bc996703014e7f104650072f95

See more details on using hashes here.

File details

Details for the file ig_trading_lib-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: ig_trading_lib-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Linux/6.11.0-1018-azure

File hashes

Hashes for ig_trading_lib-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d313d70cb544a0f4566739209e9ff63e2021ec7cbbd5e099dbd041724e836308
MD5 91fe3c7e208a0a112722096f56ad0541
BLAKE2b-256 0ca3d6ba0b8ca69528d4c3381f389de959c6ccd91f13698722961527221ad3ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page