Trendalix Entries
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MarkLudgate

//@version=5
indicator("Trendalix Entries", overlay =true)


tf = timeframe.period
ticker = syminfo.ticker
market_type = syminfo.type

unix_end_in_secs = timenow/1000
unix_in_secs = time/1000
unix_no_break = unix_in_secs
secs = time-time[1]

show_candles = input(title="Show candle gap filled", defval=true)
show_entry_stop = input(title="Show entry/stop lines",  defval=false)

if syminfo.type == 'stock' or syminfo.type == 'index'
    show_entry_stop:=true

show_mas = input(title="Show MAs", defval=true)


_open = open
_high = high
_low = low
_close = close
c_open = _close[1]


atr = ta.atr(30) // dont change will break labels

// sma_temp = ta.sma(src, length)

// smma(src, length) =>
// 	smma =  0.0
// 	smma := na(smma[1]) ? sma_tem : (smma[1] * (length - 1) + src) / length
// 	smma

ma_5 = ta.sma(_close, 5)
ma_10 = ta.sma(_close, 10)
ma_20 = ta.sma(_close, 20)
ma_30 = ta.sma(_close, 30)
ma_50 = ta.sma(_close, 50)
ma_100 = ta.sma(_close, 100)
ma_150 = ta.sma(_close, 150)
ma_200 = ta.sma(_close, 200)
ma_400 = ta.sma(_close, 400) // for stocks


hl_price = (_high+_low)/2

//~~~~~~~~~~~~~~~~~~~~~~~~~
aligator_a_ini = ta.sma(close, 10)
aligator_b_ini = ta.sma(close, 20)
aligator_c_ini = ta.sma(close, 30)

aligator_a = aligator_a_ini[5]
aligator_b = aligator_b_ini[7]
aligator_c = aligator_c_ini[10]

if(syminfo.type != 'crypto' or tf!='D')
    aligator_a := ma_30
    aligator_b := ma_50
    aligator_c := ma_100



// PLOT CANDLE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
// up_col = color.lime
// down_col = color.red
up_col = #00ffe9
down_col = #ff4930

up_col_b = color.aqua
down_col_b = color.fuchsia
mas_up = color.green
mas_down = color.red

up_dull = #005c54
down_dull = #6b0d00

color cand_col = color.yellow
if _close > c_open
    cand_col := up_col

if _close < c_open
    cand_col := down_col
    


// // highest lowest ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// highs_3 = highest(_high, 3)
highs_5 = ta.highest(_high, 5)

highs_10 = ta.highest(_high, 10)
highs_20 = ta.highest(_high, 20)
// highs_50 = highest(_high, 50)

// lows_3 = lowest(_low, 3)
lows_5 = ta.lowest(_low, 5)

lows_10 = ta.lowest(_low, 10)
lows_20 = ta.lowest(_low, 20)
// lows_50 = ta.lowest(_low, 50)




// FRACTALS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_fracs(h_xx, high_this, low_this, highs_arr,lows_arr, mid_bar, unix_in_secs)=>
    
    fr_h = high_this[mid_bar*h_xx] >= highs_arr
    fr_l = low_this[mid_bar*h_xx] <=lows_arr
    
    since_fr_h = ta.barssince(fr_h)
    since_fr_l = ta.barssince(fr_l)
    
    fr_h_a = ta.valuewhen(fr_h, high_this[mid_bar*h_xx], 0*h_xx)
    fr_l_a = ta.valuewhen(fr_l, low_this[mid_bar*h_xx], 0*h_xx)
    fr_h_b = ta.valuewhen(fr_h, high_this[mid_bar*h_xx], 1*h_xx)
    fr_l_b = ta.valuewhen(fr_l, low_this[mid_bar*h_xx], 1*h_xx)
    fr_h_c = ta.valuewhen(fr_h, high_this[mid_bar*h_xx], 2*h_xx)
    fr_l_c = ta.valuewhen(fr_l, low_this[mid_bar*h_xx], 2*h_xx)
    fr_h_d = ta.valuewhen(fr_h, high_this[mid_bar*h_xx], 3*h_xx)
    fr_l_d = ta.valuewhen(fr_l, low_this[mid_bar*h_xx], 3*h_xx)
    
    h_a_time = ta.valuewhen(fr_h, unix_in_secs[mid_bar*h_xx], 0*h_xx)
    l_a_time = ta.valuewhen(fr_l, unix_in_secs[mid_bar*h_xx], 0*h_xx)
    h_b_time = ta.valuewhen(fr_h, unix_in_secs[mid_bar*h_xx], 1*h_xx)
    l_b_time = ta.valuewhen(fr_l, unix_in_secs[mid_bar*h_xx], 1*h_xx)
    h_c_time = ta.valuewhen(fr_h, unix_in_secs[mid_bar*h_xx], 2*h_xx)
    l_c_time = ta.valuewhen(fr_l, unix_in_secs[mid_bar*h_xx], 2*h_xx)
    h_d_time = ta.valuewhen(fr_h, unix_in_secs[mid_bar*h_xx], 3*h_xx)
    l_d_time = ta.valuewhen(fr_l, unix_in_secs[mid_bar*h_xx], 3*h_xx)
    
    [fr_h_a, fr_l_a, fr_h_b, fr_l_b, fr_h_c, fr_l_c, fr_h_d, fr_l_d, fr_h, fr_l, h_a_time, l_a_time, h_b_time, l_b_time, h_c_time, l_c_time, h_d_time, l_d_time, since_fr_h, since_fr_l]


