GUIDO MENG
New member
Hi All!
I've been trying to do the following for 3 days with just partial success:
script prevPeak_Val {
input refBarNumber = 0;
input refBarValue = 0;
input numBarsInChart = 0;
def peak_Val = fold i = refBarNumber + 1 to numBarsInChart - 1
with barHigh_Val = Double.NaN
While IsNaN(barHigh_Val) do
if i == numBarsInChart - 1 and
GetValue(high, i) >= refBarValue
or
i < numBarsInChart - 1 and
GetValue(high, i) >= refBarValue and
GetValue(high, i) >= GetValue(high, i + 1) and
GetValue(high, i) >= GetValue(high, i - 1) then
#GetValue(high, i)
i
else
Double.NaN;
plot prevPeak_Val = peak_Val;
}
def firstBar = highestAll(if !IsNaN(close[0]) and IsNaN(close[-1]) then 0 else Double.NaN);
def prueba01 = if !IsNaN(firstBar) then prevPeak_Val(refBarNumber = firstBar, refBarValue = GetValue(close, firstBar), numBarsInChart = totalBars) else Double.NaN;
#def prueba02 = if !IsNaN(prueba01) then
# prevPeak_Val(refBarNumber = prueba01, refBarValue = GetValue(high, prueba01), numBarsInChart = totalBars)
# else
# Double.NaN;
def prueba02 = if !IsNaN(prueba01) then # (HERE THE VALUE OF THE FIRST PARAMETER = 4 IS THE KNOWN RESULT OF prueba01 BUT IT SHOULD USE THE VARIABLE prueba01 TO FIND THE SECOND PREVIOUS PEAK.)
prevPeak_Val(refBarNumber = 4, refBarValue = GetValue(high, prueba01), numBarsInChart = totalBars)
else
Double.NaN;
AddLabel(if !IsNaN(prueba01) then yes else no, prueba01, color.WHITE);
AddLabel(if !IsNaN(prueba02) then yes else no, prueba02, color.WHITE);
I've been trying to do the following for 3 days with just partial success:
- On a daily chart with the current close price, I want to find the previous bar number that complies with 3 conditions. Those being: a) has a higher HIGH than the current close price b) is a peak in the sense that the previous bar of the peak has a lower high and the following bar also has a lower high than the peak.
- I want the code to be able to find not just one but at least 3 peaks but the first previous peak should be higher than the current close, the second previous peak should be higher than the first previous peak and the third previous peak should be higher than the second previous peak.
- I have been able to find the first previous peak via a script but when I summon the script for the second time the code simply doesn't produce any results. The desired way would be using the code that is deactivated instead of summoning the script the second time using the known parameter that is the result of the first calling of the script function.
Here is my code:
script prevPeak_Val {
input refBarNumber = 0;
input refBarValue = 0;
input numBarsInChart = 0;
def peak_Val = fold i = refBarNumber + 1 to numBarsInChart - 1
with barHigh_Val = Double.NaN
While IsNaN(barHigh_Val) do
if i == numBarsInChart - 1 and
GetValue(high, i) >= refBarValue
or
i < numBarsInChart - 1 and
GetValue(high, i) >= refBarValue and
GetValue(high, i) >= GetValue(high, i + 1) and
GetValue(high, i) >= GetValue(high, i - 1) then
#GetValue(high, i)
i
else
Double.NaN;
plot prevPeak_Val = peak_Val;
}
def firstBar = highestAll(if !IsNaN(close[0]) and IsNaN(close[-1]) then 0 else Double.NaN);
def prueba01 = if !IsNaN(firstBar) then prevPeak_Val(refBarNumber = firstBar, refBarValue = GetValue(close, firstBar), numBarsInChart = totalBars) else Double.NaN;
#def prueba02 = if !IsNaN(prueba01) then
# prevPeak_Val(refBarNumber = prueba01, refBarValue = GetValue(high, prueba01), numBarsInChart = totalBars)
# else
# Double.NaN;
def prueba02 = if !IsNaN(prueba01) then # (HERE THE VALUE OF THE FIRST PARAMETER = 4 IS THE KNOWN RESULT OF prueba01 BUT IT SHOULD USE THE VARIABLE prueba01 TO FIND THE SECOND PREVIOUS PEAK.)
prevPeak_Val(refBarNumber = 4, refBarValue = GetValue(high, prueba01), numBarsInChart = totalBars)
else
Double.NaN;
AddLabel(if !IsNaN(prueba01) then yes else no, prueba01, color.WHITE);
AddLabel(if !IsNaN(prueba02) then yes else no, prueba02, color.WHITE);
Last edited: