Golden Level Predictions v1.0
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Golden Level Predictions (GLP) Trading Indicator

// This script is a custom trading indicator named "GLP" designed for the TradingView platform. It provides various price levels based on Fibonacci calculations and other mathematical formulas to help traders identify potential overpriced and discounted price levels.

// Key Features:

// User Inputs: Allows users to select their preferred timeframe from options like Weekly, Daily, Monthly, etc. Users can also choose to display Fibonacci lines and their corresponding prices within the levels.

// Price Level Calculations:

// Uses constants like the Golden Ratio (PHI) and Pi (PI) to derive various multipliers and factors.
// Determines if the current asset is a cryptocurrency and adjusts calculations accordingly.
// Calculates overpriced and discounted price levels based on the current open price and historical data.
// Fibonacci Levels:

// For each overpriced and discounted level, the script calculates intermediate Fibonacci levels like 23.6%, 38.2%, 50%, 61.8%, and 78.6%.
// These levels are plotted on the chart, providing traders with more granular price targets.
// Visual Elements:

// Draws horizontal lines to the next day for each calculated price level.
// Displays percentage gains or losses at each level, indicating how much the price would change if it reached that level.
// Uses different colors to represent overpriced (green) and discounted (red) levels. A neutral price is shown in yellow.
// anticipatedcipated Close Calculation: Provides an estimated closing price for the current timeframe based on various factors.

//You will see various numbers and formulas and wonder why I chose them. First through research and studying other indicators I found some commonalities
//1  - Need I say more?
//2  - It is the natural number following 1 and preceding 3. It is the smallest and only even prime number.
//3  - It is the second and only prime triangular number, and Gauss proved that every integer is the sum of at most 3 triangular numbers
//4  - It is a square number, the smallest semiprime and composite number
//5  - It is the first safe prime,[3] the first good prime,[4] the first balanced prime,[5] and the first of three known Wilson primes
//6  - It is therefore considered “perfect.” In mathematics, a perfect number is one that equals the sum of its divisors (excluding itself), and 6 is the first perfect number in this sense because its divisors are 1, 2, and 3.
//7  - Seven is considered by some to be the most 'prime' number within the first 10 numbers as you cannot multiply it within the group, making it a kind of optimal-prime
//9  - In mathematics, the number 9 has some unique properties as well. It is the highest single-digit number, and any number multiplied by 9 will always result in a number whose digits add up to 9
//11 - Number 11 is a prime number, meaning it is only divisible by 1 and itself. Its prime status contributes to its uniqueness and mathematical significance
//33 - The number of deities in the Vedic Religion is 33
//36 - The square of six and a triangular number, making it a square, triangular number. It is the smallest square, a triangular number other than one, and it is also the only triangular number other than the one whose square root is also a triangular number
//100- Need I say less
// © gggoaaat

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

//CORE CODE START
timeframeOptions = input.string("W", "Select Timeframe", options=["W", "D", "M", "12h", "8h", "4h", "2h", "1h", "30m", "15m", "5m"], tooltip = "Higher Time frames more reliable. Preferred D, W, M")
showFibLines = input.bool(true, "Show Fib Lines inside levels?", tooltip = "You can display all the fib lines between the levels.")
showFibPrices = input.bool(false, "Show Fib Lines Prices?", tooltip = "You can toggle the display of all the fib lines between the levels.")

// Function to convert timeframe
convertTimeframe(tf) =>
 tf == "12h"    ? "720" :
 tf == "8h"     ? "480" :
 tf == "4h"     ? "240" :
 tf == "2h"     ? "120" :
 tf == "1h"     ? "60" :
 tf == "30m"    ? "30" :
 tf == "15m"    ? "15" :
 tf == "5m"     ? "5" : tf

// Use the function to get the converted timeframe
timeframeOptions := convertTimeframe(timeframeOptions)

