Grid Settings & MM
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © avgtrade

//@version=5
indicator("Grid Settings & MM", overlay = true, max_labels_count = 5)

// << INPUT >>
// vvvvvvvvvvvv
// repaintInput = input.bool(false, "Position bars in the past")
HIGH = input.float(defval = 0.328, title = 'HighPrice')
LOW = input.float(defval = 0.215, title = 'LowPrice')
BALANCE = input.float(defval = 1000.00, title = 'Deposit')
GRIDS = input.int(defval = 5, title = 'Grids')
STEPS = input.int(defval = 5, title = 'Steps')
ORDER_COEF = input.float(defval = 1.2, title = 'c_Order')
PRICE_COEF = input.float(defval = 1.1, title = 'c_Price')
FIRST_LEVEL = input.float(defval = 0.5, title = 'FirstLevel')
BACK_HL = input.int(defval = 150, minval = 1, title = 'Back_H/L')

exp_steps = ORDER_COEF
if STEPS == 1
    exp_steps := ORDER_COEF
if STEPS == 2
    exp_steps := ORDER_COEF * ORDER_COEF
if STEPS == 3
    exp_steps := ORDER_COEF * ORDER_COEF * ORDER_COEF
if STEPS == 4
    exp_steps := ORDER_COEF * ORDER_COEF * ORDER_COEF * ORDER_COEF
if STEPS == 5
    exp_steps := ORDER_COEF * ORDER_COEF * ORDER_COEF * ORDER_COEF * ORDER_COEF

hi = ta.highest(BACK_HL)
newHi = ta.change(hi)
highestBarOffset = - ta.highestbars(BACK_HL)
var lbh = label.new(na, na, "", style = label.style_none, textcolor = color.yellow, size = size.normal)
if newHi
    labelText = str.tostring(hi, format.mintick)
    label.set_xy(lbh, bar_index[highestBarOffset], hi)
    label.set_text(lbh, labelText)
    //label.set_yloc(lbl, yloc.belowbar)
else
    label.set_x(lbh, bar_index[highestBarOffset])

lo = ta.lowest(BACK_HL)
newLo = ta.change(lo)
lowestBarOffset = - ta.lowestbars(BACK_HL)
var lbl = label.new(na, na, "", style = label.style_none, textcolor = color.yellow, size = size.normal)
if newLo
    labelText = str.tostring(lo, format.mintick) 
    label.set_xy(lbl, bar_index[lowestBarOffset] + 5, lo)
    label.set_text(lbl, labelText)
    //label.set_yloc(lbl, yloc.belowbar)
else
    label.set_x(lbl, bar_index[lowestBarOffset] + 5)

// << AO Cross >>
// vvvvvvvvvvvvvvv
aoValue = ta.ema(hl2,5) - ta.ema(hl2,34)
aoDown = ta.crossunder(aoValue, 0)
aoUp = ta.crossover(aoValue, 0)

// << PRICES >>
// vvvvvvvvvvvv
GRID = BALANCE / GRIDS
IMPULSE = (hi/lo-1)*100
FIX = ((HIGH/LOW-1)/2+(1-LOW/HIGH)/2)*50
USD1 = GRID / ((1 - exp_steps) / (1 - ORDER_COEF))
USD2 = USD1 * ORDER_COEF
USD3 = USD2 * ORDER_COEF
USD4 = USD3 * ORDER_COEF
USD5 = USD4 * ORDER_COEF

BUY1 = (HIGH + LOW) * FIRST_LEVEL
BUY2 = BUY1 - BUY1 * PRICE_COEF * FIX / (STEPS - 1) / 100
BUY3 = BUY2 - BUY2 * PRICE_COEF * PRICE_COEF * FIX / (STEPS - 1) / 100
BUY4 = BUY3 - BUY3 * PRICE_COEF * PRICE_COEF * PRICE_COEF * FIX / (STEPS - 1) / 100
BUY5 = BUY4 - BUY4 * PRICE_COEF * PRICE_COEF * PRICE_COEF * PRICE_COEF * FIX / (STEPS - 1) / 100

