Heiken Ashi Lower Pane
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PapaAlan
// Not one of my more challenging scripts, never the less I was requested to publish this open source indicator.
// Heiken Ashi (HA) candles indicate strength usually when the candles have wicks in the direction of the movement, ie. top wicks on green candles w/NO wicks on bottom and vice versa for bearish behavior (bottom wicks on red candles).  Weakness in the movement CAN be spotted by watching for wicks opposite the movement appearing.
// This indicator can be used in a lower pane to show heiken ashi candles concurrently with above main chart regular candles.
// Nothing special about it other than displaying bull/bear ha candles with a twist of third color candle (orange default) which is shown when HA candle gets a wick in opposite direction of movement which usually indicates potential directional weakness.
// I also provides various moving average line types based upon the HA high, low, close, open values (HLC4) that can used.
// Note:  You can also display this over the main chart as an overlay just by selecting the three dots on the indicator and "Move to" option.  Be advised doing so will probably cause too much overlapping onto the regular candles.

//@version=5
indicator("Heiken Ashi Lower Pane",shorttitle="HA Lower Pane")

ma_type_descrip = "EMA - Exponential MA, RMA - Running MA (Smoothed), WMA - Weighted MA, VWMA - Volume Weighted MA, HMA - Hull MA"
show_custom_ma  = input.bool(defval=true,title='Show Custom MA')
ma_type         = input.string(defval="HMA"  ,title='Custom MA Type', options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"],tooltip= ma_type_descrip)
ma_len          = input.int(20, minval=1, title='Custom MA Length')
tf              = input.string(defval='5',title='Timeframe')
habull_color    = input.color(defval=color.yellow,title='Bull Candle Color')
habear_color    = input.color(defval=color.fuchsia,title='Bear Candle Color')
habull_wcolor   = input.color(defval=color.yellow,title='Bull Wick Color')
habear_wcolor   = input.color(defval=color.fuchsia,title='Bear Wick Color')
haneutral_color = input.color(defval=color.white,title='Neutral Candle Color')

timeframe = tf//timeframe.period
hkopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe, open)
hkhigh = request.security(ticker.heikinashi(syminfo.tickerid), timeframe, high)
hklow = request.security(ticker.heikinashi(syminfo.tickerid), timeframe, low)
hkclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe, close)

// var testTable = table.new(position = position.bottom_right, columns = 1, rows = 1, bgcolor = color.yellow, border_width = 1)
// table.cell(table_id = testTable, column = 0, row = 0, text = str.tostring(hkopen) + "   " + str.tostring(hkclose) + "  " + str.tostring(hkhigh) + "  " + str.tostring(hklow))
IsSessionStart(sessionTime, sessionTimeZone=syminfo.timezone) =>
    inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone))
    inSess and not inSess[1]

isFirstBarOfMarket = IsSessionStart("0930-1600")

float o = na
int openbarindex = na
openbarindex := isFirstBarOfMarket ? bar_index : openbarindex[1]
o := isFirstBarOfMarket ? open : o[1]
// save code for future possible usage with lower tf request.security in obtaining the expected multiple values stored by that function
// _get_values(_array,_len) =>
//     val1 = 0.0
//     for i=0 to array.size(_array) -1
//         val1 := val1 + array.get(_array, i)
//     tval = val1 / 2

val1 = 0.0    
  
direction = hkopen < hkclose ? 1 : hkopen > hkclose ? -1 : 0

//up_downwick = direction and hkopen > hklow ? true : false
//dn_upwick = (not direction) and hkhigh > hkopen ? true : false
//c_type = up_downwick or dn_upwick ? color.new(color.orange,0) : hkopen < hkclose ? color.new(#4caf50,0) : color.new(#801922,0) 
c_type = direction == 1 ? habull_color : direction == -1 ? habear_color : haneutral_color//h hkopen < hkclose ? color.new(#4caf50,0) : color.new(#f23645,0) 
wickcolor = direction == 1 ? habull_wcolor : direction == -1 ? habear_wcolor : haneutral_color//color.new(#4caf50,0) : color.new(#f23645,0) 
plotcandle(open=hkopen,high=hkhigh,low=hklow,close=hkclose,title='PlotCandle',color=c_type,wickcolor=wickcolor)
//plotcandle(open=hkopen-o,high=hkhigh-o,low=hklow-o,close=hkclose-o,title='PlotCandle',color=c_type,wickcolor=wickcolor)

ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     type == "HMA" ? ta.hma(source, length) :
     na

tot = (hkclose + hkopen + hkhigh + hklow) / 4

combhmaclose    =   ma(tot,ma_len,ma_type) 
plot(show_custom_ma ? combhmaclose : na,color=color.white)

arrow_up = hkclose > combhmaclose and hkclose[1] <= combhmaclose 
arrow_down = hkclose < combhmaclose and hkclose[1] >= combhmaclose 

