TSG 5% Daily Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheSecretsOfTrading

//@version=5
indicator("TSG 5% Daily Calculator", overlay=true)

startBalance = input.float(50, "Initial Balance")
balance = input.float(0, "Current Balance")
dir = input.string("Long", "Trade Direction", options=["Long", "Short"])
lev = input.int(50, "Leverage")
risk = input.float(2.5, "Risk %")
goal = input.float(5, "Target %")
EntryPrice = input.float(0, "Entry Price")
EntryDate = input.time(timestamp("1 Feb 1989 12:00"), "Entry Date")
bool_table = input.bool(false, "Show Table")
table_size = input.string("Small", "Table Size", options =["Small","Normal"])

var bar_date = bar_index
if not EntryPrice
    EntryPrice := close
if year(EntryDate) != 1989 and time == EntryDate
    bar_date := bar_index

target_label_style = label.style_label_lower_left
loss_label_style = label.style_label_upper_left

//CALC
ROE = 100*(goal/risk)
TargetPrice = EntryPrice + (EntryPrice*ROE/100) / lev
StopLoss = EntryPrice - EntryPrice/lev
CurrentPercent = ( (close-EntryPrice) * goal ) / (TargetPrice-EntryPrice)
CurrentPrice = ( (close-EntryPrice) * (balance*goal/100) ) / (TargetPrice-EntryPrice)
CurrentPrice := CurrentPrice * -1

if dir == "Short"
    TargetPrice := EntryPrice - (EntryPrice*ROE/100) / lev
    StopLoss := EntryPrice + EntryPrice/lev
    CurrentPercent := ( (EntryPrice-close) * goal ) / (EntryPrice-TargetPrice)
    target_label_style := label.style_label_upper_left
    loss_label_style := label.style_label_lower_left

targetColor = color.rgb(80, 217, 255)
stopLossColor = color.rgb(249, 78, 149)
boxColor = color.rgb(130, 199, 255)
entryColor = boxColor
colorbg = color.new(color.black, 75)
colorTransparent = color.new(color.rgb(0,0,0), 99)
s = size.normal
slarge = size.large
t = 80

entry_text = "$ " + str.tostring(EntryPrice)+" - "+dir+" " + str.tostring(lev) +"x"
target_text = str.tostring(goal)+"% Target "
loss_text = str.tostring(risk)+"% Stop Loss "
if balance > 0
    entry_text += "\nCurrent Balance: $ " + str.tostring(balance) + "\nNext "+str.tostring(goal)+"% Profit: +" + str.tostring(math.round(balance*goal/100, 2)) + " $"
    money_tp = balance*goal/100
    money_sl = balance*risk/100
    target_text += "( +"+str.tostring(money_tp)+" $  /  Total: $ " + str.tostring(balance + money_tp)+" )"
    loss_text += "( -"+str.tostring(money_sl)+" $  /  Total: $ " + str.tostring(balance - money_sl)+" )"
else 
    entry_text += " )"

var entrypriceline = line.new(na,na,na,na, color=entryColor)
var boxTarget = box.new(na,na,na,na, bgcolor=color.new(targetColor, t), border_color=color.new(targetColor, t-7))
var boxStopLoss = box.new(na,na,na,na, bgcolor=color.new(stopLossColor, t), border_color=color.new(stopLossColor, t-7))
var _box = box.new(na,na,na,na, bgcolor = color.new(boxColor, t), border_color = color.new(boxColor, t-7) )
var _box_until_1M = box.new(na,na,na,na, bgcolor = color.new(boxColor, t), border_color = color.new(boxColor, t-40), xloc = xloc.bar_time)
var lab_target = label.new(na,na,target_text, textcolor=targetColor, color = colorbg, style=label.style_label_left, size = s)
var lab_stopLoss = label.new(na,na,loss_text, textcolor=stopLossColor, color = colorbg, style=label.style_label_left, size = s)
var lab_tpPrice = label.new(na,TargetPrice,str.tostring(TargetPrice), textcolor=targetColor, color = colorTransparent, style=target_label_style, size = slarge)
var lab_slPrice = label.new(na,StopLoss,str.tostring(StopLoss), textcolor=stopLossColor, color = colorTransparent, style=loss_label_style, size = slarge)
var lab_entry = label.new(na,na, entry_text, textcolor=entryColor, color = colorbg, style=label.style_label_left, size = s, textalign = text.align_left)
var lab_box = label.new(na,na,"", textcolor=boxColor, color = color.new(color.rgb(0,0,0), 99), style=label.style_label_left, size = slarge)
var lab_until_1M = label.new(na,na,"", textcolor=boxColor, color = color.new(color.rgb(0,0,0), 99), style=label.style_label_left, size = slarge, xloc = xloc.bar_time)

