Skip to main content

Pypi Publish GitHub release (latest by date) License PyPi downloads

Python Ctrader Fix API

SinanProjectCT is a Python library to access the Ctrader trading platform's FIX API.

Features

  • Market Position buy and sell
  • Pending orders limit and stop (No SL and TP)
  • Partial close (No SL and TP)
  • Stop loss & Take profit Only Position
  • Real-time bid & ask
  • Check connection status
  • Custom Client ID and commend in Position
  • Rest API server (in development)
  • Webhook for Tradingviewer (in development)

Prerequisites

The library has been tested on Python 3.7 to 3.9.

Installation

To install the latest version of SinanProjectCT, you can use pip:

pip install SinanProjectCT -U

Or if you want to install from source, you can use:

pip install git+https://github.com/Omer_CP/SinanProjectCT.git

Accessing the Ctrader FIX API

To access your API, follow these simple steps:

  1. Open the cTrader desktop or web platform.

  2. In the bottom left corner of the platform, you will find the Settings option. Click on it.

  3. A popup window will appear. In the menu of the popup, look for the last option: FIX API.

  4. First, click on the Change Password button. Make sure to add a numeric password of at least 8 digits.

  5. After changing the password, click on the Copy to Clipboard button from Trade Connection.

  6. Now, let's move to the Trade Connection section. Here, you will receive your data in the following format (this is an example with IC Markets for a real account):

    • Host name: (Current IP address 168.205.95.20 can be changed without notice)
    • Port: 5212 (SSL), 5202 (Plain text)
    • Password: (a/c 1104928 password)
    • SenderCompID: live.icmarkets.1104926 or demo.icmarkets.1104926 or live2.icmarkets.1104926
    • TargetCompID: cServer
    • SenderSubID: TRADE

Import libraries

from SinanProjectCT import Ctrader

Fix account login and details

server="168.205.95.20" # - Host name: (Current IP address 168.205.95.20 can be changed without notice)
account="live.icmarkets.1104926" #  - SenderCompID: live.icmarkets.1104926
password="12345678" # - The password you configured

api = Ctrader(server,account,password)
Check the connection status
api.isconnected()
Logout
api.logout()

Real-time quote

Subscribe to symbols
api.subscribe("EURUSD", "GBPUSD")
List of quotes for all symbols
quote = api.quote()
print(quote)

# Output

{'EURUSD': {'bid': 1.02616, 'ask': 1.02618}, 'GBPUSD': {'bid': 1.21358, 'ask': 1.21362}}

Quote for a single symbol

quote = api.quote("EURUSD")
print(quote)

# Output

{'bid': 1.02612, 'ask': 1.02614}

Market position and pending orders.

Market position
# Buy position
price = api.quote()
price = price['EURUSD']['bid'] 

symbol = "EURUSD"
volume = 0.01 # position size:
stoploss =  round(price - 0.00010,6)
takeprofit = round(price + 0.00010,6)

id = api.buy(symbol, volume, stoploss, takeprofit)
print(f"Position: {id}")

# sell position 
price = api.quote()
price = price['EURUSD']['bid'] 

symbol = "EURUSD"
volume = 0.01 # position size
stoploss =  round(price + 0.00010,6)
takeprofit = round(price - 0.00010,6)

id = api.sell(symbol, volume, stoploss, takeprofit)
print(f"Position: {id}")
Limit Orders
# Buy limit order

symbol = "EURUSD"
volume = 0.01 # order size
price = 1.18 # entry price 

id = api.buyLimit(symbol, volume, price)
print(f"Order: {id}")


# Sell limit order

symbol = "EURUSD"
volume = 0.01 # Order size
price = 1.22 # entry price 

id = api.sellLimit(symbol, volume, price)
print(f"Order: {id}")

Stop Orders

# Buy stop order

symbol = "EURUSD"
volume = 0.01 # order size
price = 1.22 # entry price

id = api.buyStop(symbol, volume, price)
print(f"Order: {id}")

# Sell stop order

symbol = "EURUSD"
volume = 0.01 # order size
price = 1.18 # entry price 

api.sellStop(symbol, volume, price)

List Positions

positions = api.positions()
print(positions)

List limit and stop Orders

orders = api.orders()
print(orders)

Cancel order by id

orders = api.orders()
for order in orders:
    api.orderCancelById(order['ord_id'])

Close position by id

for position in positions:
    api.positionCloseById(position['pos_id'], position['amount'])

Cancel all orders

api.cancel_all()

Close all positions

api.close_all()

Parcial Close position

api.positionPartialClose(id, volume) 

Disclosure

Due to certain limitations of the FIX API, there's a specific issue that arises when both the Stop Loss (SL) and Take Profit (TP) features are used concurrently. This issue occurs when one of them is triggered, the other remains open and will execute when the price reaches the specified level again, causing it to open another order. This issue needs to be addressed either within the SinanProjectCT library or the application itself.

However, you can avoid this problem by using either the SL or TP, but not both simultaneously.

Contributing

We welcome any contribution to SinanProjectCT. Here are some ways to contribute:

  1. Report issues or suggest improvements by opening an issue.
  2. Contribute with code to fix issues or add features via a Pull Request.

Before submitting a pull request, please make sure your codes are well formatted and tested.

Acknowledgements

I would like to express my gratitude to @HarukaMa for creating the initial project. Their work has been an invaluable starting point for my modifications and improvements.

Release files for SinanProjectCT 1.1.13

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

Source distribution (sdist)

Source distribution for SinanProjectCT 1.1.13
File Size Uploaded
SinanProjectCT-1.1.13.tar.gz 19.4 kB Details

Built distribution (wheel)

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

Total release size: 36.3 kB

Release files / SinanProjectCT-1.1.13.tar.gz

Download URL SinanProjectCT-1.1.13.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
46de13775cc0d3f94edbcf4e5b0b11215029725e1b0ce26aa3eefdb4e7f09209
BLAKE2b-256 checksum
How to use checksums
a54b766d5bb9a25eab7d16345fb0895fb6a5d241dc8d3d3392aa2078cc953955
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.7

Release files / SinanProjectCT-1.1.13-py3-none-any.whl

Download URL SinanProjectCT-1.1.13-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1b7b60d99ec23bc633cccc6e80ac23529cb509eee350d23ff9c946441c25955
BLAKE2b-256 checksum
How to use checksums
00c233076a0d06733e440011813a7e0f509a81d5a49b8453f2bb3bd161a45844
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.7

Release history Release notifications | RSS feed

This release

1.1.13 This release

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