Heikenashi higher timeframe indicator

This indicator gives you the ability to display a heiken ashi candle on top of your regular chart. The period of the candle can be changed. The example above shows the 1W heiken ashi candle on top of the 4h candle.


// @version=5
indicator('Heikenashi higher timeframe indicator', overlay=true)


// define the global variable to keep the scope data
var resolutionContextIndex = 0


// define the resolution input field
resolution = input.timeframe(title='Candle resolution', defval='', tooltip="Select the target timeframe of the candle. Ensure that the selected value equals or higher than the chart's timeframe.")


// *******************************************************************************************
// Indicator helper functions
// *******************************************************************************************
heikenashi(resolution, finalised) =>
    rhaopen = request.security(ticker.heikinashi(syminfo.tickerid), resolution, open)
    rhahigh = request.security(ticker.heikinashi(syminfo.tickerid), resolution, high)
    rhalow = request.security(ticker.heikinashi(syminfo.tickerid), resolution, low)
    rhaclose = request.security(ticker.heikinashi(syminfo.tickerid), resolution, close)

    if finalised
        [rhaopen, rhahigh, rhalow, rhaclose]
    else
        [float(na), float(na), float(na), float(na)]

resolutionToSeconds() =>
    timeframe = timeframe.isseconds ? 1 : timeframe.isminutes ? 1 * 60 : timeframe.isdaily ? 60. * 24 * 60 : timeframe.isweekly ? 60. * 24 * 7 * 60 : int(na)
    seconds = timeframe.multiplier * timeframe
    seconds

convertDirectionToInteger(value) =>
    na(value) ? 0 : value ? 1 : -1

convertBooleanToInteger(value) =>
    value ? 1 : 0


// *******************************************************************************************
// Indicator rendering logic
// *******************************************************************************************
// get current timeframe value in seconds
currentResolutionSeconds = resolutionToSeconds()

// get target timeframe value in seconds
targetResoultionSeconds = not na(resolution) ? request.security(syminfo.tickerid, resolution, resolutionToSeconds()) : currentResolutionSeconds

// calculate count of small bars within higher bar
resolutionStep = math.round(targetResoultionSeconds / currentResolutionSeconds)
plotchar(resolutionStep, title='Count of smallers bars in one higher bar', char='', color=color.new(color.purple, 0))

// resolve smaller bar initial value on higher bar index change
resolutionIndex = request.security(syminfo.tickerid, resolution, bar_index, barmerge.gaps_off, barmerge.lookahead_on)
plotchar(resolutionIndex, title='Resultion bar index', char='', color=color.new(color.purple, 0))

// if the bar is under the new higher resolution
// we reset bar index into zero so we can determine
// when the new bigger bar is started with the current bar
resolutionContextIndex := resolutionIndex[0] != resolutionIndex[1] ? 0 : resolutionContextIndex + 1
plotchar(resolutionContextIndex, title='Smaller bar index within higher bar', char='', color=color.new(color.purple, 0))

// resolve whether current bar is latest under the higher bar or not
isResolutionFinalised = resolutionContextIndex == resolutionStep - 1
plotchar(convertBooleanToInteger(isResolutionFinalised), title='Is resolution finalised', char='', color=color.new(color.purple, 0))


// calculate target candle
[rhaopen, rhahigh, rhalow, rhaclose] = heikenashi(resolution, isResolutionFinalised)
rhacolor = rhaclose > rhaopen ? #66CCFF : #FF9966
plotcandle(rhaopen, rhahigh, rhalow, rhaclose, title='Candle', color=rhacolor, wickcolor=color.black, bordercolor=#333333)
Made on
Tilda