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 PyPI using pip
:
pip install pygcapi
Or 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
= "your_username"
IDLOG = "your_password"
PSWD = "your_appkey"
APKEY
= GCapiClientV2(username=IDLOG, password=PSWD, appkey=APKEY) client
Example Usage
#Retrieve Account Information:
= client.get_account_info()
account_info print(account_info)
# Retrieve a specific field (e.g., TradingAccountId)
= client.get_account_info(key="TradingAccountId")
trading_account_id print(trading_account_id)
#Get Market Information:
= client.get_market_info("EUR/USD")
market_info print(market_info)
= client.get_market_info("EUR/USD", key="MarketId")
market_id = client.get_market_info("EUR/USD", key="Name")
market_name print(market_id)
print(market_name)
#Retrieve Price Data
import time
import datetime
from datetime import timedelta
# Define time period
= int((datetime.datetime.utcnow() - timedelta(days=30)).timestamp())
one_month_ago = int(datetime.datetime.utcnow().timestamp())
now
= client.get_prices(
prices =market_id,
market_id=100,
num_ticks=one_month_ago,
from_ts=now,
to_ts="MID"
price_type
)print(prices.head())
#Retrieve OHLC Data
= int((datetime.datetime.utcnow() - timedelta(days=1)).timestamp())
one_day_ago
= client.get_ohlc(
ohlc =market_id,
market_id=4000,
num_ticks="MINUTE",
interval=30,
span=one_day_ago,
from_ts=now
to_ts
)print(ohlc.head())
# Place a Trade Order:
= client.get_prices(market_id=market_id, from_ts=from_ts,to_ts=to_ts, num_ticks=1,price_type="BID")
pricesB =client.get_prices(market_id=market_id, from_ts=from_ts,to_ts=to_ts, num_ticks=1,price_type="ASK")
pricesA
= client.trade_order(
trade_resp =1020,
quantity="buy",
direction=market_id,
market_id=market_name,
market_name=float(pricesB.Price[0]), # Convert to scalar float
bid_price=float(pricesA.Price[0]) # Convert to scalar float
offer_price
)
# Close a Trade Order:
= client.trade_order(
close_resp =1020,
quantity="sell",
direction=True,
close=trade_resp.get('OrderId'),
order_id=market_id,
market_id=market_name,
market_name=float(pricesB.Price[0]), # Convert to scalar float
bid_price=float(pricesA.Price[0]) # Convert to scalar float
offer_price
)
#List Open Positions:
= client.list_open_positions()
open_positions print(open_positions)
#Close All Trades:
= client.close_all_trades(tolerance=0.0005)
close_responses print(close_responses)
#Retrieve Trade History:
= client.get_trade_history(max_results=50)
trade_history print(trade_history)
Getting Help or Reporting an Issue
To report bugs/issues/feature requests, please file an issue.
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