//#region COMMON CODE for Pricing
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// Constants
PI = 3.141592653589793
PHI = +(1 + math.sqrt(5)) / 2

isCrypto = syminfo.type == "crypto"

upMagicNo     = input.float(1 + (((PI*4)-PHI*(PHI+1))/100), "Overprice Magic #", display = display.none )
downMagicNo   = input.float(1 + (((PI*2)-(PHI*0.36))/100), "Discount Magic #", display = display.none)

baseWeekOffset = 0.007

timeFrameOffset = 
 timeframeOptions == "M"    ? baseWeekOffset*52/12 :        //1 Month    
 timeframeOptions == "W"    ? baseWeekOffset :              //1 Week
 timeframeOptions == "D"    ? 0 :                           //1 Day
 timeframeOptions == "720"  ? -(baseWeekOffset/7/24)*2 :    //12 Hour
 timeframeOptions == "480"  ? -(baseWeekOffset/7/24)*3 :    //8 Hour
 timeframeOptions == "240"  ? -(baseWeekOffset/7/24)*6 :    //4 Hour
 timeframeOptions == "120"  ? -(baseWeekOffset/7/24)*12 :   //2 Hour
 timeframeOptions == "60"   ? -(baseWeekOffset/7/24)*24 :   //1 Hour
 timeframeOptions == "30"   ? -(baseWeekOffset/7/24)*((isCrypto ? 48 : 12)*PI) :   //30 Min
 timeframeOptions == "15"   ? -(baseWeekOffset/7/24)*((isCrypto ? 96 : 36)*PI) :   //15 Min
 timeframeOptions == "5"    ? -(baseWeekOffset/7/24)*((isCrypto ? 288 : 108)*(1+ (PHI/5))) :  //5 Min
 0

timeFrameLabel = 
 timeframeOptions == "M"  ? "Month" :
 timeframeOptions == "W"  ? "Week" :
 timeframeOptions == "D"  ? "Day" :
 timeframeOptions == "720"? "12 Hour" :
 timeframeOptions == "480"? "8 Hour" :
 timeframeOptions == "240"? "4 Hour" :
 timeframeOptions == "120"? "2 Hour" :
 timeframeOptions == "60" ? "1 Hour" :
 timeframeOptions == "30" ? "30 Min" :
 timeframeOptions == "15" ? "15 Min" :
 timeframeOptions == "5"  ? "5 Min" : timeframeOptions
 
upMagicNo += timeFrameOffset
downMagicNo -= timeFrameOffset
stocksOffset = input.float((PHI/100/2), "Stocks Offset", tooltip = "Only change when using stocks. Does not apply to crypto.", display = display.none)

o_overpriced_multiplier     = (isCrypto ? upMagicNo : upMagicNo-stocksOffset) 
o_discounted_multiplier     = (isCrypto ? downMagicNo  : downMagicNo+stocksOffset) 
scaler                      = 100000000000
piFifth                     = PI * 5/100
neutral_color               = color.yellow
discounted_color            = color.red
overpriced_color            = color.green

// fibOpacity = showFibLines ? 0 : 100
// hotShort                    = color.rgb(178, 34, 34, fibOpacity)
// lukeWarmShort               = color.rgb(240, 128, 128, fibOpacity)
// lukeWarmLong                = color.rgb(144, 238, 144, fibOpacity)
// hotLong                     = color.rgb(50, 205, 50, fibOpacity)
// neutralColor                = color.rgb(120, 123, 134, fibOpacity)

//fibPlotStyle                = plot.style_line
fibPlotDisplay              = showFibLines and showFibPrices ? display.data_window+display.pane+display.price_scale : showFibLines ? display.pane : showFibPrices ? display.price_scale : display.none

