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

Pine Script策略求助:添加stop to breakeven与分批止盈出场逻辑

调整后Pine Script策略代码

核心修改点

  • 新增仓位状态标记,区分是否已完成首次50%止盈
  • 首次触发1×ATR止盈位时仅平仓50%仓位,同步将剩余持仓止损调整为入场价锁定盈亏平衡
  • 新增SSL通道反向交叉平仓逻辑,触发时平仓剩余全部持仓
  • 保留原策略所有指标计算、参数设置、绘图及信号标注逻辑不变
//@version=4
strategy("for jcolina1 v2 盈亏平衡版",overlay=true)
//Inputs------------------------------------------------------------------------
startDate        = input(timestamp("2020-07-30T00:00:00"), type = input.time, group="回测周期")
finishDate       = input(timestamp("2022-09-09T00:00:00"), type = input.time, group="回测周期")
profit_multiplier= input(1,title="止盈倍数", type=input.float)
stop_multiplier  = input(1.5, title="止损倍数", type=input.float)
level_offset     = input(5, type=input.integer, title="线长", group="价格水平标注") 
bearish_levels   = input(color.new(color.red,0), type=input.color,title="看跌线", inline ="0",group="价格水平标注" )
bearish_L_text   = input(color.new(color.purple,0), type=input.color,title="文字", inline="0", group="价格水平标注")
Bullish_levels   = input(color.new(color.green,0), type=input.color,title="看涨线", inline ="00",group="价格水平标注" )
Bullish_L_text   = input(color.new(color.yellow,0), type=input.color,title="文字", inline="00", group="价格水平标注")
ssl_len          = input(title="SSL通道长度", defval=10,inline="1",group="基础指标")
en_ssl           = input(true,title="启用", type=input.bool,inline="1",group="基础指标")
Up               = input(#66ff00, type=input.color,title="上升", inline="1",group="基础指标")
Dn               = input(#ff0000, type=input.color, title="下降", inline="1",group="基础指标")
kiju_len         = input(26, minval=1, title="基准线周期",type=input.integer,inline="2",group="基础指标")
en_ki            = input(true,title="启用", type=input.bool,inline="2",group="基础指标")
col_kijun        = input(color.white, type=input.color, title="颜色", inline="2", group="基础指标")
Sensitivity      = input(150, title="灵敏度", type=input.integer,group="Waddah attar爆炸指标")
Fast_Length      = input(20 , title="快周期", type=input.integer,group="Waddah attar爆炸指标")
Slow_Length      = input(40 , title="慢周期", type=input.integer,group="Waddah attar爆炸指标")
Channel_Length   = input(20 , title="通道周期",type=input.integer,group="Waddah attar爆炸指标")
Multi            = input(2.0, title="布林带倍数",type=input.float,group="Waddah attar爆炸指标")
// 新增仓位状态标记
var bool partial_exit_long = false
var bool partial_exit_short = false
//Computing required calculations-----------------------------------------------
smaHigh = sma(high, ssl_len)
smaLow = sma(low, ssl_len)
Hlv = int(na)
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh : smaLow //output
sslUp = Hlv < 0 ? smaLow : smaHigh  // output
donchian(d_len) =>
    avg(lowest(d_len), highest(d_len))
baseLine = donchian(kiju_len)
Calc_Macd(Source, Fast_Length, Slow_Length) =>
    Fast_Ma         =   ema(Source, Fast_Length)
    Slow_Ma         =   ema(Source, Slow_Length)
    Fast_Ma - Slow_Ma
Calc_Upper(Source, length, Multi) =>
    Basis           =   sma(Source, length)
    Dev             =   Multi * stdev(Source, length)
    Basis + Dev
Calc_Lower(Source, length, Multi) =>
    Basis           =   sma(Source, length)
    Dev = Multi * stdev(Source, length)
    Basis - Dev
t1  = (Calc_Macd(close, Fast_Length, Slow_Length) - Calc_Macd(close[1], Fast_Length, Slow_Length)) * Sensitivity
e1  = Calc_Upper(close, Channel_Length, Multi) - Calc_Lower(close, Channel_Length, Multi)
trendUp   =   t1 >= 0 ? t1 : 0
trendDown =   t1 < 0 ? -1 * t1 : 0
DEAD_ZONE =   nz(rma(tr(true),100)) * 3.7
atr = atr(14)
time_cond  = time >= startDate and time <= finishDate
// entry logic------------------------------------------------------------------
ssl_bul = crossover(sslUp,sslDown) , ssl_bear = crossunder(sslUp,sslDown)
kij_bul = (close > baseLine) , kij_bear = (close < baseLine)
wae_filter = ((trendUp > e1) or (trendDown > e1))
bullish = ((ssl_bul and kij_bul) and wae_filter)
bearish = ((ssl_bear and kij_bear) and wae_filter)
// computing required dynamic data for short
var float S_stop   = na 
var float S_entry  = na
var float S_target = na 
if ((bearish and barstate.isconfirmed) and time_cond)
    S_entry := close
    S_stop := (close + (atr * stop_multiplier))
    S_target := (close - (atr * profit_multiplier ))
    strategy.entry("Short", strategy.short)
    // 首次止盈仅平50%
    strategy.exit("S partial exit", "Short",limit = S_target, stop = S_stop, qty_percent=50)
    
    tt = (time - time[1]) * level_offset
    //stop
    line.new(x1=time, y1=S_stop,
         x2=time + tt , y2=S_stop , color=bearish_levels,
         xloc=xloc.bar_time,width=2)
    label.new(x=time +(time - time[1]), y=S_stop, text=tostring(S_stop)+" 空头止损",color=color.new(color.black,100), style=label.style_label_lower_left,textcolor=bearish_L_text,xloc=xloc.bar_time,textalign= text.align_left)
    //target
    line.new(x1=time, y1=S_target,
         x2=time + tt , y2=S_target , color=bearish_levels,
         xloc=xloc.bar_time,width=2)
    label.new(x=time +(time - time[1]) , y=S_target, text=tostring(S_target)+" 空头止盈",color=color.new(color.black,100), style=label.style_label_upper_left,textcolor=bearish_L_text,xloc=xloc.bar_time,textalign= text.align_left)

// computing required dynamic data for long
var float L_stop   = na 
var float L_entry  = na
var float L_target = na 
if ((bullish and barstate.isconfirmed) and time_cond)
    L_entry := close
    L_stop := (close - (atr * stop_multiplier))
    L_target := (close + (atr * profit_multiplier ))
    strategy.entry("Long", strategy.long)
    // 首次止盈仅平50%
    strategy.exit("L partial exit", "Long",limit = L_target, stop = L_stop, qty_percent=50)
    tt = (time - time[1]) * level_offset
    //stop
    line.new(x1=time, y1=L_stop,
         x2=time + tt , y2=L_stop , color=Bullish_levels,
         xloc=xloc.bar_time,width=2)
    label.new(x=time +(time - time[1]), y=L_stop, text=tostring(L_stop)+" 多头止损",color=color.new(color.black,100), style=label.style_label_upper_left,textcolor=Bullish_L_text,xloc=xloc.bar_time,textalign= text.align_left)
    //target
    line.new(x1=time, y1=L_target,
         x2=time + tt , y2=L_target , color=Bullish_levels,
         xloc=xloc.bar_time,width=2)
    label.new(x=time +(time - time[1]) , y=L_target, text=tostring(L_target)+" 多头止盈",color=color.new(color.black,100), style=label.style_label_lower_left,textcolor=Bullish_L_text,xloc=xloc.bar_time,textalign= text.align_left)

// 移动止损到盈亏平衡逻辑
if (strategy.position_size > 0 and not partial_exit_long and close >= L_target)
    partial_exit_long := true
    // 剩余仓位止损调整为入场价
    strategy.exit("L breakeven exit", "Long", stop = L_entry)

if (strategy.position_size < 0 and not partial_exit_short and close <= S_target)
    partial_exit_short := true
    // 剩余仓位止损调整为入场价
    strategy.exit("S breakeven exit", "Short", stop = S_entry)

// SSL反向交叉平仓剩余仓位
if (partial_exit_long and ssl_bear)
    strategy.close("Long")
    partial_exit_long := false

if (partial_exit_short and ssl_bul)
    strategy.close("Short")
    partial_exit_short := false

// 仓位清空后重置状态标记
if (strategy.position_size == 0)
    partial_exit_long := false
    partial_exit_short := false

// 信号标注 -------------------------------------------------
if (bullish and time_cond)
    lbl = label.new(bar_index, low,text = "做多" ,textcolor = color.white)
    label.set_color(lbl,color.green)
    label.set_yloc(lbl,yloc.belowbar)
    label.set_style(lbl,label.style_label_up)
if (bearish and time_cond)
    lbl = label.new(bar_index, low,text ="做空",textcolor = color.white)
    label.set_color(lbl,color.red)
    label.set_yloc(lbl,yloc.abovebar)
    label.set_style(lbl,label.style_label_down)
    
// 指标绘图------------------------------------------------------------------------
plot(en_ssl ? sslDown : na, linewidth=2, color=Up)
plot(en_ssl ? sslUp   : na, linewidth=2, color=Dn)
plot(en_ki ? baseLine : na, color=col_kijun, title="基准线")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 15:06:06