1

I don't need a whole array of this expression, just its very recent (current) value. How to modify its code?

UpperBollinger = ta.sma(close, 20) + 2 * ta.stdev(close, 20)

(Of course, I can use it to get the same for the lower band.)

Vendrel
  • 865
  • 9
  • 19
  • Why do you need this? What's your use case? – vitruvius Sep 07 '22 at 17:12
  • I'd like to make a label that contains flags based on multi-timeframe Bollinger Bands calculations. So, for the content of the label (displaying ✓ or × characters per lines based on certain Bollinger Bands conditions, boolean values dependent), I must calculate a bunch of Bollinger Bands calculations, multi-timeframe (18 BBs, to be precise). BUT if I do it the usual way, then it calculates several arrays of Bollinger Bands entirely unnecessarily. I need the last value only, don't need past values. Also want to save CPU power and RAM usage. – Vendrel Sep 07 '22 at 17:47
  • As I understand it, the calculation is done in the cloud, it then sends the calculated data to your browser, only the data rendering part consumes your hardware. If you ever experienced slowness, maybe because there are too many `request.security()` calls in your code. That can not be avoided afaik.To simplify your code, you can use the built-in `ta.bb()` function which is more efficient instead of multiple lines of code. And you may want to use [table](https://www.tradingview.com/pine-script-docs/en/v5/concepts/Tables.html) instead of label to present screener-like result. – tentme Sep 07 '22 at 23:53

1 Answers1

1

You cannot do that.

Once a series always a series. You want to have the "last" value but on the next bar you need the last value again, and on the next bar again. Isn't your requirement creates a series?

As explained in the type system, using certain built-in functions and variables yield a result of series. close, ta.sma(), ta.stdev all return series. So your end result will also be a series.

Built-in variables such as open, close, high, time or volume are of “series” form, as would be the result of expressions calculated using them. Functions such as barssince() or crossover() yield a result of “series” form because it varies bar to bar, as does that of the [] history-referencing operator used to access past values of a time series.

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • So I definitely must make a huge array of calculations (18 or even 36 Bollingers) even if I use the last column of data from each? – Vendrel Sep 07 '22 at 20:28
  • 2
    Arrays and series variables are not exactly the same thing. If you face performance issues, I suggest you look at somewhere else. It could be, loops, security calls, creating/deleting drawing objects etc. – vitruvius Sep 08 '22 at 09:57
  • 1
    Like @vitruvius pointed out, you're not making your calculation on an array. With "UpperBollinger" you are using only the latest available value, your "current". Though in other languages UpperBollinger would be an array where you can point to values by the "myArray[index]" index reference in pine script it's a value of form "series" that's completely different. – elod008 Sep 08 '22 at 12:21
  • @vitruvius Well, let me ask a focused question, perhaps you are one of those who actually can answer. It seems, my machine gets laggy over a runtime of several opened charts with several indicators on them. It's true there are a lot of drawings. CPU load is max. 25%. RAM is 64 GB, TradingView-related load is 2-3 GB. Memory Stress is low. Deleting TradingView's browser datas then restarting Safari helps a lot, until the machine gets laggy again after some sleep-modes, after some days of TradingView runtime. Is there any logic behind this behaviour? :) – Vendrel Sep 08 '22 at 15:48