tickwire 0.1.0__py3-none-any.whl
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.
- tickwire/__init__.py +44 -0
- tickwire/_operations.py +113 -0
- tickwire/_version.py +1 -0
- tickwire/agent.py +137 -0
- tickwire/backoff.py +57 -0
- tickwire/client.py +844 -0
- tickwire/config.py +53 -0
- tickwire/errors.py +107 -0
- tickwire/models.py +167 -0
- tickwire/py.typed +0 -0
- tickwire/results.py +81 -0
- tickwire/stream.py +281 -0
- tickwire/webhooks.py +133 -0
- tickwire-0.1.0.dist-info/METADATA +191 -0
- tickwire-0.1.0.dist-info/RECORD +18 -0
- tickwire-0.1.0.dist-info/WHEEL +5 -0
- tickwire-0.1.0.dist-info/licenses/LICENSE +21 -0
- tickwire-0.1.0.dist-info/top_level.txt +1 -0
tickwire/__init__.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Tickwire SDK for the hosted MT4/MT5 API.
|
|
2
|
+
|
|
3
|
+
>>> from tickwire import AsyncTickwire
|
|
4
|
+
>>> async with AsyncTickwire(sandbox=True) as tw: # TICKWIRE_SANDBOX_URL, TICKWIRE_API_KEY
|
|
5
|
+
... acc = tw.account(account_id)
|
|
6
|
+
... print((await acc.health())["status"])
|
|
7
|
+
... await acc.market_order("EURUSD", 0.01) # idempotent, audited
|
|
8
|
+
... async with acc.stream() as s:
|
|
9
|
+
... state = await s.wait_synchronized()
|
|
10
|
+
"""
|
|
11
|
+
from ._version import __version__
|
|
12
|
+
from .agent import AgentClient, AsyncAgentClient
|
|
13
|
+
from .backoff import Backoff, RetryBudget
|
|
14
|
+
from .client import Account, AsyncAccount, AsyncTickwire, Tickwire, new_idempotency_key, sandbox_signup
|
|
15
|
+
from .config import resolve_base_url
|
|
16
|
+
from .errors import (
|
|
17
|
+
BadRequest,
|
|
18
|
+
ConfigError,
|
|
19
|
+
Conflict,
|
|
20
|
+
Forbidden,
|
|
21
|
+
NotFound,
|
|
22
|
+
NotSupportedOnMt4,
|
|
23
|
+
OutcomeUnknown,
|
|
24
|
+
RateLimited,
|
|
25
|
+
RetryBudgetExhausted,
|
|
26
|
+
SandboxKeyRestricted,
|
|
27
|
+
ServerError,
|
|
28
|
+
TickwireError,
|
|
29
|
+
TransportError,
|
|
30
|
+
Unauthorized,
|
|
31
|
+
)
|
|
32
|
+
from .models import TRADE_SUCCESS_CODES, trade_succeeded
|
|
33
|
+
from .results import TradeResult
|
|
34
|
+
from .stream import AccountStream, TerminalState
|
|
35
|
+
from .webhooks import WebhookVerificationError, verify as verify_webhook, verify_standard as verify_standard_webhook
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"Account", "AccountStream", "AgentClient", "AsyncAccount", "AsyncAgentClient", "AsyncTickwire", "Backoff",
|
|
39
|
+
"BadRequest", "ConfigError", "Conflict", "Forbidden", "NotFound", "NotSupportedOnMt4", "OutcomeUnknown",
|
|
40
|
+
"RateLimited", "RetryBudget", "RetryBudgetExhausted", "SandboxKeyRestricted", "ServerError", "TRADE_SUCCESS_CODES", "TerminalState",
|
|
41
|
+
"Tickwire", "TickwireError", "TradeResult", "TransportError", "Unauthorized", "WebhookVerificationError",
|
|
42
|
+
"__version__", "new_idempotency_key", "resolve_base_url", "sandbox_signup", "trade_succeeded", "verify_standard_webhook",
|
|
43
|
+
"verify_webhook",
|
|
44
|
+
]
|
tickwire/_operations.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Generated by sdk/openapi/generate.py from the public OpenAPI spec. Do not edit."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import Any, Literal, Required, TypedDict # noqa: F401
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CreateWebhook(TypedDict, total=False):
|
|
8
|
+
secret: str | None
|
|
9
|
+
url: Required[str]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class KeyBody(TypedDict, total=False):
|
|
13
|
+
name: str
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class MoveAccounts(TypedDict, total=False):
|
|
17
|
+
account_ids: Required[list[str]]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class PatchEndpoint(TypedDict, total=False):
|
|
21
|
+
enabledEvents: list[str] | None
|
|
22
|
+
url: str | None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class RotateSecret(TypedDict, total=False):
|
|
26
|
+
overlapHours: float
|
|
27
|
+
secret: str | None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class SignupBody(TypedDict, total=False):
|
|
31
|
+
platform: Literal["mt4", "mt5"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
OPERATIONS: dict[str, dict[str, Any]] = {
|
|
35
|
+
"GET /users/current/accounts/{account_id}/account-information": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/account-information", "pathParams": ["account_id"], "pending": False, "query": ["refreshTerminalState"]},
|
|
36
|
+
"GET /users/current/accounts/{account_id}/audit": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/audit", "pathParams": ["account_id"], "pending": False, "query": ["limit"]},
|
|
37
|
+
"GET /users/current/accounts/{account_id}/capabilities": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/capabilities", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
38
|
+
"GET /users/current/accounts/{account_id}/health": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/health", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
39
|
+
"GET /users/current/accounts/{account_id}/history-deals/position/{position_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/history-deals/position/{position_id}", "pathParams": ["account_id", "position_id"], "pending": False, "query": []},
|
|
40
|
+
"GET /users/current/accounts/{account_id}/history-deals/time/{start_time}/{end_time}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/history-deals/time/{start_time}/{end_time}", "pathParams": ["account_id", "start_time", "end_time"], "pending": False, "query": ["limit", "offset"]},
|
|
41
|
+
"GET /users/current/accounts/{account_id}/history-orders/time/{start_time}/{end_time}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/history-orders/time/{start_time}/{end_time}", "pathParams": ["account_id", "start_time", "end_time"], "pending": False, "query": ["limit", "offset"]},
|
|
42
|
+
"GET /users/current/accounts/{account_id}/orders": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/orders", "pathParams": ["account_id"], "pending": False, "query": ["refreshTerminalState"]},
|
|
43
|
+
"GET /users/current/accounts/{account_id}/orders/{order_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/orders/{order_id}", "pathParams": ["account_id", "order_id"], "pending": False, "query": ["refreshTerminalState"]},
|
|
44
|
+
"GET /users/current/accounts/{account_id}/positions": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/positions", "pathParams": ["account_id"], "pending": False, "query": ["refreshTerminalState"]},
|
|
45
|
+
"GET /users/current/accounts/{account_id}/positions/{position_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/positions/{position_id}", "pathParams": ["account_id", "position_id"], "pending": False, "query": ["refreshTerminalState"]},
|
|
46
|
+
"GET /users/current/accounts/{account_id}/server-time": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/server-time", "pathParams": ["account_id"], "pending": False, "query": ["refresh"]},
|
|
47
|
+
"GET /users/current/accounts/{account_id}/snapshot": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/snapshot", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
48
|
+
"GET /users/current/accounts/{account_id}/symbols": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/symbols", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
49
|
+
"GET /users/current/accounts/{account_id}/symbols/{symbol}/current-price": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/symbols/{symbol}/current-price", "pathParams": ["account_id", "symbol"], "pending": False, "query": ["keepSubscription"]},
|
|
50
|
+
"GET /users/current/accounts/{account_id}/symbols/{symbol}/current-tick": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/symbols/{symbol}/current-tick", "pathParams": ["account_id", "symbol"], "pending": False, "query": ["keepSubscription"]},
|
|
51
|
+
"GET /users/current/accounts/{account_id}/symbols/{symbol}/specification": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/users/current/accounts/{account_id}/symbols/{symbol}/specification", "pathParams": ["account_id", "symbol"], "pending": False, "query": []},
|
|
52
|
+
"POST /users/current/accounts/{account_id}/trade": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/users/current/accounts/{account_id}/trade", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
53
|
+
"POST /users/current/accounts/{account_id}/trade/check": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": [], "method": "POST", "path": "/users/current/accounts/{account_id}/trade/check", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
54
|
+
"GET /v1/accounts": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/accounts", "pathParams": [], "pending": False, "query": []},
|
|
55
|
+
"GET /v1/accounts/{account_id}": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/accounts/{account_id}", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
56
|
+
"GET /v1/agent-audit": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent-audit", "pathParams": [], "pending": False, "query": ["account_id", "agent_key_id", "before", "cursor", "limit"]},
|
|
57
|
+
"GET /v1/agent-keys": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent-keys", "pathParams": [], "pending": False, "query": ["cursor", "limit"]},
|
|
58
|
+
"POST /v1/agent-keys": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/agent-keys", "pathParams": [], "pending": False, "query": []},
|
|
59
|
+
"DELETE /v1/agent-keys/{key_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": ["X-Actor", "X-Reason"], "method": "DELETE", "path": "/v1/agent-keys/{key_id}", "pathParams": ["key_id"], "pending": False, "query": []},
|
|
60
|
+
"GET /v1/agent-keys/{key_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent-keys/{key_id}", "pathParams": ["key_id"], "pending": False, "query": []},
|
|
61
|
+
"PATCH /v1/agent-keys/{key_id}": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["X-Actor", "X-Reason"], "method": "PATCH", "path": "/v1/agent-keys/{key_id}", "pathParams": ["key_id"], "pending": False, "query": []},
|
|
62
|
+
"PUT /v1/agent-keys/{key_id}/kill-switch": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["X-Actor", "X-Reason"], "method": "PUT", "path": "/v1/agent-keys/{key_id}/kill-switch", "pathParams": ["key_id"], "pending": False, "query": []},
|
|
63
|
+
"GET /v1/agent-kill-switches": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent-kill-switches", "pathParams": [], "pending": False, "query": ["cursor", "limit"]},
|
|
64
|
+
"PUT /v1/agent-kill-switches/accounts/{account_id}": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["X-Actor", "X-Reason"], "method": "PUT", "path": "/v1/agent-kill-switches/accounts/{account_id}", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
65
|
+
"GET /v1/agent/accounts": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts", "pathParams": [], "pending": False, "query": ["cursor", "limit"]},
|
|
66
|
+
"GET /v1/agent/accounts/{account_id}/account-information": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/account-information", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
67
|
+
"GET /v1/agent/accounts/{account_id}/health": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/health", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
68
|
+
"GET /v1/agent/accounts/{account_id}/history-deals": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/history-deals", "pathParams": ["account_id"], "pending": False, "query": ["end_time", "limit", "start_time"]},
|
|
69
|
+
"GET /v1/agent/accounts/{account_id}/orders": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/orders", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
70
|
+
"GET /v1/agent/accounts/{account_id}/positions": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/positions", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
71
|
+
"GET /v1/agent/accounts/{account_id}/snapshot": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/snapshot", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
72
|
+
"GET /v1/agent/accounts/{account_id}/symbols/{symbol}/candles": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/symbols/{symbol}/candles", "pathParams": ["account_id", "symbol"], "pending": False, "query": ["limit", "start_time", "timeframe"]},
|
|
73
|
+
"GET /v1/agent/accounts/{account_id}/symbols/{symbol}/current-candles/{timeframe}": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/symbols/{symbol}/current-candles/{timeframe}", "pathParams": ["account_id", "symbol", "timeframe"], "pending": False, "query": []},
|
|
74
|
+
"GET /v1/agent/accounts/{account_id}/symbols/{symbol}/current-price": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/symbols/{symbol}/current-price", "pathParams": ["account_id", "symbol"], "pending": False, "query": []},
|
|
75
|
+
"GET /v1/agent/accounts/{account_id}/symbols/{symbol}/specification": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/accounts/{account_id}/symbols/{symbol}/specification", "pathParams": ["account_id", "symbol"], "pending": False, "query": []},
|
|
76
|
+
"POST /v1/agent/accounts/{account_id}/trade": {"auth": "agent", "body": "object", "controlPrefixed": False, "headers": ["Idempotency-Key", "X-Reason"], "method": "POST", "path": "/v1/agent/accounts/{account_id}/trade", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
77
|
+
"GET /v1/agent/audit": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/audit", "pathParams": [], "pending": False, "query": ["before", "cursor", "limit"]},
|
|
78
|
+
"GET /v1/agent/me": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/me", "pathParams": [], "pending": False, "query": []},
|
|
79
|
+
"GET /v1/agent/requests/{idempotency_key}": {"auth": "agent", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/agent/requests/{idempotency_key}", "pathParams": ["idempotency_key"], "pending": False, "query": []},
|
|
80
|
+
"GET /v1/connected-apps": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/connected-apps", "pathParams": [], "pending": False, "query": ["cursor", "include_ended", "limit"]},
|
|
81
|
+
"DELETE /v1/connected-apps/{grant_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": ["X-Actor", "X-Reason"], "method": "DELETE", "path": "/v1/connected-apps/{grant_id}", "pathParams": ["grant_id"], "pending": False, "query": []},
|
|
82
|
+
"GET /v1/dedicated-ips": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/dedicated-ips", "pathParams": [], "pending": False, "query": []},
|
|
83
|
+
"POST /v1/dedicated-ips/move-accounts": {"auth": "key", "body": "MoveAccounts", "controlPrefixed": True, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/dedicated-ips/move-accounts", "pathParams": [], "pending": False, "query": []},
|
|
84
|
+
"POST /v1/events/{event_id}/resend": {"auth": "key", "body": None, "controlPrefixed": True, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/events/{event_id}/resend", "pathParams": ["event_id"], "pending": False, "query": ["webhookId"]},
|
|
85
|
+
"POST /v1/execution/accounts/{account_id}/trade": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["Idempotency-Key", "X-Reason"], "method": "POST", "path": "/v1/execution/accounts/{account_id}/trade", "pathParams": ["account_id"], "pending": False, "query": []},
|
|
86
|
+
"GET /v1/pretrade-holds": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/pretrade-holds", "pathParams": [], "pending": False, "query": ["account_id", "cursor", "limit", "status"]},
|
|
87
|
+
"GET /v1/pretrade-holds/{hold_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/pretrade-holds/{hold_id}", "pathParams": ["hold_id"], "pending": False, "query": []},
|
|
88
|
+
"POST /v1/pretrade-holds/{hold_id}/cancel": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "POST", "path": "/v1/pretrade-holds/{hold_id}/cancel", "pathParams": ["hold_id"], "pending": False, "query": []},
|
|
89
|
+
"GET /v1/pretrade-hooks": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/pretrade-hooks", "pathParams": [], "pending": False, "query": ["cursor", "limit"]},
|
|
90
|
+
"POST /v1/pretrade-hooks": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/pretrade-hooks", "pathParams": [], "pending": False, "query": []},
|
|
91
|
+
"DELETE /v1/pretrade-hooks/{hook_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": ["X-Actor", "X-Reason"], "method": "DELETE", "path": "/v1/pretrade-hooks/{hook_id}", "pathParams": ["hook_id"], "pending": False, "query": []},
|
|
92
|
+
"GET /v1/pretrade-hooks/{hook_id}": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/pretrade-hooks/{hook_id}", "pathParams": ["hook_id"], "pending": False, "query": []},
|
|
93
|
+
"PATCH /v1/pretrade-hooks/{hook_id}": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["X-Actor", "X-Reason"], "method": "PATCH", "path": "/v1/pretrade-hooks/{hook_id}", "pathParams": ["hook_id"], "pending": False, "query": []},
|
|
94
|
+
"POST /v1/pretrade-hooks/{hook_id}/rotate-secret": {"auth": "key", "body": None, "controlPrefixed": False, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/pretrade-hooks/{hook_id}/rotate-secret", "pathParams": ["hook_id"], "pending": False, "query": []},
|
|
95
|
+
"GET /v1/sandbox": {"auth": "key", "body": None, "controlPrefixed": False, "headers": [], "method": "GET", "path": "/v1/sandbox", "pathParams": [], "pending": False, "query": []},
|
|
96
|
+
"POST /v1/sandbox/keys": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": ["Idempotency-Key"], "method": "POST", "path": "/v1/sandbox/keys", "pathParams": [], "pending": False, "query": []},
|
|
97
|
+
"POST /v1/sandbox/signup": {"auth": "key", "body": "object", "controlPrefixed": False, "headers": [], "method": "POST", "path": "/v1/sandbox/signup", "pathParams": [], "pending": False, "query": []},
|
|
98
|
+
"GET /v1/subscription": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/subscription", "pathParams": [], "pending": False, "query": []},
|
|
99
|
+
"GET /v1/webhook-event-types": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/webhook-event-types", "pathParams": [], "pending": False, "query": []},
|
|
100
|
+
"GET /v1/webhooks": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/webhooks", "pathParams": [], "pending": False, "query": []},
|
|
101
|
+
"POST /v1/webhooks": {"auth": "key", "body": "CreateWebhook", "controlPrefixed": True, "headers": [], "method": "POST", "path": "/v1/webhooks", "pathParams": [], "pending": False, "query": []},
|
|
102
|
+
"DELETE /v1/webhooks/{webhook_id}": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "DELETE", "path": "/v1/webhooks/{webhook_id}", "pathParams": ["webhook_id"], "pending": False, "query": []},
|
|
103
|
+
"GET /v1/webhooks/{webhook_id}": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/webhooks/{webhook_id}", "pathParams": ["webhook_id"], "pending": False, "query": []},
|
|
104
|
+
"PATCH /v1/webhooks/{webhook_id}": {"auth": "key", "body": "PatchEndpoint", "controlPrefixed": True, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "PATCH", "path": "/v1/webhooks/{webhook_id}", "pathParams": ["webhook_id"], "pending": False, "query": []},
|
|
105
|
+
"GET /v1/webhooks/{webhook_id}/deliveries": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/webhooks/{webhook_id}/deliveries", "pathParams": ["webhook_id"], "pending": False, "query": ["cursor", "eventId", "eventType", "limit", "since", "status", "until"]},
|
|
106
|
+
"GET /v1/webhooks/{webhook_id}/deliveries/{delivery_id}": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/webhooks/{webhook_id}/deliveries/{delivery_id}", "pathParams": ["webhook_id", "delivery_id"], "pending": False, "query": []},
|
|
107
|
+
"POST /v1/webhooks/{webhook_id}/deliveries/{delivery_id}/resend": {"auth": "key", "body": None, "controlPrefixed": True, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/webhooks/{webhook_id}/deliveries/{delivery_id}/resend", "pathParams": ["webhook_id", "delivery_id"], "pending": False, "query": []},
|
|
108
|
+
"POST /v1/webhooks/{webhook_id}/disable": {"auth": "key", "body": None, "controlPrefixed": True, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/webhooks/{webhook_id}/disable", "pathParams": ["webhook_id"], "pending": False, "query": []},
|
|
109
|
+
"POST /v1/webhooks/{webhook_id}/enable": {"auth": "key", "body": None, "controlPrefixed": True, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/webhooks/{webhook_id}/enable", "pathParams": ["webhook_id"], "pending": False, "query": []},
|
|
110
|
+
"POST /v1/webhooks/{webhook_id}/reveal-secret": {"auth": "key", "body": None, "controlPrefixed": True, "headers": ["X-Actor", "X-Reason"], "method": "POST", "path": "/v1/webhooks/{webhook_id}/reveal-secret", "pathParams": ["webhook_id"], "pending": False, "query": []},
|
|
111
|
+
"POST /v1/webhooks/{webhook_id}/rotate-secret": {"auth": "key", "body": "object", "controlPrefixed": True, "headers": ["Idempotency-Key", "X-Actor", "X-Reason"], "method": "POST", "path": "/v1/webhooks/{webhook_id}/rotate-secret", "pathParams": ["webhook_id"], "pending": False, "query": []},
|
|
112
|
+
"GET /v1/webhooks/{webhook_id}/settings": {"auth": "key", "body": None, "controlPrefixed": True, "headers": [], "method": "GET", "path": "/v1/webhooks/{webhook_id}/settings", "pathParams": ["webhook_id"], "pending": False, "query": []},
|
|
113
|
+
}
|
tickwire/_version.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
tickwire/agent.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""Clients for the guarded agent API (``/v1/agent/…``), with an agent key (``twag_…``).
|
|
2
|
+
|
|
3
|
+
An agent key can only reach these routes (never the terminal routes), so
|
|
4
|
+
every order an agent places goes through the owner's kill switches and
|
|
5
|
+
pre-trade hooks and is written to the agent audit log.
|
|
6
|
+
|
|
7
|
+
>>> from tickwire import AgentClient
|
|
8
|
+
>>> with AgentClient(sandbox=True) as agent: # TICKWIRE_AGENT_KEY, TICKWIRE_SANDBOX_URL
|
|
9
|
+
... acc = agent.accounts()["accounts"][0]["account_id"]
|
|
10
|
+
... r = agent.trade(acc, {"actionType": "ORDER_TYPE_BUY", "symbol": "EURUSD", "volume": 0.01},
|
|
11
|
+
... reason="breakout above the Asian range")
|
|
12
|
+
... if r.is_unknown: # never retried with a new key
|
|
13
|
+
... print(agent.trade_status(r.idempotency_key))
|
|
14
|
+
"""
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
import httpx
|
|
20
|
+
|
|
21
|
+
from . import models
|
|
22
|
+
from .client import USER_AGENT, _AsyncTransport, _Base, _require_reason, _SyncTransport
|
|
23
|
+
from .config import ENV_AGENT_KEY
|
|
24
|
+
from .results import TradeResult
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class _AgentSurface:
|
|
28
|
+
"""Agent operations. On :class:`AsyncAgentClient` every method returns an awaitable."""
|
|
29
|
+
|
|
30
|
+
AUTH = "agent"
|
|
31
|
+
|
|
32
|
+
def _headers(self) -> dict:
|
|
33
|
+
return {"X-Tickwire-Agent-Key": self.opts.token, "User-Agent": USER_AGENT, "Accept": "application/json"}
|
|
34
|
+
|
|
35
|
+
def _acc(self, key: str, account_id: str, *, params: dict | None = None, **path) -> Any:
|
|
36
|
+
return self._call(key, path={"account_id": account_id, **path}, params=params)
|
|
37
|
+
|
|
38
|
+
def me(self) -> dict:
|
|
39
|
+
"""This key's permissions: accounts, read-only flag, kill switches, which hooks apply."""
|
|
40
|
+
return self._call("GET /v1/agent/me")
|
|
41
|
+
|
|
42
|
+
def accounts(self, *, limit: int | None = None, cursor: str | None = None) -> dict:
|
|
43
|
+
"""``{"accounts": [{account_id, platform, name, server, login, status}, …], has_more,
|
|
44
|
+
next_cursor}`` in this key's scope (one page; see ``pages``)."""
|
|
45
|
+
return self._call("GET /v1/agent/accounts", params={"limit": limit, "cursor": cursor})
|
|
46
|
+
|
|
47
|
+
def snapshot(self, account_id: str) -> dict:
|
|
48
|
+
"""Account information, positions, orders, health and server time in one read, with
|
|
49
|
+
``as_of`` and per-section freshness (``status``, ``read_at``, ``age_ms``)."""
|
|
50
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/snapshot", account_id)
|
|
51
|
+
|
|
52
|
+
def health(self, account_id: str) -> dict:
|
|
53
|
+
"""``connected`` only with the terminal's evidence; ``unknown`` when it cannot be asked."""
|
|
54
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/health", account_id)
|
|
55
|
+
|
|
56
|
+
def account_information(self, account_id: str) -> models.AccountInformation:
|
|
57
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/account-information", account_id)
|
|
58
|
+
|
|
59
|
+
def positions(self, account_id: str) -> list[models.Position]:
|
|
60
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/positions", account_id)
|
|
61
|
+
|
|
62
|
+
def orders(self, account_id: str) -> list[models.Order]:
|
|
63
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/orders", account_id)
|
|
64
|
+
|
|
65
|
+
def price(self, account_id: str, symbol: str) -> models.SymbolPrice:
|
|
66
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/symbols/{symbol}/current-price", account_id,
|
|
67
|
+
symbol=symbol)
|
|
68
|
+
|
|
69
|
+
def specification(self, account_id: str, symbol: str) -> dict:
|
|
70
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/symbols/{symbol}/specification", account_id,
|
|
71
|
+
symbol=symbol)
|
|
72
|
+
|
|
73
|
+
def current_candle(self, account_id: str, symbol: str, timeframe: str = "1m") -> dict:
|
|
74
|
+
"""Live forming candle for a symbol and timeframe (e.g. 1m, 5m, 1h)."""
|
|
75
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/symbols/{symbol}/current-candles/{timeframe}", account_id,
|
|
76
|
+
symbol=symbol, timeframe=timeframe)
|
|
77
|
+
|
|
78
|
+
def candles(self, account_id: str, symbol: str, *, timeframe: str = "1m", limit: int = 100,
|
|
79
|
+
start_time: str | None = None) -> list[dict]:
|
|
80
|
+
"""Historical OHLCV candles for a symbol and timeframe."""
|
|
81
|
+
params: dict[str, Any] = {"timeframe": timeframe, "limit": limit}
|
|
82
|
+
if start_time is not None:
|
|
83
|
+
params["start_time"] = start_time
|
|
84
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/symbols/{symbol}/candles", account_id,
|
|
85
|
+
symbol=symbol, params=params)
|
|
86
|
+
|
|
87
|
+
def deals(self, account_id: str, *, start_time: str | None = None, end_time: str | None = None,
|
|
88
|
+
limit: int | None = None) -> Any:
|
|
89
|
+
"""Deals in ``[start_time, end_time)`` (ISO-8601 UTC; default the last 24 h, at most 31 days)."""
|
|
90
|
+
return self._acc("GET /v1/agent/accounts/{account_id}/history-deals", account_id,
|
|
91
|
+
params={"start_time": start_time, "end_time": end_time, "limit": limit})
|
|
92
|
+
|
|
93
|
+
def trade(self, account_id: str, trade: models.Trade, *, reason: str,
|
|
94
|
+
idempotency_key: str | None = None) -> TradeResult:
|
|
95
|
+
"""Place, modify, close or cancel (MetaApi ``MetatraderTrade``, absolute prices only).
|
|
96
|
+
|
|
97
|
+
``reason`` is required. The idempotency key is generated when omitted;
|
|
98
|
+
pass your own to make a repeat of the same decision safe across
|
|
99
|
+
restarts. Always returns a :class:`TradeResult`: ``held`` (a hook holds
|
|
100
|
+
it), ``rejected`` (with ``sent`` False when nothing left Tickwire), or
|
|
101
|
+
``unknown`` (it MAY have executed; check :meth:`trade_status`)."""
|
|
102
|
+
return self._trade_call("POST /v1/agent/accounts/{account_id}/trade", path={"account_id": account_id},
|
|
103
|
+
json=dict(trade), reason=_require_reason(reason),
|
|
104
|
+
idempotency_key=idempotency_key)
|
|
105
|
+
|
|
106
|
+
def trade_status(self, idempotency_key: str) -> dict:
|
|
107
|
+
"""What happened to a trade: ``in_flight``, ``held``, ``unknown`` or the stored result."""
|
|
108
|
+
return self._call("GET /v1/agent/requests/{idempotency_key}", path={"idempotency_key": idempotency_key})
|
|
109
|
+
|
|
110
|
+
def audit(self, *, limit: int | None = None, before: str | None = None,
|
|
111
|
+
cursor: str | None = None) -> Any:
|
|
112
|
+
"""This key's own audit log, one page: ``{entries, has_more, next_cursor}``."""
|
|
113
|
+
return self._call("GET /v1/agent/audit", params={"limit": limit, "before": before,
|
|
114
|
+
"cursor": cursor})
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class AgentClient(_SyncTransport, _AgentSurface, _Base):
|
|
118
|
+
"""Blocking agent client. ``AgentClient(sandbox=True)`` targets the sandbox;
|
|
119
|
+
with no key it reads ``TICKWIRE_AGENT_KEY``."""
|
|
120
|
+
|
|
121
|
+
def __init__(self, base_url: str | None = None, agent_key: str | None = None, *, sandbox: bool = False,
|
|
122
|
+
sandbox_url: str | None = None, transport: httpx.BaseTransport | None = None,
|
|
123
|
+
**options: Any) -> None:
|
|
124
|
+
_Base.__init__(self, base_url, agent_key, sandbox=sandbox, sandbox_url=sandbox_url,
|
|
125
|
+
token_env=ENV_AGENT_KEY, **options)
|
|
126
|
+
self._open(transport)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class AsyncAgentClient(_AsyncTransport, _AgentSurface, _Base):
|
|
130
|
+
"""Async agent client with the same surface as :class:`AgentClient`."""
|
|
131
|
+
|
|
132
|
+
def __init__(self, base_url: str | None = None, agent_key: str | None = None, *, sandbox: bool = False,
|
|
133
|
+
sandbox_url: str | None = None, transport: httpx.AsyncBaseTransport | None = None,
|
|
134
|
+
**options: Any) -> None:
|
|
135
|
+
_Base.__init__(self, base_url, agent_key, sandbox=sandbox, sandbox_url=sandbox_url,
|
|
136
|
+
token_env=ENV_AGENT_KEY, **options)
|
|
137
|
+
self._open(transport)
|
tickwire/backoff.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Bounded exponential backoff with full jitter, plus a retry budget.
|
|
2
|
+
|
|
3
|
+
Two guards keep a fleet of clients from turning an outage into a retry storm:
|
|
4
|
+
|
|
5
|
+
* :class:`Backoff`: attempt ``n`` sleeps ``uniform(0, min(cap, base * 2**n))``
|
|
6
|
+
("full jitter"), so clients that failed together do not retry together, and
|
|
7
|
+
every call stops after ``max_attempts``.
|
|
8
|
+
* :class:`RetryBudget`: a token bucket shared by one client. Each retry spends
|
|
9
|
+
one token; each success earns ``refill_per_success``. Under a sustained outage
|
|
10
|
+
a client makes at most ``capacity`` retries plus a small fraction of its
|
|
11
|
+
successful traffic, instead of multiplying its load by ``max_attempts``.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import random
|
|
16
|
+
import threading
|
|
17
|
+
from dataclasses import dataclass, field
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class Backoff:
|
|
22
|
+
base_s: float = 0.25
|
|
23
|
+
cap_s: float = 8.0
|
|
24
|
+
max_attempts: int = 4 # total tries, including the first
|
|
25
|
+
rng: random.Random = field(default_factory=random.Random, repr=False)
|
|
26
|
+
|
|
27
|
+
def delay(self, attempt: int, retry_after: float | None = None) -> float:
|
|
28
|
+
"""Delay before retry number ``attempt`` (1-based). Never exceeds ``cap_s``."""
|
|
29
|
+
ceiling = min(self.cap_s, self.base_s * (2 ** max(0, attempt - 1)))
|
|
30
|
+
d = self.rng.uniform(0, ceiling)
|
|
31
|
+
if retry_after is not None:
|
|
32
|
+
# honour the server's hint, but never beyond the cap
|
|
33
|
+
d = max(d, min(float(retry_after), self.cap_s))
|
|
34
|
+
return d
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class RetryBudget:
|
|
38
|
+
def __init__(self, capacity: float = 10.0, refill_per_success: float = 0.1) -> None:
|
|
39
|
+
self.capacity = capacity
|
|
40
|
+
self.refill = refill_per_success
|
|
41
|
+
self._tokens = capacity
|
|
42
|
+
self._lock = threading.Lock()
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def tokens(self) -> float:
|
|
46
|
+
return self._tokens
|
|
47
|
+
|
|
48
|
+
def try_spend(self) -> bool:
|
|
49
|
+
with self._lock:
|
|
50
|
+
if self._tokens >= 1.0:
|
|
51
|
+
self._tokens -= 1.0
|
|
52
|
+
return True
|
|
53
|
+
return False
|
|
54
|
+
|
|
55
|
+
def on_success(self) -> None:
|
|
56
|
+
with self._lock:
|
|
57
|
+
self._tokens = min(self.capacity, self._tokens + self.refill)
|