Skip to content

Commit 150737c

Browse files
Merge pull request #14 from ejtraderLabs/dev
Refactoring
2 parents b5af509 + 128d739 commit 150737c

23 files changed

+2780
-1085
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 110

.github/workflows/update_contributors.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ dist/
44
*.egg-info/
55
.vscode/
66
__pycache__
7-
.DS_Store
7+
.DS_Store
8+
fix.ipynb
9+
examples/test
10+
.app.py
11+
.env

LICENSE

Lines changed: 21 additions & 674 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 89 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,85 @@
55

66
# Python Ctrader Fix API
77

8-
### ToDo
8+
`ejtraderCT` is a Python library to access the Ctrader trading platform's FIX API.
9+
10+
## Features
911

10-
- [ ] Account Information "not possible fix limitation"
1112
- [x] Market Position buy and sell
12-
- [x] Peding orders limit and stop
13-
- [x] Partial close
14-
- [x] Stop loss & Take profit
15-
- [x] Modify Orders
16-
- [x] Modify position
17-
- [x] real time bid & ask
13+
- [x] Pending orders limit and stop (No SL and TP)
14+
- [x] Partial close (No SL and TP)
15+
- [x] Stop loss & Take profit Only Position
16+
- [x] Real-time bid & ask
17+
- [x] Check connection status
18+
- [x] Custom Client ID and commend in Position
19+
- [ ] Rest API server (in development)
20+
- [ ] Webhook for Tradingviewer (in development)
21+
22+
## Prerequisites
1823

24+
The library has been tested on Python 3.7 to 3.9.
1925

2026
## Installation
21-
#### Tested on python 3.7 to 3.9
22-
```
27+
28+
To install the latest version of `ejtraderCT`, you can use pip:
29+
30+
```shell
2331
pip install ejtraderCT -U
2432
```
25-
#### Or install from source
26-
33+
Or if you want to install from source, you can use:
34+
```shell
35+
pip install git+https://github.com/ejtraderLabs/ejtraderCT.git
2736
```
28-
git clone https://github.com/ejtraderLabs/ejtraderCT
29-
cd ejtraderCT
30-
python setup.py install
3137

32-
```
38+
## Accessing the Ctrader FIX API
3339

34-
### Import libraries
3540

36-
```python
37-
from ejtraderCT import Ctrader
41+
To access your API, follow these simple steps:
42+
43+
1. Open the cTrader desktop or web platform.
44+
2. In the bottom left corner of the platform, you will find the **Settings** option. Click on it.
45+
3. A popup window will appear. In the menu of the popup, look for the last option: **FIX API**.
46+
4. First, click on the **Change Password** button. Make sure to add a numeric password of at least 8 digits.
47+
5. After changing the password, click on the **Copy to Clipboard** button from **Trade Connection**.
48+
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):
49+
50+
- Host name: (Current IP address 168.205.95.20 can be changed without notice)
51+
- Port: 5212 (SSL), 5202 (Plain text)
52+
- Password: (a/c 1104928 password)
53+
- SenderCompID: live.icmarkets.1104926 or demo.icmarkets.1104926 or live2.icmarkets.1104926
54+
- TargetCompID: cServer
55+
- SenderSubID: TRADE
3856

39-
import time
40-
import logging
41-
from datetime import datetime
4257

43-
logging.getLogger().setLevel(logging.INFO)
4458

4559

60+
61+
### Import libraries
62+
63+
```python
64+
from ejtraderCT import Ctrader
4665
```
4766

4867
### Fix account login and details
4968

5069
```python
51-
server="h8.p.c-trader.cn" # Host name
52-
broker="icmarkets"
53-
account="3152339"
54-
password="393214"
55-
currency="EUR"
70+
server="168.205.95.20" # - Host name: (Current IP address 168.205.95.20 can be changed without notice)
71+
account="live.icmarkets.1104926" # - SenderCompID: live.icmarkets.1104926
72+
password="12345678" # - The password you configured
5673

57-
api = Ctrader(server,broker,account,password,currency)
74+
api = Ctrader(server,account,password)
5875

5976
```
60-
61-
##### To disconnect and logout from the account
77+
##### Check the connection status
78+
```python
79+
api.isconnected()
80+
```
81+
##### Logout
6282
```python
6383
api.logout()
6484
```
6585

66-
### Real-time quote
86+
#### Real-time quote
6787

6888
##### Subscribe to symbols
6989
```python
@@ -95,22 +115,28 @@ print(quote)
95115

