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

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)

Related

How do you get "1" as integer in if-else statement rather than as boolean in MATLAB?

I am trying to use MATLAB FUNCTION block in simulink .
The model is shown below
In the "time_calc" Function i want to manipulate the variable "Sector" as shown in the code below
if sector == 1 || 2
sec = 1
elseif sector == 3 || 4
sec = 2
elseif sector == 5||6
sec = 3
elseif sector == 7||8
sec = 4
elseif sector == 9||10
sec = 5
elseif sector == 11 || 12
sec = 6
end
The below is the scope and you can see the values of "sector" changing from 0 through 12 and then repeating itself
But I am getting the value of "sec" as constant "1"(shown below in figure)
(Maybe because it is evaluating the first "1" as boolean true and running that statement only over and over again)
How to correct it ?
if sector == 1 || 2 evaluates sector == 1, if it's true, the statement is true. If it's false, it evaluates 2, which is always true, and so the statement is always true.
What you intended to write was if sector == 1 || sector == 2. You can also write this as if any(sector == [1, 2]).
Your function is equivalent to:
sec=ceil(sector/2)
#Cris Luengo's answer shows why your code is wrong. But I suggest you change the entire thing by this one liner, that is much clearer.
Remove the elseif's and replace them all with just if's

OBV Calculation

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?

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

Leafletjs legand value = 0

I would like to know (if it's possible) how to add a 0 value ine Leafletjs block's legend.
For an example, this is my class :
function getColorPOPULATION(d) {
return d > 500 ? '#004590' :
d > 250 ? '#00ABFF' :
d > 1 ? '#A0E0FF' :
'#FFF4D0' ;
... and my grades (div legend) :
grades = [0, 1, 250, 500],
-> I get this result :
0 - 1
1 - 250
250 - 500
500+
I would like to isolate the 0 value (my first class will be 0). Does Leafletjs able to do this ?
Thank you,

coffeescript refactoring

Is there any coffeescript specific trickery that would make this look neater:
index = (->
if segment == 'index'
return 0
else if segment == 'inbox'
return 2
1
)()
Yes, a switch expression:
index = switch segment
when 'index' then 0
when 'inbox' then 2
else 1
You could use an inline if ... then ... else statement broken up into multiple lines (for readability) by using the \ character.
index = if segment == 'index' then 0 \
else if segment == 'inbox' then 2 \
else 1
This is useful if your conditional logic is too complex for a simple switch block.
Yes, the CoffeeScript-specific existential operator:
index = {'index': 0, 'inbox': 2}[segment] ? 1
You could also use an inline if statement to get rid of the function call:
index = if segment == 'index' then 0 else if segment == 'inbox' then 2 else 1
But an inline if wouldn't be any harder in straight Javascript:
index = segment == 'index' ? 0 : segment == 'inbox' ? 2 : 1