Pine 脚本获取某个位置的柱数

Pine Script getting the number of bars in a position

我正在尝试根据入场时的摆动低点设置静态止损。但是当我尝试以下操作时,止损随着每个柱线不断变化,因为有新的最低点

SwingLowBars=20

longStop = lowest(low, SwingLowBars)[1]

longTake = strategy.position_avg_price + ((strategy.position_avg_price-longStop)*3)

我想要一个函数,在该位置上的每个新蜡烛不断向 SwingLowBars 变量添加 +1,以便 longTake 保持静态并且当最低柱超过 20 柱时不会改变

您可以在“var”变量中记住 SL 值:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=4
strategy("My Strategy", overlay=true)

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

SwingLowBars=20
longStop = lowest(low, SwingLowBars)[1]
var sl = float(na)
if strategy.position_size !=0 and strategy.position_size[1] == 0
    sl := strategy.position_avg_price - ((strategy.position_avg_price-longStop)*3)
    
strategy.exit("x", stop = sl)