4

I want to compute the cumulative volume of bars - within each trading session - in pine-script (TradingView.com). I wrote the script below but I get error "Script could not be translated from: for i = 1 to session_bar_counter"

I've tried the below solution but it does not work.

session_timeframe = input(defval='D', type=resolution)

// Bars since session started:
session_bar_counter = n - valuewhen(change(time(session_timeframe)) != 0, n, 0)

CumVol() =>
    for i = 1 to session_bar_counter
                sum = 0.0
        sum := session_timeframe ? cum(nz(volume[i])) : na
        sum

plot(series=CumVol(), title="Cumulative volume", color=red, linewidth=4)

The expected result should be a line chart resetting each day and cumulative volume. For instance, for the first bar of the session, the value will be the volume for that bar. At the second bar, the value will be the volume[1] (volume at previous bar) + volume at current bar.

Silviu
  • 135
  • 1
  • 9
  • What are you trying to do here `sum := session_timeframe ? cum(nz(volume[i])) : na`? `session_timeframe`is a string. – vitruvius May 31 '19 at 15:49
  • Also, I'm not sure if `session_bar_counter ` is working correctly. If I plot it, I always get zero. – vitruvius May 31 '19 at 15:51
  • Hey, thanks for looking into it, Baris. Yes, you are right, session_timeframe is a string. What I want is to get the sum of all volume bars for each session_timeframe. I tried changing the condition to session_timeframe == "D" but still it does not do the trick. – Silviu Jun 02 '19 at 18:47
  • I managed to figure out that I have an issue with the browser and will need to sort it out with TV. Other people do not get the error I get (i.e. script could not be translated) on the above script. – Silviu Jun 02 '19 at 18:51
  • Regarding your second observation regarding session_bar_counter, it works fine as long as the timeframe on the chart is intraday. That's the whole purpose of the script - intraday. Here is an example of the variable counting correctly the 4H periods within the session: https://www.tradingview.com/x/sTGSCo1Y/ Well, what I want is that for each of these intraday bars, I get the cumulative volume, which is the sum of all the volume for all the bars, including the current one, for each session. I was also planning to limit the number of sessions to 100 to avoid TV crashing for small timeframes – Silviu Jun 02 '19 at 19:04
  • I made it work, posted the answer. Still struggling to find a solution for a 100-session average of the indicator. – Silviu Jun 02 '19 at 20:05

1 Answers1

4
//version=3

study("[FMF] Volume Buzz v2", shorttitle="[FMF] Volume Buzz", overlay=true)

session_timeframe = input(defval='D', type=resolution)
session_bar_counter = n - valuewhen(change(time(session_timeframe)) != 0, n, 0)

CumVol(TimeFrame, Period) => 
    sum = volume
    for i = 1 to Period 
        sum := sum + nz(volume[i]) 
    sum 
plot(CumVol(session_timeframe,session_bar_counter), color=green)
Silviu
  • 135
  • 1
  • 9
  • CumVol takes a TimeFrame param but are you using it? – JoJo May 29 '21 at 14:16
  • I tried copy/paste this into tradinview and add to chart, but it gives me `Add to Chart operation failed, reason: Script could not be translated from: for i = 1 to Period` – Kappacake Jun 24 '21 at 16:02