Skip to main content

cTrader SDK

The ctrader-sdk package provides a simple interface to interact with the cTrader API for trading and data fetching. This package supports placing orders, modifying and canceling orders, fetching historical data, and retrieving account information.

Installation

To install the package, you can use pip:

pip install ctrader-sdk

Usage

Initialization

First, initialize the CTraderBot class with your credentials:

from ctrader_sdk import CTraderBot

client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'
access_token = 'YOUR_ACCESS_TOKEN'
account_id = 'YOUR_ACCOUNT_ID'

bot = CTraderBot(client_id, client_secret, access_token, account_id)

Methods

place_order

Place a buy or sell order.

order_response = bot.place_order(
    symbol='EURUSD',
    volume=100000,
    direction='BUY',
    price=1.2000,
    take_profit=1.2100,
    stop_loss=1.1900
)
print(order_response)

Parameters:

symbol (str): The symbol to trade (e.g., 'EURUSD'). volume (int): The volume of the trade. direction (str): 'BUY' or 'SELL'. order_type (str, optional): 'LIMIT' (default) or 'MARKET'. price (float, optional): The limit price for the order. take_profit (float, optional): The take profit price. stop_loss (float, optional): The stop loss price.

modify_order

Modify an existing order.

modify_response = bot.modify_order(
    order_id='ORDER_ID',
    price=1.2050
)
print(modify_response)

Parameters:

order_id (str): The ID of the order to modify. price (float, optional): The new limit price. take_profit (float, optional): The new take profit price. stop_loss (float, optional): The new stop loss price.

cancel_order

Cancel an existing order.

cancel_response = bot.cancel_order(order_id='ORDER_ID')
print(cancel_response)

Parameters:

order_id (str): The ID of the order to cancel.

fetch_dataframe

Fetch historical data as a pandas DataFrame.

from datetime import datetime, timedelta

start_time = datetime.now() - timedelta(days=7)
end_time = datetime.now()

df = bot.fetch_dataframe(
    symbol='EURUSD',
    start_time=start_time,
    end_time=end_time,
    timeframe='H1'
)
print(df)

Parameters:

symbol (str): The symbol to fetch data for (e.g., 'EURUSD'). start_time (datetime): The start time for the data. end_time (datetime): The end time for the data. timeframe (str, optional): The timeframe for the data (default: 'H1').

get_account_equity

Get the account equity.

equity = bot.get_account_equity()
print(f'Account Equity: {equity}')

Parameters:

None

get_account_information

Fetch detailed account information.

account_info = bot.get_account_information()
print(account_info)

Parameters:

None

get_open_positions

Fetch all open positions.

positions = bot.get_open_positions()
print(positions)

Parameters:

None

get_open_orders

Fetch all open orders.

    orders = bot.get_open_orders()
    print(orders)

Parameters:

None

calculate_technical_indicators

Calculate basic technical indicators.

    df_with_indicators = bot.calculate_technical_indicators(df)
    print(df_with_indicators)

Parameters:

df (pandas.DataFrame): The DataFrame containing historical data.

Error Handling

The ctrader-sdk package includes basic error handling. If a request fails, an error message will be logged, and an exception will be raised.

Example:

try:
    order_response = bot.place_order(
        symbol='EURUSD',
        volume=100000,
        direction='BUY',
        price=1.2000,
        take_profit=1.2100,
        stop_loss=1.1900
    )
except requests.exceptions.HTTPError as err:
    print(f"HTTP error occurred: {err}")
except Exception as err:
    print(f"An error occurred: {err}")

Logging

The package uses the built-in logging module to log information and errors. By default, the logging level is set to INFO. You can customize the logging configuration as needed.

import logging

logging.basicConfig(level=logging.DEBUG)

Example Use Cases

Automated Trading Bot:

-> Place, modify, and cancel orders programmatically. -> Fetch historical data for backtesting and strategy development. -> Calculate technical indicators for decision-making.

Data Analysis:

-> Fetch historical data for multiple symbols. -> Calculate and analyze technical indicators. -> Monitor account equity and open positions.

Release files for ctrader-sdk 0.1.1

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

Source distribution (sdist)

Source distribution for ctrader-sdk 0.1.1
File Size Uploaded
ctrader-sdk-0.1.1.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ctrader-sdk 0.1.1
File Interpreter ABI Platform
ctrader_sdk-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / ctrader-sdk-0.1.1.tar.gz

Download URL ctrader-sdk-0.1.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0e4543804d1c53cdb338142507aea3a69c3f361fec0f540350899db7a21f3251
BLAKE2b-256 checksum
How to use checksums
250a050ca018bee004fc293e6bc59410faed52888b341d96fd245a689ec78b73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release files / ctrader_sdk-0.1.1-py3-none-any.whl

Download URL ctrader_sdk-0.1.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
286f28e30632a07d3292f7e20625031a89ed65caf70fd38fcdc2caa1520a592b
BLAKE2b-256 checksum
How to use checksums
eb20f1648b924bf692b17103eec77fa39fd5ec58236afefb84ec809e1c6aa65d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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