Hulk Grid Algorithm - The Quant Science
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thequantscience

//  ██╗  ██╗██╗   ██╗██╗     ██╗  ██╗     ██████╗ ██████╗ ██╗██████╗      █████╗ ██╗      ██████╗  ██████╗ ██████╗ ██╗████████╗██╗  ██╗███╗   ███╗
//  ██║  ██║██║   ██║██║     ██║ ██╔╝    ██╔════╝ ██╔══██╗██║██╔══██╗    ██╔══██╗██║     ██╔════╝ ██╔═══██╗██╔══██╗██║╚══██╔══╝██║  ██║████╗ ████║
//  ███████║██║   ██║██║     █████╔╝     ██║  ███╗██████╔╝██║██║  ██║    ███████║██║     ██║  ███╗██║   ██║██████╔╝██║   ██║   ███████║██╔████╔██║
//  ██╔══██║██║   ██║██║     ██╔═██╗     ██║   ██║██╔══██╗██║██║  ██║    ██╔══██║██║     ██║   ██║██║   ██║██╔══██╗██║   ██║   ██╔══██║██║╚██╔╝██║
//  ██║  ██║╚██████╔╝███████╗██║  ██╗    ╚██████╔╝██║  ██║██║██████╔╝    ██║  ██║███████╗╚██████╔╝╚██████╔╝██║  ██║██║   ██║   ██║  ██║██║ ╚═╝ ██║
//  ╚═╝  ╚═╝ ╚═════╝ ╚══════╝╚═╝  ╚═╝     ╚═════╝ ╚═╝  ╚═╝╚═╝╚═════╝     ╚═╝  ╚═╝╚══════╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚═╝   ╚═╝   ╚═╝  ╚═╝╚═╝     ╚═╝
                                                                                                                                                                                     
//@version=5
strategy(
     title = 'Hulk Grid Algorithm - The Quant Science', 
     overlay = true,
     initial_capital = 1000, 
     commission_type = strategy.commission.percent, 
     commission_value = 0.07, 
     pyramiding = 10,
     default_qty_type = strategy.percent_of_equity
     )

// #########################################################################################################

if not timeframe.isintraday
    runtime.error("WRONG TIMEFRAME: Please select an intraday timeframe when use this strategy backtesting.")

// #########################################################################################################

startDate   = input.int(title="Start Date", defval=1, minval=1, maxval=31, group = "DATE PERIOD CONFIGURATION")
startMonth  = input.int(title="Start Month", defval=1, minval=1, maxval=12, group = "DATE PERIOD CONFIGURATION")
startYear   = input.int(title="Start Year", defval=2018, minval=1800, maxval=2100, group = "DATE PERIOD CONFIGURATION")
endDate     = input.int(title="End Date", defval=12, minval=1, maxval=31, group = "DATE PERIOD CONFIGURATION")
endMonth    = input.int(title="End Month", defval=3, minval=1, maxval=12, group = "DATE PERIOD CONFIGURATION")
endYear     = input.int(title="End Year", defval=2022, minval=1800, maxval=2100, group = "DATE PERIOD CONFIGURATION")
inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))

// #########################################################################################################

requestpastweekclose = request.security(syminfo.tickerid,  "D", close[1], gaps = barmerge.gaps_off) 
requestpastweekopen  = request.security(syminfo.tickerid,  "D", open[1],  gaps = barmerge.gaps_off)
newday  = request.security(syminfo.tickerid,  "D", close,  gaps = barmerge.gaps_off)

// #########################################################################################################

ten_grid        = input.bool(defval = false, title = "Enable Strategy", tooltip = "Create grid backtesting.", group = "STRATEGY CONFIGURATION")
stop_loss       = input.float(defval = 2.00, step = 0.10, title = "Stop Loss: ", group = "STRATEGY CONFIGURATION")
take_profit     = input.float(defval = 2.00, step = 0.10, title = "Take Profit: ", group = "STRATEGY CONFIGURATION")
period_num      = input.int(defval = 3, step = 1, title = "Minimum Bars: ", group = "STRATEGY CONFIGURATION")
size_grid       = input.float(defval = 200.00, step = 1.00, title = "Size Grid Range: ", group = "STRATEGY CONFIGURATION")

// #########################################################################################################

