st_short
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Alferow

//@version=4
study('st_short', overlay = true, precision = 5)

dep = input(300.00, title = 'risk on deal')
pr1 = input(46000.24, title = 'price enter')

tk = input(45000.0, title = 'price take')
of = input(100.0, title = 'exceeding $')

m = input(2.0, minval = 0.10, step = 0.1, title = 'martingale')

l = input(5, minval = 2, step = 1, title = 'leverage')

n = input(4, minval = 3, maxval = 5, title = 'number enters')
com = (1 - 0.01*input(0.07, title = 'commission %'))

pos = input(title="Position table", type=input.bool, defval=true)
txt = input(title="Text size", type=input.bool, defval=true)

k1 = pr1 >= 100.0 ? 3 : pr1 >= 10.0 and pr1 < 100.0 ? 2 : pr1 >= 1.0 and pr1 < 10.0 ? 1 : 0
k2 = pr1 >= 100.0 ? 0.0005 : pr1 >= 10.0 and pr1 < 100.0 ? 0.005 : pr1 >= 1.0 and pr1 < 10.0 ? 0.05 : 0.5
k3 = pr1 >= 100.0 ? 1 : pr1 >= 1.0 and pr1 < 100.0 ? 4 : 5
k4 = pos == true ? position.bottom_right : position.top_right
k5 = txt == true ? size.auto : size.large

h = (1 + (1/l))

summ = m == 1 ? dep/n : (dep * (m - 1)/(pow(m, n) - 1))



d1 = l*summ*com
d2 = d1*m
d3 = d2*m
d4 = d3*m
d5 = d4*m


v1 = round(d1/pr1 - k2, k1)


pr2 = -of + pr1*h
v2 = round(d2/pr2 - k2, k1)


pr3 = -of + h*(pr1 * v1 + pr2*v2)/((v1 + v2))
v3 = round(d3/pr3 - k2, k1)


pr4 = -of + h*(pr1 * v1 + pr2*v2 + pr3*v3)/((v1 + v2 + v3))
v4 = round(d4/pr4 - k2, k1)

pr5 = -of + h*(pr1 * v1 + pr2*v2 + pr3*v3 + pr4*v4)/((v1 + v2 + v3 + v4))
v5 = round(d5/pr5 - k2, k1)

pr6 = -of + h*(pr1 * v1 + pr2*v2 + pr3*v3 + pr4*v4 + pr5*v5)/((v1 + v2 + v3 + v4 + v5))


av1 = pr1
av2 = (pr1 * v1 + pr2*v2)/(v1 + v2)
av3 = (pr1 * v1 + pr2*v2 + pr3*v3)/(v1 + v2 + v3)
av4 = (pr1 * v1 + pr2*v2 + pr3*v3 + pr4*v4)/(v1 + v2 + v3 + v4)
av5 = (pr1 * v1 + pr2*v2 + pr3*v3 + pr4*v4 + pr5*v5)/(v1 + v2 + v3 + v4 + v5)

p1 = v1*(pr1 - tk)*com
p2 = (v1 + v2)*(av2 - tk)*com
p3 = (v1 + v2+ v3)*(av3 - tk)*com
p4 = (v1 + v2+ v3 + v4)*(av4 - tk)*com
p5 = (v1 + v2+ v3 + v4 + v5)*(av5 - tk)*com


var testTable = table.new(position = k4, columns = 5, rows = 7, border_color = color.black, bgcolor = color.white, border_width = 1)

