Skip to main content

A Python package for interacting with the cTrader API for trading and data fetching.

Project description

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.

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

ctrader-sdk-0.1.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

ctrader_sdk-0.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file ctrader-sdk-0.1.1.tar.gz.

File metadata

  • Download URL: ctrader-sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.12

File hashes

Hashes for ctrader-sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0e4543804d1c53cdb338142507aea3a69c3f361fec0f540350899db7a21f3251
MD5 9e74a7ffaaebbe34519225bf38a6610f
BLAKE2b-256 250a050ca018bee004fc293e6bc59410faed52888b341d96fd245a689ec78b73

See more details on using hashes here.

File details

Details for the file ctrader_sdk-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ctrader_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.12

File hashes

Hashes for ctrader_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 286f28e30632a07d3292f7e20625031a89ed65caf70fd38fcdc2caa1520a592b
MD5 ccc96ceb69d68b5ae55c6d37239a1d17
BLAKE2b-256 eb20f1648b924bf692b17103eec77fa39fd5ec58236afefb84ec809e1c6aa65d

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