CashDataPloter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © WillamFletcher
//@version=5

indicator(title = "CashDataPloter", overlay = true,format=format.price, precision=2,  max_lines_count = 500, max_labels_count = 500)

StartTimeOnOff     = input.bool(false,title="Custom start  ", group="Date")
StartTime =   input.time(defval = timestamp("1 mar 2022 12:00"),title = "custom end " )
//StartTime2 =   input.time(defval = timestamp("1 feb 2022 12:00"),title = "custom start  " )
linewidth =     input.int(defval=2, title= "Width : ")
upcolor     =   input.color(title = "UpColor", defval   =   color.white,group="color")
dncolor     =   input.color(title = "DownColor", defval   =   color.white,group="color")
concolor     =   input.color(title = "ConnectionColor", defval   =   color.white,group ="color")
cashdataperiod_daily = input.string("1D", title="Daily", 
     options=["1D","2D","3D"])
cashdataperiod_weekly = input.string("1W", title="Weekly", 
     options=["1W", "2W"])
cashdataperiod_monthly = input.string("1M", title="Monthly", 
     options=["1M", "2M","3M","6M"])
cashdataperiod_yearly = input.string("12M", title="Yearly", 
     options=["12M"]) 
cashdataperiod_minute = input.string("5", title="Minute", 
   options=["5","10","15" , "30" ]) 
cashdataperiod_hourly = input.string("60", title="Hourly", 
   options=["60","120", "180" ])

//Declare Variables
var float   _highPrice  =   na
var float   _lowPrice   =   na
var int   _highTime     =   na
var int _lowTime        =   na
var int _sessionStart   =   na
var int _sessionMid     =   na
var int _barNum         =   na
var line    MainLine    =   na
var line    ConnectLine =   na
var string  bar         =   na
var string     TF       =   na
var int         bars    =   na
var int  mainlinecounter=  0

if timeframe.period == "1"
    bar:= cashdataperiod_minute
    TF:="1Min"
else if timeframe.period == "3"
    bar:= cashdataperiod_minute
    TF:="3Min"
else if timeframe.period == "5"
    bar:= cashdataperiod_minute
    TF:="5Min"
else if timeframe.period == "15"
    bar:= cashdataperiod_hourly
    TF:="15Min"
else if timeframe.period == "30"
    bar := cashdataperiod_hourly
    TF:="30Min"
else if timeframe.period == "45"
    bar := cashdataperiod_hourly
    TF:="45Min"
else if timeframe.period == "60"
    bar:=  cashdataperiod_daily
    TF:=  "1H"
else if timeframe.period == "120"
    bar:=  cashdataperiod_daily
    TF:=  "2H"
else if timeframe.period == "180"
    bar:=  cashdataperiod_daily
    TF:=  "3H"
else if timeframe.period == "240"
    bar := cashdataperiod_weekly
    TF:=  "4H"
else if timeframe.period == "D"
    bar:=cashdataperiod_monthly
    TF:="D"
else if timeframe.period == "W"
    bar:=cashdataperiod_monthly
    TF:= "W"
else if timeframe.period == "M"
    bar:=cashdataperiod_yearly
    TF:="M"
    





NewSession  =   ta.change(time(bar)) > 0 

//and  ( StartTime2 <= time ) 
if   ( StartTimeOnOff ) ?    StartTime>=time   : (  timenow >= time ) 

    if  NewSession
        _highPrice  :=   high
        _lowPrice   :=   low
        _highTime   :=   bar_index
        _lowTime    := bar_index 
        _sessionStart   :=  bar_index
        _sessionMid     :=  bar_index
        MainLine    :=  line.new(x1 = _sessionStart,y1 = _highPrice,x2 = _sessionMid, y2 =_lowPrice,xloc=xloc.bar_index,  extend = extend.none, color = color.black, style = line.style_solid, width = linewidth)
        ConnectLine :=  line.new(x1=line.get_x2(MainLine[1]),y1=line.get_y2(MainLine[1]),x2= _sessionStart,y2= _lowPrice,xloc=xloc.bar_index, extend = extend.none, color = concolor, style = line.style_solid, width = linewidth)
        _barNum     :=   0
        mainlinecounter:=mainlinecounter+1
        
    else
        _barNum :=  _barNum[1] + 1
        
barCount    =   ta.valuewhen(NewSession , _barNum[1],0)
delta       =   (barCount - barCount%2)/2
_sessionMid :=   _sessionStart  +  delta


if   ( StartTimeOnOff ) ?    StartTime>=time   : (  timenow >= time ) 
    if  high    >   _highPrice 
        _highPrice  :=  high
        _highTime   :=  bar_index
        
    if  low     <   _lowPrice
        _lowPrice   :=  low
        _lowTime    :=  bar_index
        
        
    if _highTime <= _lowTime 
        line.set_xy1(id = MainLine, x = _sessionStart, y = _highPrice )
        line.set_xy2(id = MainLine, x = _sessionMid, y = _lowPrice )
        line.set_color(id = MainLine, color = dncolor)
        
    else
        line.set_xy1(id = MainLine, x = _sessionStart, y = _lowPrice )
        line.set_xy2(id = MainLine, x = _sessionMid, y = _highPrice )
        line.set_color(id = MainLine, color = upcolor)

    
    line.set_xy2(id=ConnectLine, x=line.get_x1(MainLine[0]), y=line.get_y1(MainLine[0]))
    line.set_color(id=ConnectLine, color=concolor)
    bar:=bar=="60"?"H":bar=="120"?"2H":bar=="180"?"3H":bar
if barstate.islast
    var table atrDisplay = table.new(position.top_right, 5, 5, bgcolor = color.red, frame_width = 1, frame_color = color.white , border_color= color.white , border_width=1)
    table.cell(atrDisplay, 0, 0, "Symbol :", text_color = color.white,bgcolor=#636161,width=8,height=3.5 )
    table.cell(atrDisplay, 1, 0, syminfo.ticker, text_color= color.white , bgcolor=#8c0e0e,width=4,height=3.5)
    table.cell(atrDisplay, 0, 2, "CashData TF :", text_color = color.white,bgcolor=#636161,width=8,height=3.5 )
    table.cell(atrDisplay, 1, 2, bar, text_color= color.white , bgcolor=#8c0e0e,width=4,height=3.5)
    table.cell(atrDisplay, 0, 1, "Base TF  : ", text_color = color.white,bgcolor=#636161, height=3.5 , width=8)
    table.cell(atrDisplay, 1, 1, TF, text_color = color.white,bgcolor=#8c0e0e,width=4,height=3.5)
    table.cell(atrDisplay, 0, 3, "Current price :  ", text_color = color.white,bgcolor=#636161,width=8,height=3.5 )
    table.cell(atrDisplay, 1, 3, str.tostring(close,'#.####'), text_color= color.white , bgcolor=#8c0e0e,width=4,height=3.5)
    table.cell(atrDisplay, 0, 4, " Monowaves :", text_color = color.white,bgcolor=#636161,width=8,height=3.5 )
    table.cell(atrDisplay, 1, 4, str.tostring(mainlinecounter-1), text_color= color.white , bgcolor=#8c0e0e,width=4,height=3.5)
    
    
