松木最低,可变动态长度

Pine lowest low with variable, dynamic length

我想找到多头头寸交易入场和出场之间的最低价。因此,最低点具有动态长度,但 Pine 给了我一些错误: 为了保持一致性,应该在每次计算时调用函数 'ta.lowest'。建议从三元运算符或范围中提取调用。 图表本身无法绘制。错误消息引用 max_bars_back,这没有多大意义。

这是我的代码摘录:

var maeLong         = 0.0
var maxMaeLong      = 0.0
var sinceTrade      = 0

if strategy.position_size != 0
    sinceTrade  := sinceTrade + 1
if strategy.position_size == 0    
    sinceTrade  := 0

if strategy.closedtrades[0] > strategy.closedtrades[1] and strategy.position_size[1] > 0    
    sinceTrade          := sinceTrade[1]                                                    
    maeLong             := (open[sinceTrade] - ta.lowest(low, sinceTrade)) / open[sinceTrade]
    maxMaeLong          := maeLong > maxMaeLong ? maeLong : maxMaeLong

Pine https://www.tradingview.com/blog/en/pine-functions-support-dynamic-length-arguments-20554/ 的解决方案无法帮助我。

我有这种感觉,“if strategy.strategy_position_size != 0” 计算方式不正确。

尽管您的想法是正确的,但即使我们能够将您的最低功能从本地范围内移出,问题也会出现在我们要求过多的功能条导致您描述的错误的地方。所以我提出另一种选择。在这里,我们真正使用您的自定义函数来查找自发生其他事情以来某事物的最低值。我们将使用 low 作为输入,我们将使用头寸大小的变化作为 bool 输入。该函数将在第一次出现时分配值,然后取该值或源之间的最小值,直到再次检测到条件。我已经包含了一个绘图函数,用于观察和验证您的多头交易中的低点。

lowestSince(src, cond) =>
    var float hi = na
    hi := cond ? src : math.min(hi, src)

long = strategy.position_size > 0 

enterLong = long and not long[1]

lowestLow = lowestSince(low, enterLong)

plot(long ? lowestLow : na, style=plot.style_linebr)

祝您交易和编码顺利