[Dipiers] Phoenix MTF v2.1
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5
indicator(title='[Dipiers] Phoenix MTF v2.1', format=format.price, overlay=false, max_bars_back=5000)

x1_resolution = input.timeframe(title='Chart TF', defval='', inline='1', group='Multi-TimeFrame')
x2_resolution = input.timeframe(title='2nd TF', defval='60', inline='1', group='Multi-TimeFrame')
x3_resolution = input.timeframe(title='3rd TF', defval='120', inline='2', group='Multi-TimeFrame')
x4_resolution = input.timeframe(title='4th TF', defval='240', inline='2', group='Multi-TimeFrame')
x5_resolution = input.timeframe(title='5th TF', defval='480', inline='3', group='Multi-TimeFrame')
x6_resolution = input.timeframe(title='6th TF', defval='720', inline='3', group='Multi-TimeFrame')
x7_resolution = input.timeframe(title='7th TF', defval='1D', inline='4', group='Multi-TimeFrame')
x8_resolution = input.timeframe(title='8th TF', defval='2D', inline='4', group='Multi-TimeFrame')

// --- Helper function for retrieving previous values in multi-timeframe ---
lastSeriesValue(series) =>
    series[ta.barssince(series > series[1] or series < series[1]) + 1]


//------------------------------------------------------------------------------
//Multi Time Frames version of the Phoenix Ascending by WyckoffMode (https://www.tradingview.com/script/ay9QX4dy-Phoenix-Ascending-2-201/)
//------------------------------------------------------------------------------

// Inputs {

// Sources:
src0 = open
src1 = high
src2 = low
src3 = close
src4 = hl2
src5 = hlc3
src6 = ohlc4
src7 = ta.tr
vol = volume

// Inputs:
n1x = input.int(9, 'Phx master', inline='1', group='Phoenix')
n2x = input.int(6, 'Phx time 1', inline='2', group='Phoenix')
n3x = input.int(3, 'Phx time 2', inline='2', group='Phoenix')
n4x = input.int(32, 'LSMA 1', inline='3', group='Phoenix')
n5x = input.int(0, 'LSMA 1', inline='3', group='Phoenix')

// }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------


tci(src) =>
    ta.ema((src - ta.ema(src, n1x)) / (0.025 * ta.ema(math.abs(src - ta.ema(src, n1x)), n1x)), n2x) + 50

mfx(src, vol) =>
    100.0 - 100.0 / (1.0 + math.sum(vol * (ta.change(src) <= 0 ? 0 : src), n3x) / math.sum(vol * (ta.change(src) >= 0 ? 0 : src), n3x))

willy(src) =>
    60 * (src - ta.highest(src, n2x)) / (ta.highest(src, n2x) - ta.lowest(src, n2x)) + 80

tradition(src, vol) =>
    math.avg(tci(src), mfx(src, vol), ta.rsi(src, n3x))

phoenix(_hlc3, vol) =>
    // calculate phoneix indicator
    green = tradition(_hlc3, vol)
    red = ta.sma(green, 6)
    ext1 = red < 20 ? red + 5 : red > 80 ? red - 5 : na
    lsma = ta.linreg(green, n4x, n5x)
    energy = ta.ema((green - red) * 2 + 50, n3x)
    // get previous values
    last_green = lastSeriesValue(green)
    last_red = lastSeriesValue(red)
    last_lsma = lastSeriesValue(lsma)
    last_energy = lastSeriesValue(energy)
    // extract features
    // crosses
    green_red_crossing = ta.cross(green, red)
    green_lsma_crossing = ta.cross(green, lsma)
    red_lsma_crossing = ta.cross(red, lsma)
    // green
    green_rising = green > last_green
    green_falling = green < last_green
    // red
    red_rising = red > last_red
    red_falling = red < last_red
    // lsma
    lsma_rising = lsma > last_lsma
    lsma_falling = lsma < last_lsma
    // energy
    energy_rising = energy > last_energy
    energy_falling = energy < last_energy
    [green, red, lsma, energy, green_red_crossing, green_lsma_crossing, red_lsma_crossing, green_rising, green_falling, red_rising, red_falling, lsma_rising, lsma_falling, energy_rising, energy_falling]