AMT1 = USD1 / BUY1
AMT2 = USD2 / BUY2
AMT3 = USD3 / BUY3
AMT4 = USD4 / BUY4
AMT5 = USD5 / BUY5

SUM1 = USD1
SUM2 = SUM1 + USD2
SUM3 = SUM2 + USD3
SUM4 = SUM3 + USD4
SUM5 = SUM4 + USD5

QTY1 = AMT1
QTY2 = QTY1 + AMT2
QTY3 = QTY2 + AMT3
QTY4 = QTY3 + AMT4
QTY5 = QTY4 + AMT5

SELL1 = BUY1 + BUY1 * FIX / 100
SELL2 = SUM2 / (QTY2) + SUM2 / (QTY2) * FIX / 100
SELL3 = SUM3 / (QTY3) + SUM3 / (QTY3) * FIX / 100
SELL4 = SUM4 / (QTY4) + SUM4 / (QTY4) * FIX / 100
SELL5 = SUM5 / (QTY5) + SUM5 / (QTY5) * FIX / 100

TP1 = SELL1 * QTY1 - SUM1
TP2 = SELL2 * QTY2 - SUM2
TP3 = SELL3 * QTY3 - SUM3
TP4 = SELL4 * QTY4 - SUM4
TP5 = SELL5 * QTY5 - SUM5

// << DISPLAY >>
// vvvvvvvvvvvvv
var table impulse = table.new(position.top_right, 1, 2)
table.cell(impulse, 0, 0, str.tostring(IMPULSE, '#.##'), text_color = color.yellow, text_size = size.small)

var table grid = table.new(position.bottom_left, 5, 6)
table.cell(grid, 0, 0, "Buy", text_color = color.green, bgcolor = color.black, text_size = size.small)
table.cell(grid, 1, 0, "Amount", text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 2, 0, "Sell", text_color = color.red, bgcolor = color.black, text_size = size.small)
table.cell(grid, 3, 0, "$$$", text_color = color.white, bgcolor = color.black, text_size = size.small)
table.cell(grid, 4, 0, "TP", text_color = color.white, bgcolor = color.green, text_size = size.small)

table.cell(grid, 0, 1, str.tostring(BUY1, format.mintick), text_color = color.green, bgcolor = color.black, text_size = size.small)
table.cell(grid, 0, 2, str.tostring(BUY2, format.mintick), text_color = color.green, bgcolor = color.black, text_size = size.small)
table.cell(grid, 0, 3, str.tostring(BUY3, format.mintick), text_color = color.green, bgcolor = color.black, text_size = size.small)
table.cell(grid, 0, 4, str.tostring(BUY4, format.mintick), text_color = color.green, bgcolor = color.black, text_size = size.small)
table.cell(grid, 0, 5, str.tostring(BUY5, format.mintick), text_color = color.green, bgcolor = color.black, text_size = size.small)

