Skip to main content

pygcapi

The pygcapi Python library provides an interface to the Gain Capital API (V1 and V2), allowing users to perform various trading operations on Forex.com. This library includes functionalities for account management, market information retrieval, trading operations, and historical data extraction. It also includes helper utilities and lookup tables to facilitate the interpretation of API responses.

By leveraging the extensive Python ecosystem—which includes libraries for data manipulation, scientific computing, and machine learning—pygcapi offers a robust solution for traders and data scientists seeking to enhance their trading operations and develop automated trading strategies.

Features

  • Account Management: Initialize sessions and manage user accounts.
  • Market Information: Retrieve real-time market data and details.
  • Trading Operations: Execute trades, manage orders, and track positions.
  • Historical Data: Extract and analyze historical market data.
  • Helper Functions: Utilize various helper functions and lookup tables for easier API response interpretation.

Installation

You can install this package directly from GitHub:

pip install git+https://github.com/athammad/pygcapi.git

API Credentials

To access your trading account, you will need credentials provided by Forex.com, including a username, password, and appkey. Once obtained, you can use these credentials to initialize a session with GCapiClient:

from pygcapi.core_v2 import GCapiClientV2

# Replace with your actual credentials
IDLOG = "your_username"
PSWD = "your_password"
APKEY = "your_appkey"

client = GCapiClientV2(username=IDLOG, password=PSWD, appkey=APKEY)

Example Usage

#Retrieve Account Information:

account_info = client.get_account_info()
print(account_info)

# Retrieve a specific field (e.g., TradingAccountId)

trading_account_id = client.get_account_info(key="TradingAccountId")
print(trading_account_id)

#Get Market Information:

market_info = client.get_market_info("EUR/USD")
print(market_info)

market_id = client.get_market_info("EUR/USD", key="MarketId")
market_name = client.get_market_info("EUR/USD", key="Name")
print(market_id)
print(market_name)

#Retrieve Price Data

import time
import datetime
from datetime import timedelta

# Define time period

one_month_ago = int((datetime.datetime.utcnow() - timedelta(days=30)).timestamp())
now = int(datetime.datetime.utcnow().timestamp())

prices = client.get_prices(
    market_id=market_id,
    num_ticks=100,
    from_ts=one_month_ago,
    to_ts=now,
    price_type="MID"
)
print(prices.head())

#Retrieve OHLC Data

one_day_ago = int((datetime.datetime.utcnow() - timedelta(days=1)).timestamp())

ohlc = client.get_ohlc(
    market_id=market_id,
    num_ticks=4000,
    interval="MINUTE",
    span=30,
    from_ts=one_day_ago,
    to_ts=now
)
print(ohlc.head())

# Place a Trade Order:

pricesB = client.get_prices(market_id=market_id, from_ts=from_ts,to_ts=to_ts, num_ticks=1,price_type="BID")
pricesA=client.get_prices(market_id=market_id, from_ts=from_ts,to_ts=to_ts, num_ticks=1,price_type="ASK")

trade_resp = client.trade_order(
    quantity=1020,
    direction="buy",
    market_id=market_id,
    market_name=market_name,
    bid_price=float(pricesB.Price[0]),  # Convert to scalar float
    offer_price=float(pricesA.Price[0])  # Convert to scalar float
)


# Close a Trade Order:
close_resp = client.trade_order(
    quantity=1020,
    direction="sell",
    close=True,
    order_id=trade_resp.get('OrderId'),
    market_id=market_id,
    market_name=market_name,
    bid_price=float(pricesB.Price[0]),  # Convert to scalar float
    offer_price=float(pricesA.Price[0])  # Convert to scalar float
)


#List Open Positions:

open_positions = client.list_open_positions()
print(open_positions)

#Close All Trades:

close_responses = client.close_all_trades(tolerance=0.0005)
print(close_responses)

#Retrieve Trade History:

trade_history = client.get_trade_history(max_results=50)
print(trade_history)

Getting Help or Reporting an Issue

To report bugs/issues/feature requests, please file an issue.

Author

pygcapi is written by Ahmed T. Hammad and is under active development. Please feel free to contribute by submitting any issues or requests—or by solving any current issues!

R version

If you prefer R, you can use the rgcapi library.

Disclaimer

This package is not supported by Forex.com, and the author does not hold any responsibility for how users decide to use the library. Use it at your own risk.

Official API documentation

https://docs.labs.gaincapital.com/index.html

Metadata

Release files for pygcapi 0.1.0

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

Source distribution (sdist)

Source distribution for pygcapi 0.1.0
File Size Uploaded Provenance Actions
pygcapi-0.1.0.tar.gz 18.3 kB Without provenance Details for pygcapi-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pygcapi 0.1.0
File Interpreter ABI Platform Actions
pygcapi-0.1.0-py3-none-any.whl Python 3 none any Details for pygcapi-0.1.0-py3-none-any.whl

Total release size: 39.6 kB

Release files / pygcapi-0.1.0.tar.gz

Download URL pygcapi-0.1.0.tar.gz
Size 18.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2e9520c466b5554edf7de0aba622db312220f4df6064c0e1e5a5f19bf7eaf6b7
BLAKE2b-256 checksum
How to use checksums
3076a8f1a5a99406afb19a0ef7dddf18a3a7002d95266b273fe30752111a87dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.11.6 Linux/6.9.3-76060903-generic

Release files / pygcapi-0.1.0-py3-none-any.whl

Download URL pygcapi-0.1.0-py3-none-any.whl
Size 21.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ff6989d72110d8a6c1b73c92350db025880564e425c88d4d116d118e9d8e472
BLAKE2b-256 checksum
How to use checksums
55d42b8c17f901db6b8b09fc87dbe13fe366a26da0ffb12e0a484c0184a42744
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.11.6 Linux/6.9.3-76060903-generic

Security for pygcapi 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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