Wavechart v2 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MNR_Anas

//@version=5
indicator("Wave Chart v2","WaveChartV2", overlay=true ,max_lines_count=500,max_bars_back=5000)

////input         
TF              = input.timeframe('W', "Resolution", options=['30','60','240','D','W','2W','M','2M','3M','6M','1Y','2Y','2.5Y','5Y']) 
lineWidth       = input.int(2, title="line        ", minval=1,inline = "2")        
linecolor       = input.color(color.blue,title="",inline = "2")
show_unsure     = input.bool(false,title="Show an unsure line",inline = "3",tooltip = "Not sure whether high or low comes first,\nIt is recommended to check in a lower Timeframe")       
unsureColor     = input.color(color.gray,title="",inline = "3")  
bg              = input.bool(false,title="Show Time Divider")      
last_bar_plot   = input.bool(false,title="Force plot on the last Bar")  
   
targetTime1     = timestamp(year, month, dayofmonth, 00, 00, 00) //  1D

var count = 0
var year_count = year
var mont_count = 0

if ta.change(time('12M')) > 0
    year_count:=year_count+1

if TF == '2.5Y' and ta.change(time('M')) > 0 and mont_count != 0
    mont_count:=mont_count+1

if TF == '2.5Y' and year_count%5==0
    mont_count:=month

preAction = switch TF
    "1Y" => year_count != year_count[1] ? true  : false
    "2Y" => year_count != year_count[1] and year_count%2 == 0? true  : false
    "2.5Y" => mont_count != mont_count[1] and mont_count%30 == 1 and mont_count != 0 ? true  : false
    "5Y" => year_count != year_count[1] and year_count%5 == 0? true  : false
    => ta.change(time(TF)) > 0

TFCheck = switch TF
    "D" => timeframe.isdwm ? false  : true
    "W" => timeframe.isintraday or timeframe.isdaily ? true  : false
    "2W" => timeframe.isintraday or timeframe.isdaily ? true  : false
    "M" => timeframe.isintraday or timeframe.isdaily or (timeframe.isweekly and timeframe.multiplier == 1) ? true  : false
    "2M" => timeframe.isintraday or timeframe.isdaily or (timeframe.isweekly and timeframe.multiplier == 1) ? true  : false
    "3M" => timeframe.ismonthly and timeframe.multiplier>1 ? false  : true
    "6M" => timeframe.ismonthly and timeframe.multiplier>1 ? false  : true
    "1Y" => timeframe.ismonthly and timeframe.multiplier>1 ? false  : true
    "2Y" => timeframe.ismonthly and timeframe.multiplier>1 ? false  : true
    "2.5Y" => timeframe.ismonthly and timeframe.multiplier>1 ? false  : true
    "5Y" => timeframe.ismonthly and timeframe.multiplier>1 ? false  : true
    => timeframe.isintraday and timeframe.multiplier <= str.tonumber(TF)/4 ? true  : false

action = preAction and TFCheck

if(last_bar_plot and barstate.islast)
    action:=true

bgcolor(action and bg?color.new(color.gray,70):na)

if(TFCheck)
    count:=count+1

if(action)
    count:=1

hbar=TFCheck?ta.highestbars(count):na                              //bar that highest in range
lbar=TFCheck?ta.lowestbars(count):na                               //bar that lowest in range

whigh=(high[-hbar])[1]                                  //value of height from habr
wlow=(low[-lbar])[1]                                    //value of low from lbar

var vx1=0, vx2=0
var px=0                                                //Previous x
var py=0.0                                              //Provious y
var vy1=0.0
var vy2=0.0

center = ((count[1]+1)/2)                               // Center Point
mid_left = (center+1)/2                                 // Center of first to center
mid_right = count[1]-(mid_left-1)                       // Center of center to lastBar

h_before_l = false
l_before_h = false
h_l_in_samebar = false

if(action and hbar[1]<lbar[1] )                      //check whether high or low comes first                              
    h_before_l:=true
else  if(action and hbar[1]>lbar[1] )               
    l_before_h:=true
else if(action and hbar[1]==lbar[1])                //But if the highest and lowest are from the same bar,
    linecolor:=show_unsure?unsureColor:linecolor
    h_l_in_samebar:=true

// Assume it is from a bar close     
if (h_l_in_samebar and (close[-hbar])[1]<(open[-hbar])[1])
    h_before_l:=true
else if(h_l_in_samebar)
    l_before_h:=true
 
if(action)                                              //Runs only action Bar
    if(h_before_l)                                      //If the highest point comes before the lowest point
        vx1:=bar_index[mid_right]
        vy1:=whigh
        vx2:=bar_index[mid_left]
        vy2:=wlow
    if(l_before_h)                                      //If the Lowestest point comes before the lowest point
        vx1:=bar_index[mid_right]
        vy1:=wlow
        vx2:=bar_index[mid_left]
        vy2:=whigh

    //plot line
    if(px!=0 and py!=0)  
        line.new(x1=px, y1=py, x2=vx1, y2=vy1,color=linecolor,width=lineWidth)  //line between previous points
    line.new(x1=vx1, y1=vy1, x2=vx2, y2=vy2,color=linecolor,width=lineWidth)    //line between high and low
    
    //memory last point 
    px:=vx2
    py:=vy2

/////

// Message Box
if(not TFCheck and barstate.islast)
    statusText = "please use Lower Timeframe"
    text_location = 0.0 
    l = label.new(bar_index,na) //label.new(bar_index, na)
    label.set_text(l, statusText)
    label.set_y(l,high)
    boxColor =  color.lime 
    label.set_color(l, color.red)
    label.set_size(l,size.huge)