Kitti-Playbook Request.Volume X Price 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 Request.Volume X Price R0.01")
//=================================================================================================
// Date July 30 2023. R 0.01
//  Add Setting functions 
//    setting Chart  for  Column / Area 
//    setting Chart color
//    setting Label for Short / Full  Type
//    setting Front size of TEXT label 
//    add cutoff line  ON OFF 
// Date Jan 31 2023
// OVERVIEW :  Kitti-Playbook Request.Volume X Price R0.00
// Objective : Visualize Volume X Last Price 
// 
// ==============================================================================================

// ======   Setting  Menu.   ============================================================================   

i_MKCapCutoffInt = input.float( 5," CUTOFF level" ,0.1,1000,step=0.1 ,
               tooltip=" It is recommended to use more than 100 times of the investment." ,group="Market Trade Value ( Currency) ",inline="L1" )
i_MKCapCutoffUnit = input.session("Million", " : ", options=["Thousand", "10Thousand","Million", "10Million","Billion","10Billion","Non" ],
               tooltip=" It is recommended to use more than 100 times of the investment." ,group="Market Trade Value ( Currency) ",inline="L1" )
i_Cutoff_Dis = input.bool(false,"Show CUTOFF Line " ,group="Market Trade Value ( Currency) ",inline="L2")
i_Cutoff_lin__col= input.color(color.new(color.orange,70),": Line Color",group="Market Trade Value ( Currency) ",inline="L2")

i_TopRange0 = input.float( 8.6," Zoom level (0 - 10)  " ,0,10,step=0.2 ,
               tooltip="   0 = Zoom turn Off   "  ,group="Market Trade Value ( Currency) ",inline="L3" )
i_BarStstue  = input.string("Current", "Current Bar / Previoue Bar",["Current","Previous"],
               tooltip=" In case the market is openning , the previous bar also needs to be considered." ,group="Market Trade Value ( Currency) ",inline="L4")

// ====   PLot cut off line  ====

DisplaySW3 = i_Cutoff_Dis ? display.all : display.none
plot(i_MKCapCutoffInt," Cutoff Line ",color=i_Cutoff_lin__col,style = plot.style_circles, display=DisplaySW3 )
//-------------------------------

// Unit Converter

DivUnit = switch i_MKCapCutoffUnit
    "Thousand"   => 1e3
    "10Thousand" => 1e3 *10
    "Million"    => 1e6  
    "10Million"  => 1e6 *10 
    "Billion"    => 1e9
    "10Billion"  => 1e9 *10
    "Non" => 1 

i_MKCapCutoff = i_MKCapCutoffInt*DivUnit / DivUnit

//
TopRange=   (10.5-i_TopRange0) *i_MKCapCutoff    

// Data Collection 
VOL=i_BarStstue== "Current" ? volume : i_BarStstue== "Previous" ? volume[1] : na
Sour= i_BarStstue== "Current" ? close : i_BarStstue== "Previous" ? close[1] : na
offset_Plot= i_BarStstue== "Current" ? 0 : i_BarStstue== "Previous" ? -1 : na

// Requesr Market cap

TSO = request.financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", "FQ")   // TTM  , FQ , FY // SET Use FQ
MarketCap = TSO * close /DivUnit

// Market Cap Calculation
Value = (VOL * Sour) / DivUnit

Valuex=Value/MarketCap *100
// Market Cap Limit Calculation 

ValueP = i_TopRange0==0? Value :Value >=TopRange ? TopRange : Value

// Display  Graph
i_Dis_Type = input.string("Column", 'Plot Chart Type', options=
     ["Column", 
      "Area"], 
     group='Display Setting',
     inline="L0")

i_Dis_Chart_col= input.color(color.new(color.blue,70)," Color",group='Display Setting', inline="L0",tooltip="The graph is below the CUTOFF  value, showing red.")

plot(ValueP,color=i_Dis_Chart_col, linewidth=2, style=plot.style_area, offset=offset_Plot, trackprice=true)

ColorFill=color.from_gradient(ValueP,0,i_MKCapCutoff,color.rgb(214, 10, 10), i_Dis_Chart_col ) 
Dis_Area=i_Dis_Type == "Area" ? true : false
Dis_Colums=i_Dis_Type == "Column" ? true : false
DisplaySW1 = Dis_Area ? display.all : display.none
DisplaySW2 = Dis_Colums ? display.all : display.none

