redditalpha logoredditalpha
← 返回看板
分享
151%
r/optionsr/options· u/saffystaffie· 5 天前 11

我用 ChatGPT 回测了 SPY 0DTE 策略,并用 Codex 构建了 IBKR 自动化机器人

投资者摘要中性

作者分享 SPY 0DTE 开盘区间突破策略及用 AI 构建的 IBKR 自动化机器人,在实盘前寻求反馈。

帖子正文
高质量模型翻译结果

过去几周,我一直在开发一个机械化的SPY 0DTE开盘区间突破策略。

我使用ChatGPT来帮助制定规则、分析历史SPY数据,并对不同变体进行回测。规则确定后,我用Codex编写了一个完全自动化的Python机器人,通过官方TWS API连接到Interactive Brokers交易工作站。

我计划明天在实盘账户上测试它。最初我会使用有限的资金,因为这是首次真实环境测试,而0DTE期权显然风险极高。

这不构成财务建议,我也未声称该策略能盈利。我分享它是为了获取反馈,尤其是关于自动化、执行假设以及规则中的潜在弱点。

交易标的

标的资产:SPY

产品类型:当日到期的SPY期权

看涨设置:买入看涨期权

看跌设置:买入看跌期权

每日最多一个设置

一个设置可包含多个合约

时间设置

所有操作均以纽约时间为准。

开盘区间:09:30:00–10:29:59 ET

14:00 ET及之后不再开新仓

15:30 ET强制平掉所有剩余合约

周末、美国市场节假日或提前收盘日不交易

开盘区间与突破

机器人基于前60根1分钟SPYK线构建开盘区间。

记录以下内容:

开盘区间最高价

开盘区间最低价

开盘区间宽度

触发条件为:

看涨触发点 = 开盘区间最高价 + $0.80

看跌触发点 = 开盘区间最低价 − $0.80

10:30之后,哪个触发点先被触及,即锁定当日方向。

若看涨触发点先被触及,机器人仅考虑看涨期权。

若看跌触发点先被触及,机器人仅考虑看跌期权。

不会中途反转方向。

五根K线确认

确认窗口包括:

触发点首次被触及的那一根1分钟K线

接下来的四根1分钟K线

对于看涨设置,至少有一根已完成的K线必须严格收于看涨触发点之上。

对于看跌设置,至少有一根必须严格收于看跌触发点之下。

恰好等于触发点的收盘不计入。

一旦有K线确认,机器人将在下一分钟K线开始时准备入场。

若五根K线均未确认,则当天无交易。

期权选择

机器人选择:

同日到期的SPY期权

看涨设置选看涨期权

看跌设置选看跌期权

行权价数值最接近当前实时SPY价格

平局规则:

看涨期权:选择较低行权价

看跌期权:选择较高行权价

不依据delta、隐含波动率、成交量、未平仓合约量或最低溢价来选择合约。

报价要求

入场前,机器人要求:

实时SPY行情数据

实时期权行情数据

买价和卖价均为正值

卖价 ≥ 买价

SPY报价不超过两秒

期权报价不超过三秒

不得使用延迟或冻结数据

价差计算:

价差百分比 = (卖价 - 买价) / 中间价

允许的最大价差为中间价的15%。

头寸规模

该策略仅使用现金。

现金基础:

min(已结算现金, 总现金价值)

不包含:

保证金购买力

现有股票

现有期权

未实现利润

净值清算总额

借入资金

最大交易预算为:

可用合格现金的20%

机器人通过以下方式计算可承受性:

初始期权卖价

允许的最高入场价为初始卖价 × 1.10

100股期权乘数

预估佣金和费用

买入尽可能多的整手合约,直至达到20%现金限额。

没有固定的合约数量上限。

合约分配

分配方式取决于开盘区间宽度。

若开盘区间低于$4.50

不买入“跑步者”合约。

分配模式重复:

TP1 → TP2 → TP2

示例:

1份合约:1个TP1

