Kitti-Playbook Fibonacci retracement  GAGE R0.00
// 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 R0.042",overlay=true)

// Date Jul 24 2023
// 1 Change Setting Menu of Fibonacci color Zone
// 2 The color of background of label is  change follow setting of Fibonacci  zone color seting  
// 3 Able to Adjust Label Text Size


// Date Apr 12 2023 R0.041
// Debug label 61.,8% --> 61.8%
// Date Apr 11 2023 R4
// 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 
// 4) Debug Source of Price for calculate fibonacci   


//Date Feb 28 2023  R3
//   Color of Label 
//   If last Price  is lower than  61.8%  of the New highest price  -  color  Orange
// Date Feb 28 2023 
//   Color of Label 
//   If last Price  is lower than  the New highest price  -  color  gray
//   If last Price  is lower than  61.8%  of the New highest price  -  color  Bule
//   If last Price  is lower than  78.6%  of the New highest price  -  color  Green
//   If last Price  is lower than  94.2%  of the New highest price  -  color  Yellow
// Date Dec 20 2022
//  Jan 7 2023  Label layout adjust 
//  Addition  12 26 EMS cross Indicator
// 
// Date Oct 25 2021
// OVERVIEW :Kitti-Playbook Fibonacci retracement  GAGE R0.00
// Easy for  visualize  Fibonacci retracement level 
// CONCEPTS
//   1)Minimum Line = Lowest level of Source form start
//   2)Maximum Line = Highest level of Source  form New Low 
//   3)Calculation 
//       a) Fibonacci Retracement of 61.8% form Maximum level
//       b) Fibonacci Retracement of 78.6% form Maximum level
//       c) Fibonacci Retracement of 88.7% form Maximum level
//       d) Fibonacci Retracement of 94.2% form Maximum level
//   4)Information Display 
//       a) Information Bar show  Number of New Low , Max level , Min Level , Last Bar No, Current Pricel
//       b) GAGE Scale Number
//       c) New Low 



// ==>  1)Minimum Line = Lowest level of Source form start
i_astart= 0
i_aend= 1
i_aSource=input.source(close, "Source Min",group="Source" , inline= "A",display=display.data_window)

a = array.new_float(0)           
for i = i_astart to i_aend       
	array.push(a, i_aSource[i])    

aMin0 = array.min(a)

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

aMin = ( x ) 

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

//HL=close>aMin[1]  and  close[1]==aMin

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)Maximum Line = Highest level of Source  form New Low 

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

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 

//  ==> 3)Calculation 
//       a) Fibonacci Retracement of 61.8% form Maximum level
//       b) Fibonacci Retracement of 78.6% form Maximum level
//       c) Fibonacci Retracement of 88.7% form Maximum level
//       d) Fibonacci Retracement of 94.2% form Maximum level


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

// Calculate Current price %

SPercent = ( bMax- i_aSource )/ Range 

//  Plot Min Max Line


Lmin=plot(aMin,"Lowest",color=color.new(color.red,50))
Lmax=plot(bMax,"Highest*",color=color.new(color.yellow,50))

// 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_ColRangeH00 = color.new(#6d1818, 100) 


