Bursa Malaysia Index Series
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RozaniGhani-RG

//@version=5
indicator('Bursa Malaysia Index Series')

// 0. Inputs
// 1. Switch
// 2. Variables
// 3. Constructs

//#region ———————————————————— 0. Inputs
stringIndex = input.string(  defval  = 'Construction',
                             title   = 'Choose Sector',
                             options = ['Construction', 'Consumer Products', 'Energy', 'Finance', 'Health Care',
                                         'Industrial Products', 'Plantation', 'Property', 'REIT', 'Technology',
                                         'Telecommunications & Media', 'Transportation & Logistics', 'Utilities'])
displayPlot = input.string('close', 'Display Plot', options = ['close', 'ohlc'])
T1          = '1) Tick to show table\n2) Small font size recommended for mobile app or multiple layout'
tblY        = input.string( 'bottom', 'Table Position', inline = '1', options = [ 'top', 'middle', 'bottom'])
tblX        = input.string(  'right',               '', inline = '1', options = ['left', 'center', 'right'])
tblfont     = input.string(  'small',      'Font size', inline = '2', options = ['tiny',  'small', 'normal', 'large', 'huge'], tooltip = T1)
//#endregion
//#region ———————————————————— 1. Switch
index = switch stringIndex
    'Construction'                => 'MYX:CONSTRUCTN'
    'Consumer Products'           => 'MYX:CONSUMER'
    'Energy'                      => 'MYX:ENERGY'
    'Finance'                     => 'MYX:FINANCE'
    'Health Care'                 => 'MYX:HEALTH'
    'Industrial Products'         => 'MYX:IND-PROD'
    'Plantation'                  => 'MYX:PLANTATION'
    'Property'                    => 'MYX:PROPERTIES'
    'REIT'                        => 'MYX:REIT'
    'Technology'                  => 'MYX:TECHNOLOGY'
    'Telecommunications & Media'  => 'MYX:TELECOMMUNICATIONS'
    'Transportation & Logistics'  => 'MYX:TRANSPORTATION'
    'Utilities'                   => 'MYX:UTILITIES'
//#endregion

//#region ———————————————————— 2. Variables
OHLC(bool enable = true) => [open, high, low, close]
RS                       =  request.security(index, '', close, lookahead = barmerge.lookahead_off)
[O, H, L, C]             =  request.security(index, '', OHLC(), lookahead = barmerge.lookahead_off)
var TBL                  =  table.new(tblY + '_' +  tblX, 1, 1, border_width = 1)
//#endregion

//#region ———————————————————— 3. Constructs
plot(displayPlot == 'close' ? RS : na, 'Index', chart.fg_color)
plotcandle(  displayPlot == 'ohlc' ? O : na,
             displayPlot == 'ohlc' ? H : na,
             displayPlot == 'ohlc' ? L : na,
             displayPlot == 'ohlc' ? C : na,
             'OHLC',
             color = open < close ? color.teal : color.red,
             wickcolor   = open < close ? color.teal : color.red,
             bordercolor = open < close ? color.teal : color.red)
if barstate.islast
    table.cell(TBL, 0, 0, str.upper(stringIndex), text_color = chart.bg_color, bgcolor = chart.fg_color)
//#endregion