/////////////////////////////////////////////// Getting values - - - Coded by Neuromantic based on Dipiers indications

[x1_green, x1_red, x1_lsma, x1_energy, x1_green_red_crossing, x1_green_lsma_crossing, x1_red_lsma_crossing, x1_green_rising, x1_green_falling, x1_red_rising, x1_red_falling, x1_lsma_rising, x1_lsma_falling, x1_energy_rising, x1_energy_falling] = request.security(syminfo.tickerid, x1_resolution, phoenix(hlc3[barstate.isrealtime ? 1 : 0], volume[barstate.isrealtime ? 1 : 0]), barmerge.gaps_off, barmerge.lookahead_off)
[x2_green, x2_red, x2_lsma, x2_energy, x2_green_red_crossing, x2_green_lsma_crossing, x2_red_lsma_crossing, x2_green_rising, x2_green_falling, x2_red_rising, x2_red_falling, x2_lsma_rising, x2_lsma_falling, x2_energy_rising, x2_energy_falling] = request.security(syminfo.tickerid, x2_resolution, phoenix(hlc3[barstate.isrealtime ? 1 : 0], volume[barstate.isrealtime ? 1 : 0]), barmerge.gaps_off, barmerge.lookahead_off)
[x3_green, x3_red, x3_lsma, x3_energy, x3_green_red_crossing, x3_green_lsma_crossing, x3_red_lsma_crossing, x3_green_rising, x3_green_falling, x3_red_rising, x3_red_falling, x3_lsma_rising, x3_lsma_falling, x3_energy_rising, x3_energy_falling] = request.security(syminfo.tickerid, x3_resolution, phoenix(hlc3[barstate.isrealtime ? 1 : 0], volume[barstate.isrealtime ? 1 : 0]), barmerge.gaps_off, barmerge.lookahead_off)
[x4_green, x4_red, x4_lsma, x4_energy, x4_green_red_crossing, x4_green_lsma_crossing, x4_red_lsma_crossing, x4_green_rising, x4_green_falling, x4_red_rising, x4_red_falling, x4_lsma_rising, x4_lsma_falling, x4_energy_rising, x4_energy_falling] = request.security(syminfo.tickerid, x4_resolution, phoenix(hlc3[barstate.isrealtime ? 1 : 0], volume[barstate.isrealtime ? 1 : 0]), barmerge.gaps_off, barmerge.lookahead_off)
[x5_green, x5_red, x5_lsma, x5_energy, x5_green_red_crossing, x5_green_lsma_crossing, x5_red_lsma_crossing, x5_green_rising, x5_green_falling, x5_red_rising, x5_red_falling, x5_lsma_rising, x5_lsma_falling, x5_energy_rising, x5_energy_falling] = request.security(syminfo.tickerid, x5_resolution, phoenix(hlc3[barstate.isrealtime ? 1 : 0], volume[barstate.isrealtime ? 1 : 0]), barmerge.gaps_off, barmerge.lookahead_off)
[x6_green, x6_red, x6_lsma, x6_energy, x6_green_red_crossing, x6_green_lsma_crossing, x6_red_lsma_crossing, x6_green_rising, x6_green_falling, x6_red_rising, x6_red_falling, x6_lsma_rising, x6_lsma_falling, x6_energy_rising, x6_energy_falling] = request.security(syminfo.tickerid, x6_resolution, phoenix(hlc3[barstate.isrealtime ? 1 : 0], volume[barstate.isrealtime ? 1 : 0]), barmerge.gaps_off, barmerge.lookahead_off)
[x7_green, x7_red, x7_lsma, x7_energy, x7_green_red_crossing, x7_green_lsma_crossing, x7_red_lsma_crossing, x7_green_rising, x7_green_falling, x7_red_rising, x7_red_falling, x7_lsma_rising, x7_lsma_falling, x7_energy_rising, x7_energy_falling] = request.security(syminfo.tickerid, x7_resolution, phoenix(hlc3[barstate.isrealtime ? 1 : 0], volume[barstate.isrealtime ? 1 : 0]), barmerge.gaps_off, barmerge.lookahead_off)
[x8_green, x8_red, x8_lsma, x8_energy, x8_green_red_crossing, x8_green_lsma_crossing, x8_red_lsma_crossing, x8_green_rising, x8_green_falling, x8_red_rising, x8_red_falling, x8_lsma_rising, x8_lsma_falling, x8_energy_rising, x8_energy_falling] = request.security(syminfo.tickerid, x8_resolution, phoenix(hlc3[barstate.isrealtime ? 1 : 0], volume[barstate.isrealtime ? 1 : 0]), barmerge.gaps_off, barmerge.lookahead_off)

