Skip to main content

Interface to the Gain Capital API V1 and V2, enabling users to perform various trading operations on Forex.com.

Project description

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

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

pygcapi-0.1.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

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

pygcapi-0.1.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file pygcapi-0.1.0.tar.gz.

File metadata

  • Download URL: pygcapi-0.1.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.6 Linux/6.9.3-76060903-generic

File hashes

Hashes for pygcapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2e9520c466b5554edf7de0aba622db312220f4df6064c0e1e5a5f19bf7eaf6b7
MD5 18059a64ecd295fefaa398ad0c12e7a2
BLAKE2b-256 3076a8f1a5a99406afb19a0ef7dddf18a3a7002d95266b273fe30752111a87dd

See more details on using hashes here.

File details

Details for the file pygcapi-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pygcapi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.6 Linux/6.9.3-76060903-generic

File hashes

Hashes for pygcapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ff6989d72110d8a6c1b73c92350db025880564e425c88d4d116d118e9d8e472
MD5 72fa53527a0b47de62be98720eb2a2e7
BLAKE2b-256 55d42b8c17f901db6b8b09fc87dbe13fe366a26da0ffb12e0a484c0184a42744

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