2份合约:1个TP1,1个TP2

3份合约:1个TP1,2个TP2

6份合约:2个TP1,4个TP2

若开盘区间为$4.50或以上

分配模式重复:

TP1 → TP2 → TP2 → 条件性跑步者

示例:

4份合约:1个TP1,2个TP2,1个跑步者

8份合约:2个TP1,4个TP2,2个跑步者

12份合约:3个TP1,6个TP2,3个跑步者

当开盘区间低于$4.50时,绝不买入跑步者合约。

入场执行

机器人提交可成交的限价买入单。

初始限价:当前期权卖价

重定价增量:$0.02

重定价间隔:每0.50秒一次

最大入场尝试时间:三秒

最高允许期权价格:初始卖价的10%以上

最大有利的SPY追击距离:缓冲触发点之外$0.25

对于看涨期权,若SPY上涨超过$0.25高于看涨触发点,未成交订单将被取消。

对于看跌期权,若SPY下跌超过$0.25低于看跌触发点,未成交订单将被取消。

若订单部分成交:

已成交合约保留

未成交部分取消

机器人不会后续重试或补仓

根据实际成交数量重新计算分配

一旦任何入场订单发出,当天不再允许第二个设置。

止盈目标

所有目标均基于SPY从缓冲触发点的移动幅度,而非期权溢价或成交价。

做多设置

TP1 = 看涨触发点 + $1.50

TP2 = 看涨触发点 + $2.00

TP5 = 看涨触发点 + $5.00

做空设置

TP1 = 看跌触发点 − $1.50

TP2 = 看跌触发点 − $2.00

TP5 = 看跌触发点 − $5.00

当实时SPY交易触及或突破目标水平时触发目标。无需K线收盘确认。

一旦触发,即使SPY立即反转,机器人仍继续执行期权平仓。

跑步者规则

跑步者合约仅在开盘区间至少为$4.50时存在。

在TP2处,机器人检查四个条件:

TP2在预期入场K线开盘后30分钟内达成

SPY在前30分钟内至少有$1.50的有利移动

TP2前最大不利移动不超过$0.50

TP2前无任何一根完成的1分钟K线收盘回到缓冲触发点下方

跑步者需满足至少三个条件。

得分3/4或4/4:持有所有跑步者至TP5

得分低于3/4:在TP2卖出所有跑步者

平仓执行

机器人使用卖出限价单。

初始平仓限价:当前期权买价

向下重定价$0.02

正常重定价间隔:约每秒一次

强制平仓重定价间隔:约每0.50秒一次

跟踪部分成交

仅重新提交或修改剩余数量

机器人从不故意提交超过实际持有的卖出合约数量。

收盘处理

15:30 ET时,机器人强制平掉所有剩余策略合约。

此操作覆盖:

TP1分配

TP2分配

跑步者状态

待定目标逻辑

目标是当天结束时所有策略合约归零。

止损机制

目前系统无早期策略止损。

机器人不使用:

SPY止损

期权溢价止损

追踪止损

保本止损

百分比止损

早期时间止损

唯一退出方式为:

TP1

TP2

TP2处跑步者拒绝

TP5

15:30强制平仓

我知道这是系统中最大的风险之一,也是我在测试期间会重点关注的部分。

机器人安全与恢复机制

机器人还包含:

正确实盘账户验证

实盘模式确认

启动时对账

持久化SQLite状态

重复订单保护

唯一订单标识符

持仓与执行对账

部分成交处理

断线重连处理

未知持仓/订单锁定

过期数据检查

强制平仓保护

入场、成交、目标、错误和断线的通知

若检测到未知的SPY当日到期期权持仓或订单,系统拒绝开启新交易。

开发流程

我使用ChatGPT:

协助创建策略

分析历史1分钟SPY数据

回测入场与目标规则

优化跑步者标准

将策略转化为详细编码规范

随后我使用Codex为Interactive Brokers TWS API构建了Python自动化交易机器人。