i_ColRangeH67 = i_ColRangeH00  //  color.new(#6d1818, 100) // input.color(color.new(#6d1818, 100), "Waiting Zone",group="61.8-88.7 Zone", inline="Line 1", display=display.none,group="Fibinacci Zone color Setting")
i_ColRangeL67 = input.color(color.new(#29b968, 55), "61.8% ~ 78.6%", inline="Line 1", display=display.none,group="Fibinacci Zone color Setting")

i_ColRangeH78 =  i_ColRangeH00  //color.new(#6d1818, 100)  //input.color(color.new(#6d1818, 100), "Waiting Zone",group="78.6-88.7 Zone", inline="Line 2", display=display.none,group="Fibinacci Zone color Setting")
i_ColRangeL78 = input.color(color.new(#29b968, 35), "78.6% ~  88.7%", inline="Line 2", display=display.none,group="Fibinacci Zone color Setting")

i_ColRangeH89 =  i_ColRangeH00  // color.new(#6d1818, 100)// input.color(color.new(#6d1818, 100), "Waiting Zone",group="88.7-94.2 Zone", inline="Line 3", display=display.none,group="Fibinacci Zone color Setting")
i_ColRangeL89 = input.color(color.new(#29b968, 55),"88.7% ~ 94.2%" , inline="Line 3", display=display.none,group="Fibinacci Zone color Setting")

i_ColRangeH90 = i_ColRangeH00   // color.new(#6d1818, 100) //input.color(color.new(#6d1818, 100), "Waiting Zone",group="94.2-100 Zone", inline="Line 4", display=display.none,group="Fibinacci Zone color Setting")
i_ColRangeL90 = input.color(color.new(#b92989, 75), "94.2% ~ 100%", inline="Line 4", display=display.none,group="Fibinacci Zone color Setting")


ColorFill67=color.from_gradient(close,aMin,bMax-Range*.618,i_ColRangeL67,i_ColRangeH67)
ColorFill78=color.from_gradient(close,aMin,bMax-Range*.786,i_ColRangeL78,i_ColRangeH78)
ColorFill89=color.from_gradient(close,aMin,bMax-Range*.887,i_ColRangeL89,i_ColRangeH89)
ColorFill90=color.from_gradient(close,aMin,bMax-Range*.942,i_ColRangeL90,i_ColRangeH90)


fill(L6,L7,color=ColorFill67)

fill(L7,L8,color=ColorFill78)

fill(L8,L9,color=ColorFill89)

fill(L9,Lmin,color=ColorFill90)


// ==> 4)Information Display 
//       a) Information Bar show  Number of New Low , Max level , Min Level , Last Bar No, Current Pricel
//       b) GAGE Scale Number
//       c) New Low 
//

//       a) Information Bar show  Number of New Low , Max level , Min Level , Last Bar No, Current Pricel
LabelSW=  input.bool(title="Fibonacci value  On/Off", defval=true,group='Fibinacci Display Setting',display=display.none) 

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


tColor1 = #aaaaaa  // Text Color
plot(SPercent,"Spercent",color.green, display=display.data_window) 



// bColor0 =  SPercent >= 0.942 ?  color.new(color.yellow,80) :
//              SPercent >= 0.786 ?  color.new(color.green,80) :
//              SPercent >= 0.618 ? color.new(color.orange,80) : color.new(color.gray,90)


bColor0 =  SPercent >= 0.942 ?  i_ColRangeL90:
             SPercent >= 0.887 ?  i_ColRangeL89:
             SPercent >= 0.786 ?  i_ColRangeL78:
             SPercent >= 0.618 ? i_ColRangeL67: color.new(color.gray,90)



ShowInfoTab = input.bool(title='Show Infomation Bar ', defval=true, group='Information Bar Display Setting',display=display.none,tooltip = "The color of the background changes according to the Fibonacci level at that time.")  // Show Tab
Tab_Width = input.int(16, "Information Bar's  Width", minval=10, maxval=200, step=5,group='Information Bar Display Setting',display=display.none)

table_position = input.string(position.top_right, 'Information Bar position', options=
     [position.middle_left, 
     position.middle_center, 
     position.middle_right, 
     position.bottom_left, 
     position.bottom_center, 
     position.bottom_right, 
     position.top_center, 
     position.top_right], 
     group='Information Bar Display Setting',
     display=display.none    
     )


Front1=input.string(size.normal,"Label Text Size",options=
     [
      size.tiny, 
      size.small, 
      size.normal,
      size.large, 
      size.huge],
      group='Information Bar Display Setting',
      display=display.none)



var table Signal_table = table.new(position=table_position, columns=1, rows=1, border_width=2)

_TEXT_DisplayA0 ="  Last (-" +str.format("{0,number,percent}",SPercent ) + ")  = " + str.tostring(math.round_to_mintick(close )) +" " + syminfo.currency
                        
                         + " \n  100%  = " + str.tostring(math.round_to_mintick(bMax))  +" " + syminfo.currency
                         + " \n  0.0%  = " + str.tostring(math.round_to_mintick(aMin))   +" " + syminfo.currency
                         + " \n  No.of Bar = "  +str.tostring(bar_index) + " bars"
                         + " \n  No.of New Low  " + " = " + str.tostring(Count_HL) + "  Times"
                      
if ShowInfoTab == true
    table.cell(Signal_table, 0, 0, _TEXT_DisplayA0, bgcolor=bColor0, text_color=tColor1,text_halign=text.align_left,text_size = Front1, width=Tab_Width)
 

//       b) GAGE Scale Number


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)


//       c) New Low 

bgcolor(HL == true ? color.new(#888888,50) : color.new(#888888,100))


///////////////////////////  END of Fibo Retracement   //////////////////////////////////////


// -------                        EMA 12 26 Cross Indicator          ----------------- //////


xsrc = close 
xprd1 = 12 
xprd2 = 26 
xsmooth = 1 

fillSW = input.bool(title='Paint Bar Colors', defval=false ,group= "EMA 12 26 Cross Indicator" , inline = "line1",display=display.none)
labelSwitch = input.bool(title='Turn on assistive text', defval=false,group= "EMA 12 26 Cross Indicator" , inline = "line1",display=display.none)

fastSW = input.bool(title='Show 12 EMA line', defval=false , group= "EMA 12 26 Cross Indicator" , inline = "line2",display=display.none)
slowSW = input.bool(title='Show 26 EMA line', defval=false ,group= "EMA 12 26 Cross Indicator" , inline = "line2",display=display.none)

plotSigsw = input.bool(title='Plot Buy/Sell Signals? ', defval=false ,group= "EMA 12 26 Cross Indicator" , inline = "line3",display=display.none)
plotRibsw = input.bool(title='Plot Buy/Sell Ribbon', defval=true,group= "EMA 12 26 Cross Indicator" , inline = "line3",display=display.none)
plotRibbonPos = input.string(title='Ribbon Position', options=['Top', 'Bottom'], defval='Bottom',group= "EMA 12 26 Cross Indicator" , inline = "line4",display=display.none)


//****************************************************************************//
//Calculate Indicators

f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on) // Using f_secureSecurity to avoid repainting

xPrice = ta.ema(xsrc, xsmooth)
FastMA =  ta.ema(xPrice, xprd1)
SlowMA = ta.ema(xPrice, xprd2)


Bull = FastMA > SlowMA
Bear = FastMA < SlowMA

//****************************************************************************//
// Define Color Zones

Green = Bull and xPrice > FastMA  // Buy
Blue = Bear and xPrice > FastMA and xPrice > SlowMA  //Pre Buy 2
LBlue = Bear and xPrice > FastMA and xPrice < SlowMA  //Pre Buy 1

Red = Bear and xPrice < FastMA  // Sell
Orange = Bull and xPrice < FastMA and xPrice < SlowMA  // Pre Sell 2
Yellow = Bull and xPrice < FastMA and xPrice > SlowMA  // Pre Sell 1

//****************************************************************************//
// Display color on chart


bColor = Green ? color.green :
 Blue ? color.blue : 
 LBlue ? color.aqua :
 Red ? color.red : 
 Orange ? color.orange : 
 Yellow ? color.yellow : 
 color.black
barcolor(color=fillSW ? bColor : na)


//****************************************************************************//
// Display MA lines



FastL = plot(fastSW ? FastMA : na, 'Fast EMA', color=color.new(color.red, 0), style =  plot.style_line)
SlowL = plot(slowSW ? SlowMA : na, 'Slow EMA', color=color.new(color.blue, 0), style = plot.style_line)

fillcolor = Bull ? color.new(color.green,90) : Bear ? color.new(color.red,90) : color.new(color.black,90) // fillcolor = Bull ? color.green : Bear ? color.red : color.black
fill(FastL, SlowL, fillcolor) // fill(FastL, SlowL, fillcolor, transp=90)

//****************************************************************************//
// Define Buy and Sell condition
// This is only for thebasic usage of CDC Actionzone (EMA Crossover) 
// ie. Buy on first green bar and sell on first red bar

buycond = Green and Green[1] == 0
sellcond = Red and Red[1] == 0


LARGE_INT = 50000
bullish = nz(ta.barssince(buycond), LARGE_INT) < nz(ta.barssince(sellcond), LARGE_INT)
bearish = nz(ta.barssince(buycond), LARGE_INT) > nz(ta.barssince(sellcond), LARGE_INT)


buy = bearish[1] and buycond
sell = bullish[1] and sellcond

bColor_BullBear = bullish ? color.green : bearish ? color.red : color.black

//****************************************************************************//
// Plot Buy and Sell point on chart

plotshape(plotSigsw ? buy : na,
 style=shape.circle,
 title='Buy Signal', 
 location=location.belowbar, 
 color=color.new(color.green, 0))
plotshape(plotSigsw ? sell : na, 
 style=shape.circle, 
 title='Sell Signal', 
 location=location.abovebar, 
 color=color.new(color.red, 0))


// Display Buy/Sell Ribbon

plotshape(plotRibsw ? plotRibbonPos == 'Top' ? close : na : na, 
 style=shape.diamond, 
 title='Buy/Sell Ribbon', 
 location=location.top, 
 color=bColor_BullBear)

plotshape(plotRibsw ? plotRibbonPos == 'Bottom' ? close : na : na,
 style=shape.diamond, 
 title='Buy/Sell Ribbon', 
 location=location.bottom, 
 color=bColor_BullBear)


//****************************************************************************//
// Label

labelstyle = close > SlowMA ? label.style_label_down : label.style_label_up
labelyloc = close > SlowMA ? yloc.abovebar : yloc.belowbar
labeltcolor = buy ? color.black : 
 sell ? color.white :
 close > close[1] ? color.green : 
 color.red
labelbgcolor = buy ? color.green : sell ? color.red : color.silver
labeltext = buy ? 'BUY next bar\n' : sell ? 'SELL next bar\n' : ' '
trendText = bullish ? 'bullish' : bearish ? 'bearish' : 'sideways'


l1 = label.new(bar_index, na,
 text=labeltext + syminfo.ticker + ' ' + str.tostring(close) + ' ' + syminfo.currency + '\n currently in a ' + trendText + ' trend \n', 
 color=labelbgcolor, 
 textcolor=labeltcolor, 
 yloc=labelyloc, 
 style=labelstyle)

label.delete(labelSwitch ? l1[1] : l1)


//****************** End 12 26 EMA Indicator  **********************************************************//
//******************                 End      **********************************************************//

