PINE v5 - 有条件绘图

发布于 2025-01-18 03:08:26 字数 2779 浏览 1 评论 0原文

当有“ Na”值时,我无法在Pine V5上有条件地绘制。

//@version=5
strategy("Discontinuous plots v5", "", true)

atr = ta.atr(7)

longCondition = ta.crossover(ta.sma(close, 100), ta.sma(close, 200))

long_stop_level = float(na)
long_profit_level = float(na)

long_stop_level := longCondition ? low - atr : long_stop_level[1]
long_profit_level := longCondition ? close + atr : long_profit_level[1]

if (longCondition)

    strategy.entry("Long Entry", strategy.long)
    strategy.exit("TP/SL", "Long Entry", stop= low - atr, limit= close + atr)

shortCondition = ta.crossunder(ta.sma(close, 100), ta.sma(close, 200))

short_stop_level = float(na)
short_profit_level = float(na)

short_stop_level := shortCondition ? high + atr : short_stop_level[1]
short_profit_level := shortCondition ? close - atr : short_profit_level[1]

if (shortCondition)

    strategy.entry("Short Entry", strategy.short)
    strategy.exit("TP/SL", "Short Entry", stop= high + atr, limit= close - atr)

plot(strategy.position_size <= 0 ? na : long_stop_level, color=color.new(#FF0000, 70), linewidth=2)
plot(strategy.position_size <= 0 ? na : long_profit_level, color=color.new(#008000, 70), linewidth=2)
plot(strategy.position_size >= 0 ? na : short_stop_level, color=color.new(#FF0000, 70), linewidth=2)
plot(strategy.position_size >= 0 ? na : short_profit_level, color=color.new(#008000, 70), linewidth=2)

转换为V3的相同代码结构如下所示:

//@version=3
strategy("Discontinuous plots", "", true)

atr = atr(7)

longCondition = crossover(sma(close, 100), sma(close, 200))

long_stop_level = na
long_profit_level = na

long_stop_level := longCondition ? low - atr : long_stop_level[1]
long_profit_level := longCondition ? close + atr : long_profit_level[1]

if (longCondition)

    strategy.entry("Long Entry", strategy.long)
    strategy.exit("TP/SL", "Long Entry", stop= low - atr, limit= close + atr)

shortCondition = crossunder(sma(close, 100), sma(close, 200))

short_stop_level = na
short_profit_level = na

short_stop_level := shortCondition ? high + atr : short_stop_level[1]
short_profit_level := shortCondition ? close - atr : short_profit_level[1]

if (shortCondition)

    strategy.entry("Short Entry", strategy.short)
    strategy.exit("TP/SL", "Short Entry", stop= high + atr, limit= close - atr)

plot(strategy.position_size <= 0 ? na : long_stop_level, color=red, style=linebr, linewidth=2)
plot(strategy.position_size <= 0 ? na : long_profit_level, color=green, style=linebr, linewidth=2)
plot(strategy.position_size >= 0 ? na : short_stop_level, color=red, style=linebr, linewidth=2)
plot(strategy.position_size >= 0 ? na : short_profit_level, color=green, style=linebr, linewidth=2)

我听说已计划的参数列表的扩展名是计划的,但我找不到任何解决方法。

有可解决方法吗?

I'm unable to plot conditionally on Pine v5 when there is a 'na' value.

//@version=5
strategy("Discontinuous plots v5", "", true)

atr = ta.atr(7)

longCondition = ta.crossover(ta.sma(close, 100), ta.sma(close, 200))

long_stop_level = float(na)
long_profit_level = float(na)

long_stop_level := longCondition ? low - atr : long_stop_level[1]
long_profit_level := longCondition ? close + atr : long_profit_level[1]

if (longCondition)

    strategy.entry("Long Entry", strategy.long)
    strategy.exit("TP/SL", "Long Entry", stop= low - atr, limit= close + atr)

shortCondition = ta.crossunder(ta.sma(close, 100), ta.sma(close, 200))

short_stop_level = float(na)
short_profit_level = float(na)

short_stop_level := shortCondition ? high + atr : short_stop_level[1]
short_profit_level := shortCondition ? close - atr : short_profit_level[1]

if (shortCondition)

    strategy.entry("Short Entry", strategy.short)
    strategy.exit("TP/SL", "Short Entry", stop= high + atr, limit= close - atr)

plot(strategy.position_size <= 0 ? na : long_stop_level, color=color.new(#FF0000, 70), linewidth=2)
plot(strategy.position_size <= 0 ? na : long_profit_level, color=color.new(#008000, 70), linewidth=2)
plot(strategy.position_size >= 0 ? na : short_stop_level, color=color.new(#FF0000, 70), linewidth=2)
plot(strategy.position_size >= 0 ? na : short_profit_level, color=color.new(#008000, 70), linewidth=2)

Same code structure converted to v3 works like a charm, as follows:

//@version=3
strategy("Discontinuous plots", "", true)

atr = atr(7)

longCondition = crossover(sma(close, 100), sma(close, 200))

long_stop_level = na
long_profit_level = na

long_stop_level := longCondition ? low - atr : long_stop_level[1]
long_profit_level := longCondition ? close + atr : long_profit_level[1]

if (longCondition)

    strategy.entry("Long Entry", strategy.long)
    strategy.exit("TP/SL", "Long Entry", stop= low - atr, limit= close + atr)

shortCondition = crossunder(sma(close, 100), sma(close, 200))

short_stop_level = na
short_profit_level = na

short_stop_level := shortCondition ? high + atr : short_stop_level[1]
short_profit_level := shortCondition ? close - atr : short_profit_level[1]

if (shortCondition)

    strategy.entry("Short Entry", strategy.short)
    strategy.exit("TP/SL", "Short Entry", stop= high + atr, limit= close - atr)

plot(strategy.position_size <= 0 ? na : long_stop_level, color=red, style=linebr, linewidth=2)
plot(strategy.position_size <= 0 ? na : long_profit_level, color=green, style=linebr, linewidth=2)
plot(strategy.position_size >= 0 ? na : short_stop_level, color=red, style=linebr, linewidth=2)
plot(strategy.position_size >= 0 ? na : short_profit_level, color=green, style=linebr, linewidth=2)

I heard that an extension to the argument list to plot()'s display parameter is planned, but I could not find any workaround.

Is there a workaround available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

番薯 2025-01-25 03:08:26
//@version=5

strategy('Discontinuous plots v5', '', true)

atr = ta.atr(7)

longCondition = ta.crossover(ta.sma(close, 100), ta.sma(close, 200))

long_stop_level = float(na)
long_profit_level = float(na)

long_stop_level := longCondition ? low - atr : long_stop_level[1]
long_profit_level := longCondition ? close + atr : long_profit_level[1]

if longCondition

    strategy.entry('Long Entry', strategy.long)
    strategy.exit('TP/SL', 'Long Entry', stop=low - atr, limit=close + atr)

shortCondition = ta.crossunder(ta.sma(close, 100), ta.sma(close, 200))

short_stop_level = float(na)
short_profit_level = float(na)

short_stop_level := shortCondition ? high + atr : short_stop_level[1]
short_profit_level := shortCondition ? close - atr : short_profit_level[1]

if shortCondition

    strategy.entry('Short Entry', strategy.short)
    strategy.exit('TP/SL', 'Short Entry', stop=high + atr, limit=close - atr)

plot(strategy.position_size <= 0 ? na : long_stop_level, color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size <= 0 ? na : long_profit_level, color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size >= 0 ? na : short_stop_level, color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size >= 0 ? na : short_profit_level, color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)
//@version=5

strategy('Discontinuous plots v5', '', true)

atr = ta.atr(7)

longCondition = ta.crossover(ta.sma(close, 100), ta.sma(close, 200))

long_stop_level = float(na)
long_profit_level = float(na)

long_stop_level := longCondition ? low - atr : long_stop_level[1]
long_profit_level := longCondition ? close + atr : long_profit_level[1]

if longCondition

    strategy.entry('Long Entry', strategy.long)
    strategy.exit('TP/SL', 'Long Entry', stop=low - atr, limit=close + atr)

shortCondition = ta.crossunder(ta.sma(close, 100), ta.sma(close, 200))

short_stop_level = float(na)
short_profit_level = float(na)

short_stop_level := shortCondition ? high + atr : short_stop_level[1]
short_profit_level := shortCondition ? close - atr : short_profit_level[1]

if shortCondition

    strategy.entry('Short Entry', strategy.short)
    strategy.exit('TP/SL', 'Short Entry', stop=high + atr, limit=close - atr)

plot(strategy.position_size <= 0 ? na : long_stop_level, color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size <= 0 ? na : long_profit_level, color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size >= 0 ? na : short_stop_level, color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size >= 0 ? na : short_profit_level, color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文