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.positions import CreatePosition, PositionService

# 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 position service
position_service = PositionService(
    api_key="your_api_key",
    tokens=auth_response.tokens,
    base_url="https://demo-api.ig.com"
)

# 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 (ig_trading_lib.trading.positions.models)

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

Models (ig_trading_lib.trading.orders.models)

  • 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.positions import PositionService, CreatePosition

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

# 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.orders import OrderService, CreateWorkingOrder

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

# 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.0.1.tar.gz (13.6 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.0.1-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ig_trading_lib-2.0.1.tar.gz
  • Upload date:
  • Size: 13.6 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.0.1.tar.gz
Algorithm Hash digest
SHA256 27f9072ad100abfc298f272508ff215cb9669576ec5f8766853d168b4fc17985
MD5 4ca0d98631f4f31241ec63005be913c0
BLAKE2b-256 0c5c57b44d8398b65860f8de7a6b976413e71fc699004693588154f81ec86101

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ig_trading_lib-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.0 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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1d0e840f87c699a4f1423d5ea222bd6aa793ba81aec0c6bd4bc93b25efb00d69
MD5 314fcbeb717512e8d65bf3bc54c29beb
BLAKE2b-256 1f11b9ae49c0366671c4cbc0f8e5a18667497e9285d4f88c0c67e73693582717

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