index-generator-by-muqwishi.pine
Copy
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MUQWISHI
//@version=5
indicator("Index Generator [MUQWISHI]")
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// | INPUT |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
var G1 = "Mode & Style Settings"
// Index Name
indNme = input.string("My Oil & Gas Index", "Index Name ", inline = "1", group = G1, display = display.none)
// Index Currency
curncy = input.string("USD", "Index Currency ", inline = "0", display = display.none)
// Table Location
tblPos = input.string( "Top Right" , "Table Location ",
["Top Right" , "Middle Right" , "Bottom Right" ,
"Top Center", "Middle Center", "Bottom Center" ,
"Top Left" , "Middle Left" , "Bottom Left" ] , inline = "1", display = display.none)
// Table Size
tblSiz = input.string("Small", "Table Size ",
["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], inline = "2", display = display.none)
// Table Colors
tBgCol = input.color(#5c698c, " Title", inline = "0")
cBgCol = input.color(#778899, " Cell", inline = "1")
txtCol = input.color(#ffffff, " Text", inline = "2")
// Sorting
sortBy = input.string("Market Cap", "Sort Table By ",
["Symbols", "Change%", "Last", "Market Cap", "None"], inline = "4", display = display.none)
sortOd = input.string("Descending", "", ["Ascending", "Descending"], inline = "4", display = display.none)
// Index Plot Type
pltTyp = input.string("Candle", "Index Plot Type ", ["Candle", "Bar", "Line"], inline = "5", display = display.none)
pltCr1 = input.color(color.rgb(61, 158, 64), " ", inline = "5")
pltCr2 = input.color(color.red, "", inline = "5")
// Show & Hide
indChk = input.bool(true, "Show Index Title")
tblChk = input.bool(true, "Show Index Holdings")
pltChk = input.bool(true, "Show Index Plot")
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// | SYMBOLS |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// Symbols Checkmark
c01 = input.bool(true, "", group = "Symbols", inline = "s01")
c02 = input.bool(true, "", group = "Symbols", inline = "s02")
c03 = input.bool(true, "", group = "Symbols", inline = "s03")
c04 = input.bool(true, "", group = "Symbols", inline = "s04")
c05 = input.bool(true, "", group = "Symbols", inline = "s05")
c06 = input.bool(true, "", group = "Symbols", inline = "s06")
c07 = input.bool(true, "", group = "Symbols", inline = "s07")
c08 = input.bool(true, "", group = "Symbols", inline = "s08")
c09 = input.bool(true, "", group = "Symbols", inline = "s09")
c10 = input.bool(true, "", group = "Symbols", inline = "s10")
c11 = input.bool(true, "", group = "Symbols", inline = "s11")
c12 = input.bool(true, "", group = "Symbols", inline = "s12")
c13 = input.bool(true, "", group = "Symbols", inline = "s13")
c14 = input.bool(true, "", group = "Symbols", inline = "s14")
c15 = input.bool(true, "", group = "Symbols", inline = "s15")
c16 = input.bool(true, "", group = "Symbols", inline = "s16")
c17 = input.bool(true, "", group = "Symbols", inline = "s17")
c18 = input.bool(true, "", group = "Symbols", inline = "s18")
c19 = input.bool(true, "", group = "Symbols", inline = "s19")
c20 = input.bool(true, "", group = "Symbols", inline = "s20")
c21 = input.bool(true, "", group = "Symbols", inline = "s21")
c22 = input.bool(true, "", group = "Symbols", inline = "s22")
c23 = input.bool(true, "", group = "Symbols", inline = "s23")
c24 = input.bool(true, "", group = "Symbols", inline = "s24")
c25 = input.bool(true, "", group = "Symbols", inline = "s25")
c26 = input.bool(true, "", group = "Symbols", inline = "s26")
c27 = input.bool(true, "", group = "Symbols", inline = "s27")
c28 = input.bool(true, "", group = "Symbols", inline = "s28")
c29 = input.bool(true, "", group = "Symbols", inline = "s29")
c30 = input.bool(true, "", group = "Symbols", inline = "s30")
c31 = input.bool(true, "", group = "Symbols", inline = "s31")
c32 = input.bool(true, "", group = "Symbols", inline = "s32")
c33 = input.bool(true, "", group = "Symbols", inline = "s33")
c34 = input.bool(true, "", group = "Symbols", inline = "s34")
// Symbol Input
s01 = input.symbol("2222", "", group = "Symbols", inline = "s01", display = display.none)
s02 = input.symbol("XOM", "", group = "Symbols", inline = "s02", display = display.none)
s03 = input.symbol("CVX", "", group = "Symbols", inline = "s03", display = display.none)
s04 = input.symbol("601857", "", group = "Symbols", inline = "s04", display = display.none)
s05 = input.symbol("SHEL", "", group = "Symbols", inline = "s05", display = display.none)
s06 = input.symbol("TTE", "", group = "Symbols", inline = "s06", display = display.none)
s07 = input.symbol("COP", "", group = "Symbols", inline = "s07", display = display.none)
s08 = input.symbol("HKEX:883", "", group = "Symbols", inline = "s08", display = display.none)
s09 = input.symbol("BP", "", group = "Symbols", inline = "s09", display = display.none)
s10 = input.symbol("PBR", "", group = "Symbols", inline = "s10", display = display.none)
s11 = input.symbol("ADX:TAQA", "", group = "Symbols", inline = "s11", display = display.none)
s12 = input.symbol("CNQ", "", group = "Symbols", inline = "s12", display = display.none)
s13 = input.symbol("EQNR", "", group = "Symbols", inline = "s13", display = display.none)
s14 = input.symbol("SLB", "", group = "Symbols", inline = "s14", display = display.none)
s15 = input.symbol("600028", "", group = "Symbols", inline = "s15", display = display.none)
s16 = input.symbol("SO", "", group = "Symbols", inline = "s16", display = display.none)
s17 = input.symbol("MPC", "", group = "Symbols", inline = "s17", display = display.none)
s18 = input.symbol("EOG", "", group = "Symbols", inline = "s18", display = display.none)
s19 = input.symbol("ENB", "", group = "Symbols", inline = "s19", display = display.none)
s20 = input.symbol("DUK", "", group = "Symbols", inline = "s20", display = display.none)
s21 = input.symbol("PSX", "", group = "Symbols", inline = "s21", display = display.none)
s22 = input.symbol("ROSN", "", group = "Symbols", inline = "s22", display = display.none)
s23 = input.symbol("ADNOCGAS", "", group = "Symbols", inline = "s23", display = display.none)
s24 = input.symbol("EPD", "", group = "Symbols", inline = "s24", display = display.none)
s25 = input.symbol("PXD", "", group = "Symbols", inline = "s25", display = display.none)
s26 = input.symbol("VLO", "", group = "Symbols", inline = "s26", display = display.none)
s27 = input.symbol("OXY", "", group = "Symbols", inline = "s27", display = display.none)
s28 = input.symbol("LKOH", "", group = "Symbols", inline = "s28", display = display.none)
s29 = input.symbol("ET", "", group = "Symbols", inline = "s29", display = display.none)
s30 = input.symbol("E", "", group = "Symbols", inline = "s30", display = display.none)
s31 = input.symbol("SU", "", group = "Symbols", inline = "s31", display = display.none)
s32 = input.symbol("WMB", "", group = "Symbols", inline = "s32", display = display.none)
s33 = input.symbol("HES", "", group = "Symbols", inline = "s33", display = display.none)
s34 = input.symbol("OKE", "", group = "Symbols", inline = "s34", display = display.none)
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// | CALCULATION |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// +++++++++++++ Round Functions
s(x) => str.tostring(x, format.volume)
m(x) => math.round_to_mintick(x)
// +++++++++++++ Create Matrices
valMtx = matrix.new<float> (0, 10, na)
datMtx = matrix.new<string>(0, 07, na)
exclud = array.new<string>(1, "")
// +++++++++++++ Main Function
main(flg) =>
var dec = string(na), var cnt = string(na), var sec = string(na),
var ind = string(na), var mkt = float(na) ,
var c0 = float(na) , var c1 = float(na)
o = float(na), c = float(na), h = float(na), l = float(na), chg = float(na)
if flg
o := m(open), c := m(close), h := m(high), l := m(low)
// Calculate Change
c0 := session.ismarket ? close : c0
if timeframe.change("D")
c1 := c0[1]
chg := (close - c1)/c1 * 100
// Get Symbol's Infos
if barstate.islastconfirmedhistory
dec := syminfo.description
cnt := syminfo.country
sec := syminfo.sector
ind := syminfo.industry
// Market Cap
shr = syminfo.shares_outstanding_total
if barstate.isrealtime
mkt := shr * c1
else
mkt := shr * close
[o, c, h, l, mkt, chg, dec, cnt, sec, ind, time, timenow]
// +++++++++++++ Security Function
request(num, sym, flg) =>
[o, c, h, l, mkt, chg, dec, cnt, sec, ind, tim, real] =
request.security(ticker.modify(sym, syminfo.session), timeframe.period,
main(flg), ignore_invalid_symbol = true, currency = curncy)
if flg
if na(mkt)
exclud.set(0, (exclud.get(0) != "" ? exclud.get(0) + ", " : "") + str.split(sym, ":").get(1))
else
if tim >= time
valMtx.add_row(valMtx.rows(), array.from(num, o, c, h, l, mkt, 0, tim, chg, real))
else
valMtx.add_row(valMtx.rows(), array.from(num, c, c, c, c, mkt, 0, tim, chg, real))
datMtx.add_row(datMtx.rows(), array.from(s(num), str.split(sym, ":").get(1), str.split(sym, ":").get(0), dec, cnt, sec, ind))
// +++++++++++++ Import Data
request(01, s01, c01), request(02, s02, c02), request(03, s03, c03),
request(04, s04, c04), request(05, s05, c05), request(06, s06, c06),
request(07, s07, c07), request(08, s08, c08), request(09, s09, c09),
request(10, s10, c10), request(11, s11, c11), request(12, s12, c12),
request(13, s13, c13), request(14, s14, c14), request(15, s15, c15),
request(16, s16, c16), request(17, s17, c17), request(18, s18, c18),
request(19, s19, c19), request(20, s20, c20), request(21, s21, c21),
request(22, s22, c22), request(23, s23, c23), request(24, s24, c24),
request(25, s25, c25), request(26, s26, c26), request(27, s27, c27),
request(28, s28, c28), request(29, s29, c29), request(30, s30, c30),
request(31, s31, c31), request(32, s32, c32), request(33, s33, c33),
request(34, s34, c34), //request(35, s35, c35),
// +++++++++++++ Prepare Data
o = float(na), c = float(na), h = float(na), l = float(na)
plotCond = valMtx.col(7).max() >= time // Does lastLin bar's time equal to max time of all holding?
if valMtx.rows() > 0
totMrkt = valMtx.col(5).sum()
for i = 0 to valMtx.rows() - 1
wight = valMtx.get(i, 5)/totMrkt
valMtx.set(i, 6, wight)
if plotCond
o := nz(o) + wight * valMtx.get(i, 1)
c := nz(c) + wight * valMtx.get(i, 2)
h := nz(h) + wight * valMtx.get(i, 3)
l := nz(l) + wight * valMtx.get(i, 4)
if sortBy != "None"
order = sortOd == "Ascending" ? order.ascending : order.descending
if sortBy == "Symbols"
datMtx.sort(1, order)
else
a = sortBy == "Change%" ? 8 : sortBy == "Last" ? 2 : 6
valMtx.sort(a, order)
// Index Change
indexChange(c) =>
var c0 = 0.0 // Close at Regular Session.
var c1 = 0.0 // Close at Day Session.
var isChgD = false // Change in Day
c0 := not na(c) ? c : c0
isChgD := dayofmonth != dayofmonth[1] ? true : isChgD
if isChgD and not na(c)
c1 := c0[1], isChgD := false
[c0, (c0 - c1), (c0 - c1)/c1 * 100]
[indxCls, indxChg, indxChgP] = indexChange(c)
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// | PLOTTING |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
var cndColor1 = pltCr1, cndColor1 := na(c) ? cndColor1 : (c > o ? pltCr1 : c < o ? pltCr2 : pltCr1)
var cndColor2 = pltCr1, cndColor2 := na(c) ? cndColor2 : (c > c[1] ? pltCr1 : c < c[1] ? pltCr2 : pltCr1)
// Candle Plot
plotcandle(o, h, l, c, "Candle", cndColor1, color.gray, true, bordercolor = cndColor1,
display = pltChk and pltTyp == "Candle" ? display.all : display.none)
// Bar Plot
plotbar (o, h, l, c, "Bar", cndColor1, true, display = pltChk and pltTyp == "Bar" ? display.all : display.none)
// Line Plot
plot(c, "Line", cndColor2, 2, editable = true, display = pltChk and pltTyp == "Line" ? display.all : display.none)
// Change Percentage
plot(timeframe.isintraday ? (c-c[1])/c[1] * 100 : not na(c) ? indxChgP : na, "", cndColor1, editable = false,
display = pltChk ? display.status_line : display.none, format = format.percent)
// Last Price Line
var line lastLin = na
var label lastLab = na
if not na(lastLin)
lastLin.set_xy1(bar_index , indxCls)
lastLin.set_xy2(bar_index + 1, indxCls)
lastLin.set_color(pltTyp == "Line" ? cndColor2 : cndColor1)
else if pltChk
lastLin := line.new(0, 0, 0, 0, xloc.bar_index, extend.both, na, line.style_dotted, 1)
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// | TABLE |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// +++++++++++++ Get Tbale Location & Size
locNsze(x) =>
y = str.split(str.lower(x), " ")
out = ""
for i = 0 to y.size() - 1
out := out + y.get(i)
if i != y.size() - 1
out := out + "_"
out
// +++++++++++++ Create Table
var table tbl = na
var nRows = 36
var nCols = 5
if nRows > 5
tbl := table.new(locNsze(tblPos), nCols, nRows, color(na), color(na), 1, color(na), 1)
// +++++++++++++ Cell Function
cell(col, row, txt, bCol, tCol) =>
tbl.cell(col, row, txt, 0, 0, tCol, bgcolor = bCol, text_size = locNsze(tblSiz))
// +++++++++++++ Draw Table
if barstate.islast and not na(tbl)
tbl.clear(0, 0, nCols - 1, nRows - 1)
v = 0
if indChk
// Index
r = indxChg > 0 ? pltCr1 : indxChg < 0 ? pltCr2 : txtCol
cell(0, v, indNme + "\n" +
str.tostring(indxCls > 1 ? math.round(indxCls, 2) : indxCls, "0.00######") + " " +
str.tostring(indxCls > 1 ? math.round(indxChg, 2) : indxCls, "0.00######") + " " +
str.tostring(indxChgP, format.percent), color(na), r),
tbl.cell_set_text_size(0, v, size.auto)
tbl.cell_set_text_font_family(0, v, font.family_default)
tbl.merge_cells(0, v, nCols - 1, v),
v += 1
if tblChk
excld = array.get(exclud, 0)
// Holdings
cell(0, v, "Symbols" + (excld != "" ? "\n⚠️" : ""), tBgCol, txtCol)
if excld != ""
tbl.cell_set_tooltip(0, v, "These symbols are not included in the index's holdings" +
" because their total number of shares are not provided/NaN.\n\n" + excld)
cell(1, v, "Last\n("+curncy+")", tBgCol, txtCol)
cell(2, v, "Chg%\n(" + (timeframe.isintraday ? "D" : timeframe.period) + ")", tBgCol, txtCol)
cell(3, v, "Market\nCap", tBgCol, txtCol)
cell(4, v, "Weight%", tBgCol, txtCol)
v += 1
if valMtx.rows() > 0
for i = 0 to valMtx.rows() - 1
// Symbol
idx = datMtx.col(0).indexof(str.tostring(valMtx.get(i, 0)))
symbl = datMtx.get(idx, 1)
r = sortBy == "Symbols" ? v + idx : v
cell(0, r, symbl, tBgCol, txtCol)
tbl.cell_set_tooltip(0, r, datMtx.get(idx, 3) +
"\n=================================" +
"\nExᴄʜᴀɴɢᴇ﹕" + datMtx.get(idx, 2) +
"\nCᴏᴜɴᴛʀʏ﹕" + datMtx.get(idx, 4) +
"\nSᴇᴄᴛᴏʀ﹕" + datMtx.get(idx, 5) +
"\nIɴᴅᴜsᴛʀɪᴀʟ﹕" + datMtx.get(idx, 6))
// Last
cell(1, r, str.tostring(valMtx.get(i, 2), "0.00########"), cBgCol, txtCol)
// Change
symChg = valMtx.get(i, 8)
symCol = symChg > 0 ? pltCr1 : symChg < 0 ? pltCr2 : cBgCol
cell(2, r, str.tostring(symChg, format.percent), symCol, txtCol)
// Market Cap
cell(3, r, str.tostring(valMtx.get(i, 5), format.volume), cBgCol, txtCol)
// Weight %
cell(4, r, str.tostring(valMtx.get(i, 6) * 100, format.percent), cBgCol, txtCol)
v := sortBy == "Symbols" ? v : v + 1
Show all 358 lines