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

基于ATR的固定止损线设置问题求助(Pine Script)

问题解决:固定ATR止损线不再随重复信号移动

问题根源

你代码中更新固定止损/止盈线的条件为nz(longcondition[1]),该条件会在每一次前K线触发多单信号时更新止损线。即便pyramiding=0限制了不能加仓,持有仓位期间longcondition仍可能再次满足,导致已设置的固定止损线被覆盖,出现非预期的移动。

解决方案

仅在**策略从无仓位变为持有多单(实际建仓)**的时刻,才计算并保存基于当时ATR的固定止损和止盈线。通过判断仓位状态的变化来精准捕捉建仓时机,避免重复更新。同时将止损线设置为虚线样式,匹配你的需求。

修改后的完整代码

//@version=5
strategy("strategy learning", overlay=true, initial_capital=1000, commission_value = 0.2, pyramiding = 0)

var GRP0 = "------------------------------ ATR  ------------------------------"

// Input for ATR Period
atrPeriod = input.int(25, "ATR Period", group = GRP0)

// Input for ATR Multiplier
atrMultiplier = input.float(2.0, "ATR Multiplier")

var GRP1 = "------------------------------ EMA  ------------------------------"
// Draw 2 EMA lines.
len1 = input(50, "EMA 1", group =GRP1)
len2 = input(200, "EMA 2", group =GRP1)
ema1 = ta.ema(close, len1)
ema2 = ta.ema(close, len2)
plot(ema1, color=color.yellow, linewidth = 2)
plot(ema2, color=color.blue, linewidth = 2)

// Define ATR Band Upline and Bottomline.
atr = ta.atr(atrPeriod)
bottomline = open - atrMultiplier * atr
atr_risk = atrMultiplier * atr
plot(bottomline, color=color.white)

var GRP2 = "------------------------------ STOCH RSI  ------------------------------"
smoothK = input.int(3, "K", minval=1, group = GRP2)
smoothD = input.int(3, "D", minval=1, group = GRP2)
lengthRSI = input.int(14, "RSI Length", minval=1, group = GRP2)
lengthStoch = input.int(14, "Stochastic Length", minval=1, group = GRP2)
src = input(close, title="RSI Source", group = GRP2)
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
crossup = ta.crossover(k,d)

//condition long
xx = close < ema1
xx2 = close[1] > ema1
xxx = ema1 > ema2
xy = close > ema2
xxy = xx and xxx and xy and xx2
plotshape(xxy, location= location.belowbar, style =shape.triangleup, color=color.green, size=size.normal)

//LONG
// Check if current crossup is higher than previous crossup
prev_k = ta.valuewhen(crossup, k, 1)
prev_d = ta.valuewhen(crossup, d, 1)
crossup_highest = k > prev_k and d > prev_d

longcondition = xxy and crossup_highest

if(longcondition) 
    strategy.entry("buy", strategy.long, qty=1)

var GRP3 = "--------------------------MULTIPLIER--------------------------"
m = input(3, "take profit multiplier", group = GRP3)
upline = bottomline + m * atr_risk
var float fixedBottonline = 0
var float fixedUpline = 0

// 仅在实际建仓(仓位从0变为1)时更新固定止损/止盈线
isNewLong = strategy.position_size[1] == 0 and strategy.position_size > 0
if isNewLong
    fixedUpline := upline
    fixedBottonline := bottomline
    strategy.exit("exitBuy", "buy", limit=fixedUpline, stop=fixedBottonline)

longEnterPrice = strategy.opentrades.entry_price(0)

p1 = plot(strategy.position_size > 0 ? fixedUpline : na, color = color.green, style = plot.style_linebr,linewidth = 2, title="Long Profit Line")
// 设置止损线为红色虚线样式
p3 = plot(strategy.position_size > 0 ? fixedBottonline : na, color=color.red, style=plot.style_dashed, linewidth=2, title="Long Stop Loss Line")
p2 = plot(strategy.position_size > 0 ? longEnterPrice : na, color=color.white, style=plot.style_linebr, linewidth = 2, title="Long Entry Line")

fill(p1,p2, color= color.new(color.green, 80), title="Long Profit Background")
fill(p2,p3, color= color.new(color.red,   80), title="Short Stoploss Background") 

修改说明

  1. 精准建仓判断:新增isNewLong变量,仅当策略从空仓变为持有多单时才更新固定止损/止盈线,彻底避免重复信号覆盖已设置的止损线。
  2. 虚线样式设置:将止损线的style参数改为plot.style_dashed,实现需求中的红色虚线效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 04:10:34