ext1 = x1_red < 20 ? x1_red + 5 : x1_red > 80 ? x1_red - 5 : na
ext2 = x2_red < 20 ? x2_red + 5 : x2_red > 80 ? x2_red - 5 : na
ext3 = x3_red < 20 ? x3_red + 5 : x3_red > 80 ? x3_red - 5 : na
ext4 = x4_red < 20 ? x4_red + 5 : x4_red > 80 ? x4_red - 5 : na
ext5 = x5_red < 20 ? x5_red + 5 : x5_red > 80 ? x5_red - 5 : na
ext6 = x6_red < 20 ? x6_red + 5 : x6_red > 80 ? x6_red - 5 : na
ext7 = x7_red < 20 ? x7_red + 5 : x7_red > 80 ? x7_red - 5 : na
ext8 = x8_red < 20 ? x8_red + 5 : x8_red > 80 ? x8_red - 5 : na

ext1over = x1_red < 5 ? x1_red - 5 : x1_red > 95 ? x1_red + 5 : na
ext2over = x2_red < 5 ? x2_red - 5 : x2_red > 95 ? x2_red + 5 : na
ext3over = x3_red < 5 ? x3_red - 5 : x3_red > 95 ? x3_red + 5 : na
ext4over = x4_red < 5 ? x4_red - 5 : x4_red > 95 ? x4_red + 5 : na
ext5over = x5_red < 5 ? x5_red - 5 : x5_red > 95 ? x5_red + 5 : na
ext6over = x6_red < 5 ? x6_red - 5 : x6_red > 95 ? x6_red + 5 : na
ext7over = x7_red < 5 ? x7_red - 5 : x7_red > 95 ? x7_red + 5 : na
ext8over = x8_red < 5 ? x8_red - 5 : x8_red > 95 ? x8_red + 5 : na



//------------------------------------------------------------------------------
//Real Time plotting idea by HedgeMode - https://www.tradingview.com/u/HedgeMode/#published-charts
//------------------------------------------------------------------------------

isBarConfirmed = barstate.isconfirmed

plot_x1 = input.bool(false, title='Unplot Current TF Phoenix?', group='Plots')
plot(plot_x1 ? na : x1_green, 'Curren Green Line',  color = isBarConfirmed ? color.new(color.lime, 0) : color.new(color.lime, 50), linewidth=2)
plot(plot_x1 ? na : x1_red, 'Current Red RSI', color=color.new(color.red, 0), linewidth=2)
plot(plot_x1 ? na : x1_lsma, 'Current LSMA', color=color.new(color.blue, 0), linewidth=2)
plot(plot_x1 ? na : x1_energy, 'Current Energy', color = isBarConfirmed ? color.new(color.white, 70) : color.new(color.white, 80), style=plot.style_area, histbase=50)
plot(plot_x1 ? na : ext1 ? ext1 : na, 'Current Pressure', color=color.new(color.yellow, 0), style=plot.style_circles, linewidth=2)
plot(plot_x1 ? na : ext1over ? ext1over : na, 'Current Pressure Over', color=color.new(color.red, 0), style=plot.style_circles, linewidth=2)

