求助:Candle Wick/Shadow未触发平仓限制,如何实现影线越价平仓?
影线穿越目标价格时平仓的实现方案
核心逻辑是检测蜡烛的影线顶端/底端是否触及或穿越目标价格,再触发平仓指令。下面以TradingView的Pine Script为例,给出具体实现:
关键步骤
- 定义你的目标价格(对应图中的绿色区间带)
- 针对持仓方向,判断影线是否突破目标价
- 满足条件时执行平仓
代码示例
//@version=5 strategy("影线触发平仓策略", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000) // 替换为你的实际目标价格(示例用布林带上轨) targetPrice = ta.bb(close, 20, 2).upper // 多单入场逻辑(可根据你的策略修改) if ta.crossover(ta.sma(close, 5), ta.sma(close, 20)) strategy.entry("多单", strategy.long) // 多单平仓:上影线突破目标价格时平仓 if strategy.position_size > 0 and high >= targetPrice strategy.close("多单") // 空单入场逻辑(可根据你的策略修改) if ta.crossunder(ta.sma(close, 5), ta.sma(close, 20)) strategy.entry("空单", strategy.short) // 空单平仓:下影线跌破目标价格时平仓 if strategy.position_size < 0 and low <= targetPrice strategy.close("空单") // 绘制目标价格线 plot(targetPrice, color=color.green, title="目标价格")
补充说明
- 若要避免假突破,可加入收盘确认条件,比如多单平仓改成:
strategy.position_size > 0 and high >= targetPrice and close < targetPrice(即影线突破后,蜡烛收盘回落到目标价下方) - 其他交易平台(如MT4/MT5)逻辑一致:获取K线的
high/low值与目标价对比,调用平台自带的平仓函数即可 targetPrice可替换为你实际使用的区间带、支撑阻力线等指标
内容的提问来源于stack exchange,提问作者marcq
相关产品推荐
相关产品推荐

