问题描述
我在使用 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,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,"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,color.yellow)
解决方法
好的,问题是 if (barstate.islast) 调用。尽管 TradingView 建议使用它来减少计算负载和加速脚本,但如果将它们放入其中,许多函数就会中断。
我们在进入 if 块之前调用 atr(14) 以便它在每个柱上求值。如果我们在 if 块中使用 tostring(atr(14)) ,该函数将无法正确评估,因为它会在数据集的最后一根柱线上被调用,而没有从之前的柱线中计算出必要的值。 Tables - Pine Script
因此,解决方案是将所有对“rising”等的调用放在 if 块之外。