Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This script is a modified version of the original script from axg_ or u/squattingsquid
- // Get the original : https://www.tradingview.com/script/EJ1aJKOg-Relative-Strength-Screener-V2-Top-100-volume-leaders/
- // The sorting system was inspired by LuxGecko an his Bubble Sort function
- // Get the original : https://www.tradingview.com/script/mMF7fVKO-Pinescript-Bubble-Sort-using-Arrays/
- //@version=5
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- indicator("Sector Relative Strength", shorttitle = "Sector RS", overlay = true)
- // INPUTS // ---------------------------------------------------------------
- // Parameters //
- var string cl = "Price", var string ch = "Change", var string rs = "Relative strength", var string rv = "Relative volume", var string nan = "Empty"
- var string co1 = "Column 1", var string co2 = "Column 2", var string co3 = "Column 3", var string co4 = "Column 4", var string co5 = "Column 5", var string des = "Desactivated"
- var string l = "Left", var string c = "Center", var string r = "Right"
- var string s = "Small", var string m = "Medium"
- in_n = input.symbol (defval = "SPY", title = "Comparison Symbol", group = "Calculation inputs")
- //co_n = input.string (defval = ch, title = "Number of columns", group = "Table size parameters")
- co_1 = input.string (defval = ch, title = "First column content", group = "First column parameters", options = [des, cl, ch, rs, rv])
- tf_1 = input.timeframe (defval = "5", title = "First column timeframe", group = "First column parameters")
- le_1 = input.int (defval = 12, title = "First column period", group = "First column parameters")
- co_2 = input.string (defval = rs, title = "Second column content", group = "Second column parameters", options = [des, cl, ch, rs, rv])
- tf_2 = input.timeframe (defval = "5", title = "Second column timeframe", group = "Second column parameters")
- le_2 = input.int (defval = 12, title = "Second column period", group = "Second column parameters")
- co_3 = input.string (defval = rs, title = "Third column content", group = "Third column parameters", options = [des, cl, ch, rs, rv])
- tf_3 = input.timeframe (defval = "15", title = "Third column timeframe", group = "Third column parameters")
- le_3 = input.int (defval = 12, title = "Third column period", group = "Third column parameters")
- co_4 = input.string (defval = rs, title = "Fourth column content", group = "Fourth column parameters", options = [des, cl, ch, rs, rv])
- tf_4 = input.timeframe (defval = "30", title = "Fourth column timeframe", group = "Fourth column parameters")
- le_4 = input.int (defval = 12, title = "Fourth column period", group = "Fourth column parameters")
- co_5 = input.string (defval = rv, title = "Fifth column content", group = "Fifth column parameters", options = [des, cl, ch, rs, rv])
- tf_5 = input.timeframe (defval = "5", title = "Fifth column timeframe", group = "Fifth column parameters")
- le_5 = input.int (defval = 78, title = "Fifth column period", group = "Fifth column parameters")
- so_p = input.string (defval = co1, title = "Activate sorting using", group = "Sorting parameters", options = [des, co1, co2, co3, co4, co5])
- cl_1 = input.float (defval = 0.75, title = "Upper limit", group = "Heatmap parameters")
- cl_2 = input.float (defval = 0.50, title = "Middle limit", group = "Heatmap parameters")
- cl_3 = input.float (defval = 0.25, title = "Lower limit", group = "Heatmap parameters")
- cl_s = input.bool (defval = true, title = "Dark mode", group = "Color theme")
- ta_s = input.string (defval = s, title = "Text size", group = "Table display setting", options = [s, m])
- ta_p = input.string (defval = r, title = "Position of table", group = "Table display setting", options = [l, c, r])
- // Symbol selection //
- s01 = "XLC", s02 = "XLY", s03 = "XLP", s04 = "XLE", s05 = "XLF", s06 = "XLV", s07 = "XLI", s08 = "XLB", s09 = "XLRE", s10 = "XLK", s11 = "XLU"
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // TIMEFRAME // ------------------------------------------------------------
- // Timeframes converter //
- bi = (time - time_tradingday) / (60000 * 5) - (96 + 78)
- tf_f (co_x, tf_x) =>
- tf_v = (
- tf_x == "5" ? 1 :
- tf_x == "15" ? 3 :
- tf_x == "30" ? 6 :
- tf_x == "45" ? 9 :
- tf_x == "60" ? 12 :
- tf_x == "120" ? 24 :
- tf_x == "180" ? 36 :
- tf_x == "240" ? 48 :
- tf_x == "1D" ? 78 : na)
- tf_b = co_x == rs ? bi - int(bi / tf_v) * tf_v : na
- tf_bc = co_x == rs ? tf_b > 0 ? tf_b + 1 : 1 : na
- [tf_v, tf_bc]
- [tf1_v, tf1_b] = tf_f (co_1, tf_1)
- [tf2_v, tf2_b] = tf_f (co_2, tf_2)
- [tf3_v, tf3_b] = tf_f (co_3, tf_3)
- [tf4_v, tf4_b] = tf_f (co_4, tf_4)
- [tf5_v, tf5_b] = tf_f (co_5, tf_5)
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // FORMULAS // -------------------------------------------------------------
- // Power index formula //
- pi_f (tf_v, tf_b, le_x) =>
- tf_h = ta.highest (high, tf_b)
- tf_l = ta.lowest (low, tf_b)
- tf_c = ta.change (close, tf_v * (le_x - 1) + tf_b)
- tf_tr = math.max(tf_h - tf_l, math.abs(tf_h - close[tf_b]), math.abs(tf_l - close[tf_b]))
- tf_atr = 0.0
- tf_atr := (1 / le_x) * tf_tr + (1 - (1 / le_x)) * nz(tf_atr[tf_b])
- tf_pi = tf_c / tf_atr
- // Relative strength formula //
- rs_f (tf_v, tf_b, le_x, co_i) =>
- tf_h = ta.highest (high, tf_b)
- tf_l = ta.lowest (low, tf_b)
- tf_c = ta.change (close, tf_v * (le_x - 1) + tf_b)
- tf_tr = math.max(tf_h - tf_l, math.abs(tf_h - close[tf_b]), math.abs(tf_l - close[tf_b]))
- tf_atr = 0.0
- tf_atr := (1 / le_x) * tf_tr + (1 - (1 / le_x)) * nz(tf_atr[tf_b])
- tf_rs = (tf_c - co_i * tf_atr) / tf_atr
- // Relative volume formula //
- rv_f (tf_v, le_x) => math.round(math.sum (volume, tf_v) / (ta.sma(volume, le_x) * tf_v), 2)
- // Change formula //
- ch_f (tf_v) => math.round(ta.change (close, tf_v) / close [tf_v] * 100, 2)
- // Index request controler //
- ir_co (co_x, tf_v, tf_b, le_x) => co_x == cl ? close : co_x == ch ? ch_f(tf_v) : co_x == rs ? pi_f(tf_v, tf_b, le_x) : co_x == rv ? rv_f(tf_v, le_x) : na
- // Symbol request controler //
- sr_co (co_x, tf_v, tf_b, le_x, co_i) => co_x == cl ? close : co_x == ch ? ch_f(tf_v) : co_x == rs ? rs_f(tf_v, tf_b, le_x, co_i) : co_x == rv ? rv_f(tf_v, le_x) : na
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // REQUESTS // -------------------------------------------------------------
- // Index requests //
- in_r () =>
- co1_i = ir_co (co_1, tf1_v, tf1_b, le_1)
- co2_i = ir_co (co_2, tf2_v, tf2_b, le_2)
- co3_i = ir_co (co_3, tf3_v, tf3_b, le_3)
- co4_i = ir_co (co_4, tf4_v, tf4_b, le_4)
- co5_i = ir_co (co_5, tf5_v, tf5_b, le_5)
- [co1_i, co2_i ,co3_i, co4_i, co5_i]
- [co1_i, co2_i ,co3_i, co4_i, co5_i] = request.security(in_n, "5", in_r())
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // Symbol requests //
- sy_r () =>
- co1_s = sr_co (co_1, tf1_v, tf1_b, le_1, co1_i)
- co2_s = sr_co (co_2, tf2_v, tf2_b, le_2, co2_i)
- co3_s = sr_co (co_3, tf3_v, tf3_b, le_3, co3_i)
- co4_s = sr_co (co_4, tf4_v, tf4_b, le_4, co4_i)
- co5_s = sr_co (co_5, tf5_v, tf5_b, le_5, co5_i)
- [co1_s, co2_s ,co3_s, co4_s, co5_s]
- [co1_01, co2_01, co3_01, co4_01, co5_01] = request.security(s01, "5", sy_r())
- [co1_02, co2_02, co3_02, co4_02, co5_02] = request.security(s02, "5", sy_r())
- [co1_03, co2_03, co3_03, co4_03, co5_03] = request.security(s03, "5", sy_r())
- [co1_04, co2_04, co3_04, co4_04, co5_04] = request.security(s04, "5", sy_r())
- [co1_05, co2_05, co3_05, co4_05, co5_05] = request.security(s05, "5", sy_r())
- [co1_06, co2_06, co3_06, co4_06, co5_06] = request.security(s06, "5", sy_r())
- [co1_07, co2_07, co3_07, co4_07, co5_07] = request.security(s07, "5", sy_r())
- [co1_08, co2_08, co3_08, co4_08, co5_08] = request.security(s08, "5", sy_r())
- [co1_09, co2_09, co3_09, co4_09, co5_09] = request.security(s09, "5", sy_r())
- [co1_10, co2_10, co3_10, co4_10, co5_10] = request.security(s10, "5", sy_r())
- [co1_11, co2_11, co3_11, co4_11, co5_11] = request.security(s11, "5", sy_r())
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // ARRAYS // ---------------------------------------------------------------
- s_arr = array.new_string (0)
- co1_arr = array.new_float (0), co2_arr = array.new_float (0)
- co3_arr = array.new_float (0), co4_arr = array.new_float (0)
- co5_arr = array.new_float (0)
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // Filling the arrays //
- array.push(s_arr, "Communication - XLC"), array.push(co1_arr, co1_01), array.push(co2_arr, co2_01), array.push(co3_arr, co3_01), array.push(co4_arr, co4_01), array.push(co5_arr, co5_01)
- array.push(s_arr, "Cons Disctret - XLY"), array.push(co1_arr, co1_02), array.push(co2_arr, co2_02), array.push(co3_arr, co3_02), array.push(co4_arr, co4_02), array.push(co5_arr, co5_02)
- array.push(s_arr, "Cons Staples - XLP"), array.push(co1_arr, co1_03), array.push(co2_arr, co2_03), array.push(co3_arr, co3_03), array.push(co4_arr, co4_03), array.push(co5_arr, co5_03)
- array.push(s_arr, "Energy - XLE"), array.push(co1_arr, co1_04), array.push(co2_arr, co2_04), array.push(co3_arr, co3_04), array.push(co4_arr, co4_04), array.push(co5_arr, co5_04)
- array.push(s_arr, "Financial - XLF"), array.push(co1_arr, co1_05), array.push(co2_arr, co2_05), array.push(co3_arr, co3_05), array.push(co4_arr, co4_05), array.push(co5_arr, co5_05)
- array.push(s_arr, "Healthcare - XLV"), array.push(co1_arr, co1_06), array.push(co2_arr, co2_06), array.push(co3_arr, co3_06), array.push(co4_arr, co4_06), array.push(co5_arr, co5_06)
- array.push(s_arr, "Industrials - XLI"), array.push(co1_arr, co1_07), array.push(co2_arr, co2_07), array.push(co3_arr, co3_07), array.push(co4_arr, co4_07), array.push(co5_arr, co5_07)
- array.push(s_arr, "Materials - XLB"), array.push(co1_arr, co1_08), array.push(co2_arr, co2_08), array.push(co3_arr, co3_08), array.push(co4_arr, co4_08), array.push(co5_arr, co5_08)
- array.push(s_arr, "Real Estate - XLRE"), array.push(co1_arr, co1_09), array.push(co2_arr, co2_09), array.push(co3_arr, co3_09), array.push(co4_arr, co4_09), array.push(co5_arr, co5_09)
- array.push(s_arr, "Technology - XLK"), array.push(co1_arr, co1_10), array.push(co2_arr, co2_10), array.push(co3_arr, co3_10), array.push(co4_arr, co4_10), array.push(co5_arr, co5_10)
- array.push(s_arr, "Utilities - XLU"), array.push(co1_arr, co1_11), array.push(co2_arr, co2_11), array.push(co3_arr, co3_11), array.push(co4_arr, co4_11), array.push(co5_arr, co5_11)
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // Sorting the arrays //
- if so_p != des
- so_c = so_p == co1 ? co1_arr : so_p == co2 ? co2_arr : so_p == co3 ? co3_arr : so_p == co4 ? co4_arr : co5_arr
- n = array.size(so_c) - 1
- for i = 0 to n - 1
- for j = 0 to n - i - 1
- if array.get(so_c , j) < array.get(so_c, j+1)
- s_temp = array.get(s_arr, j)
- array.set(s_arr, j, array.get(s_arr, j + 1))
- array.set(s_arr, j + 1, s_temp)
- co1_temp = array.get(co1_arr, j)
- array.set(co1_arr, j, array.get(co1_arr, j + 1))
- array.set(co1_arr, j + 1, co1_temp)
- co2_temp = array.get(co2_arr, j)
- array.set(co2_arr, j, array.get(co2_arr, j + 1))
- array.set(co2_arr, j + 1, co2_temp)
- co3_temp = array.get(co3_arr, j)
- array.set(co3_arr, j, array.get(co3_arr, j + 1))
- array.set(co3_arr, j + 1, co3_temp)
- co4_temp = array.get(co4_arr, j)
- array.set(co4_arr, j, array.get(co4_arr, j + 1))
- array.set(co4_arr, j + 1, co4_temp)
- co5_temp = array.get(co5_arr, j)
- array.set(co5_arr, j, array.get(co5_arr, j + 1))
- array.set(co5_arr, j + 1, co5_temp)
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // TABLE // ----------------------------------------------------------------
- // Graphical variables //
- g_1 = color.rgb(016, 158, 119), r_1 = color.rgb(239, 083, 080)
- g_2 = cl_s ? color.rgb(017, 124, 098) : color.rgb(076, 182, 153), r_2 = cl_s ? color.rgb(184, 068, 069) : color.rgb(245, 126, 124)
- g_3 = cl_s ? color.rgb(017, 090, 077) : color.rgb(135, 206, 187), r_3 = cl_s ? color.rgb(129, 053, 057) : color.rgb(247, 169, 167)
- g_4 = cl_s ? color.rgb(018, 057, 055) : color.rgb(195, 231, 221), r_4 = cl_s ? color.rgb(073, 037, 046) : color.rgb(249, 193, 192)
- bkg = cl_s ? color.rgb(019, 023, 034) : color.white, txt = cl_s ? color.white : color.black
- cl_f1 (val) => val > 0 ? g_1 : val < 0 ? r_1 : txt
- cl_f2 (val) => val > cl_1 ? g_1 : val > cl_2 ? g_2 : val > cl_3 ? g_3 : val > 0.0 ? g_4 : val > -cl_3 ? r_4 : val > -cl_2 ? r_3 : val > -cl_1 ? r_2 : r_1
- brd_s = ta_s == s ? 2 : 3
- txt_s = ta_s == s ? size.small : size.normal
- ta_pl = ta_p == r ? position.top_right : ta_p == c ? position.top_center : position.top_left
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // Columns calculator //
- co1_pl = (co_1 != des ? 1 : 0)
- co2_pl = (co_2 != des ? 1 : 0) + co1_pl
- co3_pl = (co_3 != des ? 1 : 0) + co2_pl
- co4_pl = (co_4 != des ? 1 : 0) + co3_pl
- co5_pl = (co_4 != des ? 1 : 0) + co4_pl
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- // Filling the table //
- co_n (co_x, tf_x) => co_x == cl ? "Price" : co_x == ch ? "Change " + tf_x : co_x == rs ? " RS " + tf_x + " ": co_x == rv ? " RV " + tf_x + " " : na
- in_ns = str.substring(in_n, str.pos(in_n, ":") + 1)
- var tbl = table.new(ta_pl, co5_pl + 1, 14, border_width = brd_s, border_color = bkg)
- if barstate.islast
- table.cell(tbl, 0, 0, "Symbol", text_halign = text.align_right, bgcolor = bkg, text_color = txt, text_size = size.normal)
- table.cell(tbl, 0, 1, str.tostring(in_ns), text_halign = text.align_right, bgcolor = bkg, text_color = txt, text_size = txt_s)
- for i = 0 to 10
- table.cell(tbl, 0, i + 2, array.get(s_arr, i), text_halign = text.align_right, bgcolor = bkg, text_color = txt, text_size = txt_s)
- if co_1 != des
- table.cell(tbl, co1_pl, 0, co_n (co_1, tf_1), text_halign = text.align_center, bgcolor = bkg, text_color = txt, text_size = size.normal)
- table.cell(tbl, co1_pl, 1, str.tostring(co1_i, "#.##") + (co_1 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_1 == rs ? cl_f2 (co1_i) : bkg, text_color = co_1 == rs ? txt : co_1 == cl ? txt : cl_f1 (co1_i), text_size = txt_s)
- for i = 0 to 10
- table.cell(tbl, co1_pl, i + 2, str.tostring(array.get(co1_arr, i), "#.##") + (co_1 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_1 == rs ? cl_f2 (array.get(co1_arr, i)) : bkg, text_color = co_1 == rs ? txt : cl_f1 (array.get(co1_arr, i)), text_size = txt_s)
- if co_2 != des
- table.cell(tbl, co2_pl, 0, co_n (co_2, tf_2), text_halign = text.align_center, bgcolor = bkg, text_color = txt, text_size = size.normal)
- table.cell(tbl, co2_pl, 1, str.tostring(co2_i, "#.##") + (co_2 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_2 == rs ? cl_f2 (co2_i) : bkg, text_color = co_2 == rs ? txt : co_2== cl ? txt : cl_f1 (co2_i), text_size = txt_s)
- for i = 0 to 10
- table.cell(tbl, co2_pl, i + 2, str.tostring(array.get(co2_arr, i), "#.##") + (co_2 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_2 == rs ? cl_f2 (array.get(co2_arr, i)) : bkg, text_color = co_2 == rs ? txt : cl_f1 (array.get(co2_arr, i)), text_size = txt_s)
- if co_3 != des
- table.cell(tbl, co3_pl, 0, co_n (co_3, tf_3), text_halign = text.align_center, bgcolor = bkg, text_color = txt, text_size = size.normal)
- table.cell(tbl, co3_pl, 1, str.tostring(co3_i, "#.##") + (co_3 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_3 == rs ? cl_f2 (co3_i) : bkg, text_color = co_3 == rs ? txt : co_3 == cl ? txt : cl_f1 (co3_i), text_size = txt_s)
- for i = 0 to 10
- table.cell(tbl, co3_pl, i + 2, str.tostring(array.get(co3_arr, i), "#.##") + (co_3 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_3 == rs ? cl_f2 (array.get(co3_arr, i)) : bkg, text_color = co_3 == rs ? txt : cl_f1 (array.get(co3_arr, i)), text_size = txt_s)
- if co_4 != des
- table.cell(tbl, co4_pl, 0, co_n (co_4, tf_4), text_halign = text.align_center, bgcolor = bkg, text_color = txt, text_size = size.normal)
- table.cell(tbl, co4_pl, 1, str.tostring(co4_i, "#.##") + (co_4 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_4 == rs ? cl_f2 (co4_i) : bkg, text_color = co_4 == rs ? txt : co_4 == cl ? txt : cl_f1 (co4_i), text_size = txt_s)
- for i = 0 to 10
- table.cell(tbl, co4_pl, i + 2, str.tostring(array.get(co4_arr, i), "#.##") + (co_4 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_4 == rs ? cl_f2 (array.get(co4_arr, i)) : bkg, text_color = co_4 == rs ? txt : cl_f1 (array.get(co4_arr, i)), text_size = txt_s)
- if co_5 != des
- table.cell(tbl, co5_pl, 0, co_n (co_5, tf_5), text_halign = text.align_center, bgcolor = bkg, text_color = txt, text_size = size.normal)
- table.cell(tbl, co5_pl, 1, str.tostring(co5_i, "#.##") + (co_5 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_5 == rs ? cl_f2 (co5_i) : bkg, text_color = co_5 == rs ? txt : co_5 == cl ? txt : cl_f1 (co5_i), text_size = txt_s)
- for i = 0 to 10
- table.cell(tbl, co5_pl, i + 2, str.tostring(array.get(co5_arr, i), "#.##") + (co_5 == ch ? " %" : na), text_halign = text.align_center, bgcolor = co_5 == rs ? cl_f2 (array.get(co5_arr, i)) : bkg, text_color = co_5 == rs ? txt : cl_f1 (array.get(co5_arr, i)), text_size = txt_s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement