在 Pine 代码中使用以下语法来设置追踪止损:
// 计算追踪止损的价格
atr_value = atr(trailing_stop_atr)
trailing_stop_price = strategy.position_avg_price - atr_value*trailing_stop_mult
// 设置追踪止损
strategy.exit("Trailing Stop Loss", "Long", stop=trailing_stop_price)
在这个示例中,我们使用 ATR 值来计算追踪止损的价格,并将其保存在变量 trailing_stop_price
中。然后,在 strategy.exit()
函数中使用此价格来设置追踪止损。当价格下跌到止损价位时,仓位就会被平仓。
注意:为了避免未实现的部分利润被忽略,我们需要在每次取得部分利润时重新计算追踪止损的价格。