v2.3 Weekly Fibo Candle Middle Line
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kenny888

//@version=5

indicator(title='v2.3 Weekly Fibo Candle Middle Line', overlay=true, max_labels_count=100)

var firstcandledate = str.format('{0,date, YYYY-MM-d}', time)

//Get input ======[Groups]  BEGIN
// Group - Session Volatility Realtime

group0 = 'Period under study'
startDate = input.time(title='Start Date', defval=timestamp("10 Jun 2022 17:00:00 GMT+8"), tooltip='Date & time to begin analysis', group=group0)
endDate = input.time(title='End Date', defval=timestamp('1 Jan 2099 12:00 +0000'), tooltip='Date & time to stop analysis', group=group0)
show_centreline_ohlc = input.bool(title='show_centreline_ohlc', defval=false, group=group0, inline='HLline')

use_custom_period = input.bool(title='use_custom_period', defval=true, group=group0, inline='HLline')

var timeSession1=""
// @@@ Setting @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// 
timeSession1 := input.session(title='From to', defval='0000-0000', group=group0)  // Set the period

var timeSession = ''

if not use_custom_period
    if syminfo.ticker  == 'HSI1!'
        timeSession := "0900-0200"
    
    else if syminfo.ticker == 'ES1!' 
        timeSession := "2130-0400"  // range light line from 0500 to cash.
    
    else if syminfo.ticker == 'NQ1!'
        timeSession := "2130-0400"  // range light line from 0500 to cash.
    
    else if syminfo.ticker == 'CN50USD'
        timeSession := '0900-0415'
    
    else if syminfo.ticker  == 'FDAX1!' or syminfo.ticker == 'GER30' 
        timeSession :="1500-0345"

    else if syminfo.ticker  == 'J225' 
        timeSession :="0600-0400"
    
else
    timeSession := timeSession1
    


// @@@ Setting @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


show_colorBG = false
colorBG = color.rgb(0, 0, 255, 95)
showRangeLabel = false
RangeLabel_loc = yloc.abovebar
show_table = false
table_loc = position.bottom_right
show_table_10days = false
show_table_maxmin = false


//Get input ======[Groups]  END


// This function returns true if the current bar falls within the given time session (:1234567 is to include all weekdays)
inSession(sess) =>  //custom function input sess
    na(time(timeframe.period, sess + ':1234567', 'GMT+8')) == false and time >= startDate and time <= endDate

//Change the background color for the in sessioin part (colorBG is a check box)
bgcolor(show_colorBG and inSession(timeSession) ? colorBG : na)


// Check if a new session has begun (not in ==> in)
var withinSession = false
if not inSession(timeSession)[1] and inSession(timeSession)
    withinSession := true
    withinSession

// Declare required variables for analysis
var MAXINT = 2147483647.0

var highestHigh = -1.0
var lowestLow = MAXINT
var DailyCashHigh = -1.1
var DailyCashLow = MAXINT


// Check if a session has ended (in ==> not in)
if inSession(timeSession)[1] and not inSession(timeSession)
    withinSession := false

    // Assign daily cash high and low
    DailyCashHigh := highestHigh
    DailyCashLow := lowestLow
    // Reset stored high/low
    highestHigh := -1.0
    lowestLow := MAXINT


// Get highest high and lowest low of current session
if withinSession
    if high > highestHigh
        highestHigh := high
        highestHigh
    if low < lowestLow
        lowestLow := low


plot(withinSession ? highestHigh : na, color=color.rgb(0, 0, 255, 20), style=plot.style_linebr, linewidth=2, title='High Range line')
plot(withinSession ? lowestLow : na, color=color.rgb(0, 0, 255, 20), style=plot.style_linebr, linewidth=2, title='Low Range line')

// Label - Begin
c_highestHigh = close -highestHigh
Label_highestHigh = label.new(x=bar_index+10, y=withinSession ? highestHigh : na, color= color.new(color.blue, 70), text=str.tostring(c_highestHigh, "#")  + '/ ' +  str.tostring(highestHigh- lowestLow, "#"), style=label.style_none, size=size.small, textcolor=color.blue)
label.delete(Label_highestHigh[1])

c_lowestLow = close -lowestLow
Label_lowestLow = label.new(x=bar_index+10, y=withinSession ? lowestLow : na, color= color.new(color.blue, 70), text=str.tostring(c_lowestLow, "#")  + '/ ' +  str.tostring(highestHigh- lowestLow, "#"), style=label.style_none, size=size.small, textcolor=color.blue)
label.delete(Label_lowestLow[1])

// Label - End
var D_open = 0.0
timeinrange(res, sess) =>
    not na(time(res, sess, 'GMT+8')) and na(time(res, sess, 'GMT+8'))[1]

if timeinrange('1', timeSession)
    D_open := open

plot(withinSession ? D_open : na, title='Day Open', color=D_open != D_open[1] ? na : color.navy)



// One day ema 1
expr = ta.ema(ohlc4, 3)
s1 = request.security(syminfo.tickerid, 'D', expr)  // 240 Minutes
plot(withinSession ? s1 : na, title='Day Open', color=D_open != D_open[1] ? na : color.new(color.green, 100))
c_s1 = close -s1
// Label_s1 = label.new(x=bar_index+5, y=withinSession ? s1 : na, color= color.new(color.green, 70), text=str.tostring(c_s1, "#"), style=label.style_label_left, size=size.normal, textcolor=color.black)
// label.delete(Label_s1[1])


var candle_center = 0.0
candle_center := (highestHigh + lowestLow + D_open + close) / 4
// candle_center := (highestHigh + lowestLow) / 2
l_1D3 = s1 + (candle_center - s1) / 2
plot(withinSession ? candle_center : na, title='ohlc4 Candle Center', color=show_centreline_ohlc?color.new(color.red, 0):na, style=plot.style_linebr, linewidth=3)


