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

Pine Script策略ATR止盈止损未触发及自定义仓位管理咨询

问题1:止盈止损点位未触发的解决方法

你对strategy.exit的理解是正确的,该函数本身支持固定价位平仓,点位不触发是你的代码逻辑存在问题:当前你定义的long_sl、long_tp1等点位会随每根K线的最新ATR、收盘价重新计算,等于每次调用strategy.exit都在更新止盈止损价位,自然无法触发开仓时预设的点位。
解决方法:在开仓信号触发的那一刻,将当时计算的SL、TP值固定存储,后续调用exit时统一使用这个固定值即可,可以通过ta.valuewhen函数实现。另外如果要实现三个点位分批平仓,需要分别创建三个exit订单,每个订单平1/3的仓位即可。

问题2:自定义仓位规模的实现方式

strategy.position_size是只读变量,仅用来读取当前策略的持仓大小,无法通过修改它自定义仓位。你只需要直接在strategy.entry函数的qty参数中传入你计算好的仓位数值即可,不管是按风险比例、固定手数还是其他规则计算的仓位,都可以直接在这里传入。


修改后可直接运行的完整代码

//@version=5
strategy("Test Strategy", overlay = true, initial_capital = 10000, default_qty_type = strategy.cash)

// ———————————————————— Inputs {
atrLengthInput = input.int(14, "ATR Length")
slAtrMultiplierInput = input.float(1.5, "SL ATR Multiplier", step = 0.1)
tp1AtrMultiplierInput = input.float(1.5, "TP1 ATR Multiplier", step = 0.1)
tp2AtrMultiplierInput = input.float(2.5, "TP2 ATR Multiplier", step = 0.1)
tp3AtrMultiplierInput = input.float(3.5, "TP3 ATR Multiplier", step = 0.1)
riskPerTradePct = input.float(1, "单笔交易风险占总资金比例%", step = 0.1)
// }

// ———————————————————— Calculations {
atr = ta.atr(atrLengthInput)

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))

// 固定开仓时的点位,不随后续K线变动
entryPriceLong = ta.valuewhen(longCondition, close, 0)
atrOnEntryLong = ta.valuewhen(longCondition, atr, 0)
long_sl = entryPriceLong - atrOnEntryLong * slAtrMultiplierInput
long_tp1 = entryPriceLong + atrOnEntryLong * tp1AtrMultiplierInput
long_tp2 = entryPriceLong + atrOnEntryLong * tp2AtrMultiplierInput
long_tp3 = entryPriceLong + atrOnEntryLong * tp3AtrMultiplierInput

entryPriceShort = ta.valuewhen(shortCondition, close, 0)
atrOnEntryShort = ta.valuewhen(shortCondition, atr, 0)
short_sl = entryPriceShort + atrOnEntryShort * slAtrMultiplierInput
short_tp1 = entryPriceShort - atrOnEntryShort * tp1AtrMultiplierInput
short_tp2 = entryPriceShort - atrOnEntryShort * tp2AtrMultiplierInput
short_tp3 = entryPriceShort - atrOnEntryShort * tp3AtrMultiplierInput

// 自定义仓位计算示例:按单笔风险比例计算仓位,可自行修改规则
positionSize = strategy.equity * (riskPerTradePct / 100) / (atr * slAtrMultiplierInput)
positionSize := math.max(positionSize, 1) // 最低1手,可根据交易标的调整
// }

// ———————————————————— Plots {
plot(ta.valuewhen(longCondition, long_sl, 0), color = color.red, style = plot.style_linebr)
plot(ta.valuewhen(longCondition, long_tp1, 0), color = color.gray, style = plot.style_linebr)
plot(ta.valuewhen(longCondition, long_tp2, 0), color = color.gray, style = plot.style_linebr)
plot(ta.valuewhen(longCondition, long_tp3, 0), color = color.gray, style = plot.style_linebr)
// }

// ———————————————————— Strategy calls {
if (longCondition)
    strategy.entry("BUY", strategy.long, qty = positionSize)
    // 分三次平仓,每次平1/3仓位,共用同一个止损位
    strategy.exit("EXIT BUY TP1", "BUY", stop = long_sl, limit = long_tp1, qty = strategy.position_size / 3)
    strategy.exit("EXIT BUY TP2", "BUY", stop = long_sl, limit = long_tp2, qty = strategy.position_size / 3)
    strategy.exit("EXIT BUY TP3", "BUY", stop = long_sl, limit = long_tp3, qty = strategy.position_size / 3)

if (shortCondition)
    strategy.entry("SELL", strategy.short, qty = positionSize)
    strategy.exit("EXIT SELL TP1", "SELL", stop = short_sl, limit = short_tp1, qty = math.abs(strategy.position_size) / 3)
    strategy.exit("EXIT SELL TP2", "SELL", stop = short_sl, limit = short_tp2, qty = math.abs(strategy.position_size) / 3)
    strategy.exit("EXIT SELL TP3", "SELL", stop = short_sl, limit = short_tp3, qty = math.abs(strategy.position_size) / 3)
// }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 19:18:03