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

Pine Script技术问询:价格触及EMA时立即开仓而非周期收盘

问题描述

我想编写一个策略,在价格恰好触及EMA252时买入。因为EMA会随日内价格变动,所以我选择用ta.ema(close[1], 252)(前一日收盘计算的EMA)来简化流程和回测。

但现在遇到的问题是:订单要么在当日收盘时发送(当process_orders_on_close=true),要么在次日开盘时发送(当process_orders_on_close=false)。我试过用买入限价单,但限价单只会从次日开始生效。

这就导致当价格在前一日EMA位置反弹时,买入订单发送得太晚,成交价位过高(比如收盘时的价格)。请问该怎么修正这个问题?

我的代码
//@version=5
strategy("MA bounce simplified", overlay=true, initial_capital = 100000, default_qty_value=100, pyramiding=0, default_qty_type = strategy.percent_of_equity, calc_on_every_tick=true, process_orders_on_close=true)

//input
length_ema = input (252, "length_ema")
gp2 = "strategy inputs"
x = input (0.30, "x:", tooltip="The open order is send not right on the MA but X % above the MA", group=gp2)
z = input (5, "z:", tooltip ="Take profit Z period after the opening of the position", group=gp2)
stop_loss_pct = input(title="Stop-Loss %", defval=4.00, group=gp2)
xV = close * (x/100)

//Moving Averages (1 = day -1)
ema = ta.ema(close, length_ema)
ema_1 = ta.ema(close[1], length_ema)
ema_1x = ta.ema(close[1], length_ema) + xV

//Conditions
longcondition = ta.crossunder(low, ema_1x)

// Execute trade if condition is True
if (longcondition)
    strategy.entry("long", strategy.long)
if ta.barssince(longcondition) == z 
    strategy.close_all(comment = "close")
if strategy.position_size > 0
    strategy.exit("stop Loss", stop=strategy.position_avg_price*( (100-stop_loss_pct)/100))
解决方案

核心问题在于当前策略是K线收盘后才触发订单,而我们需要在日内价格触及目标价位时立即以限价单成交。以下是具体修正方案:

关键调整点

  1. 切换实时订单处理模式:将process_orders_on_close设为false,让策略在日内实时响应价格变动。
  2. 用限价单精准成交:提交以ema_1x为价位的限价买入单,价格回落至该价位时立即成交,无需等到收盘或次日。
  3. 过滤重复下单:加入条件确保每日仅触发一次买入信号,避免重复提交订单。
  4. 当日订单失效机制:每日收盘前取消未成交的限价单,避免订单延续到次日以过期价位成交。

修正后的完整代码

//@version=5
strategy("MA bounce fixed", overlay=true, initial_capital = 100000, 
         default_qty_value=100, pyramiding=0, default_qty_type = strategy.percent_of_equity, 
         calc_on_every_tick=true, process_orders_on_close=false)

//input
length_ema = input(252, "EMA周期")
gp2 = "策略参数"
x = input(0.30, "偏离MA百分比%", tooltip="订单触发价位为MA上方X%", group=gp2)
z = input(5, "止盈持有周期", tooltip="开仓后Z根K线平仓", group=gp2)
stop_loss_pct = input(title="止损百分比%", defval=4.00, group=gp2)

//计算目标价位(前一日EMA + 偏离值)
ema_1 = ta.ema(close[1], length_ema)
xV = ema_1 * (x/100)  // 改为基于EMA计算偏离,确保目标价位稳定
ema_1x = ema_1 + xV

//实时检测价格触及目标位,同时过滤重复信号
price_touched = ta.low <= ema_1x
can_enter = strategy.position_size == 0 and not price_touched[1] and price_touched

//提交限价买入单
if can_enter
    strategy.entry("long", strategy.long, limit=ema_1x)

//每日收盘前取消未成交的限价单
if ta.barstate.islast
    strategy.cancel("long")

//止盈逻辑
if ta.barssince(can_enter) == z 
    strategy.close_all(comment="止盈平仓")

//止损逻辑
if strategy.position_size > 0
    strategy.exit("止损", stop=strategy.position_avg_price*(1 - stop_loss_pct/100))

//绘制目标价位线
plot(ema_1x, color=color.new(color.blue, 0), title="目标买入价", style=plot.style_circles)

额外说明

  • 修正了偏离值计算逻辑:原来基于当日close计算偏离会导致目标价位随日内价格波动,改为基于前一日EMA计算后,目标价位全天稳定,更符合策略设计初衷。
  • 新增目标价位可视化:绘制ema_1x的点位线,方便直观观察价格与目标买入价的关系。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 13:04:59