frac_opt_highs = ta.highest(high, 9)
frac_opt_lows = ta.lowest(low, 9)
frac_opt_mid = 5

if syminfo.type == 'stock'
    frac_opt_highs := ta.highest(high, 15)
    frac_opt_lows := ta.lowest(low, 15)
    frac_opt_mid := 7



// [fr_h_a, fr_l_a, fr_h_b, fr_l_b, fr_h_c, fr_l_c, fr_h_d, fr_l_d,fr_h_5, fr_l_5, h_a_time, l_a_time, h_b_time, l_b_time, h_c_time, l_c_time, h_d_time, l_d_time, since_fr_h, since_fr_l] = get_fracs(1, _high, _low, highs_5, lows_5, 2, unix_in_secs)
[fr_h_a, fr_l_a, fr_h_b, fr_l_b, fr_h_c, fr_l_c, fr_h_d, fr_l_d,fr_h_5, fr_l_5, h_a_time, l_a_time, h_b_time, l_b_time, h_c_time, l_c_time, h_d_time, l_d_time, since_fr_h, since_fr_l] = get_fracs(1, _high, _low, frac_opt_highs, frac_opt_lows, frac_opt_mid, unix_in_secs)

min_frac_lows = math.min(fr_l_a, fr_l_b)

stop_buffer = (atr/5)

color col_a = #32a8a4
color col_b = mas_up
color fillColourA = #32a8a4 //#424242
color fillColourB = #32a8a4 
color fillColourC = #32a8a4 



// ---------------------------------------------
// ---------------------------------------------
// STOCKS

// sell_line_ini_stocks = ta.lowest(_low, 30)
// sell_line_stocks = sell_line_ini_stocks[20]

sell_line_stocks = fr_l_a

if sell_line_stocks > lows_10
    sell_line_stocks:= lows_10
    
if syminfo.type !='stock' and syminfo.type !='index'
    sell_line_stocks:=na
    


is_stock_entry = false
float buy_entry = na
float buy_stop = na

float stock_entry = fr_h_a
// if stock_entry>highs_5
//     stock_entry:=highs_5

stock_stop = fr_l_a
if stock_stop>lows_10
    stock_stop:=lows_10
    
since_low_under_50 = ta.barssince(_low<ma_50) 

if (syminfo.type =='stock' or syminfo.type =='index')  and show_entry_stop == true
    if (ma_100>ma_200 or ma_100>ma_150 or ma_50>ma_100 or ma_50>ma_200 ) and ma_200>ma_400
        if close < stock_entry and stock_entry>ma_200
            if since_low_under_50<=10
                is_stock_entry:=true
                buy_entry:= (stock_entry+stop_buffer)
                buy_stop:= (stock_stop-stop_buffer)
                // sell_line_stocks:=na

// plotshape(is_stock_entry, style=shape.arrowup, location=location.belowbar, color=color.lime, size=size.tiny,  title = 'sock entry')
plot(series=buy_entry, title='stock: buy line', linewidth=1, color=color.lime, style=plot.style_linebr, join=true, offset=0)
plot(series=buy_stop, title='stock: buy stop', linewidth=1, color=color.orange, style=plot.style_linebr, join=true, offset=0)

plot(series=(sell_line_stocks-stop_buffer), title='stock sell line', linewidth=1, color=color.new(color.fuchsia, 35), style=plot.style_linebr, join=true)


if ma_50 < ma_100
    fillColourA:=mas_down
    col_a:=mas_down

if ma_100 < ma_200
    fillColourB:=mas_down
    col_b:=mas_down
    
if ma_200 < ma_400
    fillColourC:=mas_down

ma_a_stock = ma_50
ma_b_stock = ma_100
ma_c_stock = ma_200
ma_d_stock = ma_400

if show_mas == false or (syminfo.type !='stock' and syminfo.type !='index' )
    ma_a_stock := na
    ma_b_stock := na
    ma_c_stock := na
    ma_d_stock := na
    
col_a_transp = color.new(col_a, 100)
col_b_transp = color.new(col_b, 100)

