PINE v5-有条件绘图



当存在'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)

我听说计划将参数列表扩展到plot((的显示参数,但我找不到任何解决方法。

是否有可用的变通方法?

//@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)

最新更新