Reputation: 380
Update 1:
Let me try to modify my question as i think it was not very clear what i wanted to achieve and I also try another solution So my goal is to Find the Highest High candle ( green circled candle) on a specific zone which is the green zone on my screenshot below .
For now what i did is a loop when d1 < 50 and d > 50 then i loop bar by bar to find the index of the highest bar Then when i find it i do a barssince(d1 < 50 and d > 50) to spot how many candle i have on the green zone and finally i will substract the barssince number with the bar index of the highest point and voila So clearer example Let s say my green zone when i loop i find that the 4th bar is the highest one after i do a barssince that tell me i have 7 bars in total on the green zone so to know which is the highest bar within that zone i would do 7-4 = 3 so if i do a offset of -3 i should find the highest candle but it doesn t work . As you can see on the screenshot if I find that the candle that should be highlighted in purple color should be a -3 off set starting from the orange highlighted zone but it does a - 5 so i am unsure what is going on
Here is my coding attempt
//@version=4
study(title="trytofindhighesthigh", shorttitle="trytofindhighesthigh", max_bars_back=5000)
//Stochastic
smooth = input(2, minval=1), K = input(3, minval=1), D=input(5,minval=1)
hhs=highest(high,K)
ll=lowest(low,K)
k = sma((close-ll)/(hhs-ll)*100, smooth)
d = sma(k, D)
up3 = d > 50
down3 = d < 50
mycolor3 = up3 ? color.green : down3 ? color.red : color.blue
backgroundColour = mycolor3
//color the chart depending on the stochastic
bgcolor(color=backgroundColour, transp=85)
// plotchar(crossabove,char='o')
var counter = 0.0
var keephigh = 0.0
var keepbarindex = 0.0
var h_indx = 0
if d > 50 and counter == 0
counter := counter + 1
keephigh := high
keepbarindex := bar_index
h_indx := 0
else if d > 50 and counter > 0
counter := counter + 1
if high >= keephigh
keephigh := high
keepbarindex := bar_index
h_indx := h_indx + 1
if d < 50
counter := 0
mycolor4 = if d[1] > 50 and d < 50
color.orange
//color the chart depending on the stochastic
bgcolor(color=mycolor4, transp=85)
mycolor5 = if d[1] > 50 and d < 50
color.purple
bgcolor(color=mycolor5, transp=85,offset=-1 * ( barssince(d[1] < 50 and d >50 ) - h_indx) )
plot(-1 * (barssince(d[1] < 50 and d >50 ) - h_indx) )
-- End of Update 1
I would like to find the highest high between two zones but I am having a hard time to understand how to achieve it
My zone are delimited between 1 and 2 ( highlight in green on my chart ) but i am not sure how to achieve it. I tried to do a combination barssince , highest and bar_index but failed to achieve it
The output i would like is to put a label on the candle that forms the highest high on the green zone
Now what i tried to do is that i look on that particular zone to find the N candle that makes the highest point
To be able to do it I have done a loop into the candle to find the highest high and store the candle with the highest high. Then I would do a barsince
My coding attempt
//@version=4
study(title="whatever", shorttitle="Colored SMA", max_bars_back=5000,overlay=true)
// SMA
smaplot = input (false, title="Show MA on chart")
len2 = input(50, minval=1, title="ma Length")
src2 = close
out2 = sma(src2, len2)
colorsma = if out2 > out2[1]
color.green
else if out2 < out2[1]
color.red
else
color.blue
plot(out2, color=colorsma )
//Stochastic
smooth = input(2, minval=1), K = input(3, minval=1), D=input(5,minval=1)
hhs=highest(high,K)
ll=lowest(low,K)
k = sma((close-ll)/(hhs-ll)*100, smooth)
d = sma(k, D)
up3 = d > 50
down3 = d < 50
mycolor3 = up3 ? color.green : down3 ? color.red : color.blue
backgroundColour = mycolor3
//color the chart depending on the stochastic
bgcolor(color=backgroundColour, transp=85)
// color when the candle cross the 50
//crossabove
// colors1 = if d[1] < 50 and d > 50
// color.yellow
// bgcolor(color=colors1, transp=85)
// //crossbelow
// colors2 = if d[1] > 50 and d < 50
// color.blue
// bgcolor(color=colors2, transp=85)
// var test = 0
// test := if d[1] > 50 and d < 50
// 1
// plot(barssince(d[1] < 50 and d > 50 and d[1] > 50 and d < 50),color=color.orange)
crossabove = if d[1] < 50 and d > 50
1
else
0
crossbelow = if d[1] > 50 and d < 50
2
else
0
plotchar(crossabove==1 ,title = "Crossabove" , char="1",color=color.green,location=location.belowbar,transp=0)
plotchar(crossbelow==2 ,title = "Crossbelow" , char="2",color=color.green,location=location.belowbar,transp=0)
temp_BarSincecrossabove = barssince(crossabove==1) + 3
temp_BarSincecrossabove_2 = temp_BarSincecrossabove <= 0 ? 1 : temp_BarSincecrossabove
temp_BarSincecrossabove_3 = na(temp_BarSincecrossabove_2) ? 1 : temp_BarSincecrossabove_2
highestpointBarSincecrossabove = highest(high,temp_BarSincecrossabove_3)
highestBarSincecrossabove = highestbars(high,temp_BarSincecrossabove_3)
plot(highestpointBarSincecrossabove)
// plotchar(bar_index[-highestBarSincecrossabove] ,title = "Highest High", text= '[HH]',color=color.green,location=location.abovebar,transp=0)
Or maybe i need to find the swing high keep track of it , i am unsure at this point. The question that will follow up is how to check the highest high between two different green zone but i guess i first need to figure what logic could be used for this question
Thanks
Upvotes: 1
Views: 2012
Reputation: 8779
Here we monitor the hi/lo when we are in the appropriate zone, and reposition the label when we find a new hi/lo. Because we're using labels, which is the only way in Pine to achieve what you need, you will only see the last ~50 labels.
While they are not needed for the labels to print, the hi
and lo
variables keep track of the values so you can reuse them if you need to:
//@version=4
study(title="whatever", shorttitle="Colored SMA", max_bars_back=5000,overlay=true)
// SMA
smaplot = input (false, title="Show MA on chart")
len2 = input(50, minval=1, title="ma Length")
src2 = close
out2 = sma(src2, len2)
colorsma = if out2 > out2[1]
color.green
else if out2 < out2[1]
color.red
else
color.blue
plot(out2, color=colorsma )
//Stochastic
smooth = input(2, minval=1), K = input(3, minval=1), D=input(5,minval=1)
hhs=highest(high,K)
ll=lowest(low,K)
k = sma((close-ll)/(hhs-ll)*100, smooth)
d = sma(k, D)
up3 = d > 50
down3 = d < 50
mycolor3 = up3 ? color.green : down3 ? color.red : color.blue
backgroundColour = mycolor3
//color the chart depending on the stochastic
bgcolor(color=backgroundColour, transp=85)
var float hi = na
var float lo = na
var label labelHi = na
var label labelLo = na
// Detect crosses.
xUp = crossover( d, 50)
xDn = crossunder(d, 50)
// When entering new zone, reset hi/lo and create a new label.
if xUp
hi := high
labelHi := label.new(bar_index, na, "▼", yloc = yloc.abovebar, color = #000000, textcolor = color.lime, style = label.style_none)
else if xDn
lo := low
labelLo := label.new(bar_index, na, "▲", yloc = yloc.belowbar, color = #000000, textcolor = color.red, style = label.style_none)
// When in a zone, continuously monitor for newer hi/lo and if one is found, move label.
if up3 and high > hi
hi := high
label.set_xy(labelHi, bar_index, na)
else if down3 and low < lo
lo := low
label.set_xy(labelLo, bar_index, na)
plotchar(xUp, title = "Crossabove" , char="1",color=color.green,location=location.belowbar,transp=0)
plotchar(xDn, title = "Crossbelow" , char="2",color=color.green,location=location.belowbar,transp=0)
Upvotes: 4