I am trying to code an indicator that plots the highest high and lowest low between each moving average crosses. Like a ZigZag indicator.
For example if the fast MA crosses the slow MA from below, the indicator should be looking for the highest high only and there should be only one value between the crosses. The same for a cross from above, the indicator should be plotting only one value which is the lowest low since the last cross.
I am new to Pine script, but not new to coding in general. I find it challenging to code the simplest things with Pine script. I am learning how to code in this language.
Please help direct me on how to find the correct answer. Thank you
//@version=4
study(title="MA Cross", overlay=true, max_bars_back=3000)
MA1 = sma(close, 20)
MA2 = sma(close, 100)
Highest(src, len) =>
max = high
for i = 1 to len
if src[i] > max
max := src[i]
max
Lowest(src, len) =>
min = src[0]
for i = 1 to len
if src[i] < min
min := src[i]
min
HH = 0.0
HH := Highest(high, barssince(cross(MA1, MA2)))
LL = 0.0
LL := Lowest(low, barssince(cross(MA1, MA2)))
HighLow = 0.0
if(MA1 > MA2)
HighLow := HH
else
if(MA1 < MA2)
HighLow := LL
else
HighLow := na
plot(series=HighLow, color = color.red, linewidth=3)
plot(series=MA1, color = color.aqua, linewidth=1)
plot(series=MA2, color = color.orange, linewidth=1)
I'm not 100% sure but, the problem seems to be related to the fact that barssince()
returns series[integer]
and when you use that in for
condition, it causes problems.
So, you need to implement your own barssince()
. I used a simple counter for that.
//@version=4
study(title="MA Cross Debug", overlay=false, max_bars_back=3000)
MA1 = sma(close, 20)
MA2 = sma(close, 100)
var cntA1LessThanA2 = 0
HighestCross = 0.0
MovingAverage_Long = crossover(MA1, MA2)
Highest(src, len) =>
max = high
for i = 1 to len
if (src[i] > max)
max := src[i]
max
if (MA1 > MA2)
cntA1LessThanA2 := cntA1LessThanA2 + 1
else
cntA1LessThanA2 := 0
if (MA1 > MA2)
HighestCross := Highest(high, cntA1LessThanA2)
else
HighestCross := na
plot(series=barssince(MA1<MA2), title="barssince", color=color.green, linewidth=3)
plot(series=cntA1LessThanA2, title="cntA1LessThanA2", color=color.red, linewidth=3)
In the indicator below in the screenshot, I plot the counter (red) and return value of barssince()
(green), to show you counter works as expected.
plot(series=barssince(MA1<MA2), title="barssince", color=color.green, linewidth=3)
plot(series=cntA1LessThanA2, title="cntA1LessThanA2", color=color.red, linewidth=3)
Note that, your indicator will not have a valid output if MA >= M1
because you are asigning na
to HighestCross
in that case. But I guess you already know that.
if (MA1 > MA2)
HighestCross := Highest(high, cntA1LessThanA2)
else
HighestCross := na
plot(HighestCross, color = color.red, linewidth = 3)
I believe you can take it from here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.