// TABLE
cells = 100
step = goal
start_table = startBalance
tcolor = color.white
t_size = table_size == "Small" ? size.small : size.normal
tablebgcolor = color.new(color.rgb(0,0,0), 5)
progressColor = color.new(color.green, 25)
columns = 5
var table _table = table.new(position.top_right, columns*2, cells+2, bgcolor = tablebgcolor, frame_color = color.gray)

if barstate.islast
    //TABLES

    d_1M = 0
    d_done = 0

    if bool_table
        days = 0
        col = 0
        for i=0 to (cells / columns*2 )
            col := 0
            for a = i to i + columns - 1
                
                value = math.round(start_table + (start_table*goal/100))
                if value > 1000000 and d_1M == 0
                    d_1M := days - d_done
                cellColor = tablebgcolor
                if start_table < balance
                    cellColor := progressColor
                if cellColor == tablebgcolor and d_done == 0
                    d_done := days
                table.cell(_table, col, i, "Day " + str.tostring(days), text_color = tcolor, bgcolor = cellColor, text_size = t_size)
                table.cell(_table, col+1, i, "$ " + str.tostring(start_table), text_color = tcolor, bgcolor = cellColor, text_size = t_size)
                start_table := value
                col += 2
                days+=1
                
        //Box Until 1 M
        days_to_add = d_1M * 86400000 //Miliseconds
        date = time + days_to_add
        box.set_left(_box_until_1M, time)
        box.set_right(_box_until_1M, date)
        box.set_bottom(_box_until_1M, close - close/2)
        box.set_top(_box_until_1M, close + close/2)
        label.set_x(lab_until_1M, date)
        label.set_y(lab_until_1M, close-close/2)
        label.set_text(lab_until_1M, str.tostring(d_1M) + " days to become a Millionaire! ")

    //LABELS
    bar_shift = 5
    bar_start = bar_index[bar_shift]
    if year(EntryDate) != 1989
        bar_start := bar_date
    bar_end = bar_index + bar_shift
    next_target = ""

    label.set_x(lab_tpPrice, bar_end)
    label.set_x(lab_slPrice, bar_end)
    label.set_y(lab_tpPrice, TargetPrice)
    label.set_y(lab_slPrice, StopLoss)

    box.set_left(boxTarget, bar_start)
    box.set_right(boxTarget, bar_end)
    box.set_bottom(boxTarget, TargetPrice)
    box.set_top(boxTarget, EntryPrice)
    label.set_x(lab_target, bar_end)
    label.set_y(lab_target, TargetPrice)

    box.set_left(boxStopLoss, bar_start)
    box.set_right(boxStopLoss, bar_end)
    box.set_bottom(boxStopLoss, StopLoss)
    box.set_top(boxStopLoss, EntryPrice)
    label.set_x(lab_stopLoss, bar_end)
    label.set_y(lab_stopLoss, StopLoss)

    if EntryPrice
        line.set_x1(entrypriceline, bar_start)
        line.set_x2(entrypriceline, bar_end)
        line.set_y1(entrypriceline, EntryPrice)
        line.set_y2(entrypriceline, EntryPrice)
        label.set_x(lab_entry, bar_end)
        label.set_y(lab_entry, EntryPrice)

        box.set_left(_box, bar_start)
        box.set_right(_box, bar_index)
        box.set_bottom(_box, EntryPrice)
        box.set_top(_box, close)
        box.set_bgcolor(_box, color.new(targetColor, t))
        box.set_border_color(_box, color.new(targetColor, t-7))

        label.set_x(lab_box, bar_end)
        label.set_y(lab_box, close)
        label.set_textcolor(lab_box, targetColor)
        add = "+"

        if CurrentPercent < 0 
            add:= ""
            label.set_textcolor(lab_box, stopLossColor)
            box.set_bgcolor(_box, color.new(stopLossColor, t))
            box.set_border_color(_box, color.new(stopLossColor, t-7))
        
        label.set_text(lab_box, add+str.tostring(math.round(CurrentPercent, 2)) + "% / " +  add+str.tostring(math.round(CurrentPrice, 2)) + " $")
        lab_dir = close >= EntryPrice ? label.style_label_upper_left : label.style_label_lower_left
        label.set_style(lab_box, lab_dir)