plot_x2 = input.bool(true, title='Unplot x2 TF Phoenix?', group='Plots')
plot(plot_x2 ? na : x2_green, '2nd Green Line', color=color.new(color.lime, 0), linewidth=2)
plot(plot_x2 ? na : x2_red, '2nd Red RSI', color=color.new(color.red, 0), linewidth=2)
plot(plot_x2 ? na : x2_lsma, '2nd LSMA', color=color.new(color.blue, 0), linewidth=2)
plot(plot_x2 ? na : x2_energy, '2nd Energy', color = isBarConfirmed ? color.new(color.white, 70) : color.new(color.white, 80), style=plot.style_area, histbase=50)
plot(plot_x2 ? na : ext2 ? ext2 : na, '2nd Pressure', color=color.new(color.yellow, 0), style=plot.style_circles, linewidth=2)
plot(plot_x2 ? na : ext2over ? ext2over : na, '2nd Pressure Over', color=color.new(color.red, 0), style=plot.style_circles, linewidth=2)

plot_x3 = input.bool(true, title='Unplot x3 TF Phoenix?', group='Plots')
plot(plot_x3 ? na : x3_green, '3rd Green Line', color=color.new(color.lime, 0), linewidth=2)
plot(plot_x3 ? na : x3_red, '3rd Red RSI', color=color.new(color.red, 0), linewidth=2)
plot(plot_x3 ? na : x3_lsma, '3rd LSMA', color=color.new(color.blue, 0), linewidth=2)
plot(plot_x3 ? na : x3_energy, '3rd Energy', color = isBarConfirmed ? color.new(color.white, 70) : color.new(color.white, 80), style=plot.style_area, histbase=50)
plot(plot_x3 ? na : ext3 ? ext3 : na, '3rd Pressure', color=color.new(color.yellow, 0), style=plot.style_circles, linewidth=2)
plot(plot_x3 ? na : ext3over ? ext3over : na, '3rd Pressure Over', color=color.new(color.red, 0), style=plot.style_circles, linewidth=2)

plot_x4 = input.bool(true, title='Unplot x4 TF Phoenix?', group='Plots')
plot(plot_x4 ? na : x4_green, '4th Green Line', color=color.new(color.lime, 0), linewidth=2)
plot(plot_x4 ? na : x4_red, '4th Red RSI', color=color.new(color.red, 0), linewidth=2)
plot(plot_x4 ? na : x4_lsma, '4th LSMA', color=color.new(color.blue, 0), linewidth=2)
plot(plot_x4 ? na : x4_energy, '4th Energy', color = isBarConfirmed ? color.new(color.white, 70) : color.new(color.white, 80), style=plot.style_area, histbase=50)
plot(plot_x4 ? na : ext4 ? ext4 : na, '4th Pressure', color=color.new(color.yellow, 0), style=plot.style_circles, linewidth=2)
plot(plot_x4 ? na : ext4over ? ext4over : na, '4th Pressure Over', color=color.new(color.red, 0), style=plot.style_circles, linewidth=2)

plot_x5 = input.bool(true, title='Unplot x5 TF Phoenix?', group='Plots')
plot(plot_x5 ? na : x5_green, '5th Green Line', color=color.new(color.lime, 0), linewidth=2)
plot(plot_x5 ? na : x5_red, '5th Red RSI', color=color.new(color.red, 0), linewidth=2)
plot(plot_x5 ? na : x5_lsma, '5th LSMA', color=color.new(color.blue, 0), linewidth=2)
plot(plot_x5 ? na : x5_energy, '5th Energy', color = isBarConfirmed ? color.new(color.white, 70) : color.new(color.white, 80), style=plot.style_area, histbase=50)
plot(plot_x5 ? na : ext5 ? ext5 : na, '5th Pressure', color=color.new(color.yellow, 0), style=plot.style_circles, linewidth=2)
plot(plot_x5 ? na : ext5over ? ext5over : na, '5th Pressure Over', color=color.new(color.red, 0), style=plot.style_circles, linewidth=2)

