fxsocket 0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fxsocket-0.1/.github/workflows/ci.yml +27 -0
- fxsocket-0.1/.github/workflows/publish.yml +60 -0
- fxsocket-0.1/.gitignore +22 -0
- fxsocket-0.1/LICENSE +21 -0
- fxsocket-0.1/PKG-INFO +256 -0
- fxsocket-0.1/README.md +229 -0
- fxsocket-0.1/examples/manage_accounts.py +19 -0
- fxsocket-0.1/examples/stream_quotes.py +33 -0
- fxsocket-0.1/examples/terminal_rest.py +44 -0
- fxsocket-0.1/pyproject.toml +62 -0
- fxsocket-0.1/src/fxsocket/__init__.py +134 -0
- fxsocket-0.1/src/fxsocket/_http.py +68 -0
- fxsocket-0.1/src/fxsocket/_version.py +1 -0
- fxsocket-0.1/src/fxsocket/client.py +254 -0
- fxsocket-0.1/src/fxsocket/config.py +12 -0
- fxsocket-0.1/src/fxsocket/enums.py +122 -0
- fxsocket-0.1/src/fxsocket/errors.py +163 -0
- fxsocket-0.1/src/fxsocket/management.py +118 -0
- fxsocket-0.1/src/fxsocket/models.py +391 -0
- fxsocket-0.1/src/fxsocket/py.typed +0 -0
- fxsocket-0.1/src/fxsocket/terminal/__init__.py +45 -0
- fxsocket-0.1/src/fxsocket/terminal/client.py +757 -0
- fxsocket-0.1/src/fxsocket/terminal/stream.py +594 -0
- fxsocket-0.1/tests/test_errors.py +62 -0
- fxsocket-0.1/tests/test_management.py +184 -0
- fxsocket-0.1/tests/test_stream.py +538 -0
- fxsocket-0.1/tests/test_terminal.py +681 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: ${{ matrix.python-version }}
|
|
20
|
+
- name: Install
|
|
21
|
+
run: pip install -e ".[dev]"
|
|
22
|
+
- name: Lint
|
|
23
|
+
run: ruff check .
|
|
24
|
+
- name: Type-check
|
|
25
|
+
run: mypy
|
|
26
|
+
- name: Test
|
|
27
|
+
run: pytest -q
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
name: publish
|
|
2
|
+
|
|
3
|
+
# Two paths, both using PyPI Trusted Publishing (OIDC — no API tokens):
|
|
4
|
+
# • Manual run (Actions ▸ publish ▸ "Run workflow") -> TestPyPI (dry run)
|
|
5
|
+
# • Push a version tag, e.g. git tag v0.1.0 && git push --tags -> PyPI
|
|
6
|
+
on:
|
|
7
|
+
workflow_dispatch: {}
|
|
8
|
+
push:
|
|
9
|
+
tags:
|
|
10
|
+
- "v*"
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.12"
|
|
20
|
+
- name: Build sdist + wheel
|
|
21
|
+
run: |
|
|
22
|
+
python -m pip install --upgrade build twine
|
|
23
|
+
python -m build
|
|
24
|
+
twine check dist/*
|
|
25
|
+
- uses: actions/upload-artifact@v4
|
|
26
|
+
with:
|
|
27
|
+
name: dist
|
|
28
|
+
path: dist/
|
|
29
|
+
|
|
30
|
+
testpypi:
|
|
31
|
+
# Dry run: only the manual trigger, never a tag.
|
|
32
|
+
if: github.event_name == 'workflow_dispatch'
|
|
33
|
+
needs: build
|
|
34
|
+
runs-on: ubuntu-latest
|
|
35
|
+
environment: testpypi
|
|
36
|
+
permissions:
|
|
37
|
+
id-token: write # required for trusted publishing
|
|
38
|
+
steps:
|
|
39
|
+
- uses: actions/download-artifact@v4
|
|
40
|
+
with:
|
|
41
|
+
name: dist
|
|
42
|
+
path: dist/
|
|
43
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
44
|
+
with:
|
|
45
|
+
repository-url: https://test.pypi.org/legacy/
|
|
46
|
+
|
|
47
|
+
pypi:
|
|
48
|
+
# Real release: only when a version tag was pushed.
|
|
49
|
+
if: startsWith(github.ref, 'refs/tags/v')
|
|
50
|
+
needs: build
|
|
51
|
+
runs-on: ubuntu-latest
|
|
52
|
+
environment: pypi
|
|
53
|
+
permissions:
|
|
54
|
+
id-token: write # required for trusted publishing
|
|
55
|
+
steps:
|
|
56
|
+
- uses: actions/download-artifact@v4
|
|
57
|
+
with:
|
|
58
|
+
name: dist
|
|
59
|
+
path: dist/
|
|
60
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
fxsocket-0.1/.gitignore
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.eggs/
|
|
6
|
+
build/
|
|
7
|
+
dist/
|
|
8
|
+
.venv/
|
|
9
|
+
venv/
|
|
10
|
+
env/
|
|
11
|
+
|
|
12
|
+
# Tooling
|
|
13
|
+
.pytest_cache/
|
|
14
|
+
.mypy_cache/
|
|
15
|
+
.ruff_cache/
|
|
16
|
+
.coverage
|
|
17
|
+
htmlcov/
|
|
18
|
+
|
|
19
|
+
# OS / editors
|
|
20
|
+
.DS_Store
|
|
21
|
+
.idea/
|
|
22
|
+
.vscode/
|
fxsocket-0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FxSocket
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
fxsocket-0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fxsocket
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: Python SDK for the FxSocket API — MT4/MT5 account management, trading, and real-time streaming.
|
|
5
|
+
Project-URL: Homepage, https://fxsocket.com
|
|
6
|
+
Project-URL: Documentation, https://api.fxsocket.com/v1/docs
|
|
7
|
+
Project-URL: Source, https://github.com/fxsocket-com/FxSocket-Python-SDK
|
|
8
|
+
Author: FxSocket
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: forex,fxsocket,metatrader,mt4,mt5,trading
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Requires-Dist: httpx>=0.27
|
|
18
|
+
Requires-Dist: pydantic>=2.5
|
|
19
|
+
Requires-Dist: websockets>=13
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: mypy>=1.11; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
23
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
24
|
+
Requires-Dist: respx>=0.21; extra == 'dev'
|
|
25
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# FxSocket Python SDK
|
|
29
|
+
|
|
30
|
+
[](https://pypi.org/project/fxsocket/)
|
|
31
|
+
[](https://pypi.org/project/fxsocket/)
|
|
32
|
+
[](https://github.com/fxsocket-com/FxSocket-Python-SDK/actions/workflows/ci.yml)
|
|
33
|
+
[](https://github.com/fxsocket-com/FxSocket-Python-SDK/blob/main/LICENSE)
|
|
34
|
+
|
|
35
|
+
Typed Python client for the [FxSocket](https://fxsocket.com) API. Connect your
|
|
36
|
+
MetaTrader 4 / 5 accounts, then place trades, read market data, and stream live
|
|
37
|
+
updates over REST and WebSocket — with mirrored **synchronous** and **async**
|
|
38
|
+
interfaces.
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
- **Account management** — link, list, fetch, and disconnect MT4/MT5 accounts.
|
|
43
|
+
- **Trading** — market & pending orders, modify, close, plus margin/profit calculators.
|
|
44
|
+
- **Market data** — quotes, symbol specifications, OHLC history, account state & info.
|
|
45
|
+
- **Live streaming** — ticks, bars, account, positions, trades, and terminal status
|
|
46
|
+
over WebSocket, with automatic reconnect + subscription replay.
|
|
47
|
+
- **Sync *and* async** — `Client` / `AsyncClient`, method-for-method mirrors.
|
|
48
|
+
- **Typed** — Pydantic v2 models throughout; ships `py.typed`.
|
|
49
|
+
- **One interface for MT4 and MT5** — platform differences handled for you.
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install fxsocket
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Requires Python 3.10+.
|
|
58
|
+
|
|
59
|
+
## Quickstart
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from fxsocket import Client
|
|
63
|
+
|
|
64
|
+
with Client(api_key="fxs_live_…") as fx: # or set FXSOCKET_API_KEY
|
|
65
|
+
account = fx.accounts.list()[0]
|
|
66
|
+
term = fx.terminal(account)
|
|
67
|
+
|
|
68
|
+
print("equity:", term.account_summary().equity)
|
|
69
|
+
print("EURUSD:", term.quote("EURUSD").ask)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Authentication
|
|
73
|
+
|
|
74
|
+
Every call uses your FxSocket API key (`fxs_live_…`), from the dashboard.
|
|
75
|
+
Pass it explicitly, or set the `FXSOCKET_API_KEY` environment variable and call
|
|
76
|
+
`Client()` with no arguments.
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from fxsocket import Client
|
|
80
|
+
|
|
81
|
+
with Client(api_key="fxs_live_…") as fx:
|
|
82
|
+
for account in fx.accounts.list():
|
|
83
|
+
print(account.platform, account.nickname, account.status)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Managing accounts
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from fxsocket import Client
|
|
90
|
+
|
|
91
|
+
with Client(api_key="fxs_live_…") as fx:
|
|
92
|
+
# Link a new account (platform defaults to MT5).
|
|
93
|
+
account = fx.accounts.create(
|
|
94
|
+
platform="mt5", server="ICMarkets-Demo", login=1150125, password="…",
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# Poll until it's connected.
|
|
98
|
+
account = fx.accounts.get(account.id)
|
|
99
|
+
print(account.status) # connecting → connected
|
|
100
|
+
|
|
101
|
+
# Where this account's terminal API lives (empty until provisioned).
|
|
102
|
+
print(account.rest_url, account.ws_url)
|
|
103
|
+
|
|
104
|
+
fx.accounts.delete(account.id) # unlink
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Everything is also available on `AsyncClient`:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from fxsocket import AsyncClient
|
|
111
|
+
|
|
112
|
+
async with AsyncClient(api_key="fxs_live_…") as fx:
|
|
113
|
+
accounts = await fx.accounts.list()
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Trading & market data
|
|
117
|
+
|
|
118
|
+
`fx.terminal(account)` returns a REST client bound to that account's terminal
|
|
119
|
+
(resolved from `account.rest_url`, whether it's a shared pod or a private droplet):
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
from fxsocket import Client
|
|
123
|
+
|
|
124
|
+
with Client(api_key="fxs_live_…") as fx:
|
|
125
|
+
account = fx.accounts.get("…") # must be connected (has a terminal)
|
|
126
|
+
term = fx.terminal(account)
|
|
127
|
+
|
|
128
|
+
summary = term.account_summary() # balance, equity, margin, …
|
|
129
|
+
quote = term.quote("EURUSD") # latest tick
|
|
130
|
+
bars = term.price_history("EURUSD", "M5") # recent OHLC bars
|
|
131
|
+
|
|
132
|
+
result = term.order_send( # market buy
|
|
133
|
+
symbol="EURUSD", operation="Buy", volume=0.10,
|
|
134
|
+
stop_loss=1.07, take_profit=1.10,
|
|
135
|
+
)
|
|
136
|
+
if result.success:
|
|
137
|
+
term.order_modify(result.order, take_profit=1.12) # None keeps the SL
|
|
138
|
+
term.order_close(result.order)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Inputs are validated client-side before they're sent. One guard worth knowing:
|
|
142
|
+
in `order_modify`, a literal `stop_loss=0.0` would *remove* your stop-loss, so
|
|
143
|
+
it's rejected — pass `clear_stop_loss=True` to remove one deliberately, while
|
|
144
|
+
`None` (the default) keeps the current value.
|
|
145
|
+
|
|
146
|
+
MT4 and MT5 share one interface. MT5-only timeframes (`M2`, `M3`, `H2`, `H6`,
|
|
147
|
+
`H8`, `H12`) raise `UnsupportedOnPlatformError` on MT4 before any request.
|
|
148
|
+
|
|
149
|
+
> **MT4 history note:** on MT4, `price_history` with `from_`/`to` bounds (or the
|
|
150
|
+
> `D1` timeframe) can fail server-side with `CopyRates failed` when the terminal
|
|
151
|
+
> hasn't loaded that history. Calling `price_history(symbol, timeframe)` without
|
|
152
|
+
> date bounds returns the most recent bars reliably.
|
|
153
|
+
|
|
154
|
+
## Streaming (WebSocket)
|
|
155
|
+
|
|
156
|
+
Subscribe to live ticks, bars, account, positions, trades, and terminal status.
|
|
157
|
+
Streaming is async-first; a synchronous wrapper is provided too. A dropped
|
|
158
|
+
connection auto-reconnects and replays active subscriptions
|
|
159
|
+
(`auto_reconnect=True` by default).
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
import asyncio
|
|
163
|
+
from fxsocket import AsyncClient, Tick, Bar, AccountUpdate
|
|
164
|
+
|
|
165
|
+
async def main():
|
|
166
|
+
async with AsyncClient(api_key="fxs_live_…") as fx:
|
|
167
|
+
account = await fx.accounts.get("…")
|
|
168
|
+
async with fx.stream(account) as s:
|
|
169
|
+
await s.subscribe_prices("EURUSD")
|
|
170
|
+
await s.subscribe_bars("EURUSD", "M5")
|
|
171
|
+
await s.subscribe_account()
|
|
172
|
+
async for event in s:
|
|
173
|
+
match event:
|
|
174
|
+
case Tick():
|
|
175
|
+
print(event.symbol, event.data.bid, event.data.ask)
|
|
176
|
+
case Bar():
|
|
177
|
+
print(event.symbol, event.timeframe, event.data.close)
|
|
178
|
+
case AccountUpdate():
|
|
179
|
+
print("equity", event.data.equity)
|
|
180
|
+
|
|
181
|
+
asyncio.run(main())
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Synchronous equivalent:
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from fxsocket import Client, Tick
|
|
188
|
+
|
|
189
|
+
with Client(api_key="fxs_live_…") as fx:
|
|
190
|
+
with fx.stream(fx.accounts.get("…")) as s:
|
|
191
|
+
s.subscribe_prices("EURUSD")
|
|
192
|
+
for event in s:
|
|
193
|
+
if isinstance(event, Tick):
|
|
194
|
+
print(event.data.bid, event.data.ask)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Errors
|
|
198
|
+
|
|
199
|
+
Every failure raises a subclass of `fxsocket.FxSocketError`:
|
|
200
|
+
|
|
201
|
+
| Exception | When |
|
|
202
|
+
|---|---|
|
|
203
|
+
| `AuthError` | missing/invalid API key |
|
|
204
|
+
| `RateLimitError` | rate limited (`.retry_after`) |
|
|
205
|
+
| `ValidationError` | malformed request |
|
|
206
|
+
| `NotFoundError` | account/resource not found |
|
|
207
|
+
| `AccountCapError` | plan account limit reached (`.cap`, `.current`) |
|
|
208
|
+
| `DuplicateAccountError` | account already linked |
|
|
209
|
+
| `ConnectFailedError` | broker rejected the login |
|
|
210
|
+
| `TerminalNotReadyError` | terminal not provisioned / not ready |
|
|
211
|
+
| `UnsupportedOnPlatformError` | feature not available on this platform |
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
from fxsocket import Client, AccountCapError
|
|
215
|
+
|
|
216
|
+
try:
|
|
217
|
+
fx.accounts.create(server="Demo", login=1, password="…")
|
|
218
|
+
except AccountCapError as e:
|
|
219
|
+
print(f"Plan limit reached: {e.current}/{e.cap}")
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Private hosting
|
|
223
|
+
|
|
224
|
+
Privately-hosted accounts (a dedicated droplet) are listed, traded, and streamed
|
|
225
|
+
exactly like shared-cluster accounts — their `rest_url` / `ws_url` simply point
|
|
226
|
+
at the droplet. The droplet serves a self-signed certificate, so reach it with
|
|
227
|
+
`Client(..., verify_terminal_tls=False)` (or supply a pinned CA). *Creating* a
|
|
228
|
+
private-hosted account is done in the dashboard.
|
|
229
|
+
|
|
230
|
+
## Timestamps
|
|
231
|
+
|
|
232
|
+
Terminal timestamps (`quote.time`, candle `time`, order times) are returned as
|
|
233
|
+
**strings in broker server time** — not Python `datetime`. The trailing `Z` is
|
|
234
|
+
stylistic and does **not** mean UTC. Use `terminal.server_timezone()` to get the
|
|
235
|
+
broker's UTC offset if you need to convert.
|
|
236
|
+
|
|
237
|
+
## Requirements
|
|
238
|
+
|
|
239
|
+
- Python 3.10+
|
|
240
|
+
- [`httpx`](https://www.python-httpx.org/), [`pydantic`](https://docs.pydantic.dev/) ≥ 2, [`websockets`](https://websockets.readthedocs.io/) ≥ 13
|
|
241
|
+
|
|
242
|
+
## Links
|
|
243
|
+
|
|
244
|
+
- API reference: <https://api.fxsocket.com/v1/docs>
|
|
245
|
+
- Examples: [`examples/`](https://github.com/fxsocket-com/FxSocket-Python-SDK/tree/main/examples)
|
|
246
|
+
|
|
247
|
+
## Development
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
pip install -e ".[dev]"
|
|
251
|
+
ruff check . && mypy && pytest
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## License
|
|
255
|
+
|
|
256
|
+
MIT — see [LICENSE](https://github.com/fxsocket-com/FxSocket-Python-SDK/blob/main/LICENSE).
|
fxsocket-0.1/README.md
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# FxSocket Python SDK
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/fxsocket/)
|
|
4
|
+
[](https://pypi.org/project/fxsocket/)
|
|
5
|
+
[](https://github.com/fxsocket-com/FxSocket-Python-SDK/actions/workflows/ci.yml)
|
|
6
|
+
[](https://github.com/fxsocket-com/FxSocket-Python-SDK/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
Typed Python client for the [FxSocket](https://fxsocket.com) API. Connect your
|
|
9
|
+
MetaTrader 4 / 5 accounts, then place trades, read market data, and stream live
|
|
10
|
+
updates over REST and WebSocket — with mirrored **synchronous** and **async**
|
|
11
|
+
interfaces.
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Account management** — link, list, fetch, and disconnect MT4/MT5 accounts.
|
|
16
|
+
- **Trading** — market & pending orders, modify, close, plus margin/profit calculators.
|
|
17
|
+
- **Market data** — quotes, symbol specifications, OHLC history, account state & info.
|
|
18
|
+
- **Live streaming** — ticks, bars, account, positions, trades, and terminal status
|
|
19
|
+
over WebSocket, with automatic reconnect + subscription replay.
|
|
20
|
+
- **Sync *and* async** — `Client` / `AsyncClient`, method-for-method mirrors.
|
|
21
|
+
- **Typed** — Pydantic v2 models throughout; ships `py.typed`.
|
|
22
|
+
- **One interface for MT4 and MT5** — platform differences handled for you.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install fxsocket
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Requires Python 3.10+.
|
|
31
|
+
|
|
32
|
+
## Quickstart
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from fxsocket import Client
|
|
36
|
+
|
|
37
|
+
with Client(api_key="fxs_live_…") as fx: # or set FXSOCKET_API_KEY
|
|
38
|
+
account = fx.accounts.list()[0]
|
|
39
|
+
term = fx.terminal(account)
|
|
40
|
+
|
|
41
|
+
print("equity:", term.account_summary().equity)
|
|
42
|
+
print("EURUSD:", term.quote("EURUSD").ask)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Authentication
|
|
46
|
+
|
|
47
|
+
Every call uses your FxSocket API key (`fxs_live_…`), from the dashboard.
|
|
48
|
+
Pass it explicitly, or set the `FXSOCKET_API_KEY` environment variable and call
|
|
49
|
+
`Client()` with no arguments.
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from fxsocket import Client
|
|
53
|
+
|
|
54
|
+
with Client(api_key="fxs_live_…") as fx:
|
|
55
|
+
for account in fx.accounts.list():
|
|
56
|
+
print(account.platform, account.nickname, account.status)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Managing accounts
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from fxsocket import Client
|
|
63
|
+
|
|
64
|
+
with Client(api_key="fxs_live_…") as fx:
|
|
65
|
+
# Link a new account (platform defaults to MT5).
|
|
66
|
+
account = fx.accounts.create(
|
|
67
|
+
platform="mt5", server="ICMarkets-Demo", login=1150125, password="…",
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# Poll until it's connected.
|
|
71
|
+
account = fx.accounts.get(account.id)
|
|
72
|
+
print(account.status) # connecting → connected
|
|
73
|
+
|
|
74
|
+
# Where this account's terminal API lives (empty until provisioned).
|
|
75
|
+
print(account.rest_url, account.ws_url)
|
|
76
|
+
|
|
77
|
+
fx.accounts.delete(account.id) # unlink
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Everything is also available on `AsyncClient`:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from fxsocket import AsyncClient
|
|
84
|
+
|
|
85
|
+
async with AsyncClient(api_key="fxs_live_…") as fx:
|
|
86
|
+
accounts = await fx.accounts.list()
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Trading & market data
|
|
90
|
+
|
|
91
|
+
`fx.terminal(account)` returns a REST client bound to that account's terminal
|
|
92
|
+
(resolved from `account.rest_url`, whether it's a shared pod or a private droplet):
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from fxsocket import Client
|
|
96
|
+
|
|
97
|
+
with Client(api_key="fxs_live_…") as fx:
|
|
98
|
+
account = fx.accounts.get("…") # must be connected (has a terminal)
|
|
99
|
+
term = fx.terminal(account)
|
|
100
|
+
|
|
101
|
+
summary = term.account_summary() # balance, equity, margin, …
|
|
102
|
+
quote = term.quote("EURUSD") # latest tick
|
|
103
|
+
bars = term.price_history("EURUSD", "M5") # recent OHLC bars
|
|
104
|
+
|
|
105
|
+
result = term.order_send( # market buy
|
|
106
|
+
symbol="EURUSD", operation="Buy", volume=0.10,
|
|
107
|
+
stop_loss=1.07, take_profit=1.10,
|
|
108
|
+
)
|
|
109
|
+
if result.success:
|
|
110
|
+
term.order_modify(result.order, take_profit=1.12) # None keeps the SL
|
|
111
|
+
term.order_close(result.order)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Inputs are validated client-side before they're sent. One guard worth knowing:
|
|
115
|
+
in `order_modify`, a literal `stop_loss=0.0` would *remove* your stop-loss, so
|
|
116
|
+
it's rejected — pass `clear_stop_loss=True` to remove one deliberately, while
|
|
117
|
+
`None` (the default) keeps the current value.
|
|
118
|
+
|
|
119
|
+
MT4 and MT5 share one interface. MT5-only timeframes (`M2`, `M3`, `H2`, `H6`,
|
|
120
|
+
`H8`, `H12`) raise `UnsupportedOnPlatformError` on MT4 before any request.
|
|
121
|
+
|
|
122
|
+
> **MT4 history note:** on MT4, `price_history` with `from_`/`to` bounds (or the
|
|
123
|
+
> `D1` timeframe) can fail server-side with `CopyRates failed` when the terminal
|
|
124
|
+
> hasn't loaded that history. Calling `price_history(symbol, timeframe)` without
|
|
125
|
+
> date bounds returns the most recent bars reliably.
|
|
126
|
+
|
|
127
|
+
## Streaming (WebSocket)
|
|
128
|
+
|
|
129
|
+
Subscribe to live ticks, bars, account, positions, trades, and terminal status.
|
|
130
|
+
Streaming is async-first; a synchronous wrapper is provided too. A dropped
|
|
131
|
+
connection auto-reconnects and replays active subscriptions
|
|
132
|
+
(`auto_reconnect=True` by default).
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
import asyncio
|
|
136
|
+
from fxsocket import AsyncClient, Tick, Bar, AccountUpdate
|
|
137
|
+
|
|
138
|
+
async def main():
|
|
139
|
+
async with AsyncClient(api_key="fxs_live_…") as fx:
|
|
140
|
+
account = await fx.accounts.get("…")
|
|
141
|
+
async with fx.stream(account) as s:
|
|
142
|
+
await s.subscribe_prices("EURUSD")
|
|
143
|
+
await s.subscribe_bars("EURUSD", "M5")
|
|
144
|
+
await s.subscribe_account()
|
|
145
|
+
async for event in s:
|
|
146
|
+
match event:
|
|
147
|
+
case Tick():
|
|
148
|
+
print(event.symbol, event.data.bid, event.data.ask)
|
|
149
|
+
case Bar():
|
|
150
|
+
print(event.symbol, event.timeframe, event.data.close)
|
|
151
|
+
case AccountUpdate():
|
|
152
|
+
print("equity", event.data.equity)
|
|
153
|
+
|
|
154
|
+
asyncio.run(main())
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Synchronous equivalent:
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
from fxsocket import Client, Tick
|
|
161
|
+
|
|
162
|
+
with Client(api_key="fxs_live_…") as fx:
|
|
163
|
+
with fx.stream(fx.accounts.get("…")) as s:
|
|
164
|
+
s.subscribe_prices("EURUSD")
|
|
165
|
+
for event in s:
|
|
166
|
+
if isinstance(event, Tick):
|
|
167
|
+
print(event.data.bid, event.data.ask)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Errors
|
|
171
|
+
|
|
172
|
+
Every failure raises a subclass of `fxsocket.FxSocketError`:
|
|
173
|
+
|
|
174
|
+
| Exception | When |
|
|
175
|
+
|---|---|
|
|
176
|
+
| `AuthError` | missing/invalid API key |
|
|
177
|
+
| `RateLimitError` | rate limited (`.retry_after`) |
|
|
178
|
+
| `ValidationError` | malformed request |
|
|
179
|
+
| `NotFoundError` | account/resource not found |
|
|
180
|
+
| `AccountCapError` | plan account limit reached (`.cap`, `.current`) |
|
|
181
|
+
| `DuplicateAccountError` | account already linked |
|
|
182
|
+
| `ConnectFailedError` | broker rejected the login |
|
|
183
|
+
| `TerminalNotReadyError` | terminal not provisioned / not ready |
|
|
184
|
+
| `UnsupportedOnPlatformError` | feature not available on this platform |
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from fxsocket import Client, AccountCapError
|
|
188
|
+
|
|
189
|
+
try:
|
|
190
|
+
fx.accounts.create(server="Demo", login=1, password="…")
|
|
191
|
+
except AccountCapError as e:
|
|
192
|
+
print(f"Plan limit reached: {e.current}/{e.cap}")
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Private hosting
|
|
196
|
+
|
|
197
|
+
Privately-hosted accounts (a dedicated droplet) are listed, traded, and streamed
|
|
198
|
+
exactly like shared-cluster accounts — their `rest_url` / `ws_url` simply point
|
|
199
|
+
at the droplet. The droplet serves a self-signed certificate, so reach it with
|
|
200
|
+
`Client(..., verify_terminal_tls=False)` (or supply a pinned CA). *Creating* a
|
|
201
|
+
private-hosted account is done in the dashboard.
|
|
202
|
+
|
|
203
|
+
## Timestamps
|
|
204
|
+
|
|
205
|
+
Terminal timestamps (`quote.time`, candle `time`, order times) are returned as
|
|
206
|
+
**strings in broker server time** — not Python `datetime`. The trailing `Z` is
|
|
207
|
+
stylistic and does **not** mean UTC. Use `terminal.server_timezone()` to get the
|
|
208
|
+
broker's UTC offset if you need to convert.
|
|
209
|
+
|
|
210
|
+
## Requirements
|
|
211
|
+
|
|
212
|
+
- Python 3.10+
|
|
213
|
+
- [`httpx`](https://www.python-httpx.org/), [`pydantic`](https://docs.pydantic.dev/) ≥ 2, [`websockets`](https://websockets.readthedocs.io/) ≥ 13
|
|
214
|
+
|
|
215
|
+
## Links
|
|
216
|
+
|
|
217
|
+
- API reference: <https://api.fxsocket.com/v1/docs>
|
|
218
|
+
- Examples: [`examples/`](https://github.com/fxsocket-com/FxSocket-Python-SDK/tree/main/examples)
|
|
219
|
+
|
|
220
|
+
## Development
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
pip install -e ".[dev]"
|
|
224
|
+
ruff check . && mypy && pytest
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
MIT — see [LICENSE](https://github.com/fxsocket-com/FxSocket-Python-SDK/blob/main/LICENSE).
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""List the trading accounts linked to your FxSocket API key.
|
|
2
|
+
|
|
3
|
+
Run: FXSOCKET_API_KEY=fxs_live_... python examples/manage_accounts.py
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from fxsocket import Client
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main() -> None:
|
|
10
|
+
with Client() as fx: # reads FXSOCKET_API_KEY from the environment
|
|
11
|
+
accounts = fx.accounts.list()
|
|
12
|
+
print(f"{len(accounts)} account(s):")
|
|
13
|
+
for a in accounts:
|
|
14
|
+
terminal = a.rest_url or "(no terminal — bridge-only/provisioning)"
|
|
15
|
+
print(f" {a.id} {a.platform.value:3} {a.status.value:12} {terminal}")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
if __name__ == "__main__":
|
|
19
|
+
main()
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Stream live ticks and account updates over WebSocket (async).
|
|
2
|
+
|
|
3
|
+
Run: FXSOCKET_API_KEY=fxs_live_... python examples/stream_quotes.py <account-id>
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import asyncio
|
|
7
|
+
import sys
|
|
8
|
+
|
|
9
|
+
from fxsocket import AccountUpdate, AsyncClient, Tick
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
async def main(account_id: str) -> None:
|
|
13
|
+
async with AsyncClient() as fx:
|
|
14
|
+
account = await fx.accounts.get(account_id)
|
|
15
|
+
async with fx.stream(account) as s:
|
|
16
|
+
await s.subscribe_prices("EURUSD")
|
|
17
|
+
await s.subscribe_account()
|
|
18
|
+
print("streaming — Ctrl-C to stop")
|
|
19
|
+
async for event in s:
|
|
20
|
+
if isinstance(event, Tick):
|
|
21
|
+
d = event.data
|
|
22
|
+
print(f"tick {event.symbol} bid={d.bid} ask={d.ask}")
|
|
23
|
+
elif isinstance(event, AccountUpdate):
|
|
24
|
+
print(f"account equity={event.data.equity} {event.data.currency}")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
if __name__ == "__main__":
|
|
28
|
+
if len(sys.argv) != 2:
|
|
29
|
+
sys.exit("usage: python examples/stream_quotes.py <account-id>")
|
|
30
|
+
try:
|
|
31
|
+
asyncio.run(main(sys.argv[1]))
|
|
32
|
+
except KeyboardInterrupt:
|
|
33
|
+
pass
|