Risk Calculation Table - Amount Based
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//Mac bedunduc
//@version=5
indicator('Risk Calculation Table - Amount Based', shorttitle='RRTable', overlay=true)

//Table Settings
//RR and Ticker's decimal

entry   = input.price(0.0, 'Entry Price', group='Table Settings')
stop    = input.price(0.0, 'SL Price', group='Table Settings')
exit    = input.price(0.0, 'TP Price', group='Table Settings')
amount  = input(100.0, 'Amount Traded', group='Table Settings')
decimalinput = input.string(defval='0.0', title="Ticker's decimal", tooltip="Type 0 based on the ticker, 0.0 means one number behind decimal, and 0.0000 means four numbers behind decimal",group='Table Settings')

//Manual Table
mtblPos =  input.string(title='Table Location', defval='Top Right', options=['Top Right', 'Middle Right', 'Bottom Right', 'Top Center', 'Middle Center', 'Bottom Center', 'Top Left', 'Middle Left', 'Bottom Left'], group='Table Display Settings')
mtblposition = mtblPos == 'Top Right' ? position.top_right : mtblPos == 'Middle Right' ? position.middle_right : mtblPos == 'Bottom Right' ? position.bottom_right : mtblPos == 'Top Center' ? position.top_center : mtblPos == 'Middle Center' ? position.middle_center : mtblPos == 'Bottom Center' ? position.bottom_center : mtblPos == 'Top Left' ? position.top_left : mtblPos == 'Middle Left' ? position.middle_left : position.bottom_left

size = input.string(title='Table Size', defval='Small', options=['Auto', 'Huge', 'Large', 'Normal', 'Small', 'Tiny'], group='Table Display Settings')
_size = size == 'Auto' ? size.auto : size == 'Huge' ? size.huge : size == 'Large' ? size.large : size == 'Normal' ? size.normal : size == 'Small' ? size.small : size.tiny

//plotline
plot(entry > 0 ? entry : na, "Entry Price",color=color.new(color.white,50), style=plot.style_cross, offset=10)
plot(stop > 0 ? stop : na, "StopLoss Price", color=color.new(color.red,50), style=plot.style_cross, offset = 10)
plot(exit > 0 ? exit : na, "TP Price", color=color.new(color.green,50), style=plot.style_cross, offset = 10)

//alarm line
riskcalc = entry > stop ? (((entry-stop)*amount)/entry) : entry < stop ? (((stop - entry)*amount)/entry) : na
tpcalc = exit > entry ? (((exit-entry)*amount)/entry) : exit < entry ? (((entry-exit)*amount)/entry) : na
rrmult = tpcalc / riskcalc
pos = entry > stop ? 'LONG' : entry < stop ? 'SHORT' : na
mcol = entry > stop ? color.green : entry < stop ? color.red : na

//manual table
var mTable = table.new(position=mtblposition, columns=2, rows=9, border_width=1, frame_width=2, frame_color=color.new(#000000, 50), border_color=color.new(#000000, 0))
table.cell(mTable, column=0, row=0, text='Position', text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_left, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=0, row=1, text='Entry', text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_left, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=0, row=2, text='SL', text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_left, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=0, row=3, text='TP', text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_left, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=0, row=4, text='Amount', text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_left, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=0, row=5, text='SL Risk', text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_left, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=0, row=6, text='TP Gain', text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_left, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=0, row=7, text='RR Ratio', text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_left, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))

table.cell(mTable, column=1, row=0, text=str.tostring(pos), text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_right, text_valign=text.align_center, bgcolor=mcol)
table.cell(mTable, column=1, row=1, text=str.tostring(entry, decimalinput), text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_right, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=1, row=2, text=str.tostring(stop, decimalinput), text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_right, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=1, row=3, text=str.tostring(exit, decimalinput), text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_right, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=1, row=4, text=str.tostring(amount,'0.0'), text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_right, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=1, row=5, text=str.tostring(riskcalc, '0.00'), text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_right, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=1, row=6, text=str.tostring(tpcalc,'0.00'), text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_right, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
table.cell(mTable, column=1, row=7, text=str.tostring(rrmult,'0.0'+' : 1'), text_color=color.new(#000000,0), text_size=_size, text_halign=text.align_right, text_valign=text.align_center, bgcolor=color.new(#ffffff,0))
