Optimized Logarithmic Curve for Bitcoin (BTC/USD) by FICAS
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//© anon2414691 Optimized by alimizanioskui(FICAS) originally created by @quantadelic
// @version=5
indicator('Optimized Logarithmic Curve for Bitcoin (BTC/USD) by FICAS', overlay=true)

// Group String //
var group_text0="Curve Settings"
var group_text1="Curve Color Setting"
var group_text2='Extend Input'
var group_text3="Plot width Setting"

A = input.float(defval=0.01, title='A=', step=0.001,group=group_text0)
beta = input.float(defval=0.511798, title='beta=', step=0.0001,group=group_text0)
lambda = input.float(defval=0.42665, title='lambda=', step=0.0001,group=group_text0)
c = input.float(defval=-4.3, title='c=', step=0.01,group=group_text0)
m = input.float(defval=-0.000667, title='m=', step=0.0001,group=group_text0)
b = input.float(defval=-0.01, title='b=', step=0.1,group=group_text0)


//Band=input(defval=true,title="Band?", type=input.bool)
B1 = input.float(defval=1.77, title='Band 1 Multiple', step=0.1,group=group_text0)
B2 = input.float(defval=3.13, title='Band 2 Multiple', step=0.1,group=group_text0)
B3 = input.float(defval=5.55, title='Band 3 Multiple', step=0.1,group=group_text0)
B4 = input.float(defval=9.82, title='Band 4 Multiple', step=0.1,group=group_text0)
B5 = input.float(defval=17.37, title='Band 5 Multiple', step=0.1,group=group_text0)
B6 = input.float(defval=30.75, title='Band 6 Multiple', step=0.1,group=group_text0)
CB = input(defval=true, title='Show Crisis Band',group=group_text0)
B7 = input.float(defval=0.55, title='Crisis multiple', step=0.1,group=group_text0)
CB2 = input(defval=true, title='Show Crisis Band2',group=group_text0)
B8 = input.float(defval=0.3192, title='Crisis multiple 2', step=0.1,group=group_text0)
CB3 = input(defval=true, title='Hide Mid lines',group=group_text0)


