Lower TimeFrame Mini Candle[rsu]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rsu9
// 5m and 30m Mini Candle and Support 200MA Crossing Alert


//@version=5
indicator("LTF-MiniCandle[rsu]",max_bars_back=500,max_lines_count=500)


leftTF=input.timeframe("5", "Lower Time Frame", options=['1', '5', '15','30','60','120','240'],group="Left Window")
lefeWindow=input.int(50,"Windows Position, Offset 100 bars from right to left",group="Left Window")
LeftTFR=leftTF==timeframe.period?"":leftTF


rightTF=input.timeframe('30', "Lower Time Frame", options=['1', '5', '15','30','60','120','240'],group="Right Window")
rightWindow=input.int(-10,"Windows Position, push 10 to the right",group="Right Window")
RightTFR=rightTF==timeframe.period?"":rightTF

if rightTF==timeframe.period
    lefeWindow:=rightWindow


//shorten lower data to 60
shortenData(array1)=>
    m=60
    ret=array.size(array1)>m?array.slice(array1,array.size(array1)-m+1,array.size(array1)-1):array1
// order the Candle
shift_left_candles(array1,offest)=>
    // 3 "for" loops, not to get loop error
    for x = 0 to (array.size(array1) > 0 ? array.size(array1) - 1 : na)
        line.set_x1(array.get(array1, x), bar_index - x-offest)
        line.set_x2(array.get(array1, x), bar_index - x-offest)
//Label
if timeframe.period!=leftTF
    s1=label.new(bar_index-lefeWindow-50,high,text="["+str.tostring(LeftTFR)+"]",textcolor=color.new(color.gray,30),style=label.style_none)
    label.delete(s1[1])
if timeframe.period!=rightTF
    s2=label.new(bar_index-rightWindow-50,high,text="["+str.tostring(rightTF)+"]",textcolor=color.new(color.gray,30),style=label.style_none)
    label.delete(s2[1])
//------
//5m
//------

close_5m_array=shortenData(request.security_lower_tf(syminfo.tickerid, LeftTFR, close))
open_5m_array=shortenData(request.security_lower_tf(syminfo.tickerid, LeftTFR, open))
high_5m_array=shortenData(request.security_lower_tf(syminfo.tickerid, LeftTFR, high))
low_5m_array=shortenData(request.security_lower_tf(syminfo.tickerid, LeftTFR, low))
ma20_5m_array=shortenData(request.security_lower_tf(syminfo.tickerid, LeftTFR, ta.sma(close,20)))
ema20_5m_array=shortenData(request.security_lower_tf(syminfo.tickerid, LeftTFR, ta.ema(close,20)))
ma200h_5m_array=shortenData(request.security_lower_tf(syminfo.tickerid, LeftTFR, ta.sma(high,200)))
ma200l_5m_array=shortenData(request.security_lower_tf(syminfo.tickerid, LeftTFR, ta.sma(low,200)))



var candles_line_5mArray = array.new_line(0)
var avg20_line_5mArray = array.new_line(0)
var avg200_line_5mArray = array.new_line(0)
var float pre200_5m=na
kcross200_5m=false

// PLOT Candle
if array.size(close_5m_array) >0 and leftTF!=timeframe.period 
    // draw/keep candle body
    for x=0 to array.size(close_5m_array)-1 
        o=array.get(open_5m_array,x)
        c=array.get(close_5m_array,x)
        array.unshift(candles_line_5mArray,line.new(x1 = bar_index-array.size(close_5m_array)-x, y1 = c, x2 = bar_index-array.size(close_5m_array)-x, y2 = o,width = 3,color=c>o?color.green:color.red))
        // remove old array elements
        if array.size(candles_line_5mArray) > 50
            line.delete(array.pop(candles_line_5mArray))  

        o1=array.get(ma20_5m_array,x)
        c1=array.get(ema20_5m_array,x)
        array.unshift(avg20_line_5mArray,line.new(x1 = bar_index-array.size(avg20_line_5mArray)-x, y1 = c1, x2 = bar_index-array.size(avg20_line_5mArray)-x, y2 = o1,width = 2,color=color.gray))
        // remove old array elements
        if array.size(avg20_line_5mArray) > 50
            line.delete(array.pop(avg20_line_5mArray))  

        o2=array.get(ma200h_5m_array,x)
        c2=array.get(ma200l_5m_array,x)
        array.unshift(avg200_line_5mArray,line.new(x1 = bar_index-array.size(avg200_line_5mArray)-x, y1 = c2, x2 = bar_index-array.size(avg200_line_5mArray)-x, y2 = o2,width = 1,color=c2>=pre200_5m?color.new(color.green,0):color.new(color.red,0)))
        pre200_5m:=c2
        // remove old array elements
        if array.size(avg200_line_5mArray) > 50
            line.delete(array.pop(avg200_line_5mArray))  
            
        h=array.get(high_5m_array,x)
        l=array.get(low_5m_array,x) 
        if (h>o2 and l<o2) or (h>c2 and l<c2)
            kcross200_5m:=true

