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

Pine策略:两次多头平仓触发后strategy.opentrades仍显示1的异常问题

TradingView Pine策略异常:部分平仓后strategy.opentrades未归零

在TradingView的Pine策略中,我设置了两个各占50%仓位的多头平仓订单(一个止盈、一个追踪止损),但当两个订单都触发后,strategy.opentrades的值仍显示为1(预期应为0)。不过使用strategy.close_all()函数平仓时,不会出现这个问题。

以下是策略代码:

// 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 V2", process_orders_on_close=false, overlay=true, initial_capital=100, default_qty_value=1000, currency='USDT', default_qty_type=strategy.percent_of_equity, calc_on_every_tick = true, max_labels_count=500, use_bar_magnifier = true, margin_long = 1., commission_value = 0.07)
backtestStartDate = input.time(timestamp("12 Mar 2023 GMT+01:00"), title="Start Date", group="Backtest Time Period")
backtestEndDate = input.time(timestamp("13 Mar 2023 GMT+01:00"), title="End Date", group="Backtest Time Period")

// ### Four Smoothed Moving Averages

len1 = 21
//input(21, minval=1, title="Length 1", group = "Smoothed MA Inputs")
src1 = close
//input(close, title="Source 1", group = "Smoothed MA Inputs")
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
//input(50, minval=1, title="Length 2", group = "Smoothed MA Inputs")
src2 = close
//input(close, title="Source 2", group = "Smoothed MA Inputs")
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
//input(100, minval=1, title="Length 3", group = "Smoothed MA Inputs")
src3 = close
//input(close, title="Source 3", group = "Smoothed MA Inputs")
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
//input(200, minval=1, title="Length 4", group = "Smoothed MA Inputs")
src4 = close
//input(close, title="Source 4", group = "Smoothed MA Inputs")
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 condBuy = (ema2 > smma4 and trendFill) and close > smma1 and close > smma2 and close > smma3 and close > 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

var Limit_Below_Entry_Price = 0.0

// LONG TRADES
if inTradeWindow and condBuy and strategy.opentrades > 0 == false // and not inLong[1] // 
   
    strategy.entry("My Long Entry ", strategy.long, limit = close)

    strategy.exit("My Exit 1", from_entry = "My Long Entry ", profit=15000, qty_percent = 50) // 50*syminfo.mintick

    strategy.exit("My Exit 2", from_entry = "My Long Entry ", qty_percent = 50, trail_offset=10000, trail_points=15000)

Limit_Below_Entry_Price := float(strategy.opentrades.entry_price(strategy.opentrades - 1)) - 100

if close < Limit_Below_Entry_Price
    strategy.close_all()

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

异常原因解析

  • strategy.opentrades统计的是未完全平仓的交易条目(entry)数量,而非当前持仓的合约数量。当你通过两个strategy.exit对同一entry进行50%仓位的部分平仓时,虽然总仓位已归零,但策略引擎不会自动将该entry标记为“已关闭”——因为strategy.exit的设计是处理部分仓位退出,而非终止整个entry的持仓状态。
  • strategy.close_all()则会直接终止所有未平仓的entry,无论该entry是否有部分仓位已被平掉,因此会立即将strategy.opentrades置为0。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 05:43:08