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

请求为Kozlod通道突破预警Pine Script添加止损及追踪止损

Adding Stop Loss & Trailing Stop to Kozlod Channel Break Out Indicator

Hey there! I've modified the original Pine Script to include both fixed stop loss and trailing stop functionality. This will help you lock in profits and limit downside risk as requested.

Modified Code

//@version=3
study("Kozlod - Channel Break Out Alerts with Stop Loss & Trailing Stop", overlay=true)
//
// author: Kozlod (modified with stop loss/trailing stop)
// date: 2018-03-16 (updated with risk management)
//
// Params
length = input(title="Length", type=integer, minval=1, maxval=1000, defval=5)
stop_loss_points = input(title="Stop Loss (Points)", type=integer, minval=1, defval=10, tooltip="Fixed stop loss in instrument points")
trailing_stop_points = input(title="Trailing Stop (Points)", type=integer, minval=1, defval=15, tooltip="Trailing stop distance in instrument points")

// Calculate up/down bound
upBound = highest(high, length)
downBound = lowest(low, length)

// Initial signals
up = upBound > upBound[1]
down = downBound < downBound[1]

// Filter out signals if opposite signal is also on
up_filt = up and not down
down_filt = down and not up

// Filter out consecutive signals
prev = 0
prev := up_filt ? 1 : down_filt ? -1 : prev[1]
up_final = up_filt and prev[1] == -1
down_final = down_filt and prev[1] == 1

// --- Risk Management Variables ---
var in_long = false
var in_short = false
var entry_price = 0.0
var trail_stop_price = 0.0

// Update entry price and reset stop on new signals
if up_final
    in_long := true
    in_short := false
    entry_price := close
    trail_stop_price := close - trailing_stop_points

if down_final
    in_short := true
    in_long := false
    entry_price := close
    trail_stop_price := close + trailing_stop_points

// Calculate stop loss levels
long_stop_loss = entry_price - stop_loss_points
short_stop_loss = entry_price + stop_loss_points

// Trailing stop adjustment for long positions
if in_long
    // Update trailing stop if price moves in favor
    if close > trail_stop_price + trailing_stop_points
        trail_stop_price := close - trailing_stop_points
    // Check for stop loss or trailing stop hit
    if close <= long_stop_loss or close <= trail_stop_price
        in_long := false
        entry_price := na
        trail_stop_price := na

// Trailing stop adjustment for short positions
if in_short
    // Update trailing stop if price moves in favor
    if close < trail_stop_price - trailing_stop_points
        trail_stop_price := close + trailing_stop_points
    // Check for stop loss or trailing stop hit
    if close >= short_stop_loss or close >= trail_stop_price
        in_short := false
        entry_price := na
        trail_stop_price := na

// --- PLOTTING ---
plot(upBound, color = green)
plot(downBound, color = red)

// Entry signals
plotshape(up_final, style = shape.arrowup, color = blue, location = location.belowbar, size = size.small)
plotshape(down_final, style = shape.arrowdown, color = red, location = location.abovebar, size = size.small)

// Stop loss and trailing stop plots
plot(in_long ? long_stop_loss : na, color = orange, style = style.dashed, title="Long Stop Loss")
plot(in_long ? trail_stop_price : na, color = purple, style = style.dashed, title="Long Trailing Stop")
plot(in_short ? short_stop_loss : na, color = orange, style = style.dashed, title="Short Stop Loss")
plot(in_short ? trail_stop_price : na, color = purple, style = style.dashed, title="Short Trailing Stop")

// Exit signals (stop loss/trailing stop hit)
plotshape(in_long[1] and not in_long, style = shape.xcross, color = orange, location = location.abovebar, size = size.small, title="Long Exit")
plotshape(in_short[1] and not in_short, style = shape.xcross, color = orange, location = location.belowbar, size = size.small, title="Short Exit")

// Create custom alerts
alertcondition(up_final, "Long Entry", "Channel Breakout Long Entry")
alertcondition(down_final, "Short Entry", "Channel Breakout Short Entry")
alertcondition(in_long[1] and not in_long, "Long Exit (Stop/Trail)", "Long Position Closed via Stop Loss or Trailing Stop")
alertcondition(in_short[1] and not in_short, "Short Exit (Stop/Trail)", "Short Position Closed via Stop Loss or Trailing Stop")

Key Changes Explained

  • Added Risk Parameters: You can adjust stop_loss_points (fixed distance below/above entry for long/short) and trailing_stop_points (distance that follows price as it moves in your favor) directly from the settings panel.
  • Position Tracking: Variables in_long and in_short keep track of active positions. entry_price stores your entry level, while trail_stop_price updates dynamically as price moves.
  • Trailing Stop Logic: For long positions, the trailing stop moves upward when price rises by the specified trailing distance. For shorts, it moves downward when price falls by that distance—locking in profits as the trend continues.
  • Exit Conditions: Positions automatically close if either the fixed stop loss is hit (limiting initial risk) or the trailing stop is triggered (protecting accumulated profits).
  • Visualization: Added dashed lines for stop loss and trailing stop levels, plus cross markers to clearly show when positions exit.
  • Alerts: Added exit alerts so you get notified immediately when a stop loss or trailing stop is triggered.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 20:07:55