//@version=5 strategy(title = 'Grid Spot Trading Algorithm', overlay = true, calc_on_every_tick = true, initial_capital = 1000, commission_type = strategy.commission.percent, commission_value = 0.03, pyramiding = 100, default_qty_type = strategy.percent_of_equity, process_orders_on_close = true, close_entries_rule = 'ANY') //--------------------------------------------------------------- startDate = input.int(title="START DAY DATE", defval=1, minval=1, maxval=31, group = '1. DATE RANGE: Select the date range for backtesting', tooltip = "Enter the day of start backtesting.") startMonth = input.int(title="START MONTH", defval=1, minval=1, maxval=12, group = '1. DATE RANGE: Select the date range for backtesting', tooltip = "Enter the month of start backtesting.") startYear = input.int(title="START YEAR", defval=2018, minval=1800, maxval=2100, group = '1. DATE RANGE: Select the date range for backtesting', tooltip = "Enter the year of start backtesting.") endDate = input.int(title="END DAY DATE", defval=30, minval=1, maxval=31, group = '1. DATE RANGE: Select the date range for backtesting', tooltip = "Enter the day of end backtesting.") endMonth = input.int(title="END MONTH", defval=12, minval=1, maxval=12, group = '1. DATE RANGE: Select the date range for backtesting', tooltip = "Enter the month of end backtesting.") endYear = input.int(title="END YEAR", defval=2022, minval=1800, maxval=2100, group = '1. DATE RANGE: Select the date range for backtesting', tooltip = "Enter the year of end backtesting.") inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0)) //--------------------------------------------------------------- PercentProfit = input.float(defval = 0.50, title = "GRID PERCENT VALUE", group = "2. GRID CUSTOM SETTING", tooltip = "This feature refers to the percentage value of each grid. The value you enter here will allow you to create your custom grid.", confirm = true) UpperLimit = input.price(defval = 0.00, title = 'TOP GRID POSITION', group = "2. GRID CUSTOM SETTING", confirm = true, tooltip = "This feature allows you to set the initial price level of your grid. Click on a price level to start.") feePerTrade = input.float(defval = 0.03, minval = 0.00, maxval = 100, title = "FEE PER TRADE", group = "2. GRID CUSTOM SETTING", tooltip = "Set the commission cost your broker charges you for each trade.", confirm = true) //--------------------------------------------------------------- x1 = (100 - (PercentProfit * 1)) / 100 x2 = (100 - (PercentProfit * 2)) / 100 x3 = (100 - (PercentProfit * 3)) / 100 x4 = (100 - (PercentProfit * 4)) / 100 x5 = (100 - (PercentProfit * 5)) / 100 x6 = (100 - (PercentProfit * 6)) / 100 x7 = (100 - (PercentProfit * 7)) / 100 x8 = (100 - (PercentProfit * 8)) / 100 x9 = (100 - (PercentProfit * 9)) / 100 x10 = (100 - (PercentProfit * 10)) / 100 LowerLimit = UpperLimit * x10 //--------------------------------------------------------------- Linea_A = float(UpperLimit * x1) Linea_B = float(UpperLimit * x2) Linea_C = float(UpperLimit * x3) Linea_D = float(UpperLimit * x4) Linea_50Percent = float(UpperLimit * x5) Linea_E = float(UpperLimit * x6) Linea_F = float(UpperLimit * x7) Linea_G = float(UpperLimit * x8) Linea_H = float(UpperLimit * x9) //--------------------------------------------------------------- Long1 = ta.crossunder(close, Linea_A) and inDateRange Long2 = ta.crossunder(close, Linea_B) and inDateRange Long3 = ta.crossunder(close, Linea_C) and inDateRange Long4 = ta.crossunder(close, Linea_D) and inDateRange Long5 = ta.crossunder(close, Linea_50Percent) and inDateRange Long6 = ta.crossunder(close, Linea_E) and inDateRange Long7 = ta.crossunder(close, Linea_F) and inDateRange Long8 = ta.crossunder(close, Linea_G) and inDateRange Long9 = ta.crossunder(close, Linea_H) and inDateRange Long10 = ta.crossunder(close, LowerLimit) and inDateRange //--------------------------------------------------------------- isExit1 = ta.crossover(close, UpperLimit) isExit2 = ta.crossover(close, Linea_A) isExit3 = ta.crossover(close, Linea_B) isExit4 = ta.crossover(close, Linea_C) isExit5 = ta.crossover(close, Linea_D) isExit6 = ta.crossover(close, Linea_50Percent) isExit7 = ta.crossover(close, Linea_E) isExit8 = ta.crossover(close, Linea_F) isExit9 = ta.crossover(close, Linea_G) isExit10 = ta.crossover(close, Linea_H) //--------------------------------------------------------------- plot(LowerLimit, color = color.purple, linewidth = 2) plot(UpperLimit, color = color.purple, linewidth = 2) plot(Linea_A, color = color.blue, linewidth = 1) plot(Linea_B, color = color.blue, linewidth = 1) plot(Linea_C, color = color.blue, linewidth = 1) plot(Linea_D, color = color.blue, linewidth = 1) plot(Linea_50Percent, color = color.blue, linewidth = 1) plot(Linea_E, color = color.blue, linewidth = 1) plot(Linea_F, color = color.blue, linewidth = 1) plot(Linea_G, color = color.blue, linewidth = 1) plot(Linea_H, color = color.blue, linewidth = 1) //--------------------------------------------------------------- fill(plot(UpperLimit), plot(Linea_A), color = color.new(color.purple, 90)) fill(plot(Linea_A), plot(Linea_B), color = color.new(color.purple, 85)) fill(plot(Linea_B), plot(Linea_C), color = color.new(color.purple, 80)) fill(plot(Linea_C), plot(Linea_D), color = color.new(color.purple, 70)) fill(plot(Linea_D), plot(Linea_50Percent), color = color.new(color.purple, 60)) fill(plot(Linea_50Percent), plot(Linea_E), color = color.new(color.purple, 60)) fill(plot(Linea_E), plot(Linea_F), color = color.new(color.purple, 70)) fill(plot(Linea_F), plot(Linea_G), color = color.new(color.purple, 80)) fill(plot(Linea_G), plot(Linea_H), color = color.new(color.purple, 85)) fill(plot(Linea_H), plot(LowerLimit), color = color.new(color.purple, 90)) //--------------------------------------------------------------- if Long1 strategy.entry(id = "LONG1", direction = strategy.long, qty = 1) if isExit1 strategy.exit(id = "EXIT1", from_entry = "LONG1", qty = 100, stop = UpperLimit, profit = UpperLimit) if Long2 strategy.entry(id = "LONG2", direction = strategy.long, qty = 1) if isExit2 strategy.exit(id = "EXIT2", from_entry = "LONG2", qty = 100, stop = Linea_A, limit = Linea_A) if Long3 strategy.entry(id = "LONG3", direction = strategy.long, qty = 1) if isExit3 strategy.exit(id = "EXIT3", from_entry = "LONG3", qty = 100, stop = Linea_B, limit = Linea_B) if Long4 strategy.entry(id = "LONG4", direction = strategy.long, qty = 1) if isExit4 strategy.exit(id = "EXIT4", from_entry = "LONG4", qty = 100, stop = Linea_C, limit = Linea_C) if Long5 strategy.entry(id = "LONG5", direction = strategy.long, qty = 1) if isExit5 strategy.exit(id = "EXIT5", from_entry = "LONG5", qty = 100, stop = Linea_D, limit = Linea_D) if Long6 strategy.entry(id = "LONG6", direction = strategy.long, qty = 1) if isExit6 strategy.exit(id = "EXIT6", from_entry = "LONG6", qty = 100, stop = Linea_50Percent, limit = Linea_50Percent) if Long7 strategy.entry(id = "LONG7", direction = strategy.long, qty = 1) if isExit7 strategy.exit(id = "EXIT7", from_entry = "LONG7", qty = 100, stop = Linea_E, limit = Linea_E) if Long8 strategy.entry(id = "LONG8", direction = strategy.long, qty = 1) if isExit8 strategy.exit(id = "EXIT8", from_entry = "LONG8", qty = 100, stop = Linea_F, limit = Linea_F) if Long9 strategy.entry(id = "LONG9", direction = strategy.long, qty = 1) if isExit9 strategy.exit(id = "EXIT9", from_entry = "LONG9", qty = 100, stop = Linea_G, limit = Linea_G) if Long10 strategy.entry(id = "LONG10", direction = strategy.long, qty = 1) if isExit10 strategy.exit(id = "EXIT10", from_entry = "LONG10", qty = 100, stop = Linea_H, limit = Linea_H) //--------------------------------------------------------------- trades = int(strategy.closedtrades) initialCapital = float(strategy.initial_capital) grossReturn = trades * PercentProfit gainCapital = (initialCapital * grossReturn) / 100 totalCapital = initialCapital + gainCapital feeCost = (totalCapital * feePerTrade) / 100 totalFeeCost = feeCost * trades netCapital = totalCapital - totalFeeCost - initialCapital //--------------------------------------------------------------- i_position = input.string(defval = "Bottom Right", title = "Table Placement", options = ["Top Right", "Middle Right", "Bottom Right"], group = "3. Table Characteristics") position = i_position == "Top Right" ? position.top_right : i_position == "Middle Right" ? position.middle_right : position.bottom_right i_w = input.int(title = "Width", defval = 14, group = "3. Table Characteristics") i_h = input.int(title = "Height", defval = 8, group = "3. Table Characteristics") i_text_size = input(title = "Text Size", defval = "Normal") text_size = i_text_size == "Normal" ? size.normal : i_text_size == "Auto" ? size.auto : i_text_size == "Auto" ? size.auto : i_text_size == "Tiny" ? size.tiny : i_text_size == "Small" ? size.small : i_text_size == "Large" ? size.large : size.huge i = input.bool(title = "1. Capital", defval = true, group = "4. Enable Rows", tooltip = "Initial capital of the strategy.") i_1 = input.bool(title = "2. Gross Returns", defval = true, group = "4. Enable Rows", tooltip = "Total strategy return. Contains the initial capital plus the sum of the gains.") i_2 = input.bool(title = "3. Gain Percent", defval = true, group = "4. Enable Rows", tooltip = "Percentage return of strategy gains.") i_3 = input.bool(title = "4. Commission Cost", defval = true, group = "4. Enable Rows", tooltip = "Percentage return of strategy gains.") i_4 = input.bool(title = "5. Net Returns", defval = true, group = "4. Enable Rows", tooltip = "Total commission cost.") i_5 = input.bool(title = "6. Closed Trades", defval = true, group = "4. Enable Rows", tooltip = "Total trades.") var table perfTable = table.new(position, 2, 11, frame_color = color.purple, frame_width = 1, border_width = 1) //--------------------------------------------------------------- if i table.cell(perfTable, 0, 0, "1. GROSS RETURN", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size, text_halign = text.align_left) if i table.cell(perfTable, 1, 0, text = str.tostring(totalCapital) + " $", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size) if i_1 table.cell(perfTable, 0, 1, "2. INITIAL CAPITAL", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size, text_halign = text.align_left) if i_1 table.cell(perfTable, 1, 1, text = str.tostring(initialCapital) + " $", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size) if i_2 table.cell(perfTable, 0, 2, "3. GAIN", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size, text_halign = text.align_left) if i_2 table.cell(perfTable, 1, 2, text = str.tostring(grossReturn) + " %", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size) if i_3 table.cell(perfTable, 0, 3, "4. FEE COST", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size, text_halign = text.align_left) if i_3 table.cell(perfTable, 1, 3, text = str.tostring(totalFeeCost) + " $", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size) if i_4 table.cell(perfTable, 0, 4, "5. NET RETURN", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size, text_halign = text.align_left) if i_4 table.cell(perfTable, 1, 4, text = str.tostring(netCapital) + " $", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size) if i_5 table.cell(perfTable, 0, 6, "6. CLOSED TRADES", bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size, text_halign = text.align_left) if i_5 table.cell(perfTable, 1, 6, text = str.tostring(trades), bgcolor = color.new(color.purple, 90), text_color = color.new(color.white, 0), width = i_w, height = i_h, text_size = text_size) //---------------------------------------------------------------