Pine script Pinescript(tradingview)停止丢失未在回测中触发

Pine script Pinescript(tradingview)停止丢失未在回测中触发,pine-script,Pine Script,我有一个策略,当1个条件达到或SL被击中时应该卖出,但我的回溯测试表明,即使SL被击中,交易仍会继续 //@version=4 strategy(title="Extreme highs and lows", calc_on_order_fills=true,calc_on_every_tick =true, initial_capital=21000,commission_value=.1,overlay = true,default_qty_type = strategy.percent_o

我有一个策略,当1个条件达到或SL被击中时应该卖出,但我的回溯测试表明,即使SL被击中,交易仍会继续

//@version=4
strategy(title="Extreme highs and lows", calc_on_order_fills=true,calc_on_every_tick =true, initial_capital=21000,commission_value=.1,overlay = true,default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

// Backtest Input
FromYear = input(2015, "Backtest Start Year")
FromMonth = input(1, "Backtest Start Month")
FromDay = input(1, "Backtest Start Day")
ToYear = input(2999, "Backtest End Year")
ToMonth = input(1, "Backtest End Month")
ToDay = input(1, "Backtest End Day")

// Backtest Setting
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
window()  => time >= start and time <= finish ? true : false 


// Input options
hiLen = input(title="High Length", type=input.integer, defval=20)
loLen = input(title="Low Length", type=input.integer, defval=20)

// SL input
SL =  input(defval=2.0, title='Stop Loss %', type=input.float)

// Stop loss setting
StopLossLong = strategy.position_avg_price * (1 - SL/100)
StopLossShort = strategy.position_avg_price * (1 + SL/100)

// Calculate values
hiHighs = highest(high, hiLen)[1]
loLows = lowest(low, loLen)[2]

// Plot values on the chart
plot(series=hiHighs, color=color.green, linewidth=2)
plot(series=loLows, color=color.red, linewidth=2)

// Highlight new highs and lows with
// a coloured background
bgColour = high > hiHighs ? color.teal : low < loLows ? color.maroon : na

bgcolor(color=bgColour, transp=90)

//Trade condition
long= close > hiHighs
short= close < loLows
// StopLoss Long Signal
SLLongCondition = crossunder(close, StopLossLong)
// StopLoss Short Signal
SLShortCondition = crossover(close, StopLossShort)

// Strategy
strategy.entry("long", strategy.long, when = long and window())
strategy.close("long", when = short or SLLonCondition and window())
strategy.close("long", when = SLLongCondition and window())

// Plot Stoploss
conditionLong = barssince(long)
plot(conditionLong ? StopLossLong : na, color=color.red, style=plot.style_linebr, linewidth=2)
/@version=4
策略(title=“极端高点和低点”,订单上的计算填充=真,每勾选一次计算=真,初始资本=21000,佣金值=0.1,叠加=真,默认数量类型=策略股权百分比,默认数量值=100)
//回溯测试输入
FromYear=输入(2015年,“回溯测试开始年份”)
FromMonth=输入(1,“回溯测试开始月份”)
FromDay=输入(1,“回溯测试开始日”)
ToYear=输入(2999,“回溯测试结束年份”)
ToMonth=输入(1,“回溯测试月末”)
今天=输入(1,“回溯测试结束日”)
//回测设置
开始=时间戳(FromYear、FromMonth、FromDay、00、00)//反向测试开始窗口
finish=时间戳(今年,明天,今天,23,59)//回溯测试完成窗口
window()=>时间>=开始和时间上限?颜色.蓝绿色:低<低?颜色:褐红色:na
bgcolor(颜色=bgcolor,传输=90)
//贸易条件
长=关闭>高
短=关闭

我也试过不用那一行:strategy.close(“long”,when=SLLongCondition and window())

请检查第56行的拼写--“short或SLLonCondition and window()”…您拼写错误--“g”在“short或SLLon..g..dition and window()中不存在----更正拼写,它将很好地工作

您是否也可以添加符号名称和测试它的时间范围。还有一个屏幕截图,你可以显示它在哪里触发了,但它没有。你能提供一个前后的代码片段来突出区别吗?现在很难理解解决方案是什么请给出您的答案以改进代码格式。看见