2

I'm trying to code an indicator using the heikin ashi open and close prices.

I want the program to draw a horizontal ray on the highest close price of a group of consecutive green or red heikin ashi candles.

For example, if the heikin ashi trend changes from red candles to green. I would ask the program to monitor the consecutive heikin green candles pick the highest closing price of the group of green and draw a horizontal ray.

Please see the picture above where I manually drew the horizontal rays according to the criteria I want.

I have written the below code but the problem is that it is drawing a horizontal ray on each candle that has a closing price higher(in case of green candle) or lower(in case of red candle) than the previous one. What I need to is to pick the highest closing price of a group of heikin ashi candles of the same color.

enter image description here

//@version=5
indicator("Heinkin Ashi 
lines",shorttitle="HAL",overlay=true,max_bars_back=500)
time_frame=input.timeframe('W',"timeframe", options=['D', 'W', 'M'])

haTicker = ticker.heikinashi(syminfo.tickerid)
[haO, haH, haL, haC] = request.security(haTicker, time_frame, [open, high, 
low, close])

float haC_green=0.000
if haC>=haO and haC >=haC[2] and haC >= haC[2]
    haC_green:=haC
 
line.new(0,haC_green,bar_index,haC_green, 
xloc=xloc.bar_index,extend=extend.none,color=color.black)

float haC_red=0.000
if haC<=haO and haC <=haC[2]
   haC_red:=haC

line.new(0,haC_red,bar_index,haC_red, 
xloc=xloc.bar_index,extend=extend.none,color=color.red)
vitruvius
  • 15,740
  • 3
  • 16
  • 26
Mike
  • 23
  • 3

1 Answers1

1

You need to figure out if you are still in the same color group or if it is a new one. To do that, compare current candle's color with the previous one.

Once you figure that out, compare the close price and update your line's position if necassary.

Here is an example for you:

//@version=5
indicator("Heinkin Ashi lines",shorttitle="HAL",overlay=true,max_bars_back=500)

var line green_line = na
var line red_line = na

var float highest_close = na
var float lowest_close = na

is_green = close > open
is_red = not is_green
is_new_green = is_red[1] and is_green
is_new_red = is_green[1] and is_red
is_still_green = is_green[1] and is_green
is_still_red = is_red[1] and is_red

if (is_new_green)
    highest_close := close
    green_line := line.new(bar_index, highest_close, bar_index+1, highest_close, extend=extend.right, color=color.green)

if (is_still_green)
    if (close > highest_close)
        highest_close := close
        line.set_xy1(green_line, bar_index, highest_close)
        line.set_xy2(green_line, bar_index+1, highest_close)

if (is_new_red)
    lowest_close := close
    red_line := line.new(bar_index, lowest_close, bar_index+1, lowest_close, extend=extend.right, color=color.red)

if (is_still_red)
    if (close < lowest_close)
        lowest_close := close
        line.set_xy1(red_line, bar_index, lowest_close)
        line.set_xy2(red_line, bar_index+1, lowest_close)

enter image description here

vitruvius
  • 15,740
  • 3
  • 16
  • 26