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

Pine Script问题:为何追踪止损在每根K线都触发?

Pine Script 策略重复开仓与追踪止损失效修复

问题核心

你的策略出现每根K线重复开平仓、追踪止损不生效的问题,主要原因有两点:

  • 开仓条件未做首次触发限制,只要满足条件且无持仓,每根K线都会重复执行开仓
  • 追踪止损参数单位理解错误,且strategy.exit与开仓逻辑绑定,导致每次开仓覆盖止损设置

修复后的完整代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © me
//@version=5
strategy("Just Trend", process_orders_on_close=true, overlay=true, initial_capital=50, default_qty_value = 14, currency='USDT', default_qty_type=strategy.cash, calc_on_every_tick = false, max_labels_count=500)
backtestStartDate = input.time(timestamp("31 Jan 2023"), title="Start Date", group="Backtest Time Period")
backtestEndDate = input.time(timestamp("27 Mar 2023"), title="End Date", group="Backtest Time Period")

// ### Four Smoothed Moving Averages
len1 = 21
src1 = close
smma1 = 0.0
sma_1 = ta.sma(src1, len1)
smma1 := na(smma1[1]) ? sma_1 : (smma1[1] * (len1 - 1) + src1) / len1
plot(smma1, color=color.white, linewidth=2, title="21 SMMA")

len2 = 50
src2 = close
smma2 = 0.0
sma_2 = ta.sma(src2, len2)
smma2 := na(smma2[1]) ? sma_2 : (smma2[1] * (len2 - 1) + src2) / len2
plot(smma2, color=color.new(#6aff00,0), linewidth=2, title="50 SMMA")

h100 = input.bool(title="Show 100 Line", defval=true, group = "Smoothed MA Inputs")
len3 = 100
src3 = close
smma3 = 0.0
sma_3 = ta.sma(src3, len3)
smma3 := na(smma3[1]) ? sma_3 : (smma3[1] * (len3 - 1) + src3) / len3
sma3plot = plot(h100 ? smma3 : na, color=color.new(color.yellow,0), linewidth=2, title="100 SMMA")

len4 = 200
src4 = close
smma4 = 0.0
sma_4 = ta.sma(src4, len4)
smma4 := na(smma4[1]) ? sma_4 : (smma4[1] * (len4 - 1) + src4) / len4
sma4plot = plot(smma4, color=color.new(#ff0500,0), linewidth=2, title="200 SMMA")

// Trend Fill
trendFill = input.bool(title="Show Trend Fill", defval=true, group = "Smoothed MA Inputs") 
ema2 = ta.ema(close, 2)
ema2plot = plot(ema2, color=color.new(#2ecc71, 50), style=plot.style_line, linewidth=1, title="EMA(2)", editable = false)

bool condBuy = (ema2 > smma4 and trendFill) and close > smma1 and smma1 > smma2 and smma2 > smma3 and smma3 > smma4
bool condSell = close < smma1 or close < smma2 or close < smma3 or close < smma4

fill(ema2plot, sma4plot, color=  ema2 > smma4 and trendFill ? color.new(color.green, 50) : ema2 < smma4 and trendFill ? color.new(color.red, 50) : na, title = "Trend Fill")
// End ###

inTradeWindow = time > backtestStartDate and time <= backtestEndDate
leverage = input.int(125, title='Leverage',minval = 1, maxval = 300 ,step = 1)
precision = input.int(1, title='Precision')

// 新增变量:记录开仓条件是否已触发,避免重复开仓
var bool longConditionTriggered = false

var table tabb = table.new(position.top_center, 4, 3)
print(txt1, txt2, txt3, txt4) =>
    table.cell(tabb, 0, 0, str.tostring(txt1), bgcolor = color.green)
    table.cell(tabb, 1, 0, str.tostring(txt2), bgcolor = color.orange)
    table.cell(tabb, 2, 0, str.tostring(txt3), bgcolor = color.blue)
    table.cell(tabb, 3, 0, str.tostring(txt4), bgcolor = color.yellow)

RoundDown(number, decimals) =>
    factor = math.pow(10, decimals)
    math.floor(number * factor) / factor

transactionQty = math.max(RoundDown(14 * leverage  / close, 2), 0)

// LONG TRADES
// 仅当条件从false变为true,且无持仓时开仓
if inTradeWindow and condBuy and not condBuy[1] and strategy.opentrades == 0
    strategy.entry("Enter Long", strategy.long, qty=transactionQty)
    longConditionTriggered := true

// 持仓期间持续生效追踪止损,避免每次开仓覆盖
if strategy.position_size > 0
    // 注意:trail_points单位是"点",需根据品种调整,比如BTC/USDT 1点=0.01,500点=5USDT
    strategy.exit("Exit Long", from_entry="Enter Long", trail_points=500, trail_offset=500)

// 当卖出条件触发时重置开仓标记
if condSell
    longConditionTriggered := false

if bar_index % 2 == 0 and inTradeWindow
    a = str.tostring(strategy.opentrades)
    label.new(bar_index, na, a, yloc = yloc.abovebar, size = size.normal,textcolor = color.white)

关键修改说明

  1. 避免重复开仓:新增var bool longConditionTriggered变量,且开仓逻辑改为condBuy and not condBuy[1],仅在条件首次触发(从假变真)时执行开仓
  2. 修复追踪止损:将strategy.exit移到独立的if strategy.position_size > 0块中,确保持仓期间止损逻辑持续生效,不会被重复开仓覆盖
  3. 参数单位提示:明确trail_points的单位是「点」,不同品种点值不同,需根据交易标的调整数值(比如BTC/USDT的1点对应0.01 USDT)
  4. 状态同步:当卖出条件condSell触发时,重置开仓标记,确保后续符合条件时能重新开仓

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:14:58