Kitti-Playbook Fibonacci retracement GAGE Click Start
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Kittimasak_S
//@version=5
indicator("Kitti-Playbook Fibonacci retracement GAGE Click Start R0.1",overlay=true)
// Date Apr 13 2023R0.1
// 1) Addition Setting TEXT size menu  for display Fibonacci Value and currency
// 2) Addition Unit of Currency for display Fibonacci Value and currency
// 3) Debug for small digit currency display for 2 digit to Mintick 



// Date Jan 9 2022   DEBUG Miss operation  for Minimum line Line Calculation 
// Date Jan 1 2022
// OVERVIEW :Kitti-Playbook Fibonacci retracement  GAGE R0.00
// Easy for  visualize  Fibonacci retracement level 
// CONCEPTS
//   0)Point and Click on chart to point Origin 
//   1)Calculate Minimum Line from Origin point
//   2)Calculate Maximum Line from New Low 
//   3)Calculation Fibo scale 
//       a) Fibonacci Retracement of 61.8% form Maximum level  , Defaultf Suorce = close
//       b) Fibonacci Retracement of 78.6% form Maximum level  . Defaultf Suorce = close
//       c) Fibonacci Retracement of 88.7% form Maximum level
//       d) Fibonacci Retracement of 94.2% form Maximum level
//   4)Information Display 
//        GAGE Scale Number from origin point
//        Highest  Lowest  Value from origin point




LabelSW=  input.bool(title="Show Fibonacci ", defval=true) 
LabelSW1=  input.bool(title="Show Lowest - Highes level ", defval=false) 
LabelSW2=  input.bool(title="Show Lowest - Highes label ", defval=false) 

Front0=input.string(size.normal,"Fibonacci Text Size",options=
     [
      size.tiny, 
      size.small, 
      size.normal,
      size.large, 
      size.huge],
      group='Fibinacci Display Setting')

//
// ==>  1)Minimum Line = Lowest level of Source form start


//   0)Point and Click on chart to point Origin 

startCalculationDate = input.time(timestamp("1 Dec 2021"), "Origin point by Point then CLICK on chart", confirm=true)

//   1)Calculate Minimum Line from Origin point
i_aSource=input.source(close, "Source Min",group="Fibo Source",inline= "A")
Low_calc() =>

    var srcLowArray = array.new_float(na)
    var srcHighArray = array.new_float(na)
    var srcSurArray = array.new_float(na)
    if startCalculationDate <= time     
        array.push(srcLowArray, low)     // LL Line
        array.push(srcHighArray, high)   // HH Line  
        array.push(srcSurArray, i_aSource) 
    else
        array.clear(srcLowArray)
        array.clear(srcHighArray)
        array.clear(srcSurArray)
        
    [array.min(srcLowArray),array.max(srcHighArray),array.min(srcSurArray),array.max(srcSurArray) ]
    
[Low_Line,High_Line,aMin0,bMax0a] = Low_calc()

float x=na
x :=aMin0
if x > x[1]
    x := x[1]
else    
    x := x
    
aMin = x 

//---------- Check and count Minimum Points

HL=i_aSource>aMin[1]  and  i_aSource[1]==aMin
HLC=HL==true?1:0    
Count_HL=ta.cum(HLC)   //Count Minimum Points
BSLL=ta.barssince(HL) *0.001
//plot(BSLL)

//   2)Calculate Maximum Line from New Low 

i_bstart= 0
i_bend= 1
i_bSource=input.source(close, "Source Max",group="Fibo Source",inline= "A")

b = array.new_float(0)           
for i = i_bstart to i_bend         
	array.push(b, i_bSource[i])      
bMax0 = math.round_to_mintick(array.max(b))

float y=na
//y :=bMax0
y :=bMax0

// Confirm New Low for Set New HHV 

if y < y[1]  and  BSLL > 0
    y := y[1]
else    
    y := y
    
bMax = y 

P_Low_Line   = LabelSW1 ?    Low_Line   : na
P_High_Line  = LabelSW1 ?    High_Line  : na


plot(P_Low_Line, "Lower Low",color=color.new(color.green,50) ,linewidth=1)
plot(P_High_Line, "High High", color=color.new(color.green,50),linewidth=1)


//   3)Calculation Fibo scale 
//       a) Fibonacci Retracement of 61.8% form Maximum level  , Defaultf Suorce = close
//       b) Fibonacci Retracement of 78.6% form Maximum level  . Defaultf Suorce = close
//       c) Fibonacci Retracement of 88.7% form Maximum level
//       d) Fibonacci Retracement of 94.2% form Maximum level