//Magic Math
magicNumber = (math.abs(PHI - PI) - (PHI / PI)) - (PI * ((PI * 6 / 10000) + (math.abs(PHI - PI) / 100000)))
indicatorMath = (PHI * 2) - piFifth
moveMulti = (PI /indicatorMath) - 1
moveMulti := (isCrypto ? moveMulti : moveMulti/2)
moveMulti2 = ((PI /indicatorMath)*(1+((math.pow(PHI,PI)+(PHI/PI))/100))) - 1
moveMulti2 := (isCrypto ? moveMulti2 : moveMulti2/2)
moveMulti3 = ((PI /indicatorMath)*(1+((math.pow(PHI,PI)+(PHI/PI))/(math.pow(PI, 3))))) - 1
moveMulti3 := (isCrypto ? moveMulti3 : moveMulti3/2)
moveNominal =  PI / 16
moveSmall = PI / 32

//#region Functions
ExpRM(base, exponent) =>
    var float result = 1
    for i = 0 to exponent - 1
        result := result * base
    result
    
// Function to draw a horizontal line to the next day
drawLineToNextDay(level, title, color) =>
    var line lineID = na
    // Calculate the time of the next day
    nextDay = timestamp(year, month, dayofmonth + 14, 0, 0)
    if (na(lineID))
        lineID := line.new(x1=time, y1=level, x2=nextDay, y2=level, width=4, color=color, xloc=xloc.bar_time, extend=extend.none)
    line.set_xy1(lineID, time, level)
    line.set_xy2(lineID, nextDay, level)

// Function to find the start time of the current bar based on the selected interval
findStartTime(interval) =>
    var int startTimeYear = na
    var int startTimeMonth = na
    var int startTimeDayOfMonth = na
    var int startTimeHour = na
    var int startTimeMinute = na
    startTime = time
    // Calculate the start time based on the selected interval
    if interval == "W"
        startTimeYear := year
        startTimeMonth := month
        startTimeDayOfMonth := dayofmonth
        // Calculate the start time for the current week
        startTime := timestamp(startTimeYear, startTimeMonth, startTimeDayOfMonth, 0, 0)
        // Find the day of the week for the current bar's date
        var int dayOfWeek = dayofweek(startTime)
        log.info("dayOfWeek:" + str.tostring(dayOfWeek))
        // Calculate the difference between the current day of the week and Sunday (0)
        var int daysToSunday = dayOfWeek == 0 ? 0 : 7 - dayOfWeek
        // Adjust the start time based on the days to Sunday
        startTime := startTime - daysToSunday * 86400  // 86400 seconds in a day
    else if interval == "D"
        startTimeYear := year
        startTimeMonth := month
        startTimeDayOfMonth := dayofmonth
        startTimeHour := hour
        startTimeMinute := minute
        // Calculate the start time for the current day
        startTime := timestamp(startTimeYear, startTimeMonth, startTimeDayOfMonth, 0, 0)
    else if interval == "H"
        startTimeHour := hour
        startTimeMinute := minute
        // Calculate the start time for the current hour
        startTime := timestamp(year, month, dayofmonth, startTimeHour, startTimeMinute)
    
    // Return the calculated start time
    startTime

drawLineToCurrentGLP(level, title, color) =>
    var line lineID = na
    // Calculate the time of the next day
    nextDay = findStartTime(timeframeOptions) //timestamp(year, month, dayofmonth, 0, 00)
    if (na(lineID))
        lineID := line.new(x1=time, y1=level, x2=nextDay, y2=level, width=4, color=color, xloc=xloc.bar_time, extend=extend.none)
    line.set_xy1(lineID, time, level)
    line.set_xy2(lineID, nextDay, level)
    // var line lineID = na
    // GLPStart = time
    // GLPEnd = int(na(time[1]) ? na : time[1] + timeframe.multiplier * 60)
    // str.tostring("This Level")
    // if (na(lineID))
    //     lineID := line.new(x1=bar_index[0], y1=level, x2=bar_index[100], y2=level, width=2, color=color, xloc=xloc.bar_time, extend=extend.right)
    // line.set_xy1(lineID, GLPStart, level)
    // line.set_xy2(lineID, GLPEnd, level)
    //label.new(x=bar_index + 3, y=level, text="(" + timeFrameLabel + ") " + title, style=label.style_label_left, color=color)


