# PINE fallingsince Edit Subject

1 more issue: i get this error:

Error: Cannot call `falling` with arguments (series, series); available overloads: falling(series, integer) => series;

when writing this:

_cross = cross(_sma10, _sma21)
_isfalling = falling(_cross, _barssince) ? true : false

the lookback argument 'arg1' into falling(arg0, arg1) is of type 'int'. but the function 'barssince()' only returns the # of bars as a series.

how can we extract an int from a series?

Note: This topic was created from a reply on the Pine scripting language topic.
2 people have
this question
• It's not currently possible to use series instead of int. But here's how to do it differently

``` _cross = cross(_sma10, _sma21) fallingsince(s,cond) => acc = cond ? 1 : na(acc[1]) ? 1 : acc[1] ? s < s[1] : 0 acc _isfalling = fallingsince(close, _cross)```

Note, that there is an error in your code. In falling() first argument shouldn't be _cross as it has only 1 and 0. Most probably there should be something like close.
How does this make you feel?
I'm
• ah right, my bad re the 1st arg into falling().

quick question though: where the hell does 'acc' come from in the following line:

acc = cond ? 1 : na(acc[1]) ? 1 : acc[1] ? s < s[1] : 0

is it possible to use a variable like this when it hasn't been declared yet?

also, i am not sure i fully understand this line. for instance, let's assume that the 2nd arg into fallingsince(s, cond) is true. then:

'cond ? 1' is going to execute regardless of whether or not the cross was down or up. or am i mistaken?

and how is 'na(acc[1])' a condition? yes, it returns either 1 or 0 BUT if it's not a valid #, why make the ?: operator return '1'? how do we know at that point that the series is indeed falling?

also, what confuses the hell out of me is that i am not sure what happens to a variable with function scope on the next tick or bar as the script is run again from line 1? isn't the variable re-assigned and the value it held from the previous iteration overwritten? therefore 'acc[1]' is meaningless? and if not, isn't the '[]' operator the array access operator to access a specific value inside a series, BUT here we have a variable that only holds '1' or '0', in essence, a boolean and not a series, so how could this work? (s and s[1] i understand, since it refers to a series with external scope to the function.)
How does this make you feel?
I'm
• Good questions.

acc = cond ? 1 : na(acc[1]) ? 1 : acc[1] ? s < s[1] : 0

is it possible to use a variable like this when it hasn't been declared yet?

Actually it's just a declaration. What confused you is reference to it's prev value: acc[1]. Currently it's possible to reference prev value in the declaration and after it. Though soon we'll change it so it's possible to use prev values even before declaration.

also, i am not sure i fully understand this line. for instance, let's assume that the 2nd arg into fallingsince(s, cond) is true. then:

'cond ? 1' is going to execute regardless of whether or not the cross was down or up. or am i mistaken?

'cond' is evaluated on every bar and if it's true then the whole condition will be '1'. Otherwise it goes and check next condition na(acc[1]).

and how is 'na(acc[1])' a condition? yes, it returns either 1 or 0 BUT if it's not a valid #, why make the ?: operator return '1'? how do we know at that point that the series is indeed falling?

we have to check it since we use it in the next condition. It will be true on the first bar only. Actually there is a bug ;) we need to return 0 there as it's the case when the 'cond' is false on the first bar. So the corrected version is
```
fallingsince(s,cond) =>
acc = cond ? 1 : na(acc[1]) ? 0 : acc[1] ? s < s[1] : 0
acc```

also, what confuses the hell out of me is that i am not sure what happens to a variable with function scope on the next tick or bar as the script is run again from line 1? isn't the variable re-assigned and the value it held from the previous iteration overwritten? therefore 'acc[1]' is meaningless? and if not, isn't the '[]' operator the array access operator to access a specific value inside a series, BUT here we have a variable that only holds '1' or '0', in essence, a boolean and not a series, so how could this work? (s and s[1] i understand, since it refers to a series with external scope to the function.)

Anything that can be casted to double _can be_ a series. So it's possible to reference it's prev values. If it's referenced - it has a buffer of N last values. Lets consider this sample
```
mycum(s) =>
acc = na(acc[1]) ? s : s + acc[1]
acc```