if barstate.islast and n == 5
    table.cell(table_id = testTable, column = 0, row = 0, text = " ", text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 0, text = "Price enter", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 0, text = "Volume", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 0, text = "Money, $", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 0, text = "Profit, $", bgcolor=color.red, text_size = k5, text_color = color.black)

    table.cell(table_id = testTable, column = 0, row = 1, text = "1 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 2, text = "2 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 3, text = "3 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 4, text = "4 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 5, text = "5 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 6, text = "liquid", bgcolor=color.red, text_size = k5, text_color = color.black)
    
   
    table.cell(table_id = testTable, column = 1, row = 1, text = tostring(round(pr1, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 2, text = tostring(round(pr2, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 3, text = tostring(round(pr3, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 4, text = tostring(round(pr4, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 5, text = tostring(round(pr5, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 6, text = tostring(round(pr6, k3)), text_size = k5, text_color = color.black)
   
    table.cell(table_id = testTable, column = 2, row = 1, text = tostring(round(v1, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 2, text = tostring(round(v2, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 3, text = tostring(round(v3, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 4, text = tostring(round(v4, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 5, text = tostring(round(v5, k1)), text_size = k5, text_color = color.black)
    
    table.cell(table_id = testTable, column = 3, row = 1, text = tostring(round(d1, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 2, text = tostring(round(d2, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 3, text = tostring(round(d3, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 4, text = tostring(round(d4, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 5, text = tostring(round(d5, 2)), text_size = k5, text_color = color.black)
   
    table.cell(table_id = testTable, column = 4, row = 1, text = tostring(round(p1, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 2, text = tostring(round(p2, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 3, text = tostring(round(p3, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 4, text = tostring(round(p4, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 5, text = tostring(round(p5, 2)), text_size = k5, text_color = color.black)

if barstate.islast and n == 4
    table.cell(table_id = testTable, column = 0, row = 0, text = " ", text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 0, text = "Price enter", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 0, text = "Volume", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 0, text = "Money, $", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 0, text = "Profit, $", bgcolor=color.red, text_size = k5, text_color = color.black)

    table.cell(table_id = testTable, column = 0, row = 1, text = "1 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 2, text = "2 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 3, text = "3 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 4, text = "4 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 5, text = "liquid", bgcolor=color.red, text_size = k5, text_color = color.black)
   
    table.cell(table_id = testTable, column = 1, row = 1, text = tostring(round(pr1, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 2, text = tostring(round(pr2, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 3, text = tostring(round(pr3, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 4, text = tostring(round(pr4, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 5, text = tostring(round(pr5, k3)), text_size = k5, text_color = color.black)
   
    table.cell(table_id = testTable, column = 2, row = 1, text = tostring(round(v1, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 2, text = tostring(round(v2, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 3, text = tostring(round(v3, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 4, text = tostring(round(v4, k1)), text_size = k5, text_color = color.black)
    
    table.cell(table_id = testTable, column = 3, row = 1, text = tostring(round(d1, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 2, text = tostring(round(d2, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 3, text = tostring(round(d3, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 4, text = tostring(round(d4, 2)), text_size = k5, text_color = color.black)
   
    table.cell(table_id = testTable, column = 4, row = 1, text = tostring(round(p1, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 2, text = tostring(round(p2, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 3, text = tostring(round(p3, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 4, text = tostring(round(p4, 2)), text_size = k5, text_color = color.black)
    
if barstate.islast and n == 3
    table.cell(table_id = testTable, column = 0, row = 0, text = " ", text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 0, text = "Price enter", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 0, text = "Volume", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 0, text = "Money, $", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 0, text = "Profit, $", bgcolor=color.red, text_size = k5, text_color = color.black)

    table.cell(table_id = testTable, column = 0, row = 1, text = "1 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 2, text = "2 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 3, text = "3 deal", bgcolor=color.red, text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 0, row = 4, text = "liquid", bgcolor=color.red, text_size = k5, text_color = color.black)
   
    table.cell(table_id = testTable, column = 1, row = 1, text = tostring(round(pr1, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 2, text = tostring(round(pr2, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 3, text = tostring(round(pr3, k3)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 1, row = 4, text = tostring(round(pr4, k3)), text_size = k5, text_color = color.black)
   
    table.cell(table_id = testTable, column = 2, row = 1, text = tostring(round(v1, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 2, text = tostring(round(v2, k1)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 2, row = 3, text = tostring(round(v3, k1)), text_size = k5, text_color = color.black)
 
    
    table.cell(table_id = testTable, column = 3, row = 1, text = tostring(round(d1, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 2, text = tostring(round(d2, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 3, row = 3, text = tostring(round(d3, 2)), text_size = k5, text_color = color.black)
   
   
    table.cell(table_id = testTable, column = 4, row = 1, text = tostring(round(p1, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 2, text = tostring(round(p2, 2)), text_size = k5, text_color = color.black)
    table.cell(table_id = testTable, column = 4, row = 3, text = tostring(round(p3, 2)), text_size = k5, text_color = color.black)
   
    

  
    