96116
```python
97117
# Buy position
118+
price = api.quote()
119+
price = price['EURUSD']['bid']
98120

99121
symbol = "EURUSD"
100122
volume = 0.01 # position size:
101-
stoploss = 1.18
102-
takeprofit = 1.19
123+
stoploss = round(price - 0.00010,6)
124+
takeprofit = round(price + 0.00010,6)
103125

104-
api.buy(symbol, volume, stoploss, takeprofit)
126+
id = api.buy(symbol, volume, stoploss, takeprofit)
127+
print(f"Position: {id}")
105128

106129
# sell position
130+
price = api.quote()
131+
price = price['EURUSD']['bid']
107132

108133
symbol = "EURUSD"
109134
volume = 0.01 # position size
110-
stoploss = 1.19
111-
takeprofit = 1.18
135+
stoploss = round(price + 0.00010,6)
136+
takeprofit = round(price - 0.00010,6)
112137

113-
api.sell(symbol, volume, stoploss, takeprofit)
138+
id = api.sell(symbol, volume, stoploss, takeprofit)
139+
print(f"Position: {id}")
114140
```
115141

116142
##### Limit Orders
@@ -120,23 +146,21 @@ api.sell(symbol, volume, stoploss, takeprofit)
120146
# Buy limit order
121147

122148
symbol = "EURUSD"
123-
volume = 0.01 # position size
124-
stoploss = 1.17
125-
takeprofit = 1.19
149+
volume = 0.01 # order size
126150
price = 1.18 # entry price
127151

128-
api.buyLimit(symbol, volume, stoploss, takeprofit, price)
152+
id = api.buyLimit(symbol, volume, price)
153+
print(f"Order: {id}")
129154

130155

131156
# Sell limit order
132157

133158
symbol = "EURUSD"
134-
volume = 0.01 # position size
135-
stoploss = 1.23
136-
takeprofit = 1.17
159+
volume = 0.01 # Order size
137160
price = 1.22 # entry price
138161

139-
api.sellLimit(symbol, volume, stoploss, takeprofit, price)
162+
id = api.sellLimit(symbol, volume, price)
163+
print(f"Order: {id}")
140164
```
141165

142166
#### Stop Orders
@@ -146,22 +170,19 @@ api.sellLimit(symbol, volume, stoploss, takeprofit, price)
146170
# Buy stop order
147171

148172
symbol = "EURUSD"
149-
volume = 0.01 # position size
150-
stoploss = 1.20
151-
takeprofit = 1.24
173+
volume = 0.01 # order size
152174
price = 1.22 # entry price
153175

154-
api.buyStop(symbol, volume, stoploss, takeprofit, price)
176+
id = api.buyStop(symbol, volume, price)
177+
print(f"Order: {id}")
155178

156179
# Sell stop order
157180

158181
symbol = "EURUSD"
159-
volume = 0.01 # position size
160-
stoploss = 1.19
161-
takeprofit = 1.17
182+
volume = 0.01 # order size
162183
price = 1.18 # entry price
163184

164-
api.sellStop(symbol, volume, stoploss, takeprofit, price)
185+
api.sellStop(symbol, volume, price)
165186

166187
```
167188

@@ -206,31 +227,28 @@ api.cancel_all()
206227
```python
207228
api.close_all()
208229
```
209-
#### Modify position SL and TP
230+
#### Parcial Close position
231+
210232
```python
211-
id = "position id "
212-
stoploss = "stop loss price""
213-
takeprofit "stop gain price"
233+
api.positionPartialClose(id, volume)
234+
```
214235

215-
api.positionModify(id, stoploss, takeprofit)
236+
### Disclosure
216237

217-
```
238+
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 ejtraderCT library or the application itself.
218239

219-
#### Modify order SL and TP and entry price
220-
```python
221-
id = "order id "
222-
stoploss = "stop loss price""
223-
takeprofit= "stop gain price"
224-
price = "limit or stop entry price"
240+
However, you can avoid this problem by using either the SL or TP, but not both simultaneously.
225241

226-
api.orderModify(id, stoploss, takeprofit, price)
242+
## Contributing
227243

228-
```
229-
## Contributors:
244+
We welcome any contribution to `ejtraderCT`. Here are some ways to contribute:
245+
246+
1. Report issues or suggest improvements by opening an [issue](https://github.com/ejtraderLabs/ejtraderCT/issues).
247+
2. Contribute with code to fix issues or add features via a [Pull Request](https://github.com/ejtraderLabs/ejtraderCT/pulls).
230248

231-
<!-- CONTRIBUTORS:START -->
232-
<!-- CONTRIBUTORS:END -->
249+
Before submitting a pull request, please make sure your codes are well formatted and tested.
233250

234251
## Acknowledgements
235252

236253
I would like to express my gratitude to [@HarukaMa](https://github.com/HarukaMa) for creating the initial project. Their work has been an invaluable starting point for my modifications and improvements.
254+

0 commit comments

Comments
 (0)