tradingplan-cli 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.
- tradingplan_cli/SKILL.md +38 -0
- tradingplan_cli/__init__.py +3 -0
- tradingplan_cli/__main__.py +4 -0
- tradingplan_cli/cli.py +362 -0
- tradingplan_cli/client.py +39 -0
- tradingplan_cli/config.py +103 -0
- tradingplan_cli/errors.py +56 -0
- tradingplan_cli-0.1.0.dist-info/METADATA +123 -0
- tradingplan_cli-0.1.0.dist-info/RECORD +12 -0
- tradingplan_cli-0.1.0.dist-info/WHEEL +5 -0
- tradingplan_cli-0.1.0.dist-info/entry_points.txt +2 -0
- tradingplan_cli-0.1.0.dist-info/top_level.txt +1 -0
tradingplan_cli/SKILL.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# TradingPlan CLI
|
|
2
|
+
|
|
3
|
+
Read-only access to the current user's TradingPlan data (plans, node analysis, journal, reviews, trades, disciplines, strategies). Never write or delete.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
Environment (preferred for agents):
|
|
8
|
+
|
|
9
|
+
- `TRADINGPLAN_API_KEY` — membership API key (`sk_...`)
|
|
10
|
+
- `TRADINGPLAN_API_URL` — `https://longtou.online/api/v1` (china) or `https://tradingplan.day/api/v1` (overseas)
|
|
11
|
+
|
|
12
|
+
Or `tradingplan --site china|overseas` plus the key.
|
|
13
|
+
|
|
14
|
+
## Command routing
|
|
15
|
+
|
|
16
|
+
Prefer **one day at a time**:
|
|
17
|
+
|
|
18
|
+
```text
|
|
19
|
+
tradingplan day YYYY-MM-DD
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Returns that day's plans, node analysis, journal, daily review, transactions (with per-trade review notes), and discipline records.
|
|
23
|
+
|
|
24
|
+
Then drill in only if needed:
|
|
25
|
+
|
|
26
|
+
- `tradingplan plans list --from DATE --to DATE`
|
|
27
|
+
- `tradingplan plans get ID`
|
|
28
|
+
- `tradingplan node-analysis DATE`
|
|
29
|
+
- `tradingplan journals list|get`
|
|
30
|
+
- `tradingplan reviews daily list|get`
|
|
31
|
+
- `tradingplan reviews notes list --tx ID --code CODE`
|
|
32
|
+
- `tradingplan txs list --from DATE --to DATE --code CODE --type buy|sell|add|reduce`
|
|
33
|
+
- `tradingplan strategies list|get`
|
|
34
|
+
- `tradingplan disciplines list|get`
|
|
35
|
+
- `tradingplan disciplines records --date DATE --status followed|violated`
|
|
36
|
+
- `tradingplan whoami` — confirm the key and site
|
|
37
|
+
|
|
38
|
+
`--from` / `--to` are `YYYY-MM-DD`. Output is JSON on stdout. On error, JSON on stderr with `code` (`unauthorized`, `not_found`, `invalid_args`, `rate_limited`, `network`).
|
tradingplan_cli/cli.py
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
from importlib.resources import files
|
|
8
|
+
from typing import Optional
|
|
9
|
+
|
|
10
|
+
from . import client as api
|
|
11
|
+
from .config import CONFIG_PATH, ConfigError, SITES, dump_file, resolve_settings
|
|
12
|
+
from .errors import CliError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _emit(payload) -> None:
|
|
16
|
+
print(json.dumps(payload, ensure_ascii=False))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _settings(args):
|
|
20
|
+
return resolve_settings(
|
|
21
|
+
api_key=getattr(args, "api_key", None),
|
|
22
|
+
api_url=getattr(args, "api_url", None),
|
|
23
|
+
site=getattr(args, "site", None),
|
|
24
|
+
environ=os.environ,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _get(args, path: str, params: Optional[dict] = None) -> None:
|
|
29
|
+
try:
|
|
30
|
+
settings = _settings(args)
|
|
31
|
+
_emit(api.get_json(settings, path, params))
|
|
32
|
+
except ConfigError as exc:
|
|
33
|
+
raise CliError(exc.message, "invalid_args", "See `tradingplan --help` or `tradingplan init`", 4) from exc
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _add_auth(parser: argparse.ArgumentParser) -> None:
|
|
37
|
+
parser.add_argument("--api-key", dest="api_key", default=None)
|
|
38
|
+
parser.add_argument("--api-url", dest="api_url", default=None)
|
|
39
|
+
parser.add_argument("--site", dest="site", choices=sorted(SITES), default=None)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _add_page(parser: argparse.ArgumentParser) -> None:
|
|
43
|
+
parser.add_argument("--limit", type=int, default=50)
|
|
44
|
+
parser.add_argument("--offset", type=int, default=0)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _add_range(parser: argparse.ArgumentParser, *, with_date: bool = True) -> None:
|
|
48
|
+
if with_date:
|
|
49
|
+
parser.add_argument("--date", default=None)
|
|
50
|
+
parser.add_argument("--from", dest="date_from", default=None)
|
|
51
|
+
parser.add_argument("--to", dest="date_to", default=None)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def cmd_whoami(args) -> None:
|
|
55
|
+
_get(args, "/cli/me")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def cmd_day(args) -> None:
|
|
59
|
+
_get(args, "/cli/day", {"date": args.date})
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def cmd_node_analysis(args) -> None:
|
|
63
|
+
_get(args, "/cli/node-analysis", {"date": args.date})
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def cmd_init(args) -> None:
|
|
67
|
+
if CONFIG_PATH.exists() and not args.force and not sys.stdin.isatty():
|
|
68
|
+
raise CliError("Config already exists", "invalid_args", "Pass --force to overwrite", 4)
|
|
69
|
+
chosen_site = args.site
|
|
70
|
+
if not chosen_site:
|
|
71
|
+
if not sys.stdin.isatty():
|
|
72
|
+
raise CliError("site is required", "invalid_args", "Pass --site china|overseas", 4)
|
|
73
|
+
chosen_site = input("Site [china]: ").strip() or "china"
|
|
74
|
+
if chosen_site not in SITES:
|
|
75
|
+
raise CliError("site must be china or overseas", "invalid_args", "Use china or overseas", 4)
|
|
76
|
+
key = args.api_key
|
|
77
|
+
if not key:
|
|
78
|
+
if not sys.stdin.isatty():
|
|
79
|
+
raise CliError("API key is required", "invalid_args", "Pass --api-key", 4)
|
|
80
|
+
key = input("API key: ").strip()
|
|
81
|
+
if not key:
|
|
82
|
+
raise CliError("API key is required", "invalid_args", "Pass --api-key", 4)
|
|
83
|
+
dump_file(CONFIG_PATH, {"site": chosen_site, "api_url": SITES[chosen_site], "api_key": key})
|
|
84
|
+
_emit({"ok": True, "path": str(CONFIG_PATH)})
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def cmd_skill(_args) -> None:
|
|
88
|
+
text = files("tradingplan_cli").joinpath("SKILL.md").read_text(encoding="utf-8")
|
|
89
|
+
sys.stdout.write(text)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def cmd_plans_list(args) -> None:
|
|
93
|
+
_get(
|
|
94
|
+
args,
|
|
95
|
+
"/cli/plans",
|
|
96
|
+
{
|
|
97
|
+
"date": args.date,
|
|
98
|
+
"start_date": args.date_from,
|
|
99
|
+
"end_date": args.date_to,
|
|
100
|
+
"plan_type": args.type,
|
|
101
|
+
"limit": args.limit,
|
|
102
|
+
"offset": args.offset,
|
|
103
|
+
},
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def cmd_plans_get(args) -> None:
|
|
108
|
+
_get(args, f"/cli/plans/{args.id}")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def cmd_journals_list(args) -> None:
|
|
112
|
+
_get(
|
|
113
|
+
args,
|
|
114
|
+
"/cli/journals",
|
|
115
|
+
{
|
|
116
|
+
"date": args.date,
|
|
117
|
+
"start_date": args.date_from,
|
|
118
|
+
"end_date": args.date_to,
|
|
119
|
+
"limit": args.limit,
|
|
120
|
+
"offset": args.offset,
|
|
121
|
+
},
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def cmd_journals_get(args) -> None:
|
|
126
|
+
_get(args, f"/cli/journals/{args.id}")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def cmd_reviews_daily_list(args) -> None:
|
|
130
|
+
_get(
|
|
131
|
+
args,
|
|
132
|
+
"/cli/daily-reviews",
|
|
133
|
+
{
|
|
134
|
+
"date": args.date,
|
|
135
|
+
"start_date": args.date_from,
|
|
136
|
+
"end_date": args.date_to,
|
|
137
|
+
"limit": args.limit,
|
|
138
|
+
"offset": args.offset,
|
|
139
|
+
},
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def cmd_reviews_daily_get(args) -> None:
|
|
144
|
+
_get(args, f"/cli/daily-reviews/{args.id}")
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def cmd_reviews_notes_list(args) -> None:
|
|
148
|
+
_get(
|
|
149
|
+
args,
|
|
150
|
+
"/cli/review-notes",
|
|
151
|
+
{
|
|
152
|
+
"transaction_id": args.tx,
|
|
153
|
+
"start_date": args.date_from,
|
|
154
|
+
"end_date": args.date_to,
|
|
155
|
+
"stock_code": args.code,
|
|
156
|
+
"limit": args.limit,
|
|
157
|
+
"offset": args.offset,
|
|
158
|
+
},
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def cmd_reviews_notes_get(args) -> None:
|
|
163
|
+
_get(args, f"/cli/review-notes/{args.id}")
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def cmd_txs_list(args) -> None:
|
|
167
|
+
_get(
|
|
168
|
+
args,
|
|
169
|
+
"/cli/transactions",
|
|
170
|
+
{
|
|
171
|
+
"start_date": args.date_from,
|
|
172
|
+
"end_date": args.date_to,
|
|
173
|
+
"stock_code": args.code,
|
|
174
|
+
"trade_type": args.type,
|
|
175
|
+
"limit": args.limit,
|
|
176
|
+
"offset": args.offset,
|
|
177
|
+
},
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def cmd_txs_get(args) -> None:
|
|
182
|
+
_get(args, f"/cli/transactions/{args.id}")
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def cmd_strategies_list(args) -> None:
|
|
186
|
+
_get(args, "/cli/strategies", {"limit": args.limit, "offset": args.offset})
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def cmd_strategies_get(args) -> None:
|
|
190
|
+
_get(args, f"/cli/strategies/{args.id}")
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def cmd_disciplines_list(args) -> None:
|
|
194
|
+
_get(args, "/cli/disciplines", {"limit": args.limit, "offset": args.offset})
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def cmd_disciplines_get(args) -> None:
|
|
198
|
+
_get(args, f"/cli/disciplines/{args.id}")
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def cmd_disciplines_records(args) -> None:
|
|
202
|
+
_get(
|
|
203
|
+
args,
|
|
204
|
+
"/cli/discipline-records",
|
|
205
|
+
{
|
|
206
|
+
"date": args.date,
|
|
207
|
+
"start_date": args.date_from,
|
|
208
|
+
"end_date": args.date_to,
|
|
209
|
+
"status": args.status,
|
|
210
|
+
"limit": args.limit,
|
|
211
|
+
"offset": args.offset,
|
|
212
|
+
},
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
217
|
+
parser = argparse.ArgumentParser(prog="tradingplan", description="Read-only TradingPlan CLI for agents")
|
|
218
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
219
|
+
|
|
220
|
+
whoami = sub.add_parser("whoami", help="Confirm the API key and site")
|
|
221
|
+
_add_auth(whoami)
|
|
222
|
+
whoami.set_defaults(func=cmd_whoami)
|
|
223
|
+
|
|
224
|
+
day = sub.add_parser("day", help="One-day snapshot")
|
|
225
|
+
day.add_argument("date")
|
|
226
|
+
_add_auth(day)
|
|
227
|
+
day.set_defaults(func=cmd_day)
|
|
228
|
+
|
|
229
|
+
node = sub.add_parser("node-analysis", help="Node analysis for one day")
|
|
230
|
+
node.add_argument("date")
|
|
231
|
+
_add_auth(node)
|
|
232
|
+
node.set_defaults(func=cmd_node_analysis)
|
|
233
|
+
|
|
234
|
+
init = sub.add_parser("init", help="Write ~/.config/tradingplan/config.toml")
|
|
235
|
+
init.add_argument("--force", action="store_true")
|
|
236
|
+
_add_auth(init)
|
|
237
|
+
init.set_defaults(func=cmd_init)
|
|
238
|
+
|
|
239
|
+
skill = sub.add_parser("skill", help="Print the agent skill text")
|
|
240
|
+
skill.set_defaults(func=cmd_skill)
|
|
241
|
+
|
|
242
|
+
plans = sub.add_parser("plans").add_subparsers(dest="plans_cmd", required=True)
|
|
243
|
+
plans_list = plans.add_parser("list")
|
|
244
|
+
_add_range(plans_list)
|
|
245
|
+
plans_list.add_argument("--type", dest="type", default=None)
|
|
246
|
+
_add_page(plans_list)
|
|
247
|
+
_add_auth(plans_list)
|
|
248
|
+
plans_list.set_defaults(func=cmd_plans_list)
|
|
249
|
+
plans_get = plans.add_parser("get")
|
|
250
|
+
plans_get.add_argument("id", type=int)
|
|
251
|
+
_add_auth(plans_get)
|
|
252
|
+
plans_get.set_defaults(func=cmd_plans_get)
|
|
253
|
+
|
|
254
|
+
journals = sub.add_parser("journals").add_subparsers(dest="journals_cmd", required=True)
|
|
255
|
+
journals_list = journals.add_parser("list")
|
|
256
|
+
_add_range(journals_list)
|
|
257
|
+
_add_page(journals_list)
|
|
258
|
+
_add_auth(journals_list)
|
|
259
|
+
journals_list.set_defaults(func=cmd_journals_list)
|
|
260
|
+
journals_get = journals.add_parser("get")
|
|
261
|
+
journals_get.add_argument("id", type=int)
|
|
262
|
+
_add_auth(journals_get)
|
|
263
|
+
journals_get.set_defaults(func=cmd_journals_get)
|
|
264
|
+
|
|
265
|
+
reviews = sub.add_parser("reviews").add_subparsers(dest="reviews_cmd", required=True)
|
|
266
|
+
daily = reviews.add_parser("daily").add_subparsers(dest="reviews_daily_cmd", required=True)
|
|
267
|
+
daily_list = daily.add_parser("list")
|
|
268
|
+
_add_range(daily_list)
|
|
269
|
+
_add_page(daily_list)
|
|
270
|
+
_add_auth(daily_list)
|
|
271
|
+
daily_list.set_defaults(func=cmd_reviews_daily_list)
|
|
272
|
+
daily_get = daily.add_parser("get")
|
|
273
|
+
daily_get.add_argument("id", type=int)
|
|
274
|
+
_add_auth(daily_get)
|
|
275
|
+
daily_get.set_defaults(func=cmd_reviews_daily_get)
|
|
276
|
+
|
|
277
|
+
notes = reviews.add_parser("notes").add_subparsers(dest="reviews_notes_cmd", required=True)
|
|
278
|
+
notes_list = notes.add_parser("list")
|
|
279
|
+
notes_list.add_argument("--tx", type=int, default=None)
|
|
280
|
+
_add_range(notes_list, with_date=False)
|
|
281
|
+
notes_list.add_argument("--code", default=None)
|
|
282
|
+
_add_page(notes_list)
|
|
283
|
+
_add_auth(notes_list)
|
|
284
|
+
notes_list.set_defaults(func=cmd_reviews_notes_list)
|
|
285
|
+
notes_get = notes.add_parser("get")
|
|
286
|
+
notes_get.add_argument("id", type=int)
|
|
287
|
+
_add_auth(notes_get)
|
|
288
|
+
notes_get.set_defaults(func=cmd_reviews_notes_get)
|
|
289
|
+
|
|
290
|
+
txs = sub.add_parser("txs").add_subparsers(dest="txs_cmd", required=True)
|
|
291
|
+
txs_list = txs.add_parser("list")
|
|
292
|
+
_add_range(txs_list, with_date=False)
|
|
293
|
+
txs_list.add_argument("--code", default=None)
|
|
294
|
+
txs_list.add_argument("--type", dest="type", default=None)
|
|
295
|
+
_add_page(txs_list)
|
|
296
|
+
_add_auth(txs_list)
|
|
297
|
+
txs_list.set_defaults(func=cmd_txs_list)
|
|
298
|
+
txs_get = txs.add_parser("get")
|
|
299
|
+
txs_get.add_argument("id", type=int)
|
|
300
|
+
_add_auth(txs_get)
|
|
301
|
+
txs_get.set_defaults(func=cmd_txs_get)
|
|
302
|
+
|
|
303
|
+
strategies = sub.add_parser("strategies").add_subparsers(dest="strategies_cmd", required=True)
|
|
304
|
+
strategies_list = strategies.add_parser("list")
|
|
305
|
+
_add_page(strategies_list)
|
|
306
|
+
_add_auth(strategies_list)
|
|
307
|
+
strategies_list.set_defaults(func=cmd_strategies_list)
|
|
308
|
+
strategies_get = strategies.add_parser("get")
|
|
309
|
+
strategies_get.add_argument("id", type=int)
|
|
310
|
+
_add_auth(strategies_get)
|
|
311
|
+
strategies_get.set_defaults(func=cmd_strategies_get)
|
|
312
|
+
|
|
313
|
+
disciplines = sub.add_parser("disciplines").add_subparsers(dest="disciplines_cmd", required=True)
|
|
314
|
+
disciplines_list = disciplines.add_parser("list")
|
|
315
|
+
_add_page(disciplines_list)
|
|
316
|
+
_add_auth(disciplines_list)
|
|
317
|
+
disciplines_list.set_defaults(func=cmd_disciplines_list)
|
|
318
|
+
disciplines_get = disciplines.add_parser("get")
|
|
319
|
+
disciplines_get.add_argument("id", type=int)
|
|
320
|
+
_add_auth(disciplines_get)
|
|
321
|
+
disciplines_get.set_defaults(func=cmd_disciplines_get)
|
|
322
|
+
records = disciplines.add_parser("records")
|
|
323
|
+
_add_range(records)
|
|
324
|
+
records.add_argument("--status", default=None)
|
|
325
|
+
_add_page(records)
|
|
326
|
+
_add_auth(records)
|
|
327
|
+
records.set_defaults(func=cmd_disciplines_records)
|
|
328
|
+
|
|
329
|
+
return parser
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def main(argv: Optional[list[str]] = None) -> int:
|
|
333
|
+
parser = build_parser()
|
|
334
|
+
try:
|
|
335
|
+
args = parser.parse_args(argv)
|
|
336
|
+
args.func(args)
|
|
337
|
+
return 0
|
|
338
|
+
except CliError as exc:
|
|
339
|
+
print(exc.to_json(), file=sys.stderr)
|
|
340
|
+
return exc.exit_code
|
|
341
|
+
except SystemExit as exc:
|
|
342
|
+
code = exc.code if isinstance(exc.code, int) else 1
|
|
343
|
+
if code == 0:
|
|
344
|
+
return 0
|
|
345
|
+
print(
|
|
346
|
+
CliError(
|
|
347
|
+
"Invalid arguments",
|
|
348
|
+
"invalid_args",
|
|
349
|
+
"See `tradingplan --help`",
|
|
350
|
+
4,
|
|
351
|
+
).to_json(),
|
|
352
|
+
file=sys.stderr,
|
|
353
|
+
)
|
|
354
|
+
return 4
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def run() -> None:
|
|
358
|
+
raise SystemExit(main())
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
if __name__ == "__main__":
|
|
362
|
+
run()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Optional
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from .config import Settings
|
|
8
|
+
from .errors import CliError, from_http
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def get_json(
|
|
12
|
+
settings: Settings,
|
|
13
|
+
path: str,
|
|
14
|
+
params: Optional[dict[str, Any]] = None,
|
|
15
|
+
*,
|
|
16
|
+
transport: Optional[httpx.BaseTransport] = None,
|
|
17
|
+
) -> Any:
|
|
18
|
+
headers = {"X-API-Key": settings.api_key}
|
|
19
|
+
try:
|
|
20
|
+
with httpx.Client(
|
|
21
|
+
base_url=settings.api_url,
|
|
22
|
+
headers=headers,
|
|
23
|
+
timeout=30.0,
|
|
24
|
+
transport=transport,
|
|
25
|
+
) as client:
|
|
26
|
+
response = client.get(path, params=_clean_params(params or {}))
|
|
27
|
+
except httpx.HTTPError as exc:
|
|
28
|
+
raise CliError(str(exc), "network", "Retry or check TRADINGPLAN_API_URL", 1) from exc
|
|
29
|
+
if response.status_code >= 400:
|
|
30
|
+
try:
|
|
31
|
+
body = response.json()
|
|
32
|
+
except ValueError:
|
|
33
|
+
body = response.text
|
|
34
|
+
raise from_http(response.status_code, body)
|
|
35
|
+
return response.json()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _clean_params(params: dict[str, Any]) -> dict[str, Any]:
|
|
39
|
+
return {key: value for key, value in params.items() if value is not None}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Mapping, Optional
|
|
6
|
+
|
|
7
|
+
SITES = {
|
|
8
|
+
"china": "https://longtou.online/api/v1",
|
|
9
|
+
"overseas": "https://tradingplan.day/api/v1",
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
CONFIG_PATH = Path.home() / ".config" / "tradingplan" / "config.toml"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ConfigError(Exception):
|
|
16
|
+
def __init__(self, message: str):
|
|
17
|
+
super().__init__(message)
|
|
18
|
+
self.message = message
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class Settings:
|
|
23
|
+
api_key: str
|
|
24
|
+
api_url: str
|
|
25
|
+
site: Optional[str] = None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _parse_simple_toml(text: str) -> dict[str, str]:
|
|
29
|
+
data: dict[str, str] = {}
|
|
30
|
+
for raw in text.splitlines():
|
|
31
|
+
line = raw.strip()
|
|
32
|
+
if not line or line.startswith("#") or "=" not in line:
|
|
33
|
+
continue
|
|
34
|
+
key, value = line.split("=", 1)
|
|
35
|
+
data[key.strip()] = value.strip().strip('"').strip("'")
|
|
36
|
+
return data
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def load_file(path: Path) -> dict[str, str]:
|
|
40
|
+
if not path.is_file():
|
|
41
|
+
return {}
|
|
42
|
+
return _parse_simple_toml(path.read_text(encoding="utf-8"))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def dump_file(path: Path, values: dict[str, str]) -> None:
|
|
46
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
47
|
+
lines = [f'{key} = "{value}"\n' for key, value in values.items() if value]
|
|
48
|
+
path.write_text("".join(lines), encoding="utf-8")
|
|
49
|
+
path.chmod(0o600)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def resolve_api_url(
|
|
53
|
+
*,
|
|
54
|
+
api_url: Optional[str] = None,
|
|
55
|
+
site: Optional[str] = None,
|
|
56
|
+
file_values: Optional[Mapping[str, str]] = None,
|
|
57
|
+
environ: Optional[Mapping[str, str]] = None,
|
|
58
|
+
) -> Optional[str]:
|
|
59
|
+
file_values = file_values or {}
|
|
60
|
+
environ = environ or {}
|
|
61
|
+
if api_url:
|
|
62
|
+
return api_url.rstrip("/")
|
|
63
|
+
env_url = environ.get("TRADINGPLAN_API_URL")
|
|
64
|
+
if env_url:
|
|
65
|
+
return env_url.rstrip("/")
|
|
66
|
+
if site:
|
|
67
|
+
if site not in SITES:
|
|
68
|
+
raise ConfigError("site must be china or overseas")
|
|
69
|
+
return SITES[site]
|
|
70
|
+
file_url = file_values.get("api_url")
|
|
71
|
+
if file_url:
|
|
72
|
+
return file_url.rstrip("/")
|
|
73
|
+
file_site = file_values.get("site")
|
|
74
|
+
if file_site:
|
|
75
|
+
if file_site not in SITES:
|
|
76
|
+
raise ConfigError("site must be china or overseas")
|
|
77
|
+
return SITES[file_site]
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def resolve_settings(
|
|
82
|
+
*,
|
|
83
|
+
api_key: Optional[str] = None,
|
|
84
|
+
api_url: Optional[str] = None,
|
|
85
|
+
site: Optional[str] = None,
|
|
86
|
+
environ: Optional[Mapping[str, str]] = None,
|
|
87
|
+
config_path: Optional[Path] = None,
|
|
88
|
+
) -> Settings:
|
|
89
|
+
environ = environ or {}
|
|
90
|
+
file_values = load_file(config_path or CONFIG_PATH)
|
|
91
|
+
key = api_key or environ.get("TRADINGPLAN_API_KEY") or file_values.get("api_key")
|
|
92
|
+
url = resolve_api_url(
|
|
93
|
+
api_url=api_url,
|
|
94
|
+
site=site,
|
|
95
|
+
file_values=file_values,
|
|
96
|
+
environ=environ,
|
|
97
|
+
)
|
|
98
|
+
if not key or not url:
|
|
99
|
+
raise ConfigError(
|
|
100
|
+
"Missing API key or URL. Run `tradingplan init` or set TRADINGPLAN_API_KEY and TRADINGPLAN_API_URL."
|
|
101
|
+
)
|
|
102
|
+
resolved_site = site or file_values.get("site")
|
|
103
|
+
return Settings(api_key=key, api_url=url, site=resolved_site)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CliError(Exception):
|
|
8
|
+
def __init__(self, error: str, code: str, hint: str, exit_code: int):
|
|
9
|
+
super().__init__(error)
|
|
10
|
+
self.error = error
|
|
11
|
+
self.code = code
|
|
12
|
+
self.hint = hint
|
|
13
|
+
self.exit_code = exit_code
|
|
14
|
+
|
|
15
|
+
def to_json(self) -> str:
|
|
16
|
+
return json.dumps(
|
|
17
|
+
{"error": self.error, "code": self.code, "hint": self.hint},
|
|
18
|
+
ensure_ascii=False,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def from_http(status_code: int, body: Any) -> CliError:
|
|
23
|
+
detail = ""
|
|
24
|
+
if isinstance(body, dict):
|
|
25
|
+
detail = str(body.get("detail") or body.get("error") or "")
|
|
26
|
+
elif isinstance(body, str):
|
|
27
|
+
detail = body
|
|
28
|
+
if status_code == 401:
|
|
29
|
+
return CliError(
|
|
30
|
+
detail or "Invalid API key",
|
|
31
|
+
"unauthorized",
|
|
32
|
+
"Check TRADINGPLAN_API_KEY and the site URL",
|
|
33
|
+
2,
|
|
34
|
+
)
|
|
35
|
+
if status_code == 404:
|
|
36
|
+
return CliError(detail or "Not found", "not_found", "Check the id or date", 3)
|
|
37
|
+
if status_code == 422:
|
|
38
|
+
return CliError(
|
|
39
|
+
detail or "Invalid arguments",
|
|
40
|
+
"invalid_args",
|
|
41
|
+
"See `tradingplan --help`",
|
|
42
|
+
4,
|
|
43
|
+
)
|
|
44
|
+
if status_code == 429:
|
|
45
|
+
return CliError(
|
|
46
|
+
detail or "Rate limited",
|
|
47
|
+
"rate_limited",
|
|
48
|
+
"Wait a minute and retry",
|
|
49
|
+
1,
|
|
50
|
+
)
|
|
51
|
+
return CliError(
|
|
52
|
+
detail or f"HTTP {status_code}",
|
|
53
|
+
"network",
|
|
54
|
+
"Retry or check TRADINGPLAN_API_URL",
|
|
55
|
+
1,
|
|
56
|
+
)
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tradingplan-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Read-only trading journal CLI for agents. Query Trading Plan pre-market plans, node analysis, daily reviews, trades, and discipline records.
|
|
5
|
+
Project-URL: Homepage, https://tradingplan.day
|
|
6
|
+
Project-URL: Trading Plan (China), https://longtou.online
|
|
7
|
+
Project-URL: App Store, https://apps.apple.com/app/id6760321164
|
|
8
|
+
Project-URL: Google Play, https://play.google.com/store/apps/details?id=online.longtou.plan
|
|
9
|
+
Keywords: trading,trading-journal,trading-plan,trade-review,pre-market,discipline,cli,cursor,claude,agent
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
Requires-Dist: httpx>=0.27
|
|
23
|
+
|
|
24
|
+
# tradingplan-cli
|
|
25
|
+
|
|
26
|
+
Read-only trading journal CLI for agents. Cursor, Claude Code, and other tools with a shell can query your [Trading Plan](https://tradingplan.day) account: pre-market plans, node analysis, trade journals, daily reviews, fills, discipline records, and strategies.
|
|
27
|
+
|
|
28
|
+
Trading Plan separates execution from judgment. The daily loop is plan before the open, log what you did, then review after the close. This CLI reads that record. It does not create, edit, or delete anything.
|
|
29
|
+
|
|
30
|
+
- China site (交易计划 / 龙头在线): https://longtou.online
|
|
31
|
+
- Overseas site: https://tradingplan.day
|
|
32
|
+
- App Store (TradingPlan, id 6760321164): https://apps.apple.com/app/id6760321164
|
|
33
|
+
- Google Play (`online.longtou.plan`): https://play.google.com/store/apps/details?id=online.longtou.plan
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
Python 3.10 or newer.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pipx install tradingplan-cli
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Or, inside a virtualenv:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install tradingplan-cli
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The console command is `tradingplan`.
|
|
50
|
+
|
|
51
|
+
## Configure
|
|
52
|
+
|
|
53
|
+
Create an API key on the website under **API Keys** (membership), then:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
tradingplan init
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Or set the environment, which is the better fit for an agent:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
export TRADINGPLAN_API_KEY=sk_...
|
|
63
|
+
export TRADINGPLAN_API_URL=https://longtou.online/api/v1 # china
|
|
64
|
+
# export TRADINGPLAN_API_URL=https://tradingplan.day/api/v1 # overseas
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Flags `--api-key`, `--api-url`, and `--site china|overseas` override the environment. `--site china` uses `https://longtou.online/api/v1`. `--site overseas` uses `https://tradingplan.day/api/v1`.
|
|
68
|
+
|
|
69
|
+
## Agent setup
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
tradingplan skill
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Paste the printed instructions into the agent. The usual question — what did I plan, trade, and review on one day — is one command:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
tradingplan day 2026-09-10
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
That returns the day's plans, node analysis, journal, daily review, transactions (with per-trade review notes), and discipline records as JSON.
|
|
82
|
+
|
|
83
|
+
## Commands
|
|
84
|
+
|
|
85
|
+
| Command | What it reads |
|
|
86
|
+
| --- | --- |
|
|
87
|
+
| `tradingplan whoami` | Confirm the API key and site |
|
|
88
|
+
| `tradingplan day YYYY-MM-DD` | One-day snapshot |
|
|
89
|
+
| `tradingplan plans list\|get` | Pre-market and action plans |
|
|
90
|
+
| `tradingplan node-analysis YYYY-MM-DD` | Node analysis for one day |
|
|
91
|
+
| `tradingplan journals list\|get` | Trade journal |
|
|
92
|
+
| `tradingplan reviews daily list\|get` | Daily review |
|
|
93
|
+
| `tradingplan reviews notes list\|get` | Per-trade review notes |
|
|
94
|
+
| `tradingplan txs list\|get` | Transactions (`buy`, `sell`, `add`, `reduce`) |
|
|
95
|
+
| `tradingplan strategies list\|get` | Strategies |
|
|
96
|
+
| `tradingplan disciplines list\|get` | Discipline rules |
|
|
97
|
+
| `tradingplan disciplines records` | Followed or violated discipline records |
|
|
98
|
+
|
|
99
|
+
`--from` and `--to` are `YYYY-MM-DD`. List commands accept `--limit` and `--offset`. Success is JSON on stdout. Errors are JSON on stderr with `code`: `unauthorized`, `not_found`, `invalid_args`, `rate_limited`, or `network`.
|
|
100
|
+
|
|
101
|
+
## 中文
|
|
102
|
+
|
|
103
|
+
给 Cursor、Claude Code 用的只读命令行,读取你在交易计划(原龙头在线)里的盘前计划、节点分析、交易日记、每日复盘、成交、纪律和策略。对应网站上的计划 → 执行 → 复盘闭环。只查询,不写入。
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
pipx install tradingplan-cli
|
|
107
|
+
export TRADINGPLAN_API_KEY=sk_...
|
|
108
|
+
export TRADINGPLAN_API_URL=https://longtou.online/api/v1
|
|
109
|
+
tradingplan day 2026-09-10
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
密钥在网站「API 密钥」页创建。`tradingplan skill` 会打印给 Agent 的使用说明。
|
|
113
|
+
|
|
114
|
+
手机端安装:
|
|
115
|
+
|
|
116
|
+
- App Store:https://apps.apple.com/app/id6760321164
|
|
117
|
+
- Google Play:https://play.google.com/store/apps/details?id=online.longtou.plan
|
|
118
|
+
|
|
119
|
+
## Develop from a checkout
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
pip install -e ./packages/tradingplan-cli
|
|
123
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
tradingplan_cli/SKILL.md,sha256=phitgfLdKJXrQPq-3-AnvX0q4ZZJArwjusaucKO1JsI,1405
|
|
2
|
+
tradingplan_cli/__init__.py,sha256=oKsCpoiQSSXx57DGs2VfYzeO1ukJQxdGnV-2hC3BI0M,42
|
|
3
|
+
tradingplan_cli/__main__.py,sha256=Fv36tXRiCbjGtIEZSpcCMZy77OcINUiSmQaZHUwlijk,74
|
|
4
|
+
tradingplan_cli/cli.py,sha256=wwotHxTxl9-6D0nMfOifkFOimJsZPYtgpsegVCk2DFg,11262
|
|
5
|
+
tradingplan_cli/client.py,sha256=9CRZ_cbnApCpc2qyKoqOqn-z7iEWJ4GS_GAxP-lk_Ms,1122
|
|
6
|
+
tradingplan_cli/config.py,sha256=V5hze6aC3RaezX-P5VHRor-fuPnVsYHIZnSxZ9AeWt0,3020
|
|
7
|
+
tradingplan_cli/errors.py,sha256=qtpqMfBFovShtq-iM38CjJrYNyWuSWoZ_BuSYvPSzI0,1560
|
|
8
|
+
tradingplan_cli-0.1.0.dist-info/METADATA,sha256=X99VwNpgvzo55eU_bN3xN2AREJQTuX0NmwZU4HOqf_0,4798
|
|
9
|
+
tradingplan_cli-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
10
|
+
tradingplan_cli-0.1.0.dist-info/entry_points.txt,sha256=RWiV2jqAc8zVORReWjmYLosVOZt_mdgu58uHJzEKdyY,56
|
|
11
|
+
tradingplan_cli-0.1.0.dist-info/top_level.txt,sha256=nbYCv6KwyvMoc-maFuGhsyjweT59aXC5VCil88FPnDI,16
|
|
12
|
+
tradingplan_cli-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tradingplan_cli
|