var float high_start = 0 
var float high_end   = 0 
checking = requestpastweekclose > requestpastweekopen 
if checking==true
    high_start := requestpastweekclose
    high_end   := requestpastweekopen
grid_range = high_start - high_end
period = ta.barssince(ta.change(checking==true))==period_num
TriggerGrid() =>
    trigger = checking==true and period and grid_range > size_grid and close 
    trigger
trigger_entry = TriggerGrid() 

var float grid_1  = 0
var float grid_2  = 0
var float grid_3  = 0
var float grid_4  = 0
var float grid_5  = 0
var float grid_6  = 0
var float grid_7  = 0
var float grid_8  = 0
var float grid_9  = 0
var float grid_10 = 0

var float factor = 0 

if ten_grid == true and trigger_entry==true and strategy.opentrades==0
    factor := grid_range / 10
    // UP GRID
    grid_1  := (high_start) 
    grid_2  := (high_start + (factor * 1)) 
    grid_3  := (high_start + (factor * 2)) 
    grid_4  := (high_start + (factor * 3)) 
    grid_5  := (high_start + (factor * 4))
    // DOWN GRID
    grid_6  := (high_start - (factor * 1)) 
    grid_7  := (high_start - (factor * 2)) 
    grid_8  := (high_start - (factor * 3)) 
    grid_9  := (high_start - (factor * 4))
    grid_10 := (high_start - (factor * 5))

// #########################################################################################################

