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

求助:为Pine Script策略编写Take Profit与动态Stop Loss逻辑

Pine Script 止盈止损逻辑实现方案

以下是针对你需求的完整代码实现,已集成入场逻辑与止盈止损规则:

//@version=5
strategy("带自定义止盈止损的策略", overlay=true)

// --- 输入参数 ---
atrLength = input.int(14, "ATR周期")
// 替换为你的ATR Stop Loss Finder指标计算逻辑
atrValue = ta.atr(atrLength)

// --- 原入场逻辑(保留你的代码) ---
direction = input.int(0, "方向") // 假设你已有direction和buySignal/sellSignal的定义
buySignal = input.bool(true, "买入信号")
sellSignal = input.bool(false, "卖出信号")

Buy = direction < 0 and buySignal
plotshape(Buy, style = shape.triangleup, location=location.belowbar, size=size.small, color = color.green)

Sell = direction > 0 and sellSignal
plotshape(Sell, style = shape.triangledown, location=location.abovebar, size=size.small, color = color.red)

// --- 全局变量:跟踪多头仓位状态 ---
var float longEntryPrice = na
var float longTP1 = na
var float initialLongSL = na
var bool isLongTP1Hit = false
var float currentLongTrailSL = na
var float longTp1Amplitude = na

// --- 多头入场与初始参数记录 ---
longCondition = Buy
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)
    longEntryPrice = close
    // 替换为你的ATR Stop Loss Finder指标的前一根K线高点
    longTP1 = high[1] + atrValue 
    // 初始止损:前一根K线的ATR低点(替换为你的实际计算逻辑)
    initialLongSL = low[1] - atrValue 
    longTp1Amplitude = longTP1 - longEntryPrice // TP1相对于入场价的幅度
    isLongTP1Hit = false
    currentLongTrailSL = initialLongSL

// --- 多头TP1触发:平仓50%仓位并移动止损至入场价 ---
if (strategy.position_size > 0 and close >= longTP1 and not isLongTP1Hit)
    strategy.close("My Long Entry Id", qty_percent=50)
    isLongTP1Hit = true
    currentLongTrailSL = longEntryPrice

// --- 多头TP1后的动态止损调整 ---
if (isLongTP1Hit and strategy.position_size > 0)
    currentAmplitude = close - longEntryPrice
    multiple = math.floor(currentAmplitude / longTp1Amplitude)
    if (multiple >= 2)
        // 计算目标止损位:入场价 + (倍数-1)*TP1幅度/2
        targetSL = longEntryPrice + (multiple - 1) * longTp1Amplitude / 2
        // 仅向上移动止损(锁定利润)
        if (targetSL > currentLongTrailSL)
            currentLongTrailSL = targetSL

// --- 应用多头止损 ---
if (strategy.position_size > 0)
    strategy.exit("Long Exit", "My Long Entry Id", stop=currentLongTrailSL)

// --- 全局变量:跟踪空头仓位状态 ---
var float shortEntryPrice = na
var float shortTP1 = na
var float initialShortSL = na
var bool isShortTP1Hit = false
var float currentShortTrailSL = na
var float shortTp1Amplitude = na

// --- 空头入场与初始参数记录 ---
shortCondition = Sell
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)
    shortEntryPrice = close
    // 替换为你的ATR Stop Loss Finder指标的前一根K线低点
    shortTP1 = low[1] - atrValue 
    // 初始止损:前一根K线的ATR高点(替换为你的实际计算逻辑)
    initialShortSL = high[1] + atrValue 
    shortTp1Amplitude = shortEntryPrice - shortTP1 // TP1相对于入场价的幅度
    isShortTP1Hit = false
    currentShortTrailSL = initialShortSL

// --- 空头TP1触发:平仓50%仓位并移动止损至入场价 ---
if (strategy.position_size < 0 and close <= shortTP1 and not isShortTP1Hit)
    strategy.close("My Short Entry Id", qty_percent=50)
    isShortTP1Hit = true
    currentShortTrailSL = shortEntryPrice

// --- 空头TP1后的动态止损调整 ---
if (isShortTP1Hit and strategy.position_size < 0)
    currentAmplitude = shortEntryPrice - close
    multiple = math.floor(currentAmplitude / shortTp1Amplitude)
    if (multiple >= 2)
        // 计算目标止损位:入场价 - (倍数-1)*TP1幅度/2
        targetSL = shortEntryPrice - (multiple - 1) * shortTp1Amplitude / 2
        // 仅向下移动止损(锁定利润)
        if (targetSL < currentShortTrailSL)
            currentShortTrailSL = targetSL

// --- 应用空头止损 ---
if (strategy.position_size < 0)
    strategy.exit("Short Exit", "My Short Entry Id", stop=currentShortTrailSL)

关键说明:

  1. ATR Stop Loss Finder 替换:代码中longTP1和shortTP1的计算是基于标准ATR的示例,请替换为你实际使用的ATR Stop Loss Finder指标的前一根K线高低点值。
  2. 初始止损调整:initialLongSL和initialShortSL的逻辑同理,需根据你的指标规则修改。
  3. 动态止损规则:已实现“价格达到TP1的N倍幅度时,将止损移至入场价与(N-1)倍TP1幅度价的中间位置”的逻辑,确保利润逐步锁定。
  4. 部分平仓:通过strategy.close的qty_percent=50参数实现TP1触发时平仓一半仓位。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 20:30:56