【问题标题】:Rising() does not produce expected resultsRising() 不会产生预期的结果
【发布时间】:2021-06-19 20:28:55
【问题描述】:

我在使用 Pine Script/TradingView 中的rising() 函数时遇到问题。下面是代码。我希望在图表下方有一个表格,其中包含一些基于信号是看跌还是看涨的关键指标颜色代码,以及指示最近变化方向的箭头。

当我覆盖这个测试脚本时,正如预期的那样,fastTema 在上升时以绿色绘制,在下降时绘制为红色。但是,在汇总表中,即使上面以绿色绘制,它也显示为红色并带有下降箭头。似乎rising(fastTema,1) 在 plot() 调用中返回“真”结果,而在表格中返回错误结果。

我在这里错过了什么?

//@version=4
study("Test", overlay=true)

// TEMA Calculation with alerts (Based on TEMA With Alert by BerkSay)
fTEMA(temaSource, temaLength) =>
    Tema1 = ema(temaSource, temaLength)
    Tema2 = ema(Tema1, temaLength)
    Tema3 = ema(Tema2, temaLength)
    3 * Tema1 - 3 * Tema2 + Tema3

showTEMA = input(true, "Show TEMA?")
temaFastLength = input(title="TEMA Fast length", defval=13, minval=1)
temaSlowLength = input(title="TEMA Slow length", defval=34, minval=1)
temaSource = input(title="TEMA Source", defval=close, type=input.source)

temaSlow = fTEMA(temaSource, temaSlowLength)
temaFast = fTEMA(temaSource, temaFastLength)
colortemaSlow = #ff0000
colortemaFast = #00ff00

plottemaSlow = plot(showTEMA ? temaSlow: na, color=color.new(colortemaSlow,20), title="TEMA Slow plot", linewidth=2)
plottemaFast = plot(showTEMA ? temaFast: na, color=rising(temaFast,1) ? color.new(color.green,20) : color.new(color.red,20), title="TEMA Fast plot", linewidth=2)

// Summary Table
//   Shows red for bearish signals, green for bullish signals.
tablePosition = input(title="Summary Table Position", defval=position.bottom_left, 
     options=[position.bottom_left,
     position.top_left, position.bottom_right, position.top_right])
var table summaryTable = table.new(tablePosition, 1, 1)
 
if (barstate.islast)
    temaDirection = rising(temaFast,1) ? "↑" : "↓"
    table.cell(summaryTable, 0, 0, "TEMA" + temaDirection,text_size=size.tiny, bgcolor = temaFast > temaSlow and rising(temaFast,1) ? color.green : color.red)
    // See if temaFast has increased over the last two periods.
    if (rising(temaFast,2) and temaFast < temaSlow)
        table.cell_set_bgcolor(summaryTable, 0, 0, color.yellow)

【问题讨论】:

  • 阅读控制台警告信息

标签: pine-script


【解决方案1】:

好的,问题出在 if (barstate.islast) 调用上。虽然 TradingView 建议使用它来减少计算负载并加速脚本,但如果将它们放入其中,许多功能就会中断。

我们在进入 if 块之前调用 atr(14),以便它对每个柱进行评估。如果我们在 if 块中使用 tostring(atr(14)),该函数将无法正确评估,因为它将在数据集的最后一个柱上调用,而没有从前一个柱计算必要的值。 Tables - Pine Script

所以解决方案是将所有对“上升”等的调用放在 if 块之外。

【讨论】:

  • 干得好。通常建议将计算保持在全局范围内,除非您有特定的理由不这样做。尤其是因为您不止一次地使用rising(temaFast, 1) 条件,给它分配一个变量也可以使您的代码更高效。这样它只计算一次,并且只是多次引用。此处的计算负载不太可能产生明显差异,但在其他情况下,它可能会在缓慢的脚本/超时和平稳运行的脚本之间产生差异,尤其是在使用 for 循环时
猜你喜欢
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多