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

Pine Script如何实现吞没蜡烛突破入场、止损止盈及追踪止损功能

Pine Script 看涨吞没突破交易策略实现方案

实现思路

  • 第一步完成看涨吞没形态的基础识别,锁定对应K线的高点、低点两个核心参考价位
  • 仅在吞没形态出现后的第一根K线周期内判断入场:价格向上突破吞没高点 × (1 + 0.1%)时触发多单入场,该K线走完未满足突破条件则直接作废本次信号,后续不再基于该吞没形态入场
  • 初始止损设置为吞没低点 × (1 - 0.1%),根据入场价与初始止损的价差,计算2倍盈亏比对应的止盈价位
  • 持仓阶段实时判断收盘价与10 EMA的关系,收盘价跌破10 EMA时触发移动止损,将止损位调整为当前K线最低价下方0.1%位置

完整可运行代码

//@version=5
strategy("看涨吞没突破交易策略", overlay=true, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=1000, commission_type=strategy.commission.percent, commission_value=0.1)

// 可调整参数设置
break_threshold = input.float(0.1, "突破阈值(%)", minval=0.01)
stop_loss_threshold = input.float(0.1, "止损阈值(%)", minval=0.01)
rr_ratio = input.float(2.0, "盈亏比", minval=1.0)
ema_length = input.int(10, "追踪止损EMA长度", minval=1)

// 计算基础指标
ema10 = ta.ema(close, ema_length)
plot(ema10, color=color.new(color.blue, 0), title="10 EMA", linewidth=2)

// 识别看涨吞没形态
bullish_engulfing = ta.crossover(close, open[1]) and close[1] < open[1] and close > open[1] and open < close[1]

// 存储吞没K线关键价位、入场等待标记
var float engulf_high = na
var float engulf_low = na
var bool wait_for_entry = false

if bullish_engulfing
    engulf_high := high
    engulf_low := low
    wait_for_entry := true

// 入场条件判断:仅吞没后第一根K线生效
var float entry_price = na
var float init_stop = na
var float take_profit = na

if wait_for_entry and not bullish_engulfing
    // 满足突破条件开仓
    if high >= engulf_high * (1 + break_threshold / 100)
        entry_price := engulf_high * (1 + break_threshold / 100)
        init_stop := engulf_low * (1 - stop_loss_threshold / 100)
        take_profit := entry_price + (entry_price - init_stop) * rr_ratio
        strategy.entry("Long", strategy.long, limit=entry_price)
        wait_for_entry := false
    // 未突破则作废信号
    else
        wait_for_entry := false

// 止盈止损+追踪止损逻辑
if strategy.position_size > 0
    // 收盘价低于10 EMA触发移动止损
    if close < ema10
        strategy.exit("Exit", "Long", stop=low * (1 - stop_loss_threshold / 100), limit=take_profit)
    // 未触发追踪时使用初始止盈止损
    else
        strategy.exit("Exit", "Long", stop=init_stop, limit=take_profit)

// 标记看涨吞没信号
plotshape(bullish_engulfing, title="看涨吞没", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, text="吞没", textcolor=color.white, size=size.small)

代码说明

  • 所有参数均支持自定义调整,可根据交易品种的波动特性修改突破阈值、止损阈值、盈亏比、EMA周期等参数
  • 严格限制入场时间窗口,仅吞没形态后的第一根K线满足条件才开仓,避免滞后信号干扰
  • 追踪止损仅在收盘价跌破10 EMA时触发,未触发前保持初始止损与止盈规则,完全匹配需求逻辑
  • 内置仓位、手续费默认配置,可根据自身交易需求修改

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 13:48:03