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

Pine Script策略使用价格条件时无法生成及平仓订单

问题原因分析

你的代码核心问题出在止损止盈价格的定义逻辑:

  • 使用var声明slPrice和tpPrice,var关键字会让变量仅在脚本初始化时赋值一次,后续K线不会更新数值。
  • 初始无持仓时strategy.position_avg_price为na,导致slPrice和tpPrice一直处于无效状态,价格对比条件close <= slPrice和close >= tpPrice永远不成立,自然无法触发平仓。
修复后的完整代码
//@version=5
strategy('SuperTrend STRATEGY', overlay=true)
Periods = input(title='ATR Period', defval=10)
src = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3.0)
changeATR = input(title='Change ATR Calculation Method ?', defval=true)
showsignals = input(title='Show Buy/Sell Signals ?', defval=false)
highlighting = input(title='Highlighter On/Off ?', defval=true)
barcoloring = input(title='Bar Coloring On/Off ?', defval=true)
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0))
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0))
plotshape(buySignal and showsignals ? up : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0))
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0))
plotshape(sellSignal and showsignals ? dn : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
mPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? trend == 1 ? color.green : color.white : color.white
shortFillColor = highlighting ? trend == -1 ? color.red : color.white : color.white
fill(mPlot, upPlot, title='UpTrend Highligter', color=longFillColor, transp=90)
fill(mPlot, dnPlot, title='DownTrend Highligter', color=shortFillColor, transp=90)
FromMonth = input.int(defval=9, title='From Month', minval=1, maxval=12)
FromDay = input.int(defval=1, title='From Day', minval=1, maxval=31)
FromYear = input.int(defval=2018, title='From Year', minval=999)
ToMonth = input.int(defval=1, title='To Month', minval=1, maxval=12)
ToDay = input.int(defval=1, title='To Day', minval=1, maxval=31)
ToYear = input.int(defval=9999, title='To Year', minval=999)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
window() =>
    time >= start and time <= finish ? true : false
longCondition = buySignal

// 记录入场价,仅在持仓时更新
var float entryPrice = na

if longCondition
    if strategy.opentrades == 0
        strategy.entry('buy', strategy.long, when=window())
        // 入场成功后记录当前持仓均价作为入场价
        entryPrice := strategy.position_avg_price

shortCondition = sellSignal
//if (shortCondition)
    //strategy.entry("SELL", strategy.short, when = window())
buy1 = ta.barssince(buySignal)
sell1 = ta.barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)

// 计算止损止盈价格(每次K线都重新计算,确保值正确)
slPrice = entryPrice * 0.95
tpPrice = entryPrice * 1.1

// 平仓条件:必须有持仓,且价格触发止损/止盈
if strategy.position_size > 0
    if close <= slPrice
        strategy.close('buy', comment='SL')
    else if close >= tpPrice
        strategy.close('buy', comment='TP')
关键修复点
  • 移除var关键字:slPrice和tpPrice不需要用var修饰,每次K线都根据entryPrice重新计算,确保价格实时有效。
  • 新增entryPrice变量:用var声明保证只初始化一次,在入场时更新为持仓均价,避免无持仓时的na值干扰。
  • 增加持仓检查:在平仓条件前判断strategy.position_size > 0,避免无持仓时执行无效平仓逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 09:57:19