Skip to main content

Ctrader Fix API

Project description

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

Python Ctrader Fix API

ToDo

  • Account Information "not possible fix limitation"
  • Market Position buy and sell
  • Peding orders limit and stop
  • Partial close
  • Stop loss & Take profit
  • Modify Orders
  • Modify position
  • real time bid & ask

Installation

Tested on python 3.7 to 3.9

pip install ejtraderCT -U

Or install from source

git clone https://github.com/ejtraderLabs/ejtraderCT
cd ejtraderCT
python setup.py install

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 ejtraderCT 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.1104928
password="12345678" # - The password you configured

api = Ctrader(server,account,password)
To disconnect and logout from the account
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

symbol = "EURUSD"
volume = 0.01 # position size:
stoploss =  1.18
takeprofit = 1.19

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

# sell position 

symbol = "EURUSD"
volume = 0.01 # position size
stoploss = 1.19
takeprofit = 1.18

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

symbol = "EURUSD"
volume = 0.01 # order size
stoploss = 1.17
takeprofit = 1.19
price = 1.18 # entry price 

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


# Sell limit order

symbol = "EURUSD"
volume = 0.01 # Order size
stoploss = 1.23
takeprofit = 1.17
price = 1.22 # entry price 

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

Stop Orders

# Buy stop order

symbol = "EURUSD"
volume = 0.01 # order size
stoploss = 1.20
takeprofit = 1.24
price = 1.22 # entry price

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

# Sell stop order

symbol = "EURUSD"
volume = 0.01 # order size
stoploss = 1.19
takeprofit = 1.17
price = 1.18 # entry price 

api.sellStop(symbol, volume, stoploss, takeprofit, 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()

Modify position SL and TP

id = "position id "
stoploss = "stop loss price""
takeprofit "stop gain price"

api.positionModify(id, stoploss, takeprofit)

Modify order SL and TP and entry price

id = "order id "
stoploss = "stop loss price""
takeprofit= "stop gain price"
price = "limit or stop entry price"

api.orderModify(id, stoploss, takeprofit, price)

Contributors:

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.

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

ejtraderCT-1.1.1a0.tar.gz (29.2 kB view hashes)

Uploaded Source

Built Distribution

ejtraderCT-1.1.1a0-py3-none-any.whl (27.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page