shift_left_candles(candles_line_5mArray,lefeWindow)    
shift_left_candles(avg20_line_5mArray,lefeWindow)
shift_left_candles(avg200_line_5mArray,lefeWindow)  

//------
//30m
//------

close_30m_array=shortenData(request.security_lower_tf(syminfo.tickerid, RightTFR, close))
open_30m_array=shortenData(request.security_lower_tf(syminfo.tickerid, RightTFR, open))
high_30m_array=shortenData(request.security_lower_tf(syminfo.tickerid, RightTFR, high))
low_30m_array=shortenData(request.security_lower_tf(syminfo.tickerid, RightTFR, low))
ma20_30m_array=shortenData(request.security_lower_tf(syminfo.tickerid, RightTFR, ta.sma(close,20)))
ema20_30m_array=shortenData(request.security_lower_tf(syminfo.tickerid, RightTFR, ta.ema(close,20)))
ma200h_30m_array=shortenData(request.security_lower_tf(syminfo.tickerid, RightTFR, ta.sma(high,200)))
ma200l_30m_array=shortenData(request.security_lower_tf(syminfo.tickerid, RightTFR, ta.sma(low,200)))

var candles_line_30mArray = array.new_line(0)
var avg20_line_30mArray = array.new_line(0)
var avg200_line_30mArray = array.new_line(0)
var float pre200_30m=na
kcross200_30m=false

// PLOT Candle
if array.size(close_30m_array) >0 and rightTF!=timeframe.period
    // draw/keep candle body
    for x=0 to array.size(close_30m_array)-1 
        o=array.get(open_30m_array,x)
        c=array.get(close_30m_array,x)
        array.unshift(candles_line_30mArray,line.new(x1 = bar_index-array.size(close_30m_array)-x, y1 = c, x2 = bar_index-array.size(close_30m_array)-x, y2 = o,width = 3,color=c>o?color.green:color.red))
        // remove old array elements
        if array.size(candles_line_30mArray) > 50
            line.delete(array.pop(candles_line_30mArray))  

        o1=array.get(ma20_30m_array,x)
        c1=array.get(ema20_30m_array,x)
        array.unshift(avg20_line_30mArray,line.new(x1 = bar_index-array.size(avg20_line_30mArray)-x, y1 = c1, x2 = bar_index-array.size(avg20_line_30mArray)-x, y2 = o1,width = 2,color=color.gray))
        // remove old array elements
        if array.size(avg20_line_30mArray) > 50
            line.delete(array.pop(avg20_line_30mArray))  

        o2=array.get(ma200h_30m_array,x)
        c2=array.get(ma200l_30m_array,x)
        array.unshift(avg200_line_30mArray,line.new(x1 = bar_index-array.size(avg200_line_30mArray)-x, y1 = c2, x2 = bar_index-array.size(avg200_line_30mArray)-x, y2 = o2,width = 1,color=c2>=pre200_30m?color.new(color.green,0):color.new(color.red,0)))
        pre200_30m:=c2
        // remove old array elements
        if array.size(avg200_line_30mArray) > 50
            line.delete(array.pop(avg200_line_30mArray))  
            
        h=array.get(high_30m_array,x)
        l=array.get(low_30m_array,x) 
        if (h>o2 and l<o2) or (h>c2 and l<c2)
            kcross200_5m:=true
            
shift_left_candles(candles_line_30mArray,rightWindow)    
shift_left_candles(avg20_line_30mArray,rightWindow)
shift_left_candles(avg200_line_30mArray,rightWindow)    


////////////
// Alerts //
////////////
alertname=syminfo.ticker

if kcross200_5m
    alert(alertname+" 5m k cross 200",alert.freq_once_per_bar)
if kcross200_30m
    alert(alertname+" 30m k cross 200",alert.freq_once_per_bar)
