[JL] Cross Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jesse.Lau

//@version=5
indicator('[JL] Cross Candles', overlay=true,max_bars_back = 4900)


Cross_select = input.string(title='Select Indicator:', defval='Stoch', options=['Stoch','RSI', 'MovingAverage', 'MACD','StochRSI','SAR','DMI','SuperTrend'])

// RSI
rsi_len = input.int(14, minval=1, title="RSI Length",     group = 'RSI')
rsi_len1 = input.int(5, minval=1, title="Average Period",     group = 'RSI')
rsi = ta.rsi(close, rsi_len)
rsia = ta.sma(rsi,rsi_len1)
rsi_green = ta.crossover(rsi,rsia) 
rsi_red = ta.crossunder(rsi,rsia)

//MA Cross
shortlen = input.int(9, "Short MA Length", minval=1,     group = 'MovingAverage')
longlen = input.int(21, "Long MA Length", minval=1,     group = 'MovingAverage')
short = ta.sma(close, shortlen)
long = ta.sma(close, longlen)
ma_green = ta.crossover(short,long) 
ma_red = ta.crossunder(short,long)

//Stochastic
periodK = input.int(14, title="Stochastic %K Length", minval=1,     group = 'Stochastic')
smoothK = input.int(1, title="Stochastic %K Smoothing", minval=1,     group = 'Stochastic')
periodD = input.int(3, title="Stochastic %D Smoothing", minval=1,     group = 'Stochastic')
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
stoch_green = ta.crossover(k,d) 
stoch_red = ta.crossunder(k,d)

   
// MACD
fast_length = input(title="MACD Fast Length", defval=12,     group = 'MACD')
slow_length = input(title="MACD Slow Length", defval=26,     group = 'MACD')
src = input(title="MACD Source", defval=close,     group = 'MACD')
signal_length = input.int(title="MACD Signal Smoothing",  minval = 1, maxval = 50, defval = 9,     group = 'MACD')
sma_source = input.string(title="MACD Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"],     group = 'MACD')
sma_signal = input.string(title="MACD Signal Line MA Type", defval="EMA", options=["SMA", "EMA"],     group = 'MACD')
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
macd_green = ta.crossover(macd,signal) 
macd_red = ta.crossunder(macd,signal)

   

//SAR
start = input(0.02,     group = 'Parabolic SAR')
increment = input(0.02,     group = 'Parabolic SAR')
maximum = input(0.2, "Max Value",     group = 'Parabolic SAR')
out = ta.sar(start, increment, maximum)
sar_green = ta.crossover(close,out) 
sar_red = ta.crossunder(close,out)

//DMI

len = input.int(14, minval=1, title="DI Length",     group = 'DMI')
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
dmi_green = ta.crossover(plus,minus) 
dmi_red = ta.crossunder(plus,minus)

//Stochastic RSI
sr_smoothK = input.int(3, "K", minval=1,     group = 'Stochastic RSI')
sr_smoothD = input.int(3, "D", minval=1,     group = 'Stochastic RSI')
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
sr_src = input(close, title="RSI Source")
rsi1 = ta.rsi(sr_src, lengthRSI)
sr_k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), sr_smoothK)
sr_d = ta.sma(sr_k, sr_smoothD)
stochrsi_green = ta.crossover(sr_k,sr_d) 
stochrsi_red = ta.crossunder(sr_k,sr_d)

//Supertrend

atrPeriod = input(10, "ATR Length",     group = 'Supertrend')
factor = input.float(3.0, "Factor", step = 0.01,     group = 'Supertrend')
[supertrend, direction] = ta.supertrend(factor, atrPeriod)

Supertrend_green = ta.crossunder(direction,0) 
Supertrend_red = ta.crossover(direction,0)



// Cross Signals
turnGreen = Cross_select == 'RSI' ? rsi_green : Cross_select == 'MovingAverage' ? ma_green : Cross_select == 'Stoch' ? stoch_green : Cross_select == 'MACD' ? macd_green : Cross_select == 'StochRSI' ? stochrsi_green : Cross_select == 'SAR' ? sar_green : Cross_select == 'DMI' ? dmi_green : Cross_select == 'SuperTrend' ? Supertrend_green : stoch_green

turnRed = Cross_select == 'RSI' ? rsi_red : Cross_select == 'MovingAverage' ? ma_red : Cross_select == 'Stoch' ? stoch_red : Cross_select == 'MACD' ? macd_red : Cross_select == 'StochRSI' ? stochrsi_red : Cross_select == 'SAR' ? sar_red : Cross_select == 'DMI' ? dmi_red : Cross_select == 'SuperTrend' ? Supertrend_red : stoch_red

//Draw Candles
barsturngreen = bar_index - ta.valuewhen(turnGreen, bar_index, 0)
barsturnred = bar_index - ta.valuewhen(turnRed, bar_index, 0)
barsg = barsturngreen>0 ? barsturngreen : 1

h1 = ta.highest(high,barsg)
l1 = ta.lowest(low,barsg)
barsr = barsturnred>0 ? barsturnred : 1

h2 = ta.highest(high,barsr)
l2 = ta.lowest(low,barsr)
 

if turnRed 
    box.new(bar_index - barsg, h1, bar_index, l1, border_color = na, bgcolor = color.new(#7CFC00, 50))
if turnGreen
    
    box.new(bar_index - barsr, h2, bar_index, l2, border_color = na, bgcolor = color.new(#FF4500, 50))