var candle_center_breakpoint = 0.0
candle_center_breakpoint :=(highestHigh + lowestLow + D_open) / 3
plot(withinSession ? candle_center_breakpoint : na, title='ohl3 Candle Center', color=color.new(color.blue, 0), style=plot.style_linebr, linewidth=3)

var fibo_projection_upper = 0.0
fibo_projection_upper :=candle_center_breakpoint+(candle_center_breakpoint-lowestLow)*1.618
plot(withinSession ? fibo_projection_upper : na, title='Fibo upper target', color=color.new(color.blue, 0), style=plot.style_circles, linewidth=1)

var fibo_projection_lower = 0.0
fibo_projection_lower :=candle_center_breakpoint-(highestHigh-candle_center_breakpoint)*1.618
plot(withinSession ? fibo_projection_lower : na, title='Fibo lower target', color=color.new(color.blue, 0), style=plot.style_circles, linewidth=1)


var fibo0618 = 0.0
fibo0618 :=(highestHigh - lowestLow)*0.618+lowestLow
plot(withinSession ? fibo0618 : na, title='fibo0618', color=color.new(color.blue, 0), style=plot.style_linebr, linewidth=1)
Label_fibo0618 = label.new(x=bar_index+1, y=withinSession ? fibo0618 : na, color= color.new(color.orange, 70), text="Fibo_up "+ str.tostring (close-fibo0618, "#.#"), style=label.style_none, size=size.small, textcolor=color.blue)
label.delete(Label_fibo0618[1])

var fibo0500 = 0.0
fibo0500 :=(highestHigh - lowestLow)*0.5+lowestLow
plot(withinSession ? fibo0500 : na, title='fibo0500', color=color.new(color.red, 0), style=plot.style_linebr, linewidth=1)
Label_fibo0500 = label.new(x=bar_index+1, y=withinSession ? fibo0500 : na, color= color.new(color.orange, 70), text="Fibo_mid " + str.tostring (close-fibo0500, "#.#"), style=label.style_none, size=size.small, textcolor=color.red)
label.delete(Label_fibo0500[1])

var fibo0382 = 0.0
fibo0382 :=(highestHigh - lowestLow)*0.382+lowestLow
plot(withinSession ? fibo0382 : na, title='fibo0382', color=color.new(color.blue, 0), style=plot.style_linebr, linewidth=1)
Label_fibo0382 = label.new(x=bar_index+1, y=withinSession ? fibo0382 : na, color= color.new(color.orange, 70), text="Fibo_low " +str.tostring (close-fibo0382, "#.#") , style=label.style_none, size=size.small, textcolor=color.blue)
label.delete(Label_fibo0382[1])



plot(withinSession ? l_1D3 : na, title='1D3ema', color=color.new(color.orange, 100), style=plot.style_linebr)

c_l_1D3 = close -l_1D3
// Label_l_1D3 = label.new(x=bar_index+5, y=withinSession ? l_1D3 : na, color= color.new(color.orange, 70), text=str.tostring(c_l_1D3, "#"), style=label.style_label_left, size=size.normal, textcolor=color.black)
// label.delete(Label_l_1D3[1])


e=ta.ema(ohlc4,3)
plot(e,color=color.black, title="ema3", linewidth = 2)


// // --> Custom Function to Check if New Bar == True
// // --> (https://www.tradingview.com/wiki/Sessions_and_Time_Functions)
// is_newbar(res) =>
//     t = time(res)
//     ta.change(t) != 0 ? 1 : 0


// // newDay = ta.change(time("W")) != 0
// newDay = is_newbar('W')
// plot(ta.vwap, title='VWAP ohlc4',color =  newDay? color.new(color.black,100) :color.new( color.red,50), linewidth=5)


// swt = input(true, title="Show This Weeks OHLC?")
// swy = input(false, title="Show Previous Weeks OHLC?")

// //Weekly
// wtdo = request.security(syminfo.tickerid, 'W', open)
// wpdo = request.security(syminfo.tickerid, 'W', open[1])
// wpc = request.security(syminfo.tickerid, 'W', close)
// wpdc = request.security(syminfo.tickerid, 'W', close[1])
// wph = request.security(syminfo.tickerid, 'W', high)
// wpdh = request.security(syminfo.tickerid, 'W', high[1])
// wpl = request.security(syminfo.tickerid, 'W', low)
// wpdl = request.security(syminfo.tickerid, 'W', low[1])

// //Weekly Plots
// plot(swt and wtdo ? wtdo : na, title="Weekly Open", style=plot.style_circles, linewidth=3, color=color.silver)
// plot(swy and wpdo ? wpdo : na, title="Previous Weeks Open", style=plot.style_cross, linewidth=3, color=color.silver)
// plot(swt and wpc ? wpc : na, title="Weekly Close", style=plot.style_circles, linewidth=3, color=color.fuchsia)
// plot(swy and wpdc ? wpdc : na, title="Previous Weeks Close", style=plot.style_cross, linewidth=3, color=color.fuchsia)
// plot(swt and wph ? wph : na, title="Weekly High", style=plot.style_circles, linewidth=3, color=color.green)
// plot(swy and wpdh ? wpdh : na, title="Previous Weeks High", style=plot.style_cross, linewidth=3, color=color.green)
// plot(swt and wpl ? wpl : na, title="Weekly Low", style=plot.style_circles, linewidth=3, color=color.red)
// plot(swy and wpdl ? wpdl : na, title="Previous Weeks Low", style=plot.style_cross, linewidth=3, color=color.red)