明天将是首次在实盘账户上的测试。

我将谨慎起步,因为实盘成交、价差、API行为和期权定价可能与基于K线的回测存在显著差异。

我很乐意听取反馈,包括:

确认逻辑是否合理

20%现金分配比例

缺乏早期止损

跑步者资格规则

入场追击限制

潜在的IBKR/TWS自动化问题

可能遗漏的前瞻偏差或回测错误

讨论 · 高赞评论12 条精选
u/RationalBeliever 16· 5 天前

I highly suggest paper trading. You have so much complexity to your system that there is real risk of overfitting.

u/Shoggophant 10· 5 天前

How did it fare in backtesting, or on a paper account?

If you're trusting codex to write something without error and willing to put money on the line for a first test, I'd say you're gambling in more ways than one.

If the question is 'what do you think of these rules, will thia be profitable? ' the answer is perform backtesting.

That being said, way to vibe code, must have had a lot of work put in, sounds like you put a lot of work Into it.

Worst case scenerio you taught yourself a lot of stock trading mechanics details, which even if the program flops, will be useful some way or another.

u/SometimesWr0ng 5· 5 天前

This looks great, what could possibly go wrong. Try out on paper first, please.

u/saffystaffie 1· 1 天前

I listened to those that mentioned a stop loss, and I changed the strategy- i posted my new one. the first one didn't run, partly because of me not being able to leave my laptop running and partly due to the trades not filling the requirements. I also looked at QQQ as well, and made a similar strategy. have a look at my new post on here.

u/options-ModTeam 1· 4 天前

Removed for RULE: No spam, no cross-posts, no copy/paste of posts.

We incorporate the existing site-wide anti-spam rules explicitly. We consider cross-posting of all types and/or copy/pasting of posts as spam. Posts to this sub are expected to be unique and custom tailored to our community.

u/CODE_HEIST 1· 4 天前

Paper trade it first. The weak point is not that AI wrote code, it is that 0DTE plus IBKR automation gives tiny bugs real money consequences fast. Log every decision, order, fill, rejection, and skipped trade before letting it touch size.

u/ThetaEdgeHQ 1· 4 天前

The rules are the part everyone backtests and the part that matters least here. On a same day SPY breakout system your edge lives or dies on execution, and that is the one thing a historical bar backtest cannot see. You are almost certainly filling at the mid in the test. Live, you cross a bid ask that on 0DTE can be 10 to 20 percent of the premium, and an opening range breakout puts your entries in the widest spread window of the whole session. Round trip that on every signal and a system that shows positive expectancy on paper can sit underwater before the rules ever get a chance to work. Before you risk real capital, rerun it charging yourself the full spread plus a tick of slippage on every fill and see if the edge survives. If it only works at the mid, it does not work.

u/Miamiconnectionexo 1· 4 天前

this is genuinely helpful, not just the usual fluff. bookmarking this thread.

u/Worf_Of_Wall_St 1· 4 天前

You didn't explain your data source, but you should backtest whatever you come up with on historical tick by tick data to get any confidence on how it would perform on a live feed. You won't get the same result as backtesting on aggregated bars, and even tick data is not identical to what you would have received in real time because of data errors and latency.

u/Automate_The_Boring 1· 4 天前

Where did you get the data for option premium

u/Fauxial 1· 4 天前

Good luck, please post results of testing.

u/FURyannnn 1· 5 天前

For backtesting, it may be worth exploring the parameter set and seeing how various parameter changes (i.e. chosen range level plus +.90 instead of .80, etc) affect profit factor or PnL. A robust strategy exists when the hyperparameter changes but what you're optimizing (i.e. profit factor or PnL or whatever metric) is still roughly consistent and doesn't drastically change.

A good metaphor is a mountain range: if your strategy results in a continuous set of elevation across parameter changes, that's good. But if you have a few defined peaks of your metric and a set of valleys where a slight parameter change results in drastic performance changes, it's something to be more skeptical of