// Grab data from previousious daily candle and scale
fetchData(field) =>
    request.security(syminfo.tickerid, timeframeOptions, field, lookahead=barmerge.lookahead_on) * scaler

calculateFibLevels(start, end) =>
    diff = end - start
    fib_236 = start + diff * 0.236
    fib_382 = start + diff * 0.382
    fib_5 = start + diff * 0.5
    fib_618 = start + diff * 0.618
    fib_786 = start + diff * 0.786
    [fib_236, fib_382, fib_5, fib_618, fib_786]

//#endregion

// Data Fetching
previous_iclose                 = fetchData(close[1])
previous_iopen                  = fetchData(open[1])
previous_ihigh                  = fetchData(high[1])
previous_ilow                   = fetchData(low[1])
previous_ivolume                = na(fetchData(volume[1])) ? 1 : fetchData(volume[1])

current_open                  = fetchData(open)
current_low                   = fetchData(low)
current_high                  = fetchData(high)

// Calculations
avg_previous = math.avg(current_open, previous_iclose, previous_iopen, previous_ihigh, previous_ilow)
adjusted_open = current_open * magicNumber
neutral_target = math.avg(avg_previous, adjusted_open)
neutral = neutral_target / scaler

// Daily Overpriced Level III Targets Plot
// Overpriced & Discounted Targets

overpriceLowMultiplier  = -((PI+3.0)*PI/100)
previousOpenMultiplier  = (PI+(PHI/2)+(0.09))*PI/100 
previousHighMultiplier  = -(((PI*11)-(PHI*6))/100) 
discountLowMultiplier   = ((PI*6)-(PHI))/100 

currentOpenMultiplied = current_open * o_overpriced_multiplier
previousIntervalLowMultiplied = previous_ilow * overpriceLowMultiplier 
previousIntervalOpenMultiplied = previous_iopen * previousOpenMultiplier 
previousVolumeRegulated = previous_ivolume * (currentOpenMultiplied / ExpRM(10, 20))
overpriceIScaled = (currentOpenMultiplied + previousIntervalLowMultiplied + previousIntervalOpenMultiplied + previousVolumeRegulated) - moveMulti
overpriceIIScaled = overpriceIScaled + (overpriceIScaled * moveMulti)
overpriceIIIScaled = overpriceIIScaled + (overpriceIIScaled * moveMulti2)
overpriceIVScaled = overpriceIIScaled + (overpriceIIIScaled * moveMulti3)
overpriceI = overpriceIScaled / scaler
overpriceII = overpriceIIScaled / scaler
overpriceIII = overpriceIIIScaled / scaler
overpriceIV = overpriceIVScaled / scaler

// Discounted Targets
currentOpenMultiplied := current_open * o_discounted_multiplier
previousHighMultiplied = previous_ihigh * previousHighMultiplier
previousLowMultiplied = previous_ilow * discountLowMultiplier
discountIScaled = (currentOpenMultiplied + previousHighMultiplied + previousLowMultiplied) + moveNominal
discountIIScaled = discountIScaled - (discountIScaled * moveMulti)
discountIIIScaled = discountIIScaled - (discountIIScaled * moveMulti2)
discountIVScaled = discountIIIScaled - (discountIIIScaled * moveMulti3)
discountedI = discountIScaled / scaler
discountedII = discountIIScaled / scaler
discountedIII = discountIIIScaled / scaler
discountedIV = discountIVScaled / scaler

anticipatedCurrentHighMultiplier    = ((PI*33)+(PHI*11))/100 //1.214
anticipatedPreviousHighMultiplier   = -(PI*(5.09))/100 //-0.160 
anticipatedCurrentOpenMultiplier    = -((PI*6)+(PHI*1))/100 //-0.209 
anticipatedCurrentLowMultiplier     = ((PI*PI)+(PHI*2))/100 //0.130 

