Pine 脚本:如果 x -> 百分比停止 else -> ATR 停止
Pine Script: If x -> percentage stop else -> ATR stop
我正在尝试为我的百分比追踪止损添加一个触发器,同时为未触发追踪止损的时间设置 ATR 止损。请记住,我对编码还很陌生。
尾随触发是在慢速 MA 之上的快速 MA 和 ATR 止损的初始止损。触发器和初始停止的类型并不那么重要。问题是我不知道如何同时实现它们。可以像我在这里尝试的那样简单吗?止损单独工作,但同时使用时,它仅使用 ATR 止损(从不追踪止损)并且仅当快速 MA 低于慢速 MA(未满足触发条件)时才使用。当快速 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)
您可以切换 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)
不过我现在遇到了另一个问题。我无法让 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
0
我正在尝试为我的百分比追踪止损添加一个触发器,同时为未触发追踪止损的时间设置 ATR 止损。请记住,我对编码还很陌生。
尾随触发是在慢速 MA 之上的快速 MA 和 ATR 止损的初始止损。触发器和初始停止的类型并不那么重要。问题是我不知道如何同时实现它们。可以像我在这里尝试的那样简单吗?止损单独工作,但同时使用时,它仅使用 ATR 止损(从不追踪止损)并且仅当快速 MA 低于慢速 MA(未满足触发条件)时才使用。当快速 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)
您可以切换 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)
不过我现在遇到了另一个问题。我无法让 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
0