OBV Calculation - pine-script-v5

obv = cum(change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume)
obv2 = cum(sign(change(src)) * volume )
obv and obv2 return different results
ta.obv(df['close'], df['volume']) returns the same result as obv2
I will also need the return value of obv calculation with talib
OBV calculation
Thanks in advance for your help.

I've added an OBV indicator to TradingView, opened it's sources and found
obv = ta.cum(math.sign(ta.change(src)) * volume)
Then I cloned the indicator and replaced this line with
obv = ta.cum(ta.change(src) > 0 ? volume : ta.change(src) < 0 ? -volume : 0*volume)
And added it as a second indicator. I've got a different results. But also I've got a warning:
line 9: The function 'ta.change' should be called on each calculation
for consistency. It is recommended to extract the call from the
ternary operator or from the scope
So, I replaced the line with
c = ta.change(src)
obv = ta.cum(c > 0 ? volume : c < 0 ? -volume : 0*volume)
And got exactly the same results as original OBV!
So your code for obv is incorrect and you shall google for explanation of this warning. For example: The function should be called on each calculation for consistency, console output?

Related

Mismatched Input-Ternary Operator (PineScript)

On using ternary operator in Plotchar() , the true condition is not fully recognized (in spite of placing it in "()" brackets). Mismatched inpur error is given(refer image).
How to resolve this ?
Code:
k = ta.sma(...)
plotchar((k < 20) ? (xUp , "Go Long", "▲", location.bottom, color.lime, size = size.tiny) : na)
Image:
The ternary operator uses the series (xUp : na) only for conditionals,not the entire plotchar parameters.
k = ta.sma(...)
plotchar(k < 20 ? xUp: na , "Go Long", "▲", location.bottom, color.lime, size = size.tiny)

I would like to change candle colors using the offset function with a variable/conditional offset time

Version 5.0. iff_17 determines whether my conditions are met. It then sets the candlecolor and the offset time (pasttime) which is either -4 or 0. However, it seems that pasttime is not recognized as a numerical value (also using int pasttime = iff_17 == 2 ? -4 : 0) does not work Help would be greatly appreciated. Thanks
iff_17 = va1 == 1 and ((_hh or _lh) or (_hl or _ll)) ? 2 : 0
candleColor2 = iff_17 == 2 ? candleColor1 : candleColor
pasttime = iff_17 == 2 ? -4 : 0
barcolor(candleColor2, offset = pasttime)

How to normalize Integer variable to positive negative or zero with bit operations

7 -> 1
0 -> 0
-7 -> -1
I've have code:
(x == 0 ? 0 : x / abs(x)) + 1
but is it possible to avoid division and make it faster?
How about
(x == 0 ? 0 : (x < 0 ? -1 : 1))
The idea was to use bit operations to avoid branching code or value conversion.
Haven't found how to do it with bit operations but apple already add this function
https://developer.apple.com/documentation/swift/int/2886673-signum
signum()
Returns -1 if this value is negative and 1 if it’s positive; otherwise, 0.
so simple) raw test shows ~x100 faster implementation

Matlab function calling basic

I'm new to Matlab and now learning the basic grammar.
I've written the file GetBin.m:
function res = GetBin(num_bin, bin, val)
if val >= bin(num_bin - 1)
res = num_bin;
else
for i = (num_bin - 1) : 1
if val < bin(i)
res = i;
end
end
end
and I call it with:
num_bin = 5;
bin = [48.4,96.8,145.2,193.6]; % bin stands for the intermediate borders, so there are 5 bins
fea_val = GetBin(num_bin,bin,fea(1,1)) % fea is a pre-defined 280x4096 matrix
It returns error:
Error in GetBin (line 2)
if val >= bin(num_bin - 1)
Output argument "res" (and maybe others) not assigned during call to
"/Users/mac/Documents/MATLAB/GetBin.m>GetBin".
Could anybody tell me what's wrong here? Thanks.
You need to ensure that every possible path through your code assigns a value to res.
In your case, it looks like that's not the case, because you have a loop:
for i = (num_bins-1) : 1
...
end
That loop will never iterate (so it will never assign a value to res). You need to explicitly specify that it's a decrementing loop:
for i = (num_bins-1) : -1 : 1
...
end
For more info, see the documentation on the colon operator.
for i = (num_bin - 1) : -1 : 1

What does !! (double exclamation point) mean?

In the code below, from a blog post by Alias, I noticed the use of the double exclamation mark !!. I was wondering what it meant and where I could go in the future to find explanations for Perl syntax like this. (Yes, I already searched for !! at perlsyn).
package Foo;
use vars qw{$DEBUG};
BEGIN {
$DEBUG = 0 unless defined $DEBUG;
}
use constant DEBUG => !! $DEBUG;
sub foo {
debug('In sub foo') if DEBUG;
...
}
UPDATE
Thanks for all of your answers.
Here is something else I just found that is related The List Squash Operator x!!
It is just two ! boolean not operators sitting next to each other.
The reason to use this idiom is to make sure that you receive a 1 or a 0. Actually it returns an empty string which numifys to 0. It's usually only used in numeric, or boolean context though.
You will often see this in Code Golf competitions, because it is shorter than using the ternary ? : operator with 1 and 0 ($test ? 1 : 0).
!! undef == 0
!! 0 == 0
!! 1 == 1
!! $obj   == 1
!! 100 == 1
undef ? 1 : 0 == 0
0 ? 1 : 0 == 0
1 ? 1 : 0 == 1
$obj ? 1 : 0 == 1
100 ? 1 : 0 == 1
not-not.
It converts the value to a boolean (or as close as Perl gets to such a thing).
Because three other answers claim that the range is "0" or "1", I just thought I'd mention that booleans in Perl (as returned by operators like ==, not, and so on) are undef and 1, not 0 and 1.