plot(ValueP,"Market Trade value ",color=color.new(ColorFill, 50), linewidth=2, style=plot.style_area, offset=offset_Plot, trackprice=true, display=DisplaySW1)
plot(ValueP,"Market Trade value ",color=color.new(ColorFill, 50), linewidth=2, style=plot.style_columns, offset=offset_Plot, trackprice=true, display=DisplaySW2)


// ======================  Display Switch   Full  / Short  /  Off  =============================================================

i_LabelDisplayType= input.string("Short", 'Label Display format', options=
     ["Short", 
     "Full",
     " OFF"], 
     group='Display Setting',
     inline="L1")

// ====================    Full Label ===================================================================

ShowInfoTab0 = i_LabelDisplayType== "Full" ? true : false  

i_Tab_Width0 =  input.int(20, "Label Size ", minval=1, maxval=40, step=1,group='Display Setting',inline="L2")

i_table_position0 = input.string(position.top_right, 'Label Location  : Full', 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='Display Setting',
     inline="L3")
var table Signal_table0 = table.new(position=i_table_position0, columns=1, rows=1, border_width=2)

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


tColor1 = color.new(#888888, 0)
bColor0 =  Value<i_MKCapCutoff*0.5 ? color.new(color.red,80) :Value<i_MKCapCutoff? color.new(color.yellow,80) :   color.new(color.gray,90)


_TEXT_DisplayA0 =" " + i_BarStstue  + " Bar  :  Listed Shares = " + str.format("{0,number,#.###}",(TSO/DivUnit))   +" " +i_MKCapCutoffUnit //"  M Unit " 
                         + " \n Last Price  = " + str.format("{0,number,#.##} ", Sour) + syminfo.currency + ": Market Cap.=" +  str.format("{0,number,#.##} ",MarketCap ) + i_MKCapCutoffUnit+" "+syminfo.currency
                         + " \n Volume(Share) = " + str.format("{0,number,#.###}",(VOL/DivUnit))   +" " + i_MKCapCutoffUnit +"  :  "+ str.format("{0,number,#.###}",((VOL/TSO)*100))  +" % of Listed Shares" 
                         + " \n Value (Currency) = " + str.format("{0,number,#.###}",(Value))   +" " + i_MKCapCutoffUnit +" "+ syminfo.currency+ "   :  " + str.format("{0,number,#.###}",(Valuex)) + "% of Market Cap. "
                         + " \n Cutoff Trade Value = " + str.format("{0,number,#.###}",(i_MKCapCutoff))   +"   " +i_MKCapCutoffUnit +" "+ syminfo.currency
                         + " \n Value of Trade VS Cutoff = " + str.format("{0,number,#.###}",(Value/i_MKCapCutoff))   +"  Times "

if ShowInfoTab0 == true 
    table.cell(Signal_table0, 0, 0, _TEXT_DisplayA0, bgcolor=bColor0, text_color=tColor1,text_halign=text.align_left, width=i_Tab_Width0 , text_size = i_Front0)

//========================   Short Label    =================================================================================    

ShowInfoTab3 = i_LabelDisplayType== "Short" ? true : false 

table_position3 = input.string(position.middle_right, 'Shot', 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='Display Setting',
     inline="L3")


var table Signal_table3 = table.new(position=table_position3, columns=1, rows=1, border_width=2)

tColor3 = color.new(#888888, 0)
bColor3 =  Value<i_MKCapCutoff*0.5 ? color.new(color.red,80) :Value<i_MKCapCutoff? color.new(color.yellow,80) :   color.new(color.gray,90)


_TEXT_DisplayA3 = " Value (Currency) = " + str.format("{0,number,#.###}",(Value))   +" " + i_MKCapCutoffUnit +" "+ syminfo.currency
                         + "/"+ str.format("{0,number,#.###}",(Valuex)) +" %"
                         + " \n Cutoff Trade Value = " + str.format("{0,number,#.###}",(i_MKCapCutoff))   +"   " +i_MKCapCutoffUnit +" "+ syminfo.currency
                         
if ShowInfoTab3 == true 
    table.cell(Signal_table3, 0, 0, _TEXT_DisplayA3, bgcolor=bColor0, text_color=tColor1,text_halign=text.align_left, width=i_Tab_Width0 , text_size = i_Front0)
    



//=============.   END   ==================================================================================