table.cell(grid, 1, 1, str.tostring(AMT1, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 1, 2, str.tostring(AMT2, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 1, 3, str.tostring(AMT3, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 1, 4, str.tostring(AMT4, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 1, 5, str.tostring(AMT5, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)

table.cell(grid, 2, 1, str.tostring(SELL1, format.mintick), text_color = color.red, bgcolor = color.black, text_size = size.small)
table.cell(grid, 2, 2, str.tostring(SELL2, format.mintick), text_color = color.red, bgcolor = color.black, text_size = size.small)
table.cell(grid, 2, 3, str.tostring(SELL3, format.mintick), text_color = color.red, bgcolor = color.black, text_size = size.small)
table.cell(grid, 2, 4, str.tostring(SELL4, format.mintick), text_color = color.red, bgcolor = color.black, text_size = size.small)
table.cell(grid, 2, 5, str.tostring(SELL5, format.mintick), text_color = color.red, bgcolor = color.black, text_size = size.small)

table.cell(grid, 3, 1, str.tostring(SUM1, '#.##'), text_color = color.white, bgcolor = color.black, text_size = size.small)
table.cell(grid, 3, 2, str.tostring(SUM2, '#.##'), text_color = color.white, bgcolor = color.black, text_size = size.small)
table.cell(grid, 3, 3, str.tostring(SUM3, '#.##'), text_color = color.white, bgcolor = color.black, text_size = size.small)
table.cell(grid, 3, 4, str.tostring(SUM4, '#.##'), text_color = color.white, bgcolor = color.black, text_size = size.small)
table.cell(grid, 3, 5, str.tostring(SUM5, '#.##'), text_color = color.white, bgcolor = color.black, text_size = size.small)

table.cell(grid, 4, 1, str.tostring(TP1, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 4, 2, str.tostring(TP2, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 4, 3, str.tostring(TP3, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 4, 4, str.tostring(TP4, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)
table.cell(grid, 4, 5, str.tostring(TP5, '#.##'), text_color = color.white, bgcolor = color.green, text_size = size.small)

printTable(txt) => var table t = table.new(position.top_left, 1, 1),
                   table.cell(t, 0, 0, txt, bgcolor = color.black, text_color = color.white, text_size = size.small)
printTable('...\n' +
 'Depo = ' + str.tostring(BALANCE) + ' $\n' +
 'Grids = ' + str.tostring(GRIDS) + '\n' +
 'Grid = ' + str.tostring(GRID, '#.##') + ' $\n' +
 'TP = ' + str.tostring(FIX, '#.##') + ' %')

//hiLine = line.new(bar_index - 20, HIGH, bar_index, HIGH, extend = extend.right, color = color.red, style = line.style_solid)
//lowLine = line.new(bar_index - 20, LOW, bar_index, LOW, extend = extend.right, color = color.blue, style = line.style_solid)
//firstLine = line.new(bar_index - 20, BUY1, bar_index, BUY1, extend = extend.right, color = color.green, style = line.style_solid)
//secondLine = line.new(bar_index - 20, BUY2, bar_index, BUY2, extend = extend.right, color = color.green, style = line.style_solid)
//thirdLine = line.new(bar_index - 20, BUY3, bar_index, BUY3, extend = extend.right, color = color.green, style = line.style_solid)
//fourthLine = line.new(bar_index - 20, BUY4, bar_index, BUY4, extend = extend.right, color = color.green, style = line.style_solid)
//fifthLine = line.new(bar_index - 20, BUY5, bar_index, BUY5, extend = extend.right, color = color.green, style = line.style_solid)

//hiLabel = label.new(bar_index + 10, HIGH, text =''+str.tostring(HIGH, format.mintick), style = label.style_none, textcolor = color.red)
//lowLabel = label.new(bar_index + 10, LOW, text =''+str.tostring(LOW, format.mintick), style = label.style_none, textcolor = color.blue)
//firstLabel = label.new(bar_index + 10, BUY1, text =''+str.tostring(BUY1, format.mintick), style = label.style_none, textcolor = color.green)
//secondLabel = label.new(bar_index + 10, BUY2, text =''+ str.tostring(BUY2, format.mintick), style = label.style_none, textcolor = color.green)
//thirdLabel = label.new(bar_index + 10, BUY3, text =''+ str.tostring(BUY3, format.mintick), style = label.style_none, textcolor = color.green)
//fourthLabel = label.new(bar_index + 10, BUY4, text =''+ str.tostring(BUY4, format.mintick), style = label.style_none, textcolor = color.green)
//fifthLabel = label.new(bar_index + 10, BUY5, text = ''+str.tostring(BUY5, format.mintick), style = label.style_none, textcolor = color.green)