Jeff Rice
Jeff Rice

Reputation: 11

Rising() does not produce expected results

I am having trouble with the rising() function in Pine Script/TradingView. Below is the code. I want to have a table below my plot with some key indicators color code based on whether the signal is bearish or bullish, as well as an arrow to indicate direction of most recent change.

When I overlay this test script, fastTema is plotted in green, as expected, when it is rising and red when it is falling. However, in the summary table, it is shown in red with a falling arrow even when it is plotted in green above. It seems like rising(fastTema,1) is returning a 'true' result in the plot() call and a false result when in the table.

What am I missing here?

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

Upvotes: 0

Views: 889

Answers (1)

Jeff Rice
Jeff Rice

Reputation: 11

OK, the problem is the if (barstate.islast) call. While TradingView recommends use of it to reduce computational load and speed up scripts, many functions break if they are put inside of it.

We call atr(14) prior to entry in our if block so that it evaluates on each bar. Had we used tostring(atr(14)) inside the if block, the function would not have evaluated correctly because it would be called on the dataset’s last bar without having calculated the necessary values from the previous bars. Tables - Pine Script

So the solution is to put all the calls to "rising" etc outside the if block.

Upvotes: 0

Related Questions