It's a pure Pine implementation of builtin cum() function. On each bar it sums current s value with accumulated values on the prev bar.
How does this make you feel?
I'm
• damn it!! i was wondering how to write an accumulator. brilliant!

although to be honest i wish the syntax were somewhat different. i'm thinking something along the lines of static class vs class in java. what i mean is that the former is never instantiated and always the same through the code lifecycle (i don't mean its state is necessarily immutable, just ontologically speaking) and since a class can be instantiated multiple times, each instance (like cloning at first) is its own copy/entity in memory space. ok, not perfect analogy, but i hope you see where i'm going with this.

so, i wish we had some sort of keyword to show that some variable lives outside the scope of the current bar (is global to the time stream) vs variables that always get updated on each new bar (current situation). in the former case, you suddenly have a data space 'outside of time', kinda like in a regular programming environment and then you can create and manipulate abstract data structures just like you would without having to think about how the next 'event loop bar update' might mutate that structure.
How does this make you feel?
I'm
• You probably mean constants. We don't have specific keywords for them. For example in the following
```
len = 10
src = close```

len is a constant while src is a series. len will never be changed throughout the calc cycle. But you can plot it like "plot(len)" and in this case it's a kinda series (it will be casted to series).
How does this make you feel?
I'm
• no what i meant was to have the ability to refer back to the result of some computation that occurred n bars ago.

maybe when some condition was true, we compute something, and store it for future reference. of course, as soon as the conditions that led to the computation in the first place are not true anymore, we do not perform the computation, but we still have the result stored somewhere from the last computation.

now time goes on, new bars appear, and we still can use the result of that computation because the value of the variable it is stored in is persistent across many iterations of the script. all we do, is check whether or not the condition(s) that triggered the computation have changed.

on the next condition change, we do the computation again and store it again for future reference so we can refer to it even if 100 bars have appeared since the condition was true.

because, for example, if we are 20 bars away from when the condition was true, not only we cannot do the computation because current market conditions make the conditional expression false, but also we cannot find out what the computation was 20 bars ago when the conditional expression was true, because variables do not maintain their values when the script is executed again when a new bar occurs. so the only way would be to store all these computations into a series but then how would you reference the series element that holds the last computation result since we do not know at which index position it is located and every time a new bars appears, the series grows by 1, thus adding +1 to the index of the cell (pushing the cell down the stack as it were) that contains the computation's result.
How does this make you feel?
I'm
• Looks like you are speaking about valuewhen() function. For example
valuewhen(cross(open,close),open,0) - gives you open value for the latest cross. Last argument is 0 for latest, but it could be any number. So you could reference open value for all crosses.
How does this make you feel?
I'm
• ok i am confused now.

first, on page: https://www.tradingview.com/study-scr... it is written, and i quote:

"valuewhen (Built-in function)
Source series value when the condition was true on the n-th most recent occurrence."

so, when we use valuewhen(..., ...., 0) with 0 as the last arg, 'the n-th most recent occurrence' would mean that '0' specifies the current occurrence, ie if the condition occurred on the current bar before it closed.

but you wrote: ' Last argument is 0 for latest...', which means that it could be occurring now during the current bar, or 5 bars ago, or 100 bars ago, if that were the latest occurrence and no other occurrence of condition happened since then.

these are 2 very different meanings. so which one is which?

second, in my previous comment, i was not exactly referring to the functionality provided by 'valuewhen()'.

i wrote that a user might need to be able to have access to some persistent storage mechanism that survives every new pass through the script. algos need to perform some computation and retain the value of that computation in a variable for a duration exceeding one bar at the very least, and the result of said computation need not be a series--what if it is an integer or float? then it can't be plugged into valuewhen() as an arg, which is different from wanting to capture the value of a particular series when some condition happens to be true.

therefore, even though said computation might be triggered by some conditional expression coming true, the value resulting from said computation:

1- might not be a series
2- might not get updated with every new bar but only once the original condition reappears OR the computation's result might be used to change the conditional expression itself so that next time the conditional check is performed, the conditional expression is slightly different, and so on.

does that make more sense now?
How does this make you feel?
I'm
• these are 2 very different meanings. so which one is which?

Second statement is true. For example valuewhen(open>close,high,0) gives you high for latest bar when open was greater then close.

does that make more sense now?

Got it. Yes, that makes total sense. We'll add something for this.
How does this make you feel?