plot_x6 = input.bool(true, title='Unplot x6 TF Phoenix?', group='Plots')
plot(plot_x6 ? na : x6_green, '6th Green Line', color=color.new(color.lime, 0), linewidth=2)
plot(plot_x6 ? na : x6_red, '6th Red RSI', color=color.new(color.red, 0), linewidth=2)
plot(plot_x6 ? na : x6_lsma, '6th LSMA', color=color.new(color.blue, 0), linewidth=2)
plot(plot_x6 ? na : x6_energy, '6th Energy', color = isBarConfirmed ? color.new(color.white, 70) : color.new(color.white, 80), style=plot.style_area, histbase=50)
plot(plot_x6 ? na : ext6 ? ext6 : na, '6th Pressure', color=color.new(color.yellow, 0), style=plot.style_circles, linewidth=2)
plot(plot_x6 ? na : ext6over ? ext6over : na, '6th Pressure Over', color=color.new(color.red, 0), style=plot.style_circles, linewidth=2)

plot_x7 = input.bool(true, title='Unplot x7 TF Phoenix?', group='Plots')
plot(plot_x7 ? na : x7_green, '7th Green Line', color=color.new(color.lime, 0), linewidth=2)
plot(plot_x7 ? na : x7_red, '7th Red RSI', color=color.new(color.red, 0), linewidth=2)
plot(plot_x7 ? na : x7_lsma, '7th LSMA', color=color.new(color.blue, 0), linewidth=2)
plot(plot_x7 ? na : x7_energy, '7th Energy', color = isBarConfirmed ? color.new(color.white, 70) : color.new(color.white, 80), style=plot.style_area, histbase=50)
plot(plot_x7 ? na : ext7 ? ext7 : na, '7th Pressure', color=color.new(color.yellow, 0), style=plot.style_circles, linewidth=2)
plot(plot_x7 ? na : ext7over ? ext7over : na, '7th Pressure Over', color=color.new(color.red, 0), style=plot.style_circles, linewidth=2)

plot_x8 = input.bool(true, title='Unplot x8 TF Phoenix?', group='Plots')
plot(plot_x8 ? na : x8_green, '8th Green Line', color=color.new(color.lime, 0), linewidth=2)
plot(plot_x8 ? na : x8_red, '8th Red RSI', color=color.new(color.red, 0), linewidth=2)
plot(plot_x8 ? na : x8_lsma, '8th LSMA', color=color.new(color.blue, 0), linewidth=2)
plot(plot_x8 ? na : x8_energy, '8th Energy', color = isBarConfirmed ? color.new(color.white, 70) : color.new(color.white, 80), style=plot.style_area, histbase=50)
plot(plot_x8 ? na : ext8 ? ext8 : na, '8th Pressure', color=color.new(color.yellow, 0), style=plot.style_circles, linewidth=2)
plot(plot_x8 ? na : ext8over ? ext8over : na, '8th Pressure Over', color=color.new(color.red, 0), style=plot.style_circles, linewidth=2)


hline(110, color = color.new(color.red, 70), linewidth = 2, linestyle = hline.style_solid)
hline(100, color = color.new(color.orange, 50), linewidth = 1, linestyle = hline.style_dotted)
hline(90, color = color.new(color.aqua, 50), linewidth = 1, linestyle = hline.style_dotted)
hline(80, color = color.new(color.white, 50), linewidth = 2, linestyle = hline.style_solid)
hline(70, color = color.new(color.white, 50), linewidth = 1, linestyle = hline.style_dotted)
hline(60, color = color.new(color.yellow, 50), linewidth = 1, linestyle = hline.style_dotted)
hline(50, color = color.new(color.yellow, 30), linewidth = 2, linestyle = hline.style_solid)
hline(40, color = color.new(color.yellow, 50), linewidth = 1, linestyle = hline.style_dotted)
hline(30, color = color.new(color.white, 50), linewidth = 1, linestyle = hline.style_dotted)
hline(20, color = color.new(color.white, 50), linewidth = 2, linestyle = hline.style_solid)
hline(10, color = color.new(color.aqua, 50), linewidth = 1, linestyle = hline.style_dotted)
hline(00, color = color.new(color.orange, 50), linewidth = 1, linestyle = hline.style_dotted)
hline(-10, color = color.new(color.red, 70), linewidth = 2, linestyle = hline.style_solid)