Range=math.round_to_mintick(bMax-aMin)
L618=LabelSW ? math.round_to_mintick(bMax-Range*0.618) :na
L786=LabelSW ? math.round_to_mintick(bMax-Range*0.786) :na
L887=LabelSW ? math.round_to_mintick(bMax-Range*0.887) :na
L942=LabelSW ? math.round_to_mintick(bMax-Range*0.942) :na


P_aMin = LabelSW ? aMin :na
P_bMax = LabelSW ? bMax :na 

bMax_Col= startCalculationDate <= time    ? color.new(color.yellow,50) : na


plot(P_aMin,"Min 0%",color=color.new(color.red,50))
plot(P_bMax,"Max 100%",color=bMax_Col)

// Plot 
Color_Line=color.new(#888888,50)
L6 =plot(L618 , "61.8", color=Color_Line)
L7=plot(L786 , "78.6", color=Color_Line)
L8=plot(L887 , "88.7", color=Color_Line)
L9=plot(L942,"94.2", color=Color_Line)

i_ColRangeH = input.color(color.new(color.orange,90), "Waiting Zone")
i_ColRangeL = input.color(color.new(color.blue,70), "Action  Zone")

ColorFill=color.from_gradient(close,aMin+Range*.1,bMax-0.5*Range,i_ColRangeL,i_ColRangeH)


fill(L6,L9,color=ColorFill )


//   4)Information Display 
//        GAGE Scale Number from origin point
//        Highest  Lowest  Value from origin point

bc=#88888888
styleUpDwn= label.style_none
ylocatPrice = yloc.price 

//------------ Max ,61.8 78.6 88.7 94.2 min )



lMax = label.new(bar_index, bMax,
  text= "0.00% = " +str.tostring(bMax ) + " "+syminfo.currency , color=bc,
  style= styleUpDwn, textcolor=color.new(#888888,20),size=Front0 , yloc=ylocatPrice ) 
label.delete(LabelSW? lMax[1] : lMax)

l618 = label.new(bar_index, L618,
  text= "61.8% = " +str.tostring(L618 )+ " "+syminfo.currency, color=bc,
  style= styleUpDwn, textcolor=color.new(#888888,20), size=Front0 ,yloc=ylocatPrice ) 
label.delete(LabelSW? l618[1] : l618)

l786 = label.new(bar_index, L786,
  text= "78.6% = " +str.tostring(L786 )+ " "+syminfo.currency, color=bc,
  style= styleUpDwn, textcolor=color.new(#888888,20), size=Front0 ,yloc=ylocatPrice ) 
label.delete(LabelSW? l786[1] : l786)

l887 = label.new(bar_index, L887,
  text= "88.7% = " +str.tostring(L887 )+ " "+syminfo.currency, color=bc,
  style= styleUpDwn, textcolor=color.new(#888888,20), size=Front0 ,yloc=ylocatPrice ) 
label.delete(LabelSW? l887[1] : l887)

l942 = label.new(bar_index, L942,
  text= "94.2% = " +str.tostring(L942 )+ " "+syminfo.currency, color=bc,
  style= styleUpDwn, textcolor=color.new(#888888,20), size=Front0 ,yloc=ylocatPrice ) 
label.delete(LabelSW? l942[1] : l942)

laMin = label.new(bar_index, aMin,
  text= " 100%  = " +str.tostring(math.round_to_mintick(aMin))+ " "+syminfo.currency, color=bc,
  style= styleUpDwn, textcolor=color.new(#888888,20), size=Front0 ,yloc=ylocatPrice ) 
label.delete(LabelSW? laMin[1] : laMin)



label.delete(LabelSW? laMin[1] : laMin)

// ---------------------  Highest   Lowest  label ---------------------------------


LLV = label.new(bar_index, P_Low_Line ,
  text= "Lowest = " +str.tostring(math.round_to_mintick(P_Low_Line ))+ " "+syminfo.currency, color=bc,
  style= styleUpDwn, textcolor=color.new(#888888,20), size=Front0 ,yloc=ylocatPrice ) 
label.delete(LabelSW2? LLV[1] : LLV)


HHV = label.new(bar_index, P_High_Line ,
  text= "Highest = " +str.tostring(math.round_to_mintick(High_Line ))+ " "+syminfo.currency, color=bc,
  style= styleUpDwn, textcolor=color.new(#888888,20), size=Front0 ,yloc=ylocatPrice ) 
label.delete(LabelSW2? HHV[1] : HHV)



///////////////////////////  END   //////////////////////////////////////