// anticipatedcipated Close Calculation
currentHighMultiplied = current_high * anticipatedCurrentHighMultiplier
overpriceIScaled := previous_ihigh * anticipatedPreviousHighMultiplier 
currentOpenMultiplied := current_open * anticipatedCurrentOpenMultiplier 
previousIntervalLowMultiplied := current_low * anticipatedCurrentLowMultiplier
closeguess = ((currentHighMultiplied + overpriceIScaled + currentOpenMultiplied + previousIntervalLowMultiplied) + moveSmall)/ scaler

//CORE CODE END
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
//#endregion

//#region Fib Plotting
[discounted1_fib_236, discounted1_fib_382, discounted1_fib_500, discounted1_fib_618, discounted1_fib_786] = calculateFibLevels(neutral, discountedI)
[discounted2_fib_236, discounted2_fib_382, discounted2_fib_500, discounted2_fib_618, discounted2_fib_786] = calculateFibLevels(discountedI, discountedII)
[discounted3_fib_236, discounted3_fib_382, discounted3_fib_500, discounted3_fib_618, discounted3_fib_786] = calculateFibLevels(discountedII, discountedIII)
[discounted4_fib_236, discounted4_fib_382, discounted4_fib_500, discounted4_fib_618, discounted4_fib_786] = calculateFibLevels(discountedIII, discountedIV)
[overpriced1_fib_236, overpriced1_fib_382, overpriced1_fib_500, overpriced1_fib_618, overpriced1_fib_786] = calculateFibLevels(neutral, overpriceI)
[overpriced2_fib_236, overpriced2_fib_382, overpriced2_fib_500, overpriced2_fib_618, overpriced2_fib_786] = calculateFibLevels(overpriceI, overpriceII)
[overpriced3_fib_236, overpriced3_fib_382, overpriced3_fib_500, overpriced3_fib_618, overpriced3_fib_786] = calculateFibLevels(overpriceII, overpriceIII)
[overpriced4_fib_236, overpriced4_fib_382, overpriced4_fib_500, overpriced4_fib_618, overpriced4_fib_786] = calculateFibLevels(overpriceIII, overpriceIV)
//discountedIV


// Plotting the Fibonacci levels


