filtrix-cli 0.4.0__tar.gz → 0.6.0__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.
- filtrix_cli-0.6.0/CHANGELOG.md +25 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/PKG-INFO +3 -3
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/README.md +2 -2
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/pyproject.toml +1 -1
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/AGENTS.md +11 -2
- filtrix_cli-0.6.0/src/filtrix_cli/__init__.py +1 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/client.py +278 -204
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/live.py +21 -3
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/market.py +70 -68
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/research.py +1 -1
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_live.py +129 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_market.py +185 -163
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_smoke.py +9 -1
- filtrix_cli-0.4.0/src/filtrix_cli/__init__.py +0 -1
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/.gitignore +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/LICENSE +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/__main__.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/auth.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/__init__.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/_helpers.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/account.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/authcmd.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/backtest.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/configcmd.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/filters.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/keys.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/llms.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/screen.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/commands/ticker.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/config.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/filters.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/output.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/render.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/src/filtrix_cli/state.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/__init__.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_auth.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_client.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_client_register.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_auth.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_backtest_screen.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_config.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_keys.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_llms.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_profile.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_read.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_register.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_cmd_research.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_config.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_filters.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_helpers.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_output.py +0 -0
- {filtrix_cli-0.4.0 → filtrix_cli-0.6.0}/tests/test_render.py +0 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.6.0 (2026-11-30)
|
|
4
|
+
|
|
5
|
+
- `filtrix gaps`: the statistics come from FILTRIX's own 1-minute bars since Nov 2016 for tickers in that
|
|
6
|
+
universe (`meta.source` `"filtrix_minute_bars"`), Trade-Ideas alert days otherwise. New `--events` also
|
|
7
|
+
lists every counted gap day (long output).
|
|
8
|
+
- `filtrix runs save --row-filter` help: it picks which rows are stored with the run; the run's statistics
|
|
9
|
+
always use all rows.
|
|
10
|
+
- Agent guide: events without minute bars are not simulated; `missing_minute_data` counts them.
|
|
11
|
+
|
|
12
|
+
## 0.5.0 (2026-11-11)
|
|
13
|
+
|
|
14
|
+
- A paper write the account worker has not answered within the server's 10 s wait comes back
|
|
15
|
+
`{"status": "pending", "event_id"}`. The CLI now reads its outcome for about 12 s more, with the same
|
|
16
|
+
Idempotency-Key, and prints the result.
|
|
17
|
+
- Still no answer: it prints `{"status": "pending", "event_id", "idempotency_key"}` and exits 6. The command
|
|
18
|
+
may still apply. Read it later with the new `filtrix paper command EVENT_ID`, and resend only with the same
|
|
19
|
+
`--idempotency-key`.
|
|
20
|
+
- A command refused while pending (for example 409 `version_conflict`) exits as a direct refusal would.
|
|
21
|
+
- A 500 `internal_error` on a paper write is retried once with the same key and body.
|
|
22
|
+
|
|
23
|
+
## 0.4.0 (2026-11-08)
|
|
24
|
+
|
|
25
|
+
- `filtrix scanner` and `filtrix paper` commands (Pro), available once FILTRIX enables the live API.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: filtrix-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Agent-first command-line interface for the FILTRIX backtest engine.
|
|
5
5
|
Project-URL: Homepage, https://filtrix.net
|
|
6
6
|
Project-URL: Documentation, https://pypi.org/project/filtrix-cli/
|
|
@@ -85,8 +85,8 @@ filtrix llms # machine-readable usage guide for AI agents
|
|
|
85
85
|
|
|
86
86
|
Output is human-readable in a terminal and JSON when piped or with the global `--json` flag.
|
|
87
87
|
Global flags (`--json`, `--quiet`, `--profile`, `--api-key`, `--base-url`) go BEFORE the subcommand.
|
|
88
|
-
Exit codes: 0 ok, 2 usage, 3 not-found, 4 auth, 5 rate-limited, 6 network
|
|
89
|
-
or failed job.
|
|
88
|
+
Exit codes: 0 ok, 2 usage, 3 not-found, 4 auth, 5 rate-limited, 6 network, `--wait` timeout or a paper
|
|
89
|
+
command still pending (`filtrix paper command EVENT_ID` reads it later), 1 server or failed job.
|
|
90
90
|
|
|
91
91
|
`runs save` and `simulate` start background jobs; `--wait` polls until they finish. `runs delete` needs
|
|
92
92
|
`--yes` when not interactive.
|
|
@@ -53,8 +53,8 @@ filtrix llms # machine-readable usage guide for AI agents
|
|
|
53
53
|
|
|
54
54
|
Output is human-readable in a terminal and JSON when piped or with the global `--json` flag.
|
|
55
55
|
Global flags (`--json`, `--quiet`, `--profile`, `--api-key`, `--base-url`) go BEFORE the subcommand.
|
|
56
|
-
Exit codes: 0 ok, 2 usage, 3 not-found, 4 auth, 5 rate-limited, 6 network
|
|
57
|
-
or failed job.
|
|
56
|
+
Exit codes: 0 ok, 2 usage, 3 not-found, 4 auth, 5 rate-limited, 6 network, `--wait` timeout or a paper
|
|
57
|
+
command still pending (`filtrix paper command EVENT_ID` reads it later), 1 server or failed job.
|
|
58
58
|
|
|
59
59
|
`runs save` and `simulate` start background jobs; `--wait` polls until they finish. `runs delete` needs
|
|
60
60
|
`--yes` when not interactive.
|
|
@@ -14,7 +14,8 @@ Structured command-line access to the FILTRIX historical backtest engine.
|
|
|
14
14
|
- 3 not found
|
|
15
15
|
- 4 authentication required/failed
|
|
16
16
|
- 5 rate limited (retryable)
|
|
17
|
-
- 6 network/timeout, incl. `--wait` giving up on a job that is still running (retryable)
|
|
17
|
+
- 6 network/timeout, incl. `--wait` giving up on a job that is still running (retryable), or a paper command
|
|
18
|
+
still pending (see Live Scanner and Paper Trading)
|
|
18
19
|
|
|
19
20
|
## Auth
|
|
20
21
|
- Reads/backtests: set env FILTRIX_API_KEY=flt_... (or `filtrix config set api_key ...`).
|
|
@@ -74,16 +75,24 @@ Available once FILTRIX enables the live API (until then: exit 3, "not available
|
|
|
74
75
|
| reset --capital X --limits-json JSON --yes | close-all --yes
|
|
75
76
|
- filtrix paper strategy save --spec-json JSON [--id N] [--monitor-id N] | strategy archive ID --yes
|
|
76
77
|
- filtrix paper start STRATEGY_ID | pause RUN_ID | resume RUN_ID | close RUN_ID --yes
|
|
78
|
+
- filtrix paper command EVENT_ID outcome of a write that exited 6 "pending": its result, or still pending
|
|
77
79
|
Paper money is simulated. Every paper write sends an Idempotency-Key (--idempotency-key, else a new one);
|
|
78
80
|
versions (--version, --account-version, --draft-version) are fetched from `filtrix paper account` when omitted.
|
|
79
81
|
A 409 version_conflict carries current_version. Run ids are UUIDs.
|
|
82
|
+
A write waits for the account worker (up to ~10 s on the server, then ~12 s of status reads). Still no
|
|
83
|
+
answer: stdout {"status":"pending","event_id","idempotency_key"}, exit 6; it may still apply, so read it with
|
|
84
|
+
`filtrix paper command EVENT_ID` and resend only with the same --idempotency-key. A refusal read there exits as
|
|
85
|
+
a direct one would (e.g. 409 version_conflict, exit 2). A 500 internal_error is
|
|
86
|
+
retried once with the same key and body.
|
|
80
87
|
|
|
81
88
|
## Background jobs
|
|
82
89
|
`runs save` and `simulate` print {"job_id","status":"queued","poll"}. Add `--wait` to poll until the job ends
|
|
83
90
|
(prints the finished job; a failed job exits 1, `--timeout` seconds without an end exits 6), or poll with
|
|
84
91
|
`filtrix jobs get JOB_ID`. At most 2 jobs run at a time per account (429 otherwise).
|
|
85
92
|
A simulation's result: summary (n_trades, win_rate 0..1, profit_factor, avg_pnl / total_pnl in USD per share,
|
|
86
|
-
avg_holding_min), up to 200 trades with pnl_pct, run_id.
|
|
93
|
+
avg_holding_min), up to 200 trades with pnl_pct, run_id. Events without minute bars are not simulated;
|
|
94
|
+
missing_minute_data counts them (pending: loaded every night; unavailable: none exist).
|
|
95
|
+
Definitions: `filtrix meta --json` -> "simulation".
|
|
87
96
|
--sl / --tp are fractions (0.02 = 2%).
|
|
88
97
|
|
|
89
98
|
## Filter DSL
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.6.0"
|
|
@@ -1,204 +1,278 @@
|
|
|
1
|
-
"""Synchronous httpx client for the FILTRIX API, with error -> CLIError mapping."""
|
|
2
|
-
import json
|
|
3
|
-
|
|
4
|
-
from
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
from filtrix_cli
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
def
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
def
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
def
|
|
147
|
-
return self._request("GET", "/api/v1/
|
|
148
|
-
|
|
149
|
-
def
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
def
|
|
165
|
-
return self._request("
|
|
166
|
-
|
|
167
|
-
def
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return self._request("
|
|
173
|
-
|
|
174
|
-
def
|
|
175
|
-
return self._request("GET", f"/api/v1/
|
|
176
|
-
|
|
177
|
-
def
|
|
178
|
-
return self._request("
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
1
|
+
"""Synchronous httpx client for the FILTRIX API, with error -> CLIError mapping."""
|
|
2
|
+
import json
|
|
3
|
+
import time
|
|
4
|
+
from typing import Any, List, Optional
|
|
5
|
+
from urllib.parse import quote
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
from filtrix_cli import __version__
|
|
10
|
+
from filtrix_cli.output import CLIError
|
|
11
|
+
|
|
12
|
+
# Paper commands: after the server's own 10 s wait, a pending command's result is read about 12 s more
|
|
13
|
+
# (1 quota each), as the MCP server does.
|
|
14
|
+
PENDING_POLL_DELAYS = (1.0, 1.0, 2.0, 2.0, 3.0, 3.0)
|
|
15
|
+
COMMAND_STATUS_PATH = "/api/v1/paper/commands"
|
|
16
|
+
INTERNAL_RETRY_DELAY = 1.0
|
|
17
|
+
_sleep = time.sleep
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _is_pending(body: Any) -> bool:
|
|
21
|
+
return isinstance(body, dict) and body.get("status") == "pending"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _paper_code(resp: httpx.Response) -> Optional[str]:
|
|
25
|
+
"""The code of a paper error body {"detail": {"code": ...}}, else None (plain or list detail)."""
|
|
26
|
+
try:
|
|
27
|
+
detail = resp.json().get("detail")
|
|
28
|
+
except Exception:
|
|
29
|
+
return None
|
|
30
|
+
code = detail.get("code") if isinstance(detail, dict) else None
|
|
31
|
+
return code if isinstance(code, str) and code else None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _internal_error(resp: httpx.Response) -> bool:
|
|
35
|
+
"""A 500 internal_error: the server asks for a retry with the same Idempotency-Key and body."""
|
|
36
|
+
return resp.status_code == 500 and _paper_code(resp) == "internal_error"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _is_refusal(resp: httpx.Response) -> bool:
|
|
40
|
+
"""The command's own answer from the status read: a 4xx (not 429) with a paper error code. A plain
|
|
41
|
+
detail (an old backend's 404 Not Found, an expired token, a revoked scope) or a validation list
|
|
42
|
+
is a failed read."""
|
|
43
|
+
return 400 <= resp.status_code < 500 and resp.status_code != 429 and _paper_code(resp) is not None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _safe_detail(resp: httpx.Response) -> str:
|
|
47
|
+
try:
|
|
48
|
+
data = resp.json()
|
|
49
|
+
except Exception:
|
|
50
|
+
return resp.text or f"HTTP {resp.status_code}"
|
|
51
|
+
if isinstance(data, dict) and "detail" in data:
|
|
52
|
+
detail = data["detail"]
|
|
53
|
+
return detail if isinstance(detail, str) else json.dumps(detail)
|
|
54
|
+
return json.dumps(data)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class FiltrixClient:
|
|
58
|
+
def __init__(self, base_url: str, api_key: Optional[str] = None,
|
|
59
|
+
jwt: Optional[str] = None, timeout: float = 60.0):
|
|
60
|
+
self.base_url = base_url.rstrip("/")
|
|
61
|
+
self.api_key = api_key
|
|
62
|
+
self.jwt = jwt
|
|
63
|
+
self.timeout = timeout
|
|
64
|
+
|
|
65
|
+
def _headers(self, auth: str) -> dict:
|
|
66
|
+
"""auth in {'any', 'jwt', 'none'}."""
|
|
67
|
+
headers = {
|
|
68
|
+
"User-Agent": f"filtrix-cli/{__version__}",
|
|
69
|
+
"X-Agent-Request": "true",
|
|
70
|
+
"Content-Type": "application/json",
|
|
71
|
+
}
|
|
72
|
+
if auth == "none":
|
|
73
|
+
return headers
|
|
74
|
+
if auth == "jwt":
|
|
75
|
+
if not self.jwt:
|
|
76
|
+
raise CLIError("auth_failed", "This command requires login. Run `filtrix auth login`.",
|
|
77
|
+
suggestion="filtrix auth login", exit_code=4)
|
|
78
|
+
headers["Authorization"] = f"Bearer {self.jwt}"
|
|
79
|
+
return headers
|
|
80
|
+
# auth == "any"
|
|
81
|
+
if self.api_key:
|
|
82
|
+
headers["X-API-Key"] = self.api_key
|
|
83
|
+
elif self.jwt:
|
|
84
|
+
headers["Authorization"] = f"Bearer {self.jwt}"
|
|
85
|
+
else:
|
|
86
|
+
raise CLIError("auth_failed", "No credentials. Set FILTRIX_API_KEY or run `filtrix auth login`.",
|
|
87
|
+
suggestion="filtrix auth login", exit_code=4)
|
|
88
|
+
return headers
|
|
89
|
+
|
|
90
|
+
def _send(self, method: str, path: str, *, json_body: Any = None, auth: str = "any",
|
|
91
|
+
params: Optional[dict] = None, headers: Optional[dict] = None) -> httpx.Response:
|
|
92
|
+
url = f"{self.base_url}{path}"
|
|
93
|
+
headers = {**self._headers(auth), **(headers or {})}
|
|
94
|
+
clean_params = {k: v for k, v in (params or {}).items() if v is not None} or None
|
|
95
|
+
try:
|
|
96
|
+
with httpx.Client(timeout=self.timeout) as client:
|
|
97
|
+
return client.request(method, url, headers=headers, json=json_body, params=clean_params)
|
|
98
|
+
except httpx.TimeoutException as e:
|
|
99
|
+
raise CLIError("network", f"Request timed out: {e}", retryable=True, exit_code=6)
|
|
100
|
+
except httpx.HTTPError as e:
|
|
101
|
+
raise CLIError("network", f"Network error: {e}", retryable=True, exit_code=6)
|
|
102
|
+
|
|
103
|
+
def _request(self, method: str, path: str, *, json_body: Any = None, auth: str = "any",
|
|
104
|
+
params: Optional[dict] = None, headers: Optional[dict] = None) -> Any:
|
|
105
|
+
return self._handle_response(self._send(method, path, json_body=json_body, auth=auth,
|
|
106
|
+
params=params, headers=headers))
|
|
107
|
+
|
|
108
|
+
def _handle_response(self, resp: httpx.Response) -> Any:
|
|
109
|
+
if resp.status_code < 400:
|
|
110
|
+
return resp.json() if resp.content else {}
|
|
111
|
+
detail = _safe_detail(resp)
|
|
112
|
+
code = resp.status_code
|
|
113
|
+
if code in (401, 403):
|
|
114
|
+
raise CLIError("auth_failed", detail, status=code,
|
|
115
|
+
suggestion="Run `filtrix auth login` or check your API key.", exit_code=4)
|
|
116
|
+
if code == 404:
|
|
117
|
+
raise CLIError("not_found", detail, status=404, exit_code=3)
|
|
118
|
+
if code == 429:
|
|
119
|
+
raise CLIError("rate_limited", detail, status=429, retryable=True,
|
|
120
|
+
suggestion="Run `filtrix account` to see your quota.", exit_code=5)
|
|
121
|
+
if 400 <= code < 500:
|
|
122
|
+
raise CLIError("invalid_request", detail, status=code, exit_code=2)
|
|
123
|
+
raise CLIError("server_error", detail, status=code, retryable=True, exit_code=1)
|
|
124
|
+
|
|
125
|
+
# ---- Public API v1 ----
|
|
126
|
+
def backtest(self, filters: List[dict], outcome: str = "day_close_position",
|
|
127
|
+
flat_threshold: float = 0.002, start_date: Optional[str] = None,
|
|
128
|
+
end_date: Optional[str] = None, threshold: float = 0.0,
|
|
129
|
+
pos_min: Optional[float] = None, pos_max: Optional[float] = None,
|
|
130
|
+
sample_limit: int = 0) -> Any:
|
|
131
|
+
body: dict = {"filters": filters, "outcome": outcome, "flat_threshold": flat_threshold,
|
|
132
|
+
"threshold": threshold, "sample_limit": sample_limit}
|
|
133
|
+
for key, value in (("start_date", start_date), ("end_date", end_date),
|
|
134
|
+
("pos_min", pos_min), ("pos_max", pos_max)):
|
|
135
|
+
if value is not None:
|
|
136
|
+
body[key] = value
|
|
137
|
+
return self._request("POST", "/api/v1/backtest", json_body=body)
|
|
138
|
+
|
|
139
|
+
def screen(self, filters: List[dict], limit: int = 50, offset: int = 0,
|
|
140
|
+
fields: Optional[List[str]] = None) -> Any:
|
|
141
|
+
body: dict = {"filters": filters, "limit": limit, "offset": offset}
|
|
142
|
+
if fields:
|
|
143
|
+
body["fields"] = list(fields)
|
|
144
|
+
return self._request("POST", "/api/v1/screen", json_body=body)
|
|
145
|
+
|
|
146
|
+
def ticker_info(self, symbol: str) -> Any:
|
|
147
|
+
return self._request("GET", f"/api/v1/ticker/{symbol.upper()}")
|
|
148
|
+
|
|
149
|
+
def list_filters(self, q: Optional[str] = None, value_type: Optional[str] = None,
|
|
150
|
+
limit: Optional[int] = None) -> Any:
|
|
151
|
+
return self._request("GET", "/api/v1/filters", auth="none",
|
|
152
|
+
params={"q": q, "type": value_type, "limit": limit})
|
|
153
|
+
|
|
154
|
+
def account(self) -> Any:
|
|
155
|
+
return self._request("GET", "/api/v1/account")
|
|
156
|
+
|
|
157
|
+
def meta(self) -> Any:
|
|
158
|
+
return self._request("GET", "/api/v1/meta")
|
|
159
|
+
|
|
160
|
+
def coverage(self, symbol: Optional[str] = None) -> Any:
|
|
161
|
+
return self._request("GET", "/api/v1/coverage",
|
|
162
|
+
params={"symbol": symbol.upper() if symbol else None})
|
|
163
|
+
|
|
164
|
+
def ticker_overview(self, symbol: str) -> Any:
|
|
165
|
+
return self._request("GET", f"/api/v1/ticker/{symbol.upper()}/overview")
|
|
166
|
+
|
|
167
|
+
def ticker_gap_stats(self, symbol: str, gap_threshold: float = 10.0,
|
|
168
|
+
direction: str = "up", stat: str = "avg", include_events: bool = False) -> Any:
|
|
169
|
+
params = {"gap_threshold": gap_threshold, "direction": direction, "stat": stat}
|
|
170
|
+
if include_events:
|
|
171
|
+
params["include_events"] = "true"
|
|
172
|
+
return self._request("GET", f"/api/v1/ticker/{symbol.upper()}/gap-stats", params=params)
|
|
173
|
+
|
|
174
|
+
def ticker_daily(self, symbol: str, limit: int = 250) -> Any:
|
|
175
|
+
return self._request("GET", f"/api/v1/ticker/{symbol.upper()}/daily", params={"limit": limit})
|
|
176
|
+
|
|
177
|
+
def premarket_gappers(self) -> Any:
|
|
178
|
+
return self._request("GET", "/api/v1/premarket/gappers")
|
|
179
|
+
|
|
180
|
+
def call(self, method: str, path: str, *, json_body: Any = None, params: Optional[dict] = None,
|
|
181
|
+
headers: Optional[dict] = None) -> Any:
|
|
182
|
+
"""Any Public API v1 call with the configured credential (live scanner / paper reads)."""
|
|
183
|
+
return self._request(method, path, json_body=json_body, params=params, headers=headers)
|
|
184
|
+
|
|
185
|
+
def paper_command(self, method: str, path: str, key: str, *, json_body: Any = None,
|
|
186
|
+
params: Optional[dict] = None) -> Any:
|
|
187
|
+
"""A paper write with its Idempotency-Key. A 500 internal_error was not applied and is retried
|
|
188
|
+
once with the same key and body. A pending answer (the account worker had not answered within
|
|
189
|
+
the server's wait) is followed on /paper/commands?event_id= for about 12 s more; still pending,
|
|
190
|
+
it returns {"status": "pending", "event_id", "idempotency_key"} (and "status_error" when a
|
|
191
|
+
status read failed)."""
|
|
192
|
+
headers = {"Idempotency-Key": key}
|
|
193
|
+
resp = self._send(method, path, json_body=json_body, params=params, headers=headers)
|
|
194
|
+
if _internal_error(resp):
|
|
195
|
+
_sleep(INTERNAL_RETRY_DELAY)
|
|
196
|
+
resp = self._send(method, path, json_body=json_body, params=params, headers=headers)
|
|
197
|
+
result = self._handle_response(resp)
|
|
198
|
+
if not (_is_pending(result) and result.get("event_id")):
|
|
199
|
+
return result
|
|
200
|
+
pending = {"status": "pending", "event_id": str(result["event_id"]), "idempotency_key": key}
|
|
201
|
+
for delay in PENDING_POLL_DELAYS:
|
|
202
|
+
_sleep(delay)
|
|
203
|
+
try:
|
|
204
|
+
resp = self._send("GET", COMMAND_STATUS_PATH, params={"event_id": pending["event_id"]})
|
|
205
|
+
except CLIError as err: # network
|
|
206
|
+
return {**pending, "status_error": err.message}
|
|
207
|
+
if resp.status_code >= 400 and not _is_refusal(resp):
|
|
208
|
+
return {**pending, "status_error": f"HTTP {resp.status_code}: {_safe_detail(resp)}"}
|
|
209
|
+
status = self._handle_response(resp) # a refusal raises as a direct answer would
|
|
210
|
+
if not _is_pending(status):
|
|
211
|
+
return status
|
|
212
|
+
return pending
|
|
213
|
+
|
|
214
|
+
def paper_command_status(self, event_id: str) -> Any:
|
|
215
|
+
"""A paper command that answered pending: its result, its refusal as an error, or
|
|
216
|
+
{"status": "pending"}. The id goes in the query, where no proxy decodes a "/" of the key."""
|
|
217
|
+
return self._request("GET", COMMAND_STATUS_PATH, params={"event_id": event_id})
|
|
218
|
+
|
|
219
|
+
# ---- Saved runs, simulations, jobs (v1) ----
|
|
220
|
+
def runs_list(self, limit: int = 20, offset: int = 0) -> Any:
|
|
221
|
+
return self._request("GET", "/api/v1/runs", params={"limit": limit, "offset": offset})
|
|
222
|
+
|
|
223
|
+
def run_get(self, run_id: int) -> Any:
|
|
224
|
+
return self._request("GET", f"/api/v1/runs/{int(run_id)}")
|
|
225
|
+
|
|
226
|
+
def run_rows(self, run_id: int, limit: int = 100, offset: int = 0) -> Any:
|
|
227
|
+
return self._request("GET", f"/api/v1/runs/{int(run_id)}/rows", params={"limit": limit, "offset": offset})
|
|
228
|
+
|
|
229
|
+
def run_save(self, body: dict) -> Any:
|
|
230
|
+
return self._request("POST", "/api/v1/runs", json_body=body)
|
|
231
|
+
|
|
232
|
+
def run_share(self, run_id: int) -> Any:
|
|
233
|
+
return self._request("POST", f"/api/v1/runs/{int(run_id)}/share")
|
|
234
|
+
|
|
235
|
+
def run_unshare(self, run_id: int) -> Any:
|
|
236
|
+
return self._request("DELETE", f"/api/v1/runs/{int(run_id)}/share")
|
|
237
|
+
|
|
238
|
+
def run_delete(self, run_id: int) -> Any:
|
|
239
|
+
return self._request("DELETE", f"/api/v1/runs/{int(run_id)}")
|
|
240
|
+
|
|
241
|
+
def trade_context(self, run_id: int, symbol: str, date: str, daily_lookback: int = 120) -> Any:
|
|
242
|
+
return self._request("GET", f"/api/v1/runs/{int(run_id)}/trade-context",
|
|
243
|
+
params={"symbol": symbol.upper(), "date": date, "daily_lookback": daily_lookback})
|
|
244
|
+
|
|
245
|
+
def simulate(self, body: dict) -> Any:
|
|
246
|
+
return self._request("POST", "/api/v1/simulations", json_body=body)
|
|
247
|
+
|
|
248
|
+
def job_get(self, job_id: str) -> Any:
|
|
249
|
+
return self._request("GET", f"/api/v1/jobs/{quote(job_id, safe='')}")
|
|
250
|
+
|
|
251
|
+
def job_cancel(self, job_id: str) -> Any:
|
|
252
|
+
return self._request("POST", f"/api/v1/jobs/{quote(job_id, safe='')}/cancel")
|
|
253
|
+
|
|
254
|
+
# ---- Key management (JWT) ----
|
|
255
|
+
def keys_list(self) -> Any:
|
|
256
|
+
return self._request("GET", "/api/keys", auth="jwt")
|
|
257
|
+
|
|
258
|
+
def keys_create(self, name: str = "default") -> Any:
|
|
259
|
+
return self._request("POST", "/api/keys", json_body={"name": name}, auth="jwt")
|
|
260
|
+
|
|
261
|
+
def keys_revoke(self, key_id: int) -> Any:
|
|
262
|
+
return self._request("DELETE", f"/api/keys/{key_id}", auth="jwt")
|
|
263
|
+
|
|
264
|
+
# ---- Registration (pre-auth) ----
|
|
265
|
+
def register(self, email: str, password: str, delivery: str = "code",
|
|
266
|
+
ref_source: Optional[str] = None) -> Any:
|
|
267
|
+
body: dict = {"email": email, "password": password, "delivery": delivery}
|
|
268
|
+
if ref_source:
|
|
269
|
+
body["ref_data"] = {"ref_source": ref_source}
|
|
270
|
+
return self._request("POST", "/api/auth/register", json_body=body, auth="none")
|
|
271
|
+
|
|
272
|
+
def verify_email_code(self, email: str, code: str) -> Any:
|
|
273
|
+
return self._request("POST", "/api/auth/verify-email-code",
|
|
274
|
+
json_body={"email": email, "code": code}, auth="none")
|
|
275
|
+
|
|
276
|
+
def resend_code(self, email: str) -> Any:
|
|
277
|
+
return self._request("POST", "/api/auth/resend-code",
|
|
278
|
+
json_body={"email": email}, auth="none")
|