PineScript - ATR % 止损

PineScript - ATR % Stoploss

我想在策略 placed/detected 入场时添加 ATR 止损。到目前为止,我想出了这个脚本:

@version=4    
// Stop Loss inputs atr     
longLossPerc = input(title="Long Stop Loss (%)",type=input.float, minval=0.0, step=0.1, defval=1) * 0.01    
atrLength = input(title="ATR Length", type=input.integer, defval=6, minval=1)
userStructure = input(title="Use Structure", type=input.bool, defval=true)    
lookback = input(title="How far to look back for High/Low",type=input.integer, defval=7, minval=1)    
atrStopMultiplier = input(title="ATR x ? ", type=input.float, defval=1.0, minval=0.1)    
longStopPrice = strategy.position_avg_price * (1 - longLossPerc)

// calculate data atr    
atr=atr(atrLength)    
longStop = (userStructure ? lowest(low, lookback) : close) - atr * atrStopMultiplier    
shortStop = (userStructure ? highest(high,lookback) : close) + atr * atrStopMultiplier

// plot atr Long/Short    
plot(longStop, color=color.green, style=plot.style_linebr, title="Long Trailing Stop-ATR")    
plot(shortStop, color=color.red, style=plot.style_linebr, title="Short Trailing Stop-ATR")

我的问题是,我不知道如何将此脚本 relate/connect 添加到我的脚本参数中。我是否必须创建一个新变量,然后将其插入我的 strategy.close?

strategy.entry("LongA", strategy.long,1, when= x and y)    
strategy.close("LongA", when= z or t )

注意:x、y、z、t 是预定义变量。

您想使用 strategy.exit() 功能。它有 stoploss 参数。

loss (series int/float) An optional parameter. Stop loss (specified in ticks). If it is specified, a stop order is placed to exit market position when the specified amount of loss (in ticks) is reached. The default value is 'NaN'.

stop (series int/float) An optional parameter. Stop loss (requires a specific price). If it is specified, a stop order is placed to exit market position at the specified price (or worse). Priority of the parameter 'stop' is higher than priority of the parameter 'loss' ('stop' is used instead of 'loss', if its value is not 'NaN'). The default value is 'NaN'.

strategy.entry("LongA", strategy.long,1, when= x and y)
strategy.exit("Long Exit", "LongA", loss=longStop)