我正在尝试为我的百分比跟踪止损添加一个触发器,同时有一个ATR止损,用于未触发拖尾止损的时间。请记住,我对编码非常陌生。
拖尾的触发器是在慢速MA之上的快速MA和ATR停止的初始停止。触发器的类型和初始停止并不那么重要。问题是我想不出如何同时实现它们。它能像我在这里尝试的那样简单吗?停止在那里工作,但当同时使用时,它只使用ATR停止(从不尾随停止),并且仅当快速MA低于慢MA时(触发条件不满足)。当快速移动平均线高于慢移动平均线时,它永远不会触发停止,但如果我用ATR停止删除else语句,它就可以很好地工作。现在对我来说没什么意义。
如果您能提供一些指导,我们将不胜感激!
//Moving Average calculation and plotting
Not important
//ATR input
ATR = input(14, step=1, title='ATR periode')
LongstopMult = input(3, step=0.1, title='ATR Long Stop Multiplier')
//Trailing stop inputs
longTrailPerc = input(title="Trail Long Loss (%)",
type=float, minval=0.0, step=0.1, defval=4.6) * 0.01
// Determine trail stop loss prices
longStopPrice = 0.0
longStopPrice := if (strategy.position_size > 0)
stopValue = close * (1 - longTrailPerc)
max(stopValue, longStopPrice[1])
else
0
//Strategy entry conditions
XXX
// Submit exit orders for trail stop loss price
if (fastSMMA > slowSMMAlong)
strategy.exit(id="XL TRL STP1", stop=longStopPrice)
else
stop_level = low - (ATR * LongstopMult)
strategy.exit("TP/SL", stop=stop_level)发布于 2020-07-09 06:04:18
您可以切换longStopPrice值,只使用1个strategy.exit函数,如下所示。
fastSMMA = sma(close, 20)
slowSMMAlong = sma(close, 200)
//ATR input
ATR = input(14, step=1, title='ATR periode')
LongstopMult = input(3, step=0.1, title='ATR Long Stop Multiplier')
//Trailing stop inputs
longTrailPerc = input(title="Trail Long Loss (%)",
type=float, minval=0.0, step=0.1, defval=4.6) * 0.01
// Determine trail stop loss prices
longStopPrice = 0.0
if fastSMMA > slowSMMAlong
longStopPrice := if (strategy.position_size > 0)
stopValue = close * (1 - longTrailPerc)
max(stopValue, longStopPrice[1])
else
0
else
longStopPrice := low - (ATR * LongstopMult)
// debug
bgcolor(fastSMMA > slowSMMAlong ? red : na) // highlight the type of trigger
plot(longStopPrice, style = stepline, offset = 1) // print the exit line on the chart
//Strategy entry conditions // example entry
strategy.entry("enter long", true, 1, when = open > high[20])
// Submit exit orders for trail stop loss price
strategy.exit(id="XL TRL STP1", stop=longStopPrice)发布于 2020-07-11 23:00:07
不过,我现在有了另一个问题。我不能让TrailPerc在短裤上工作。我试着把所有的东西都改成相反的。
这是我唯一修改过的布局是"strategy.position_size < 0“。你会改变什么来让它在短裤上发挥作用?
longTrailPerc = input(title="Trail Long Loss (%)",
type=float, minval=0.0, step=0.1, defval=0.19) * 0.01
shortTrailPerc = input(title="Trail Short Loss (%)",
type=float, minval=0.0, step=0.1, defval=0.19) * 0.01
// Determine trail stop loss prices
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if (strategy.position_size > 0)
stopValue = low * (1 - longTrailPerc)
max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if (strategy.position_size < 0)
stopValue = high * (1 - shortTrailPerc)
max(stopValue, longStopPrice[1])
else
0https://stackoverflow.com/questions/62731398
复制相似问题