i_extend = input.int(0, title=' Extend', group=group_text2)
line_width=input.int(0,title = "Plot Width",group = group_text3)
// color input ///////////////
var p0_col=input.color(#ff0000,title = "P Color",group = group_text1,inline = "El")
var p1_col=input.color( #ffa500,title = "P1 Color",group = group_text1,inline = "El")
var p2_col=input.color(#ffff00,title = "P2 Color",group = group_text1,inline = "El")
var p3_col=input.color(#008000,title = "P3 Color",group = group_text1,inline = "EX")
var p4_col=input.color(#0000ff,title = "P4 Color",group = group_text1,inline = "EX")
var p5_col=input.color(#4b0082,title = "P5 Color",group = group_text1,inline = "EX")
var p6_col=input.color(#ee82ee,title = "P6 Color",group = group_text1,inline = "EZ")
var p7_col=input.color(#aaff00,title = "P7 Color",group = group_text1,inline = "EZ")
var p8_col=input.color(#0033ff,title = "P8 Color",group = group_text1,inline = "EZ")


t = time
genesis = timestamp(2009, 1, 3, 0, 0, 0)
x = (t - genesis) / 86400000
p = A * math.exp(beta * math.pow(x, lambda) + c) + m * x + b
P = plot(p, linewidth=1, color=p0_col, offset=i_extend)

p1 = p * B1
p2 = p * B2
p3 = p * B3
p4 = p * B4
p5 = p * B5
p6 = p * B6
p7 = p * B7
p8 = p*B8

// median line calculation 

fun_medi(J1,k1)=>math.sqrt(J1*k1)
p_8median=math.sqrt(p8*p7)



P1 = plot(p1, linewidth=line_width, color=p1_col, offset=i_extend)
P2 = plot(p2, linewidth=line_width, color=p2_col, offset=i_extend)
P3 = plot(p3, linewidth=line_width, color=p3_col, offset=i_extend)
P4 = plot(p4, linewidth=line_width, color=p4_col, offset=i_extend)
P5 = plot(p5, linewidth=line_width, color=p5_col, offset=i_extend)
P6 = plot(p6, linewidth=line_width, color=p6_col, offset=i_extend)
P7 = plot(CB ? p7 : p, linewidth=line_width, color=p7_col, offset=i_extend)
P8=plot(CB2 ? p8 : p, linewidth=line_width, color=p8_col, offset=i_extend)

// plot meidan 
plot(CB3 ? p :fun_medi(p7,p),title  = "P7-P Median" ,linewidth=line_width, color=color.gray, offset=i_extend,style = plot.style_circles)
plot(CB3 ? p :fun_medi(p,p1),title  = "P-P1 Median" ,linewidth=line_width, color=color.gray, offset=i_extend,style = plot.style_circles)
plot(CB3 ? p :fun_medi(p1,p2),title = "P1-P2 Median" ,linewidth=line_width, color=color.gray, offset=i_extend,style = plot.style_circles)
plot(CB3 ? p :fun_medi(p2,p3),title = "P2-P3 Median" ,linewidth=line_width, color=color.gray, offset=i_extend,style = plot.style_circles)
plot(CB3 ? p :fun_medi(p3,p4),title = "P3-P4 Median" ,linewidth=line_width, color=color.gray, offset=i_extend,style = plot.style_circles)
plot(CB3 ? p :fun_medi(p4,p5),title = "P4-P5 Median" ,linewidth=line_width, color=color.gray, offset=i_extend,style = plot.style_circles)
plot(CB3 ? p :fun_medi(p5,p6),title = "P6-P7 Median" ,linewidth=line_width, color=color.gray, offset=i_extend,style = plot.style_circles)
plot(CB3 ? p :fun_medi(p8,p7),title = "P7-P8 Median" ,linewidth=line_width, color=color.gray, offset=i_extend,style = plot.style_circles)


// table information about curve 
var string GP2 = "Table"
bool    en_table=input.bool(true,title = "Enable/Disable Table",group = GP2)
string  tableYposInput = input.string("top", "Y position", inline = "11", options = ["top", "middle", "bottom"], group = GP2)
string  tableXposInput = input.string("right", "X position", inline = "11", options = ["left", "center", "right"], group = GP2)
int     tb_size=input.int(5,title = "Input table border width ",group = GP2)
var tbl = table.new(tableYposInput + "_" + tableXposInput, 6, 41, frame_color=#151715, frame_width=3, border_width=tb_size, border_color=color.new(color.white, 100))


if barstate.islast and en_table
    table.cell(tbl, 0, 0, 'Data',   text_halign = text.align_center, bgcolor =color.silver, text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 1, 'P',      text_halign = text.align_center, bgcolor =p0_col,text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 2, 'P1',     text_halign = text.align_center, bgcolor =p1_col, text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 3, 'P2',     text_halign = text.align_center, bgcolor =p2_col, text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 4, 'P3',     text_halign = text.align_center, bgcolor =p3_col, text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 5, 'P4',     text_halign = text.align_center, bgcolor =p4_col, text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 6, 'P5',     text_halign = text.align_center, bgcolor =p5_col, text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 7, 'P6',     text_halign = text.align_center, bgcolor =p6_col, text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 8, 'P7',     text_halign = text.align_center, bgcolor =p7_col, text_color = color.white, text_size = size.small)
    table.cell(tbl, 0, 9, 'P8',     text_halign = text.align_center, bgcolor =p8_col, text_color = color.white, text_size = size.small)


    table.cell(tbl, 1, 0, 'value', text_halign = text.align_center, bgcolor = color.silver, text_color = color.white, text_size = size.small)
    table.cell(tbl, 1, 1, str.tostring(p,'#.###'), text_halign = text.align_center, bgcolor  = close>p ? color.green:color.red, text_color  =color.white, text_size = size.small)
    table.cell(tbl, 1, 2, str.tostring(p1,'#.###'), text_halign = text.align_center, bgcolor = close>p1? color.green:color.red, text_color =color.white, text_size = size.small)
    table.cell(tbl, 1, 3, str.tostring(p2,'#.###'), text_halign = text.align_center, bgcolor = close>p2? color.green:color.red, text_color =color.white, text_size = size.small)
    table.cell(tbl, 1, 4, str.tostring(p3,'#.###'), text_halign = text.align_center, bgcolor = close>p3? color.green:color.red, text_color =color.white, text_size = size.small)
    table.cell(tbl, 1, 5, str.tostring(p4,'#.###'), text_halign = text.align_center, bgcolor = close>p4? color.green:color.red, text_color =color.white, text_size = size.small)
    table.cell(tbl, 1, 6, str.tostring(p5,'#.###'), text_halign = text.align_center, bgcolor = close>p5? color.green:color.red, text_color =color.white, text_size = size.small)
    table.cell(tbl, 1, 7, str.tostring(p6,'#.###'), text_halign = text.align_center, bgcolor = close>p6? color.green:color.red, text_color =color.white, text_size = size.small)
    table.cell(tbl, 1, 8, str.tostring(p7,'#.###'), text_halign = text.align_center, bgcolor = close>p7? color.green:color.red, text_color =color.white, text_size = size.small)
    table.cell(tbl, 1, 9, str.tostring(p8,'#.###'), text_halign = text.align_center, bgcolor = close>p8? color.green:color.red, text_color =color.white, text_size = size.small)


// label 
var string gp3="Label Set"

en_label=input.bool(false,title = "Enable Label",group = gp3)

f_lineLabel(_label, _line, _barsBack, _color, _text) =>
    label.delete(_label)
    if en_label
        label.new(time - _barsBack * (time - time[1]), _line[_barsBack], xloc=xloc.bar_time, yloc=yloc.price, text=_text + '\n', color=_color, textcolor=color.new(color.white, 0), size=size.small)

var plabel  =label(na)
var p1Label = label(na)
var p2Labe2 = label(na)
var p3Label = label(na)
var p4Labe2 = label(na)
var p5Label = label(na)
var p6Labe2 = label(na)
var p7Label = label(na)


plabel  :=f_lineLabel(plabel , p,  0, close>p ? color.green:color.red,  str.tostring(p,'#.###'))
p1Label :=f_lineLabel(p1Label, p1, 0, close>p1? color.green:color.red,  str.tostring(p1,'#.###'))
p2Labe2 :=f_lineLabel(p2Labe2, p2, 0, close>p2? color.green:color.red,  str.tostring(p2,'#.###'))
p3Label :=f_lineLabel(p3Label, p3, 0, close>p3? color.green:color.red,  str.tostring(p3,'#.###'))
p4Labe2 :=f_lineLabel(p4Labe2, p4, 0, close>p4? color.green:color.red,  str.tostring(p4,'#.###'))
p5Label :=f_lineLabel(p5Label, p5, 0, close>p5? color.green:color.red,  str.tostring(p5,'#.###'))
p6Labe2 :=f_lineLabel(p6Labe2, p6, 0, close>p6? color.green:color.red,  str.tostring(p6,'#.###'))
p7Label :=f_lineLabel(p7Label, p7, 0, close>p7? color.green:color.red,  str.tostring(p7,'#.###'))