You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Pine Script策略在纽交所9:30精准以开盘价开多仓?

如何在纽交所9:30精准以开盘价开多仓?

我正尝试编写一个交易策略脚本,希望在纽交所9:30精准以开盘价对标的资产开多仓(类似9:30市价买入),但当前脚本总是在下一根1分钟K线(9:31)执行开仓操作。请问是否有方法实现以每日开盘价精准开多仓?

原代码

//@version=2
// This script is a tradingbot that buys NYSE:GOLD based on the performance of TVC:GOLD
strategy(title="Barrick Gold Buy, overlay=true, process_orders_on_close = true")

// Define the symbols
gold = "TVC:GOLD"

// Get the close prices for the previous trading day
prev_close_gold = security(gold, 'D', close[1])

// Get the current close prices
close_gold = security(gold, '1', close)

// Check if the current close price is greater than the previous close price
if close_gold > prev_close_gold
    // Check if it is 09:30
    if hour(time) == 09 and minute(time) == 30
        // If it is, then buy NYSE:GOLD
        strategy.entry("BUY", strategy.long, 1000)

// Check if NYSE:GOLD is in a long position
if strategy.position_size > 0
    // If it is, then take profit when the price reaches 20 cents higher
    strategy.exit("SELL", "BUY", strategy.position_size, profit = 20, loss = 10)

问题原因

  • 脚本设置了process_orders_on_close = true,这会让策略在K线收盘时才执行订单。9:30的1分钟K线要到9:31才收盘,因此订单会延迟到下一根K线触发。
  • 时间判断逻辑虽然定位到了9:30的K线,但结合收盘执行的规则,无法在9:30即时下单。

解决方案

修改要点

  1. 将process_orders_on_close改为false,让策略在K线生成时立即执行订单,无需等待收盘。
  2. 获取标的资产的当日开盘价,通过strategy.entry的limit参数指定以开盘价成交,确保精准性。
  3. 修正标题语法错误,保证脚本正常运行。

修改后的代码

//@version=2
strategy(title="Barrick Gold Buy", overlay=true, process_orders_on_close = false)

// 定义标的
gold = "TVC:GOLD"

// 获取黄金前一日收盘价
prev_close_gold = security(gold, 'D', close[1])
// 获取黄金当前1分钟收盘价
close_gold = security(gold, '1', close)
// 获取NYSE:GOLD当日开盘价
daily_open = security(syminfo.tickerid, 'D', open)

// 黄金当前价高于前一日收盘价,且当前时间为9:30时开仓
if close_gold > prev_close_gold
    if hour(time) == 9 and minute(time) == 30
        // 以当日开盘价限价开多仓
        strategy.entry("BUY", strategy.long, 1000, limit=daily_open)

// 持仓时设置止盈止损
if strategy.position_size > 0
    strategy.exit("SELL", "BUY", profit=20, loss=10)

补充说明

  • 如果希望更贴近市价买入的效果,也可以使用market订单类型,但结合limit=daily_open能确保以开盘价成交,符合需求。
  • 确保图表的时间周期设置为1分钟,这样时间判断逻辑才能精准触发。

内容的提问来源于stack exchange,提问作者Magnus Opus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 23:11:13