//drawLineToCurrentGLP(overpriced3_fib_786, "O3 78.6%", color.blue)
plot(overpriced4_fib_786, "O4 78.6%", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 78.6% Bull"
plot(overpriced4_fib_618, "O4 61.8%", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 61.8% Bull"
plot(overpriced4_fib_500, "O4 50%  ", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 50% Bull"
plot(overpriced4_fib_382, "O4 38.2%", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 38.2% Bull"
plot(overpriced4_fib_236, "O4 23.6%", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 23.6% Bull"

plot(overpriced3_fib_786, "O3 78.6%", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 78.6% Bull"
plot(overpriced3_fib_618, "O3 61.8%", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 61.8% Bull"
plot(overpriced3_fib_500, "O3 50%  ", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 50% Bull"
plot(overpriced3_fib_382, "O3 38.2%", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 38.2% Bull"
plot(overpriced3_fib_236, "O3 23.6%", color=color.rgb(178, 34, 34, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 23.6% Bull"

plot(overpriced2_fib_786, "O2 78.6%", color=color.rgb(240, 128, 128, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 78.6% Bull"
plot(overpriced2_fib_618, "O2 61.8%", color=color.rgb(240, 128, 128, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 61.8% Bull"
plot(overpriced2_fib_500, "O2 50%  ", color=color.rgb(240, 128, 128, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 50% Bull"
plot(overpriced2_fib_382, "O2 38.2%", color=color.rgb(240, 128, 128, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 38.2% Bull"
plot(overpriced2_fib_236, "O2 23.6%", color=color.rgb(240, 128, 128, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 23.6% Bull"

plot(overpriced1_fib_786, "O1 78.6%", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 78.6% Bull"
plot(overpriced1_fib_618, "O1 61.8%", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 61.8% Bull"
plot(overpriced1_fib_500, "O1 50%  ", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 50% Bull"
plot(overpriced1_fib_382, "O1 38.2%", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 38.2% Bull"
plot(overpriced1_fib_236, "O1 23.6%", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 23.6% Bull"

plot(discounted1_fib_236, "D1 23.6%", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 23.6% Bear"
plot(discounted1_fib_382, "D1 38.2%", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 38.2% Bear"
plot(discounted1_fib_500, "D1 50%  ", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 50% Bear"
plot(discounted1_fib_618, "D1 61.8%", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 61.8% Bear"
plot(discounted1_fib_786, "D1 78.6%", color=color.rgb(120, 123, 134, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 78.6% Bear"

plot(discounted2_fib_236, "D2 23.6%", color=color.rgb(144, 238, 144, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 23.6% Bear"
plot(discounted2_fib_382, "D2 38.2%", color=color.rgb(144, 238, 144, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 38.2% Bear"
plot(discounted2_fib_500, "D2 50%  ", color=color.rgb(144, 238, 144, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 50% Bear"
plot(discounted2_fib_618, "D2 61.8%", color=color.rgb(144, 238, 144, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 61.8% Bear"
plot(discounted2_fib_786, "D2 78.6%", color=color.rgb(144, 238, 144, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 78.6% Bear"

plot(discounted3_fib_236, "D3 23.6%", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 23.6% Bear"
plot(discounted3_fib_382, "D3 38.2%", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 38.2% Bear"
plot(discounted3_fib_500, "D3 50%  ", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 50% Bear"
plot(discounted3_fib_618, "D3 61.8%", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 61.8% Bear"
plot(discounted3_fib_786, "D3 78.6%", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 78.6% Bear"

plot(discounted4_fib_236, "D4 23.6%", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 23.6% Bear"
plot(discounted4_fib_382, "D4 38.2%", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 38.2% Bear"
plot(discounted4_fib_500, "D4 50%  ", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 50% Bear"
plot(discounted4_fib_618, "D4 61.8%", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 61.8% Bear"
plot(discounted4_fib_786, "D4 78.6%", color=color.rgb(50, 205, 50, 50), linewidth=1, style=plot.style_line, display = fibPlotDisplay) //"Fib 78.6% Bear"
//drawLineToCurrentGLP(discounted3_fib_786, "D3 78.6%", color.blue)
//#endregion

//#region Plotting
// Draw a line from the current bar to the end of the day at the level of max1
pctGain1 = ((overpriceI - neutral) / neutral) * 100
pctGain2 = ((overpriceII - overpriceI) / neutral) * 100
pctGain3 = ((overpriceIII - overpriceII) / neutral) * 100
pctGain4 = ((overpriceIV - overpriceIII) / neutral) * 100
pctLoss1 = ((discountedI -neutral) / neutral) * 100
pctLoss2 = ((discountedII - discountedI) / neutral) * 100
pctLoss3 = ((discountedIII - discountedII) / neutral) * 100
pctLoss4 = ((discountedIV - discountedIII) / neutral) * 100


pctGainRndUp1 = math.ceil(pctGain1 * 100) / 100
pctGainRndUp2 = math.ceil(pctGain2 * 100) / 100
pctGainRndUp3 = math.ceil(pctGain3 * 100) / 100
pctGainRndUp4 = math.ceil(pctGain4 * 100) / 100
pctLossRndUp1 = math.ceil(pctLoss1 * 100) / 100
pctLossRndUp2 = math.ceil(pctLoss2 * 100) / 100
pctLossRndUp3 = math.ceil(pctLoss3 * 100) / 100
pctLossRndUp4 = math.ceil(pctLoss4 * 100) / 100

fontColor = color.white
labelStyle = label.style_label_left
//SECTION Labels
var label timeframeLabel = na

if (barstate.islast)
    label.new(x=bar_index + 3, y=overpriceIV, text="(" + timeFrameLabel + ") " + str.tostring(pctGainRndUp2+pctGainRndUp1+pctGainRndUp3+pctGainRndUp4) + "%", style=labelStyle, color=fontColor)
    label.new(x=bar_index + 3, y=overpriceIII, text="(" + timeFrameLabel + ") " + str.tostring(pctGainRndUp2+pctGainRndUp1+pctGainRndUp3) + "%", style=labelStyle, color=fontColor)
    label.new(x=bar_index + 3, y=overpriceII, text="(" + timeFrameLabel + ") " + str.tostring(pctGainRndUp2+pctGainRndUp1)  + "%", style=labelStyle, color=fontColor)
    label.new(x=bar_index + 3, y=overpriceI, text="(" + timeFrameLabel + ") " + str.tostring(pctGainRndUp1) + "%", style=labelStyle, color=fontColor)
    label.new(x=bar_index + 3, y=discountedI, text="(" + timeFrameLabel + ") " + str.tostring(pctLossRndUp1) + "%", style=labelStyle, color=fontColor)
    label.new(x=bar_index + 3, y=discountedII, text="(" + timeFrameLabel + ") " + str.tostring(pctLossRndUp2+pctLossRndUp1) + "%", style=labelStyle, color=fontColor)
    label.new(x=bar_index + 3, y=discountedIII, text="(" + timeFrameLabel + ") " + str.tostring(pctLossRndUp2+pctLossRndUp1+pctLossRndUp3) + "%", style=labelStyle, color=fontColor)
    label.new(x=bar_index + 3, y=discountedIV, text="(" + timeFrameLabel + ") " + str.tostring(pctLossRndUp2+pctLossRndUp1+pctLossRndUp3+pctLossRndUp4) + "%", style=labelStyle, color=fontColor)
    label.new(x=bar_index + 3, y=neutral, text="(" + timeFrameLabel + ") Neutral", style=labelStyle, color=fontColor)


drawLineToNextDay(overpriceIV, "Overprice Level IV", overpriced_color)
drawLineToNextDay(overpriceIII, "Overprice Level III", overpriced_color)
drawLineToNextDay(overpriceII, "Overpriced Level II", overpriced_color)
drawLineToNextDay(overpriceI, "Overpriced Level I", overpriced_color)
drawLineToNextDay(discountedI, "Discount Level I", discounted_color)
drawLineToNextDay(discountedII, "Discount Level II", discounted_color)
drawLineToNextDay(discountedIII, "Discount Level III", discounted_color)
drawLineToNextDay(discountedIV, "Discount Level IV", discounted_color)
drawLineToNextDay(neutral, "Neutral Price", neutral_color)

plot(overpriceIV, "Overpriced Level IV", overpriced_color, 4, plot.style_line) 
plot(overpriceIII, "Overpriced Level III", overpriced_color, 4, plot.style_line) 
plot(overpriceII, "Overpriced Level II", overpriced_color, 4, plot.style_line)
plot(overpriceI, "Overpriced Level I", overpriced_color, 4, plot.style_line)
plot(neutral, "Neutral Price", neutral_color, 4, plot.style_line)
plot(discountedI, "Discount Level I", discounted_color, 4, plot.style_line)
plot(discountedII, "Discount Level II", discounted_color, 4, plot.style_line)
plot(discountedIII, "Discount Level III", discounted_color, 4, plot.style_line)
plot(discountedIV, "Discount Level IV", discounted_color, 4, plot.style_line)
plot(closeguess, "Hit Marks", color.purple, 2, style=plot.style_circles)

//#endregion
