Position Sizing Tool [Skiploss]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Skiploss

//@version=5
indicator("Position Sizing Tool [Skiploss]", shorttitle =  "PST v0.77" , overlay = true)

// Input account info
float   account_size        = input.float(10000, "Account Size", minval = 0, inline = '0', group = 'Basic Info')
string  account_currency    = input.string("USD", "", options = ["USD", "GBP", "EUR", "CAD", "AUD", "NZD", "CHF", "JPY", "CNY"], inline = '0', group = 'Basic Info')
float   risk_value          = input.float(10, "Risk    ", minval = 0, inline = '1', group = 'Basic Info')
string  risk_unit           = input.string("%", "", options = ["%", "Currency"], inline = '1', group = 'Basic Info')
float   contract_size       = input.float(100, "Contract Size", options = [10, 100, 1000, 10000, 100000], tooltip = "You can find details on MetaTrader, Ctrader, or your broker.", group = 'Basic Info')

// Input level
float input_price_entry         =   input.price(0, "Entry", tooltip = "Please select the entry point.", confirm = true, group = 'Price Levels')
float input_price_takeprofit    =   input.price(0, "Take Profits", tooltip = "The take profit point must be more than the entry point in the long position. On the other hand, it will be a short position.", confirm = true, group = 'Price Levels')
float input_price_stoploss      =   input.price(0, "Stop Loss", tooltip = "The stop loss point must be less than the entry point in the long position. On the other hand, it will be a short position.", confirm = true, group = 'Price Levels')

// Style Setting
color_takeprofits = input(#8ebbff, 'Take Profits', group = 'Style Settings')
color_stoploss = input(#ff8e8e, 'Stop Loss', group = 'Style Settings')
color_text = input(#93959f, 'Texts', group = 'Style Settings')
dashboard_location  =   input.session("Top Right",  "Dashboard Location",   options = ["Top Right","Bottom Right","Top Left","Bottom Left", "Middle Right","Bottom Center"], group = 'Style Settings')
dashboard_text_size =   input.session(     'Normal',      "Dashboard Size",   options = ["Tiny","Small","Normal","Large"], group = 'Style Settings')

// Plot line Entry
line_price_entry    = line.new(bar_index - 5 , input_price_entry, bar_index + 20 , input_price_entry, color = color_text, width = 1)
line.delete(line_price_entry[1])
label_price_entry   = label.new(bar_index + 27,  input_price_entry, text =  "Entry (" + str.tostring(input_price_entry, format.mintick) + ")", style = label.style_none, textcolor = color_text)
label.delete(label_price_entry[1])

// Plot line Take Profit
line_price_takeprofit   = line.new(bar_index - 5 , input_price_takeprofit, bar_index + 20 , input_price_takeprofit, color = color_takeprofits, width = 1)
line.delete(line_price_takeprofit[1])
label_price_takeprofit  = label.new(bar_index + 27,  input_price_takeprofit, text =  "TP (" + str.tostring(input_price_takeprofit, format.mintick) + ")", style = label.style_none, textcolor = color_takeprofits)
label.delete(label_price_takeprofit[1])

// Plot line Stop Loss
line_price_stoploss     = line.new(bar_index - 5 , input_price_stoploss, bar_index + 20 , input_price_stoploss, color = color_stoploss, width = 1)
line.delete(line_price_stoploss[1])
label_price_stoploss    = label.new(bar_index + 27,  input_price_stoploss, text = "SL (" + str.tostring(input_price_stoploss, format.mintick) + ")", style = label.style_none, textcolor = color_stoploss)
label.delete(label_price_stoploss[1])

// Round values 
float price_entry       = math.round_to_mintick(input_price_entry)
float price_takeprofit  = math.round_to_mintick(input_price_takeprofit)
float price_stoploss    = math.round_to_mintick(input_price_stoploss)

// Calculations
float risk_value_percentage = risk_unit == '%' ? risk_value : (risk_value / account_size)  * 100
float risk_value_currency   = risk_unit == 'Currency' ? risk_value : account_size * (risk_value / 100)

ratio_ = (price_takeprofit-price_entry)/(price_entry - price_stoploss)

Lots_ = risk_value_currency / ((price_entry - price_stoploss) * contract_size)

gain_ = Lots_ * ((price_entry - price_takeprofit) * contract_size)

// Dashboard Start
var table_position  =   dashboard_location  == 'Top Left' ? position.top_left :
  dashboard_location == 'Bottom Left'   ? position.bottom_left :
  dashboard_location == 'Middle Right'  ? position.middle_right :
  dashboard_location == 'Bottom Center' ? position.bottom_center :
  dashboard_location == 'Top Right'     ? position.top_right : position.bottom_right

var table_text_size  = dashboard_text_size == 'Tiny' ? size.tiny :
  dashboard_text_size == 'Small' ? size.small :
  dashboard_text_size == 'Normal' ? size.normal : size.large

var table_one = table.new(table_position, 7, 5, frame_color = color.new(color_takeprofits,80), frame_width = 1)
if barstate.islast
    // 1st Row
    table.cell(table_one, 0, 0, '', text_color = color_text, text_size=table_text_size, bgcolor = na)
    table.cell(table_one, 1, 0, str.replace_all(syminfo.tickerid, syminfo.prefix + ":", '') + " " + str.tostring(close, format.mintick), text_color = color.new(close > close[1] ? color_takeprofits : color_stoploss , 0),text_size=table_text_size, bgcolor = na)
    table.cell(table_one, 2, 0, '', text_color = color_text,text_size=table_text_size, bgcolor = na)
    // 2nd Row
    table.cell(table_one, 0, 1, 'Balance', text_color = color_text, text_size=table_text_size, bgcolor = color.new(color_takeprofits,90))
    table.cell(table_one, 1, 1, 'Risk %', text_color = color_text, text_size=table_text_size, bgcolor = color.new(color_takeprofits,90))
    table.cell(table_one, 2, 1, 'SL Loss', text_color = color_text, text_size=table_text_size, bgcolor = color.new(color_takeprofits,90))
    // 3nd Row
    table.cell(table_one, 0, 2, str.tostring(account_size), text_color = color_text, text_size=table_text_size, bgcolor = na)
    table.cell(table_one, 1, 2, str.tostring(risk_value_percentage, '#.##'), text_color = color_text, text_size=table_text_size, bgcolor = na)
    table.cell(table_one, 2, 2, str.tostring(risk_value_currency, '#.##'), text_color = color_text, text_size=table_text_size, bgcolor = na)
    // 4nd Row
    table.cell(table_one, 0, 3, 'Lots', text_color = color_text, text_size=table_text_size, bgcolor = color.new(color_takeprofits,90))
    table.cell(table_one, 1, 3, 'R:R', text_color = color_text, text_size=table_text_size, bgcolor = color.new(color_takeprofits,90))
    table.cell(table_one, 2, 3, 'TP Gain', text_color = color_text, text_size=table_text_size, bgcolor = color.new(color_takeprofits,90))
    // 5nd Row
    table.cell(table_one, 0, 4, str.tostring(Lots_ < 0 ? Lots_ * (-1) : Lots_ , '#.##'), text_color = color_text,text_size=table_text_size, bgcolor = na)
    table.cell(table_one, 1, 4, '1 : ' + str.tostring(ratio_ < 0 ? ratio_ * (-1) : ratio_ , '#.##'), text_color = color_text,text_size=table_text_size, bgcolor = na)
    table.cell(table_one, 2, 4, str.tostring( gain_  < 0 ? gain_ * (-1) : gain_ , '#.##'), text_color = color_text,text_size=table_text_size, bgcolor = na)