var tb = table.new(position.bottom_left, 1, 1, bgcolor = color.new(#00ff08, 74))

if barstate.isfirst
    table.cell(tb, 0, 0, 
      'Developed by The Quant Science™'
      ,text_size = size.normal
      ,text_color = color.new(#00ff08, 21))

// #########################################################################################################

var float new_ten_grid_1  = 0 
var float new_ten_grid_2  = 0 
var float new_ten_grid_3  = 0 
var float new_ten_grid_4  = 0 
var float new_ten_grid_5  = 0 
var float new_ten_grid_6  = 0 
var float new_ten_grid_7  = 0 
var float new_ten_grid_8  = 0 
var float new_ten_grid_9  = 0 
var float new_ten_grid_10 = 0 

if ten_grid == true 
    new_ten_grid_1  := grid_1
    new_ten_grid_2  := grid_2
    new_ten_grid_3  := grid_3
    new_ten_grid_4  := grid_4
    new_ten_grid_5  := grid_5
    new_ten_grid_6  := grid_6
    new_ten_grid_7  := grid_7
    new_ten_grid_8  := grid_8
    new_ten_grid_9  := grid_9
    new_ten_grid_10 := grid_10

else if ten_grid == false
    new_ten_grid_1  := na
    new_ten_grid_2  := na
    new_ten_grid_3  := na
    new_ten_grid_4  := na
    new_ten_grid_5  := na
    new_ten_grid_6  := na
    new_ten_grid_7  := na
    new_ten_grid_8  := na
    new_ten_grid_9  := na
    new_ten_grid_10 := na

destroyall = new_ten_grid_10 - (new_ten_grid_10 * stop_loss / 100)
des_function = ta.crossunder(close, destroyall)

takeprofitlevel = new_ten_grid_5 + (new_ten_grid_5 * take_profit / 100)

var float lots = 0 
if trigger_entry==true and ten_grid==true and period and inDateRange
    lots := (strategy.equity / close) / 5

    if close > new_ten_grid_1
        strategy.entry(id = "O #1",  direction = strategy.long, qty = lots, limit = new_ten_grid_1)
        strategy.entry(id = "O #2",  direction = strategy.long, qty = lots, limit = new_ten_grid_2)
        strategy.entry(id = "O #3",  direction = strategy.long, qty = lots, limit = new_ten_grid_3)
        strategy.entry(id = "O #4",  direction = strategy.long, qty = lots, limit = new_ten_grid_4)
        strategy.entry(id = "O #5",  direction = strategy.long, qty = lots, limit = new_ten_grid_5)
    if close < new_ten_grid_1
        strategy.entry(id = "O #6",  direction = strategy.long, qty = lots, limit = new_ten_grid_6)
        strategy.entry(id = "O #7",  direction = strategy.long, qty = lots, limit = new_ten_grid_7)
        strategy.entry(id = "O #8",  direction = strategy.long, qty = lots, limit = new_ten_grid_8)
        strategy.entry(id = "O #9",  direction = strategy.long, qty = lots, limit = new_ten_grid_9)
        strategy.entry(id = "O #10", direction = strategy.long, qty = lots, limit = new_ten_grid_10)

    if close > takeprofitlevel
        strategy.cancel_all()
    if close < destroyall
        strategy.cancel_all()

if new_ten_grid_2 and ten_grid == true
    strategy.exit(id = "C #1", from_entry = "O #1", qty = 100, limit = new_ten_grid_2)
if new_ten_grid_3 and ten_grid == true
    strategy.exit(id = "C #2", from_entry = "O #2", qty = 100, limit = new_ten_grid_3)
if new_ten_grid_4 and ten_grid == true
    strategy.exit(id = "C #3", from_entry = "O #3", qty = 100, limit = new_ten_grid_4)
if new_ten_grid_5 and ten_grid == true
    strategy.exit(id = "C #4", from_entry = "O #4", qty = 100, limit = new_ten_grid_5)
if takeprofitlevel and ten_grid == true
    strategy.exit(id = "C #5", from_entry = "O #5", qty = 100, limit = takeprofitlevel)

if new_ten_grid_5 and ten_grid == true
    strategy.exit(id = "C #6", from_entry = "O #6", qty = 100, limit = new_ten_grid_1)
if new_ten_grid_6 and ten_grid == true
    strategy.exit(id = "C #7", from_entry = "O #7", qty = 100, limit = new_ten_grid_6)
if new_ten_grid_7 and ten_grid == true
    strategy.exit(id = "C #8", from_entry = "O #8", qty = 100, limit = new_ten_grid_7)
if new_ten_grid_8 and ten_grid == true
    strategy.exit(id = "C #9", from_entry = "O #9", qty = 100, limit = new_ten_grid_8)
if new_ten_grid_9 and ten_grid == true
    strategy.exit(id = "C #10", from_entry = "O #10", qty = 100, limit = new_ten_grid_9)

if des_function 
    strategy.close_all("Destroyed Grid")
    strategy.cancel_all()
if session.islastbar_regular
    strategy.close_all("Dayli Session End")
if session.islastbar_regular
    strategy.cancel_all()

// #########################################################################################################

fill(plot(grid_1, color = color.new(color.rgb(0, 255, 132), 95)), 
     plot(grid_2, color = color.new(color.rgb(0, 255, 132), 95)),  
         color = color.new(#00ff08, 80))
fill(plot(grid_2, color = color.new(color.rgb(0, 255, 132), 95)),  
     plot(grid_3, color = color.new(color.rgb(0, 255, 132), 95)),  
         color = color.new(#00ff08, 85))
fill(plot(grid_3, color = color.new(color.rgb(0, 255, 132), 95)),  
     plot(grid_4, color = color.new(color.rgb(0, 255, 132), 95)),  
         color = color.new(#00ff08, 85))
fill(plot(grid_4, color = color.new(color.rgb(0, 255, 132), 95)), 
     plot(grid_5, color = color.new(color.rgb(0, 255, 132), 95)),  
         color = color.new(#00ff08, 90))
fill(plot(grid_1, color = color.new(color.rgb(0, 255, 132), 95)),  
     plot(grid_6, color = color.new(color.rgb(0, 255, 132), 95)), 
         color = color.new(#00ff08, 80))
fill(plot(grid_6, color = color.new(color.rgb(0, 255, 132), 95)),  
     plot(grid_7, color = color.new(color.rgb(0, 255, 132), 95)),  
         color = color.new(#00ff08, 80))
fill(plot(grid_7, color = color.new(color.rgb(0, 255, 132), 95)), 
     plot(grid_8, color = color.new(color.rgb(0, 255, 132), 95)),  
         color = color.new(#00ff08, 85))
fill(plot(grid_8, color = color.new(color.rgb(0, 255, 132), 95)),  
     plot(grid_9, color = color.new(color.rgb(0, 255, 132), 95)),  
         color = color.new(#00ff08, 85))
fill(plot(grid_9, color = color.new(color.rgb(0, 255, 132), 95)), 
     plot(grid_10, color = color.new(color.rgb(0, 255, 132), 95)), 
         color = color.new(#00ff08, 90))

// #########################################################################################################