Search code examples
pine-scriptfractalsindicator

Fractals using Heiken Ashi


I try to create a fractal indicator using Heiken Ashi candles instead of Japanese candles. With HA candles, noise is removed so it's easiest to spot local high/low. The idea is :

  • If I found 3 bullish then 3 bearish HA candles, I want to draw a shape on the highest high of theses 6 candles.

Here my code :

//@version=4
study("Stop hunt") //, overlay=true

haopen  = 0.0
haclose = (open + high + low + close) / 4
haopen := na(haopen[1]) ? (open + close) / 2 : (haopen[1] + haclose[1]) / 2
hahigh  = max(high, max(haopen, haclose))
halow   = min(low,  min(haopen, haclose))
bullHa = haclose > haopen 

cond = bullHa[6] and bullHa[5] and bullHa[4] and not bullHa[3] and not bullHa[2] and not bullHa[1] //fractal
top = highestbars(high, 7)


transpVar = 100
if (cond)
    transpVar := 70 
plot(top)
bgcolor(color.new(color.green, transpVar))
bgcolor(color.new(color.red, transpVar), offset=top)


plotshape(cond, color=color.green, style=shape.triangledown, location=location.abovebar, size=size.tiny, offset=top)

Problem is using Highestbars() and using this value as offset with plotShape(). Sometimes the offset is correctly draw, sometimes it's not. I don't understand. I would be glad if someone can figured out why.

Thank you :)


Solution

  • plotshape(cond, color=color.green, style=shape.triangledown, location=location.abovebar, size=size.tiny, offset=-4)
    

    The peak bullHa[4] is four bars back, so offset will always need to be set to -4.