Reputation: 45
Try to plot the value of the highest bars between current bar and the last cross of ema(50)
and ema(200)
using barssince(cross(ema50,ema200))
. The function highest()
wants an integer
and the Barssince gives a series integer
. All variants getting the error that pine connot compile with error:
line 4: Cannot call `highest` with arguments (series[integer]); available overloads: highest(series, integer) => series; highest(integer) => series Script.
//@version=3
study("My Script")
o = 0
o := highest(barssince(cross(sma(close,50),sma(close,200))))
plot(o, color = yellow)
The ema cross is just an example. The highest()
function with a calculated number of bars with barssince
is my goal.
How do I convert the result form barssince to an integer accepted by highest()
?
Or is there a different solution?
Upvotes: 0
Views: 3953
Reputation: 2821
Implement the function highest by yourself:
//@version=3
study("My Script", max_bars_back=5000)
highest_pine(src, len) =>
max = src[0]
for i = 1 to len
if src[i] > max
max := src[i]
max
o = 0.0
o := highest_pine(close, barssince(cross(sma(close,50),sma(close,200))))
plot(o, color = green)
Upvotes: 3
Reputation:
The reference is pretty confusing, but after playing around with your function a bit I managed to make it work:
There are two overloads for the highest()
function:
- highest(length)
, this one takes an integer
and returns a series
- highest(source, length)
, this one takes a series
and an integer
There is a hint in the function reference, saying that One arg version: x is a length. Algorithm uses high as a source series.
Checking the reference for barssince()
I see that it takes a bool
(logical condition) and returns a series of integer
.
Now it is clear that the highest(barssince(true))
is not the correct syntax because barssince()
will return a series
and not an integer
, while highest()
expects and integer or a series and and integer.
From poking around and searching online I reached a conclusion that it is not possible to have a variable of integer
in pine script that is not a series
at the same time.
This leads me to believe that the highest()
function cannot be used dynamically.
I was able to mimic the behavior using if
statement, along with the help of this migration guide
This is the version of the script that works for me, it plots a line at the max()
of the highs, resetting if there is a new MA cross.
//@version=3
study("High since SMA Cross")
fast_sma = sma(close,10)
slow_sma = sma(close,20)
high_since_cross = 0.0
resistance = if(cross(fast_sma, slow_sma))
high
else
max(nz(high_since_cross[1]), high)
plot(resistance, color=yellow)
Upvotes: 0