Lower time frame Intrabar Candles
// This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License https://creativecommons.org/licenses/by-sa/4.0/
// © dman103
// I was looking for an indicator to show me what a lower time frame is doing at the start, middle, and end of the candle, but I couldn't find one, hench,
// I made my own using Tradingview latest capabilities to fetch a lower time frame from a higher time frame chart.
// For example, if your chart is 1 hour and this indicator is set to a lower time frame of 15 minutes, then the Start, Middle, or End (Select which in settings) of the 15min candle will be displayed overlaying the 1-hour candle.
// This will always show you what the lower time frame candle is currently doing without the need to open an additional lower time frame chart. How cool is that?
// Remember to select a lower time frame in the settings than the chart time frame for it to work as expected.

// Added optional label to show lower time frame values (can be disabled via indicator settings).
// You can adjust the visuals of the chart candles at Chart Settings -> Symbol -> Body, Borders, or Wicks.
// You can also visually see better the lower time frame candles
//
// Note that lower time frame candles BORDER is set to WHITE by default, however, you can adjust the color of the candle (wicks, body, and border) of the 'LTF Candle' indicator inside its Settings -> Style
// Suitable for candles and designed to work in historical and real time.
// Will introduce in the future more interesting concepts based on lower time frame capabilities: https://www.tradingview.com/u/dman103/#published-scripts
// Follow and like if you like.
//@version=5
indicator("Lower time frame Intrabar Candles",'LTF Candle',overlay = true)
lower_time_frame= input.timeframe('5','Intrabar lower time frame', tooltip='Which lower time frame candle to overlay on the chart. Must be lower than the chart time frame.')

candle_to_display=input.string( "Candle End","Lower time frame Candle to overlay", options = ["Candle Start", "Candle End","Candle Middle"])
var candle_type=  candle_to_display=='Candle End' ? 1 :  candle_to_display=='Candle Start' ?  2 : 3
show_only_last_bar=  input.bool(false,'Show only most recent lower time frame candle')
display_lower_tf_label = input.bool(false,'Display most recent candle values')

[l_open,l_high,l_low,l_close]  = request.security_lower_tf(syminfo.tickerid, lower_time_frame,[open,high,low,close])
varip float high_value_get=0
varip float low_value_get=0
varip float open_value_get=0
varip float close_value_get=0
varip int pre_candle_length_ltf=0
if barstate.isconfirmed
    pre_candle_length_ltf:=array.size(l_close)
if (array.size(l_close)>0)
    high_value_get:= candle_type==1 and pre_candle_length_ltf==array.size(l_high) ? array.get(l_high,pre_candle_length_ltf-1)  : candle_type==2 ?  array.get(l_high,0) : candle_type==3 and  pre_candle_length_ltf/2==array.size(l_high)/2 ? array.get(l_high,pre_candle_length_ltf/2) : na
    low_value_get:= candle_type==1  and pre_candle_length_ltf==array.size(l_low)? array.get(l_low,pre_candle_length_ltf-1)  : candle_type==2   ? array.get(l_low,0) :  candle_type==3 and  pre_candle_length_ltf/2==array.size(l_low)/2 ? array.get(l_low,pre_candle_length_ltf/2) : na
    open_value_get:= candle_type==1  and pre_candle_length_ltf==array.size(l_open)? array.get(l_open,pre_candle_length_ltf-1) : candle_type==2  ?  array.get(l_open,0) :  candle_type==3 and  pre_candle_length_ltf/2==array.size(l_open)/2 ? array.get(l_open,pre_candle_length_ltf/2) : na
    close_value_get:= candle_type==1   and pre_candle_length_ltf==array.size(l_close)? array.get(l_close,pre_candle_length_ltf-1) : candle_type==2  ?   array.get(l_close,0):  candle_type==3 and  pre_candle_length_ltf/2==array.size(l_close)/2 ? array.get(l_close,pre_candle_length_ltf/2) : na
    
h_tf=(array.size(l_high)>0 and (((barstate.islast  or barstate.isrealtime) and show_only_last_bar) or show_only_last_bar==false))  ? high_value_get : na
l_tf=(array.size(l_low)>0 and  (((barstate.islast  or barstate.isrealtime) and show_only_last_bar) or show_only_last_bar==false))  ? low_value_get: na
o_tf=(array.size(l_open)>0 and  (((barstate.islast or barstate.isrealtime) and show_only_last_bar) or show_only_last_bar==false))  ? open_value_get: na
c_tf=(array.size(l_close)>0 and (((barstate.islast or barstate.isrealtime) and show_only_last_bar) or show_only_last_bar==false))  ? close_value_get : na
close_value=  c_tf
plotcandle(o_tf,h_tf,l_tf,close_value,color=close_value>o_tf ? color.new(#a5d6a7,60) : color.new(#faa1a4,60),wickcolor =close_value>o_tf ? color.new(color.lime,0) : color.new(color.red,0),bordercolor =color.white,show_last=show_only_last_bar ? 1 : na)


var label val_tf = na
if barstate.islast and display_lower_tf_label
    val_tf:=label.new(bar_index, c_tf>o_tf ? low : high, 
      "■ Open " + str.tostring(o_tf) +
      " ■ Close " + str.tostring(c_tf)+
      "\n■ High " + str.tostring(h_tf) +
      " ■ Low " +  str.tostring(l_tf),color=c_tf>o_tf ? color.new(color.aqua,70) : color.new(color.red,70),style=c_tf>o_tf ?label.style_label_up : label.style_label_down, textcolor=color.white)
label.delete(val_tf[1])    
      
