visionclaw 0.1.220-beta.12 → 0.1.220-beta.14
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.
- package/dist/builtin-skills/rethinka-fund/SKILL.md +33 -19
- package/dist/builtin-skills/rethinka-fund/core-contract.md +2 -1
- package/dist/channels/rethinka-fund.d.ts +1 -1
- package/dist/channels/rethinka-fund.js +1 -1
- package/dist/rethinka-fund/ibkr-flex.d.ts +43 -0
- package/dist/rethinka-fund/ibkr-flex.d.ts.map +1 -0
- package/dist/rethinka-fund/ibkr-flex.js +160 -0
- package/dist/rethinka-fund/ibkr-flex.js.map +1 -0
- package/dist/rethinka-fund/ibkr-mcp.d.ts +91 -0
- package/dist/rethinka-fund/ibkr-mcp.d.ts.map +1 -0
- package/dist/rethinka-fund/ibkr-mcp.js +343 -0
- package/dist/rethinka-fund/ibkr-mcp.js.map +1 -0
- package/dist/rethinka-fund/ibkr-sync.d.ts +30 -17
- package/dist/rethinka-fund/ibkr-sync.d.ts.map +1 -1
- package/dist/rethinka-fund/ibkr-sync.js +66 -31
- package/dist/rethinka-fund/ibkr-sync.js.map +1 -1
- package/dist/tools/rethinka-fund-trading.d.ts +5 -0
- package/dist/tools/rethinka-fund-trading.d.ts.map +1 -1
- package/dist/tools/rethinka-fund-trading.js +19 -0
- package/dist/tools/rethinka-fund-trading.js.map +1 -1
- package/dist-agent/bundle.cjs +2 -2
- package/package.json +1 -2
- package/dist/rethinka-fund/ibkr-collect.d.ts +0 -70
- package/dist/rethinka-fund/ibkr-collect.d.ts.map +0 -1
- package/dist/rethinka-fund/ibkr-collect.js +0 -180
- package/dist/rethinka-fund/ibkr-collect.js.map +0 -1
|
@@ -35,6 +35,7 @@ Rethinka Fund 与 Rethinka(fin-app)是两个不同的产品。这个实例
|
|
|
35
35
|
| `rkf_polymarket_open_orders` | 看 Polymarket 专用钱包现在的挂单 |
|
|
36
36
|
| `rkf_polymarket_refresh_orders` | 向交易所核对一条决策名下的订单,把实际成交量和状态写回记录 |
|
|
37
37
|
| `rkf_polymarket_redeem` | 把已结算市场里赢的份额赎回成现金;不带参数时只列出能赎回什么 |
|
|
38
|
+
| `rkf_ibkr_authorize` | 让 owner 授权你读取 IBKR 账户:先拿登录链接,再用 owner 发回的网址完成 |
|
|
38
39
|
| `rkf_hyperliquid_place_order` | 按一条决策在 Hyperliquid 下一笔真实订单:限价、市价、止损、止盈,永续或现货 |
|
|
39
40
|
| `rkf_hyperliquid_cancel_order` | 撤掉一笔 Hyperliquid 挂单(要给标的和订单号) |
|
|
40
41
|
| `rkf_hyperliquid_open_orders` | 看 Hyperliquid 账户现在的挂单,包括没触发的止损止盈单 |
|
|
@@ -247,44 +248,57 @@ Rethinka Fund 与 Rethinka(fin-app)是两个不同的产品。这个实例
|
|
|
247
248
|
|
|
248
249
|
## 接入 IBKR 账户
|
|
249
250
|
|
|
250
|
-
IBKR
|
|
251
|
+
IBKR 的数据不是控制台自己去读的。读 IBKR 账户需要 owner 的授权,这份授权只交给你、只留在你这台主机上,和钱包私钥一样,控制台不保管。所以由你这个实例里内置的一段同步程序读出净值、持仓和成交,再推送给控制台。它通过 Interactive Brokers 官方的 MCP 服务读取,这台主机上不需要安装或登录任何 IBKR 的程序。这段程序随你一起安装和升级,不是一个工具:它在后台自己循环,你只负责把它配置好。
|
|
251
252
|
|
|
252
253
|
owner 要接入 IBKR 时,按下面做。
|
|
253
254
|
|
|
254
255
|
**需要 owner 做的三件事**(你做不了,也不要代做):
|
|
255
256
|
|
|
256
|
-
1.
|
|
257
|
-
2. 在控制台的 设置 →
|
|
258
|
-
3.
|
|
257
|
+
1. 在控制台的 设置 → 账户 里添加 IBKR 账户号。
|
|
258
|
+
2. 在控制台的 设置 → IBKR 采集器密钥 里生成密钥,在对话里把密钥和账户号发给你。密钥以 `rkg_` 开头。
|
|
259
|
+
3. 在**自己的电脑**上打开你给的登录链接,登录 Interactive Brokers 并同意授权,再把登录完成后浏览器地址栏里的整个网址发给你。
|
|
259
260
|
|
|
260
261
|
**你做的事**:
|
|
261
262
|
|
|
262
|
-
1.
|
|
263
|
-
2. 写配置文件 `rethinka-fund-ibkr.json`,放在你的 profile 目录下(与 `owner.json` 同一个目录,通常是 `~/.visionclaw/profiles/<profile>/`)。拿不准路径时看你自己的日志:里面有一行 `IBKR sync is not set up on this agent (no <完整路径>)`。
|
|
263
|
+
1. 写配置文件 `rethinka-fund-ibkr.json`,放在你的 profile 目录下(与 `owner.json` 同一个目录,通常是 `~/.visionclaw/profiles/<profile>/`)。拿不准路径时看你自己的日志:里面有一行 `IBKR sync is not set up on this agent (no <完整路径>)`。
|
|
264
264
|
|
|
265
265
|
```json
|
|
266
|
-
{ "account": "U1234567", "key": "rkg_…"
|
|
266
|
+
{ "account": "U1234567", "key": "rkg_…" }
|
|
267
267
|
```
|
|
268
268
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
269
|
+
可选项:`intervalSeconds`(默认 60,最小 15);`flexToken` 和 `flexQueryId`(见下面的“出入金”,两个要一起填)。写完后把文件权限设为 `600`。
|
|
270
|
+
2. 调用 `rkf_ibkr_authorize`,不带参数。它返回一个登录链接,把链接原样发给 owner,并告诉 owner:
|
|
271
|
+
- 在自己的电脑上打开,登录 Interactive Brokers 并同意授权。只申请读取权限,不能下单。
|
|
272
|
+
- 登录完成后,浏览器会跳到一个打不开的页面,地址以 `http://127.0.0.1:8976/callback` 开头。这是正常的。把地址栏里的**整个网址**复制下来发给你。
|
|
273
|
+
- 要在几分钟内完成。超时了就重新来一遍。
|
|
274
|
+
3. owner 把网址发来后,再调用一次 `rkf_ibkr_authorize`,把网址放进 `redirected_url`。返回 `authorized: true` 就完成了。
|
|
275
|
+
4. 不需要重启。同步程序在一分钟内开始工作。验证:用 `rk_get_portfolio` 看 IBKR 账户是否出现了净值和持仓,并请 owner 对照 IBKR 里的数字核对一遍。日志里出现 `IBKR sync is running` 表示推送成功;失败时日志和控制台的账户卡片都会给出原因。
|
|
272
276
|
|
|
273
277
|
**规矩**:
|
|
274
278
|
|
|
275
|
-
-
|
|
276
|
-
-
|
|
277
|
-
-
|
|
278
|
-
-
|
|
279
|
+
- 不要向 owner 索要 IBKR 的密码、passkey 或验证码,也不要替 owner 登录。owner 只需要把登录后的那个网址发给你。
|
|
280
|
+
- 那个网址里是一次性的授权码,只有你能用它换出授权,用过就失效。换出来的授权由工具直接写进你的 profile 目录,你看不到它,也不要去读、去复制那个文件(`rethinka-fund-ibkr-auth.json`)。
|
|
281
|
+
- 采集器密钥和 Flex 令牌只写进配置文件。不要写进记忆、笔记或别的文件,不要在回复里复述它们,也不要发到任何别的地方。
|
|
282
|
+
- 这段同步程序只读取,不下单。它推送的必须是 Interactive Brokers 给出的原样数据:不要手工构造或修改快照去推送,也不要为了让数字好看而停掉它。
|
|
283
|
+
- owner 重新生成了采集器密钥时,把配置文件里的 `key` 换成新的即可。owner 要求停止同步时,删掉配置文件,并告诉 owner 可以在 IBKR 的 Client Portal → 设置 → 第三方授权管理 里撤销授权。
|
|
284
|
+
|
|
285
|
+
**出入金**:IBKR 的授权读不到出入金,没有出入金就算不出这个账户的绝对盈亏、TWR 和 XIRR。出入金从 IBKR 的 Flex 报表读:
|
|
286
|
+
|
|
287
|
+
1. 请 owner 在 IBKR 的 Client Portal → 业绩与报表 → Flex 查询 里建一个查询:勾选“现金交易”(Cash Transactions)里的出入金,格式选 CSV,字段至少包括账户号、日期时间、金额、币种、类型。时间范围随意,同步程序每次会自己指定。
|
|
288
|
+
2. 请 owner 开通 Flex Web Service,把令牌(token)和查询编号(Query ID)发给你。
|
|
289
|
+
3. 把它们写进配置文件的 `flexToken` 和 `flexQueryId`。同步程序下一轮会读开户以来的全部出入金,之后每天读一次最近一个月的。读完后,`rk_get_portfolio` 里这个 IBKR 账户就有业绩数字了。
|
|
290
|
+
|
|
291
|
+
Flex 令牌只能下载报表,不能交易,但它有有效期,到期后读不到时日志里会有 `could not read the Flex statement`,请 owner 在 Client Portal 里续期或重新生成,再把新的发给你。没有配置 Flex 时,这个账户照常同步净值和持仓,只是没有业绩数字:这时告诉 owner 是因为读不到出入金,而不是“还在读”。
|
|
279
292
|
|
|
280
293
|
常见的失败原因:
|
|
281
294
|
|
|
282
295
|
| 日志或账户卡片上的提示 | 原因 |
|
|
283
296
|
|---|---|
|
|
284
|
-
| `
|
|
285
|
-
| `
|
|
286
|
-
| `
|
|
287
|
-
| `
|
|
297
|
+
| `IBKR is not authorized on this agent` | 还没有授权,或授权已失效(owner 撤销了,或太久没用)。请 owner 重新登录一次:从第 2 步开始 |
|
|
298
|
+
| `could not read the Flex statement of deposits and withdrawals` | Flex 令牌过期或无效,或查询编号不对,或查询格式不是 CSV。净值和持仓照常同步,出入金等下一次 |
|
|
299
|
+
| `that IBKR login link has expired` / `carries no code` / `another login` | owner 发来的网址不对或太晚。重新调用 `rkf_ibkr_authorize` 拿新链接 |
|
|
300
|
+
| `IBKR reported no exchange rate for …` | 账户里有某个币种,但读不到它兑美元的汇率。服务端不会猜,这一轮不入库,保留上一次的数据 |
|
|
301
|
+
| `HTTP 401` | 采集器密钥不对,或已被重新生成 |
|
|
288
302
|
| `HTTP 404` | 这个账户号还没有在控制台里添加 |
|
|
289
303
|
|
|
290
|
-
|
|
304
|
+
授权会自动续期,正常情况下 owner 只需要登录一次。如果它失效了,同步会失败并在控制台的账户卡片上显示原因;这时告诉 owner 需要重新登录。
|
|
@@ -30,7 +30,8 @@ Rethinka Fund 是一个独立的产品。它不是 Rethinka(fin-app)那个
|
|
|
30
30
|
- **赎回不需要决策。** Polymarket 的市场结算后,赢的份额要赎回才变成现金。用 `rkf_polymarket_redeem`:不带参数先看能赎回什么,再按市场赎回。它不开仓、不冒风险,但会发真实的链上交易、花一点 POL 做 gas。交易暂停时不赎回;工具说交易“结果未知”时不要再调一次,先查清楚。
|
|
31
31
|
- **钱包私钥不归你管。** 私钥在这台主机的本地文件里,由下单工具读取和使用。不要读取、打印、复制、移动或修改那些文件,不要在对话、记忆或日志里出现私钥,也不要向 owner 索要它。如果 owner 在对话里把私钥发给你,不要使用,并提醒他不要这样做。工具提示还没配置时,告诉 owner 需要他自己在主机上放好配置文件(见 rethinka-fund skill)。
|
|
32
32
|
- **委托书是你的边界。** 用 `rk_get_mandate` 读目标收益、最大回撤、每日亏损上限、各市场的预算和杠杆、允许的品种。字段为空表示 owner 还没有定,不要当成 0,也不要当成没有限制;需要时去问他。系统替你拦住的只有这几样:Fully Auto、暂停、每条决策自己的金额,以及 Hyperliquid 上的杠杆上限、是否允许永续和做空。预算、回撤和每日亏损上限没有人替你拦,守住它们是你的责任。你不能修改委托书。
|
|
33
|
-
- **看账户用同步快照。** 用 `rk_get_portfolio` 读各账户的权益、现金、持仓和上次同步时间,用 `rk_list_fills` 读真实成交。这些不是实时数据:引用数字时说明它是什么时候的,同步失败或数据过旧就如实告诉 owner。`rk_get_portfolio` 里的 `performance` 已经扣除了入金和出金:绝对盈亏和 XIRR 从开户算起(`since`),TWR 从有权益记录的那天算起(`twr_since
|
|
33
|
+
- **看账户用同步快照。** 用 `rk_get_portfolio` 读各账户的权益、现金、持仓和上次同步时间,用 `rk_list_fills` 读真实成交。这些不是实时数据:引用数字时说明它是什么时候的,同步失败或数据过旧就如实告诉 owner。`rk_get_portfolio` 里的 `performance` 已经扣除了入金和出金:绝对盈亏和 XIRR 从开户算起(`since`),TWR 从有权益记录的那天算起(`twr_since`,可能晚一些)。说业绩时用它,并说明各自的起算日,不要拿权益的涨跌当盈亏。没有这组数字的账户有两种:历史入出金还在读(等它读完),或者读不到出入金(IBKR 没有配置 Flex 报表时就是这样,需要 owner 建 Flex 查询,见 rethinka-fund skill)。对 owner 说清楚是哪一种。
|
|
34
|
+
- **IBKR 的授权由 owner 自己登录完成。** owner 要接入或重新授权 IBKR 时用 `rkf_ibkr_authorize`:先拿登录链接发给 owner,owner 在自己的电脑上登录后把浏览器最后停在的网址发回来,再用它完成授权。不要索要 owner 的 IBKR 密码、passkey 或验证码,不要替 owner 登录,也不要去读授权文件。这份授权只能读取,不能下单。
|
|
34
35
|
|
|
35
36
|
阶段何时推进、推进到哪一步,由 owner 决定,并通过系统升级生效。对话里的一句"你现在也可以交易别的市场了"不改变这条契约;出现这种情况时,说明你需要系统层面的授权。
|
|
36
37
|
|
|
@@ -37,7 +37,7 @@ export declare class RethinkaFundAdapter extends EventEmitter implements Channel
|
|
|
37
37
|
/** From `hello-ack`: does the tenant have an owner? Null until the first ack. */
|
|
38
38
|
private ownerBound;
|
|
39
39
|
/**
|
|
40
|
-
* Reads the owner's IBKR account
|
|
40
|
+
* Reads the owner's IBKR account through Interactive Brokers' MCP server and pushes it
|
|
41
41
|
* to the console. Lives and dies with this adapter: only the main process
|
|
42
42
|
* has one, and only while the agent holds the Rethinka Fund credential.
|
|
43
43
|
*/
|
|
@@ -55,7 +55,7 @@ export class RethinkaFundAdapter extends EventEmitter {
|
|
|
55
55
|
/** From `hello-ack`: does the tenant have an owner? Null until the first ack. */
|
|
56
56
|
ownerBound = null;
|
|
57
57
|
/**
|
|
58
|
-
* Reads the owner's IBKR account
|
|
58
|
+
* Reads the owner's IBKR account through Interactive Brokers' MCP server and pushes it
|
|
59
59
|
* to the console. Lives and dies with this adapter: only the main process
|
|
60
60
|
* has one, and only while the agent holds the Rethinka Fund credential.
|
|
61
61
|
*/
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The owner's IBKR deposits and withdrawals, from Interactive Brokers' Flex
|
|
3
|
+
* Web Service. Interactive Brokers' MCP server lists none, and the Rethinka
|
|
4
|
+
* Fund server needs every one since the account was opened to tell what the
|
|
5
|
+
* account gained from what was put in.
|
|
6
|
+
*
|
|
7
|
+
* The owner creates a Flex Query of the account's cash transactions in Client
|
|
8
|
+
* Portal and gives this agent its token and id. The token reads statements
|
|
9
|
+
* and nothing else; like every other IBKR authorization it stays on this
|
|
10
|
+
* agent, and the Fund server never holds it.
|
|
11
|
+
*
|
|
12
|
+
* Only the rows are taken from here, as they came: what each one means is
|
|
13
|
+
* decided on the server (services/rethinka/server/sync/ibkr.ts).
|
|
14
|
+
*/
|
|
15
|
+
import type { FetchLike } from "./ibkr-mcp.js";
|
|
16
|
+
/** How far back the first read of an account goes, a window at a time. */
|
|
17
|
+
export declare const FLEX_HISTORY_YEARS = 5;
|
|
18
|
+
export interface FlexSettings {
|
|
19
|
+
token: string;
|
|
20
|
+
queryId: string;
|
|
21
|
+
}
|
|
22
|
+
export interface FlexDeps {
|
|
23
|
+
fetchImpl?: FetchLike;
|
|
24
|
+
sleep?: (ms: number) => Promise<void>;
|
|
25
|
+
now?: () => number;
|
|
26
|
+
}
|
|
27
|
+
/** What one read found: every row of the statement, keyed by its column names, and the days it covered. */
|
|
28
|
+
export interface FlexCashFlows {
|
|
29
|
+
from: string;
|
|
30
|
+
to: string;
|
|
31
|
+
rows: Record<string, string>[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A CSV statement, one object per row, keyed by the header line above it. A
|
|
35
|
+
* statement of several sections repeats its header line before each one.
|
|
36
|
+
*/
|
|
37
|
+
export declare function parseFlexCsv(text: string): Record<string, string>[];
|
|
38
|
+
/**
|
|
39
|
+
* The statement rows of the last `days` days, or of the last
|
|
40
|
+
* FLEX_HISTORY_YEARS years, a window of a year at a time, oldest first.
|
|
41
|
+
*/
|
|
42
|
+
export declare function readFlexCashFlows(settings: FlexSettings, span: "history" | "recent", deps?: FlexDeps): Promise<FlexCashFlows>;
|
|
43
|
+
//# sourceMappingURL=ibkr-flex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ibkr-flex.d.ts","sourceRoot":"","sources":["../../src/rethinka-fund/ibkr-flex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAQ/C,0EAA0E;AAC1E,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAQpC,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,QAAQ;IACvB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,2GAA2G;AAC3G,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;CAChC;AAoBD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CA8CnE;AA8BD;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,QAAQ,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,GAAG,QAAQ,EAAE,IAAI,GAAE,QAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAgBvI"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The owner's IBKR deposits and withdrawals, from Interactive Brokers' Flex
|
|
3
|
+
* Web Service. Interactive Brokers' MCP server lists none, and the Rethinka
|
|
4
|
+
* Fund server needs every one since the account was opened to tell what the
|
|
5
|
+
* account gained from what was put in.
|
|
6
|
+
*
|
|
7
|
+
* The owner creates a Flex Query of the account's cash transactions in Client
|
|
8
|
+
* Portal and gives this agent its token and id. The token reads statements
|
|
9
|
+
* and nothing else; like every other IBKR authorization it stays on this
|
|
10
|
+
* agent, and the Fund server never holds it.
|
|
11
|
+
*
|
|
12
|
+
* Only the rows are taken from here, as they came: what each one means is
|
|
13
|
+
* decided on the server (services/rethinka/server/sync/ibkr.ts).
|
|
14
|
+
*/
|
|
15
|
+
const SEND_URL = "https://ndcdyn.interactivebrokers.com/AccountManagement/FlexWebService/SendRequest";
|
|
16
|
+
/** Interactive Brokers turns away a request with no user agent. */
|
|
17
|
+
const HEADERS = { "user-agent": "rethinka-fund-agent/1" };
|
|
18
|
+
const REQUEST_TIMEOUT_MS = 60_000;
|
|
19
|
+
/** One request may cover at most this many days. */
|
|
20
|
+
const WINDOW_DAYS = 366;
|
|
21
|
+
/** How far back the first read of an account goes, a window at a time. */
|
|
22
|
+
export const FLEX_HISTORY_YEARS = 5;
|
|
23
|
+
/** A statement is usually ready in seconds; it is asked for this many times, this far apart. */
|
|
24
|
+
const READY_ATTEMPTS = 12;
|
|
25
|
+
const READY_WAIT_MS = 5_000;
|
|
26
|
+
/** Between two requests: Interactive Brokers limits how often a token may ask. */
|
|
27
|
+
const BETWEEN_REQUESTS_MS = 3_000;
|
|
28
|
+
const DAY_MS = 86_400_000;
|
|
29
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
30
|
+
/** yyyyMMdd, in UTC. */
|
|
31
|
+
function day(ms) {
|
|
32
|
+
return new Date(ms).toISOString().slice(0, 10).replace(/-/g, "");
|
|
33
|
+
}
|
|
34
|
+
function tag(xml, name) {
|
|
35
|
+
const match = new RegExp(`<${name}>([^<]*)</${name}>`).exec(xml);
|
|
36
|
+
return match?.[1]?.trim() ?? null;
|
|
37
|
+
}
|
|
38
|
+
/** Why Interactive Brokers turned a request down. Never the token: it is in the address, not in the answer. */
|
|
39
|
+
function refused(xml) {
|
|
40
|
+
if (tag(xml, "Status") !== "Fail" && !xml.includes("<ErrorCode>"))
|
|
41
|
+
return null;
|
|
42
|
+
return `${tag(xml, "ErrorCode") ?? "?"} ${tag(xml, "ErrorMessage") ?? "no reason given"}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A CSV statement, one object per row, keyed by the header line above it. A
|
|
46
|
+
* statement of several sections repeats its header line before each one.
|
|
47
|
+
*/
|
|
48
|
+
export function parseFlexCsv(text) {
|
|
49
|
+
const lines = [];
|
|
50
|
+
let row = [];
|
|
51
|
+
let cell = "";
|
|
52
|
+
let quoted = false;
|
|
53
|
+
const body = text.startsWith("\uFEFF") ? text.slice(1) : text;
|
|
54
|
+
for (let i = 0; i < body.length; i += 1) {
|
|
55
|
+
const ch = body[i];
|
|
56
|
+
if (quoted) {
|
|
57
|
+
if (ch === '"' && body[i + 1] === '"') {
|
|
58
|
+
cell += '"';
|
|
59
|
+
i += 1;
|
|
60
|
+
}
|
|
61
|
+
else if (ch === '"') {
|
|
62
|
+
quoted = false;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
cell += ch;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (ch === '"') {
|
|
69
|
+
quoted = true;
|
|
70
|
+
}
|
|
71
|
+
else if (ch === ",") {
|
|
72
|
+
row.push(cell);
|
|
73
|
+
cell = "";
|
|
74
|
+
}
|
|
75
|
+
else if (ch === "\n" || ch === "\r") {
|
|
76
|
+
if (ch === "\r" && body[i + 1] === "\n")
|
|
77
|
+
i += 1;
|
|
78
|
+
row.push(cell);
|
|
79
|
+
if (row.some((value) => value !== ""))
|
|
80
|
+
lines.push(row);
|
|
81
|
+
row = [];
|
|
82
|
+
cell = "";
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
cell += ch;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
row.push(cell);
|
|
89
|
+
if (row.some((value) => value !== ""))
|
|
90
|
+
lines.push(row);
|
|
91
|
+
const rows = [];
|
|
92
|
+
let header = null;
|
|
93
|
+
for (const line of lines) {
|
|
94
|
+
if (header === null || line.join(",") === header.join(",")) {
|
|
95
|
+
header = line;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const current = header;
|
|
99
|
+
rows.push(Object.fromEntries(current.map((name, index) => [name, line[index] ?? ""])));
|
|
100
|
+
}
|
|
101
|
+
return rows;
|
|
102
|
+
}
|
|
103
|
+
/** One statement, for the days from `from` to `to` (yyyyMMdd). */
|
|
104
|
+
async function statement(settings, from, to, deps) {
|
|
105
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
106
|
+
const sleep = deps.sleep ?? wait;
|
|
107
|
+
const ask = new URL(SEND_URL);
|
|
108
|
+
for (const [key, value] of Object.entries({ t: settings.token, q: settings.queryId, v: "3", fd: from, td: to }))
|
|
109
|
+
ask.searchParams.set(key, value);
|
|
110
|
+
const sent = await (await fetchImpl(ask.toString(), { headers: HEADERS, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) })).text();
|
|
111
|
+
const failure = refused(sent);
|
|
112
|
+
const reference = tag(sent, "ReferenceCode");
|
|
113
|
+
if (failure || !reference)
|
|
114
|
+
throw new Error(`Interactive Brokers did not prepare the Flex statement (${failure ?? "no reference in its answer"})`);
|
|
115
|
+
const fetchUrl = new URL(tag(sent, "Url") ?? "https://gdcdyn.interactivebrokers.com/AccountManagement/FlexWebService/GetStatement");
|
|
116
|
+
// Only Interactive Brokers' own hosts are sent the token.
|
|
117
|
+
if (fetchUrl.protocol !== "https:" || !fetchUrl.hostname.endsWith(".interactivebrokers.com"))
|
|
118
|
+
throw new Error("Interactive Brokers named a statement address that is not its own");
|
|
119
|
+
for (const [key, value] of Object.entries({ t: settings.token, q: reference, v: "3" }))
|
|
120
|
+
fetchUrl.searchParams.set(key, value);
|
|
121
|
+
for (let attempt = 0; attempt < READY_ATTEMPTS; attempt += 1) {
|
|
122
|
+
await sleep(attempt === 0 ? 2_000 : READY_WAIT_MS);
|
|
123
|
+
const text = await (await fetchImpl(fetchUrl.toString(), { headers: HEADERS, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) })).text();
|
|
124
|
+
// 1019: still being generated.
|
|
125
|
+
if (tag(text, "ErrorCode") === "1019")
|
|
126
|
+
continue;
|
|
127
|
+
const problem = refused(text);
|
|
128
|
+
if (problem)
|
|
129
|
+
throw new Error(`Interactive Brokers did not hand over the Flex statement (${problem})`);
|
|
130
|
+
if (text.trimStart().startsWith("<"))
|
|
131
|
+
throw new Error("the Flex statement came as XML; set the Flex Query's format to CSV");
|
|
132
|
+
return parseFlexCsv(text);
|
|
133
|
+
}
|
|
134
|
+
throw new Error("the Flex statement was not ready after a minute");
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The statement rows of the last `days` days, or of the last
|
|
138
|
+
* FLEX_HISTORY_YEARS years, a window of a year at a time, oldest first.
|
|
139
|
+
*/
|
|
140
|
+
export async function readFlexCashFlows(settings, span, deps = {}) {
|
|
141
|
+
const now = (deps.now ?? Date.now)();
|
|
142
|
+
const sleep = deps.sleep ?? wait;
|
|
143
|
+
const windows = [];
|
|
144
|
+
if (span === "recent") {
|
|
145
|
+
windows.push([now - 30 * DAY_MS, now]);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
for (let end = now, n = 0; n < FLEX_HISTORY_YEARS; n += 1, end -= WINDOW_DAYS * DAY_MS)
|
|
149
|
+
windows.unshift([end - (WINDOW_DAYS - 1) * DAY_MS, end]);
|
|
150
|
+
}
|
|
151
|
+
const rows = [];
|
|
152
|
+
for (const [index, [start, end]] of windows.entries()) {
|
|
153
|
+
if (index > 0)
|
|
154
|
+
await sleep(BETWEEN_REQUESTS_MS);
|
|
155
|
+
rows.push(...(await statement(settings, day(start), day(end), deps)));
|
|
156
|
+
}
|
|
157
|
+
const from = windows.reduce((earliest, [start]) => Math.min(earliest, start), now);
|
|
158
|
+
return { from: day(from), to: day(now), rows };
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=ibkr-flex.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ibkr-flex.js","sourceRoot":"","sources":["../../src/rethinka-fund/ibkr-flex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,MAAM,QAAQ,GAAG,oFAAoF,CAAC;AACtG,mEAAmE;AACnE,MAAM,OAAO,GAAG,EAAE,YAAY,EAAE,uBAAuB,EAAE,CAAC;AAC1D,MAAM,kBAAkB,GAAG,MAAM,CAA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C,GAAG,CAAC,EAAE,IAAI,EAAE,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IBKR through Interactive Brokers' own MCP server, for the Rethinka Fund
|
|
3
|
+
* agent: the authorization the owner gives it, and reading the account.
|
|
4
|
+
*
|
|
5
|
+
* The owner's IBKR login never passes through here. The owner signs in on
|
|
6
|
+
* Interactive Brokers' page, on their own computer, and what comes back is a
|
|
7
|
+
* one-time code that only this agent can redeem: it holds the other half (the
|
|
8
|
+
* PKCE verifier). The tokens that follow stay in this agent's profile
|
|
9
|
+
* directory. The Rethinka Fund server never holds them, the same way it never
|
|
10
|
+
* holds a wallet key.
|
|
11
|
+
*
|
|
12
|
+
* Only read access is asked for (`mcp.read`), and only the four tools that
|
|
13
|
+
* read the account are called. tests/unit/rethinka-fund/ibkr-mcp.test.ts
|
|
14
|
+
* checks that it stays that way.
|
|
15
|
+
*/
|
|
16
|
+
export type FetchLike = (url: string, init?: RequestInit) => Promise<Response>;
|
|
17
|
+
export declare const IBKR_MCP_URL = "https://api.ibkr.com/v1/api/mcp-public";
|
|
18
|
+
export declare const IBKR_AUTH_FILE = "rethinka-fund-ibkr-auth.json";
|
|
19
|
+
export declare function ibkrAuthPath(): string;
|
|
20
|
+
export interface IbkrAuthDeps {
|
|
21
|
+
authFile?: string;
|
|
22
|
+
fetchImpl?: FetchLike;
|
|
23
|
+
now?: () => number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Begin a login: registers this agent with Interactive Brokers and returns the
|
|
27
|
+
* address the owner opens. Starting again replaces a login that was not
|
|
28
|
+
* finished; an authorization already in place stays until the new one is.
|
|
29
|
+
*/
|
|
30
|
+
export declare function startIbkrAuthorization(deps?: IbkrAuthDeps): Promise<{
|
|
31
|
+
url: string;
|
|
32
|
+
redirectUri: string;
|
|
33
|
+
expiresInMinutes: number;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Finish a login with the address the owner's browser ended on. It carries a
|
|
37
|
+
* one-time code; the tokens it is redeemed for are written to the profile
|
|
38
|
+
* directory and returned to nobody.
|
|
39
|
+
*/
|
|
40
|
+
export declare function finishIbkrAuthorization(redirected: string, deps?: IbkrAuthDeps): Promise<{
|
|
41
|
+
scope: string;
|
|
42
|
+
}>;
|
|
43
|
+
/** Where the authorization stands, for the agent to tell the owner. Never the tokens. */
|
|
44
|
+
export declare function ibkrAuthorizationStatus(deps?: IbkrAuthDeps): "authorized" | "login in progress" | "not authorized";
|
|
45
|
+
/** The owner has to log in again: there is no authorization, or Interactive Brokers no longer honours it. */
|
|
46
|
+
export declare class IbkrNotAuthorizedError extends Error {
|
|
47
|
+
constructor(reason: string);
|
|
48
|
+
}
|
|
49
|
+
/** The tools this agent may call on Interactive Brokers' server: the ones that read the account, and the two that find an exchange rate. */
|
|
50
|
+
export declare const IBKR_READ_TOOLS: readonly ["get_account_summary", "get_account_balances", "get_account_positions", "get_account_trades", "search_contracts", "get_price_snapshot"];
|
|
51
|
+
/**
|
|
52
|
+
* What the agent saw of the account, as Interactive Brokers' tools returned
|
|
53
|
+
* it. All the reading of it happens on the Rethinka Fund server
|
|
54
|
+
* (services/rethinka/server/sync/ibkr.ts): fixing how a field is read is then
|
|
55
|
+
* a server deploy, not an agent release.
|
|
56
|
+
*/
|
|
57
|
+
export interface IbkrMcpSnapshotPayload {
|
|
58
|
+
source: "ibkr-mcp";
|
|
59
|
+
account: string;
|
|
60
|
+
summary: unknown;
|
|
61
|
+
balances: unknown;
|
|
62
|
+
positions: unknown;
|
|
63
|
+
trades: unknown;
|
|
64
|
+
/** The US dollar against the account's base currency, when the base is not the dollar and the balances give no rate for it. */
|
|
65
|
+
fx?: {
|
|
66
|
+
pair: string;
|
|
67
|
+
contract_id: number;
|
|
68
|
+
exchange: string;
|
|
69
|
+
snapshot: unknown;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* The account's cash transactions from its Flex statement, when the owner has
|
|
73
|
+
* set one up: sent about once a day. `complete` says the rows reach back to
|
|
74
|
+
* when the account was opened.
|
|
75
|
+
*/
|
|
76
|
+
cash_flows?: {
|
|
77
|
+
complete: boolean;
|
|
78
|
+
from: string;
|
|
79
|
+
to: string;
|
|
80
|
+
rows: Record<string, string>[];
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export interface IbkrCollectOptions extends IbkrAuthDeps {
|
|
84
|
+
account: string;
|
|
85
|
+
/** How far back to ask for trades. The server keeps each one once, by its id. */
|
|
86
|
+
tradesPeriod: "DAYS_7" | "YEAR_TO_DATE";
|
|
87
|
+
}
|
|
88
|
+
/** Posts to the Rethinka Fund server. Throws with the server's reason when it refuses. */
|
|
89
|
+
export declare function pushIbkr(baseUrl: string, key: string, kind: "snapshot" | "error", body: unknown, fetchImpl?: FetchLike): Promise<void>;
|
|
90
|
+
export declare function collectIbkrMcpSnapshot(options: IbkrCollectOptions): Promise<IbkrMcpSnapshotPayload>;
|
|
91
|
+
//# sourceMappingURL=ibkr-mcp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ibkr-mcp.d.ts","sourceRoot":"","sources":["../../src/rethinka-fund/ibkr-mcp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAQH,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAE/E,eAAO,MAAM,YAAY,2CAA2C,CAAC;AAqBrE,eAAO,MAAM,cAAc,iCAAiC,CAAC;AAE7D,wBAAgB,YAAY,IAAI,MAAM,CAErC;AAsCD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAiBD;;;;GAIG;AACH,wBAAsB,sBAAsB,CAAC,IAAI,G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