p1 = plot(series=ma_a_stock, title='stock ma a', linewidth=1, color=col_a_transp, style=plot.style_linebr, join=true, offset=0)
p2 = plot(series=ma_b_stock, title='stock ma b', linewidth=1, color=col_b_transp, style=plot.style_linebr, join=true, offset=0)
p3 = plot(series=ma_c_stock, title='stock ma c', linewidth=1, color=col_b_transp, style=plot.style_linebr, join=true, offset=0)

ma_100_col = color.new(mas_up, 40)
if _close<ma_400
    ma_100_col:=color.new(mas_down, 40)
    
    
p4 = plot(series=ma_d_stock, title='stock ma 400', linewidth=1, color=ma_100_col, style=plot.style_linebr, join=true, offset=0)

fillA = color.new(fillColourA, 65)
fillB = color.new(fillColourB, 50)
fillC = color.new(fillColourC, 80)
fill(plot1=p1, plot2=p2, title='stock cloud a-b', color=fillA, editable=true)
fill(plot1=p2, plot2=p3, title='stock cloud b-c', color=fillB, editable=true)
fill(plot1=p3, plot2=p4, title='stock cloud c-d', color=fillC, editable=true)



//-----------------------------
//-----------------------------
// CRYPTO



color col_a_crypto = mas_up
color col_b_crypto = mas_up
color fillColourA_crypto = color.new(#32a8a4, 60) //#424242
color fillColourB_crypto = color.new(#32a8a4, 50)

if aligator_a < aligator_b
    fillColourA_crypto:=color.new(mas_down, 70)
    col_a_crypto:=mas_down

if aligator_b < aligator_c
    fillColourB_crypto:=color.new(mas_down, 80)
    col_b_crypto:=mas_down

ma_a_crypto = aligator_a
ma_b_crypto = aligator_b
ma_c_crypto = aligator_c
ma_d_crypto = ma_100


sell_line = fr_l_a
lows_15 = ta.lowest(_low, 15)
if sell_line < lows_15
    sell_line := lows_15
    

if show_mas == false or syminfo.type !='crypto'
    ma_a_crypto := na
    ma_b_crypto := na
    ma_c_crypto := na
    ma_d_crypto := na
    sell_line:=na
       
    
//plot(series=(buy_line+stop_buffer), title='crypto buy line', linewidth=1, color=color.new(color.teal, 35), style=plot.style_linebr, join=true)   
plot(series=(sell_line-stop_buffer), title='crypto sell line', linewidth=1, color=color.new(color.fuchsia, 35), style=plot.style_linebr, join=true)    
    
col_a_crypto_transp = color.new(col_a_crypto, 100)
col_b_crypto_transp = color.new(col_b_crypto, 100)

p1_crypto = plot(series=ma_a_crypto, title='crypto ma a', linewidth=1, color=col_a_crypto_transp, style=plot.style_linebr, join=true, offset=0)
p2_crypto  = plot(series=ma_b_crypto, title='crypto ma b', linewidth=1, color=col_b_crypto_transp, style=plot.style_linebr, join=true, offset=0)
p3_crypto  = plot(series=ma_c_crypto, title='crypto ma c', linewidth=1, color=col_b_crypto_transp, style=plot.style_linebr, join=true, offset=0)

ma_100_col_crypto = color.new(mas_up, 30)
if _close<ma_d_crypto
    ma_100_col_crypto:= color.new(mas_down, 30)
    
    
plot(series=ma_d_crypto , title='crypto ma 100', linewidth=2, color=ma_100_col_crypto, style=plot.style_linebr, join=true, offset=0)

// fillA_crypto = color.new(fillColourA_crypto, 60)
// fillB_crypto = color.new(fillColourB_crypto, 50)

fill(plot1=p1_crypto, plot2=p2_crypto, title='crypto cloud a-b', color=fillColourA_crypto)
fill(plot1=p2_crypto, plot2=p3_crypto, title='crypto cloud b-c', color=fillColourB_crypto)



//-----------------------------
//-----------------------------
// FOREX

// line_a = ta.sma(close, 20)[20]
// line_b = ta.sma(close, 20)[20]
// line_c = ta.sma(close, 50)[50]

// if show_mas == false or (syminfo.type !='forex' and syminfo.type !='cfd')
//     line_b:=na
//     line_c:=na

// cloud_high = math.max(line_b, line_c)
// cloud_low = math.min(line_b, line_c)

// cloud_high_p = plot(series=cloud_high , title='forex line a', linewidth=1, color=color.white)
// cloud_low_p = plot(series=cloud_low , title='forex line b', linewidth=1, color=color.yellow)

// cloud_fill_col = color.new(color.yellow, 50)

// fill(plot1=cloud_high_p, plot2=cloud_low_p, title='forex cloud a-b', color=cloud_fill_col)



if show_candles == false
    c_open := na
    _high := na
    _low := na
    _close := na
plotcandle(c_open, _high, _low, _close, title='No Gap Candle', color=cand_col, wickcolor=cand_col, bordercolor=cand_col)

