Currency Strength by Bollinger Bands
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dairin(V1.00)

//@version=5
indicator(shorttitle = "BB_POWERS", title = "Currency Strength by Bollinger Bands", overlay = true)

//--------------------------------------------------------------------------
// Input parameter
//--------------------------------------------------------------------------

// 表示通貨
var GRP_SYM = "表示通貨"
bo_USD = input.bool(title="USD", defval = true, inline = '01', group = GRP_SYM)
bo_EUR = input.bool(title="EUR", defval = true, inline = '01', group = GRP_SYM)
bo_JPY = input.bool(title="JPY", defval = true, inline = '01', group = GRP_SYM)
bo_GBP = input.bool(title="GBP", defval = true, inline = '01', group = GRP_SYM)
bo_AUD = input.bool(title="AUD", defval = true, inline = '01', group = GRP_SYM)
bo_NZD = input.bool(title="NZD", defval = true, inline = '01', group = GRP_SYM)
bo_CAD = input.bool(title="CAD", defval = true, inline = '02', group = GRP_SYM)
bo_CHF = input.bool(title="CHF", defval = true, inline = '02', group = GRP_SYM)
bo_XAU = input.bool(title="XAU", defval = true, inline = '02', group = GRP_SYM)

var GRP_BB = 'ボリンジャーバンドの設定'
length = input.int(defval = 20, minval = 1, group = GRP_BB)

var GRP_WIN_COND = '勝敗の基準'
win_cond = input.string(title='基準', options = ['MAより上で勝利(下で敗退)', '+1σより上で勝利(下で敗退)', '+2σより上で勝利(下で敗退)'],defval = '+1σより上で勝利(下で敗退)', group = GRP_WIN_COND)


// 表示通貨
var GRP_BAR = "対象バーの設定"
bb_power_index = input.int(title="インデックス", defval = 0, group = GRP_BAR)
bo_line = input.bool(title="対象バーに垂直線を表示", defval = false, group = GRP_BAR)
bb_power_line_color = input.color(title="色", defval = color.red, inline = '11', group = GRP_BAR)
bb_power_line_style = input.string(title="線種", options = ['solid', 'dotted', 'dashed'],defval = 'dotted',inline = '11',  group = GRP_BAR)
bb_power_line_width = input.int(title="太さ", defval = 2, inline = '11', group = GRP_BAR)


//=== 表の設定 ===
var GRP_LIST = "表の設定"
// 表示位置
list_pos = input.string(title="表示位置", options = ['top_left','top_center','top_right', 'middle_left', 'middle_center','middle_right', 'bottom_left','bottom_center','bottom_right'], defval = "bottom_left", group = GRP_LIST)
// 表のセルの境界線（外枠以外）の色
border_color = input.color(title="境界線の色", defval = color.black, inline = '21',group = GRP_LIST)
// 表のセルの境界線（外枠以外）の幅
border_width = input.int(title="太さ", defval = 1, inline = '21', group = GRP_LIST)
// 表の外枠の色
frame_color = input.color(title="外枠の色　", defval = color.black, inline = '22', group = GRP_LIST)
// 表の外枠の幅
frame_width = input.int(title="太さ", defval = 1, inline = '22', group = GRP_LIST)

//=== セルのヘッダー設定 ===
var GRP_CEL = 'セルの設定'
// 文字色
head_text_color = input.color(title="ヘッダ　　色", defval = color.white, inline = '33', group = GRP_CEL)
// 背景色
head_background_color = input.color(title="背景色", defval = color.gray, inline = '33', group = GRP_CEL)
// 文字のサイズ
head_text_size = input.string(title="文字サイズ", options = ['auto', 'tiny', 'small', 'normal', 'large', 'huge'],defval = 'auto', inline = '33', group = GRP_CEL)

//=== セルのヘッダー以外の設定 ===
// 文字色
text_color = input.color(title="データ　　色", defval = color.black, inline = '34', group = GRP_CEL)
// 背景色
background_color = input.color(title="背景色", defval = color.white, inline = '34', group = GRP_CEL)
// 文字のサイズ
text_size = input.string(title="文字サイズ", options = ['auto', 'tiny', 'small', 'normal', 'large', 'huge'],defval = 'auto', inline = '34', group = GRP_CEL)

//--------------------------------------------------------------------------
// Global parameter
//--------------------------------------------------------------------------
var string win_mark = '○'
var string loss_mark = '●'

var int cel_width = 0
var int cel_height = 0

var color invalid_sym = color.gray

var int BB_EURUSD_IDX = 0
var int BB_USDJPY_IDX = 1
var int BB_GBPUSD_IDX = 2
var int BB_AUDUSD_IDX = 3
var int BB_NZDUSD_IDX = 4
var int BB_USDCAD_IDX = 5
var int BB_USDCHF_IDX = 6
var int BB_EURJPY_IDX = 7
var int BB_EURGBP_IDX = 8
var int BB_EURAUD_IDX = 9
var int BB_EURNZD_IDX = 10
var int BB_EURCAD_IDX = 11
var int BB_EURCHF_IDX = 12
var int BB_GBPJPY_IDX = 13
var int BB_AUDJPY_IDX = 14
var int BB_NZDJPY_IDX = 15
var int BB_CADJPY_IDX = 16
var int BB_CHFJPY_IDX = 17
var int BB_GBPAUD_IDX = 18
var int BB_GBPNZD_IDX = 19
var int BB_GBPCAD_IDX = 20
var int BB_GBPCHF_IDX = 21
var int BB_AUDNZD_IDX = 22
var int BB_AUDCAD_IDX = 23
var int BB_AUDCHF_IDX = 24
var int BB_NZDCAD_IDX = 25
var int BB_NZDCHF_IDX = 26
var int BB_CADCHF_IDX = 27
var int BB_XAUUSD_IDX = 28
var int BB_XAUEUR_IDX = 29
var int BB_XAUJPY_IDX = 30
var int BB_XAUGBP_IDX = 31
var int BB_XAUAUD_IDX = 32
var int BB_XAUNZD_IDX = 33
var int BB_XAUCAD_IDX = 34
var int BB_XAUCHF_IDX = 35

var int BB_IDX_MAX = BB_XAUCHF_IDX + 1

//--------------------------------------------------------------------------
// 指定レートがボリバンのどの位置にあるかを取得する
// price    指定レート
// ma_price MAの値
// dev_price 標準偏差値
// 復帰値(int)
//  0 = +3σより上
//  1 = +3a ～ +2σの間
//  2 = +2a ～ +1σの間
//  3 = +1σ ～ middleの間
//  4 = middleb ～ -1aの間
//  5 = -1σ ～ -2aの間
//  6 = -2σ ～ -3aの間
//  7 = -3σより下
//--------------------------------------------------------------------------
get_bb_rate(price, ma_price, dev_price) =>
    ret = 0

    plus3a = ma_price + dev_price * 3
    plus2a = ma_price + dev_price * 2
    plus1a = ma_price + dev_price * 1
    middle = ma_price
    minus1a = ma_price - dev_price * 1
    minus2a = ma_price - dev_price * 2
    minus3a = ma_price - dev_price * 3

    if (price > plus3a)
        ret := 0
    else if price <= plus3a and price >= plus2a
        ret := 1
    else if price <= plus2a and price >= plus1a
        ret := 2
    else if price <= plus1a and price >= middle
        ret := 3
    else if price <= middle and price >= minus1a
        ret := 4
    else if price <= minus1a and price >= minus2a
        ret := 5
    else if price <= minus2a and price >= minus3a
        ret := 6
    else
        ret := 7

//--------------------------------------------------------------------------
// 指定した時間足は現在チャートの時間足よりしたかどうかをチェックする
//--------------------------------------------------------------------------
is_lower_time_frame(time_frame) =>
    cur_time = timeframe.in_seconds(timeframe.period)
    target_time = timeframe.in_seconds(time_frame)

    if (target_time < cur_time)
        true
    else
        false

//--------------------------------------------------------------------------
// ボリバン位置を取得
//--------------------------------------------------------------------------
get_bb_rate_sym(sym) =>
    ma = ta.sma(close[bb_power_index], 20)
    dev = ta.stdev(close[bb_power_index] ,20)
    
    [ma_price, dev_price, price] = request.security(sym, timeframe.period, [ma, dev, close[bb_power_index]], barmerge.gaps_off, barmerge.lookahead_on)
    ret_val = get_bb_rate(price, ma_price, dev_price)
    

//--------------------------------------------------------------------------
// 表のポジションを取得
//--------------------------------------------------------------------------
get_list_pos(pos) =>
    switch pos
        'top_left'      => position.top_left
        'top_center'    => position.top_center
        'top_right'     => position.top_right
        'middle_left'   => position.middle_left
        'middle_center' => position.middle_center
        'middle_right'  => position.middle_right
        'bottom_left'   => position.bottom_left
        'bottom_center' => position.bottom_center
        'bottom_right'  => position.bottom_right
        => position.bottom_left

//--------------------------------------------------------------------------
// 表のポジションを取得
//--------------------------------------------------------------------------
get_text_size(text_sizepos) =>
    switch text_sizepos
        'auto'   => size.auto
        'tiny'   => size.tiny
        'small'  => size.small
        'normal' => size.normal
        'large'  => size.large
        'huge'   => size.huge
        => size.auto

//--------------------------------------------------------------------------
// 表のポジションを取得
//--------------------------------------------------------------------------
get_line_style(line_style) =>
    switch line_style
        "solid" => line.style_solid
        "dotted" => line.style_dotted
        "dashed" => line.style_dashed
        => line.style_solid

//--------------------------------------------------------------------------
// 勝利条件の取得
//--------------------------------------------------------------------------
get_victory_condition_type(vic_cond) =>
    switch vic_cond
        'MAより上で勝利(下で敗退)'  => 1
        '+1σより上で勝利(下で敗退)' => 2
        '+2σより上で勝利(下で敗退)' => 3
        => 2

//--------------------------------------------------------------------------
// シンボル数取得
//--------------------------------------------------------------------------
get_sym_count() =>
    sym_count = 0

    sym_count := sym_count + ((bo_USD) ? 1 : 0)
    sym_count := sym_count + ((bo_EUR) ? 1 : 0)
    sym_count := sym_count + ((bo_JPY) ? 1 : 0)
    sym_count := sym_count + ((bo_GBP) ? 1 : 0)
    sym_count := sym_count + ((bo_AUD) ? 1 : 0)
    sym_count := sym_count + ((bo_NZD) ? 1 : 0)
    sym_count := sym_count + ((bo_CAD) ? 1 : 0)
    sym_count := sym_count + ((bo_CHF) ? 1 : 0)
    sym_count := sym_count + ((bo_XAU) ? 1 : 0)


var string BB_WIN_MARK = '○'
//--------------------------------------------------------------------------
// 勝敗取得
//--------------------------------------------------------------------------
get_bb_power(bb_rate, bo_front) =>
    
    string ret_mark = ''
    win_cond_type = get_victory_condition_type(win_cond)
    
    // draw = 0, win = 1, loss = 2
    ret = switch win_cond_type
        1 => ( bb_rate <= 3 ? 1 : (bb_rate >= 4 ? 2 : 0) )
        2 => ( bb_rate <= 2 ? 1 : (bb_rate >= 5 ? 2 : 0) )
        3 => ( bb_rate <= 1 ? 1 : (bb_rate >= 6 ? 2 : 0) )
        => 0

    if (bo_front)
        ret_mark := ( ret == 1 ? win_mark : (ret == 2 ? loss_mark : '') )
    else
        ret_mark := ( ret == 1 ? loss_mark : (ret == 2 ? win_mark : '') )
    
//--------------------------------------------------------------------------
// ヘッダー領域に項目追加
//--------------------------------------------------------------------------
set_bb_power_head(table_id, col, row, cel_text) =>
    table.cell(table_id, column = col, row = row, text = cel_text, width = cel_width, height = cel_height, text_color = head_text_color, bgcolor = head_background_color, text_size = get_text_size(head_text_size))

//--------------------------------------------------------------------------
// データ領域に項目追加
//--------------------------------------------------------------------------
set_bb_power_item(table_id, col, row, cel_text) =>
    if (cel_text == '－')
        table.cell(table_id, column = col, row = row, text = cel_text, width = cel_width, height = cel_height, text_color = text_color, bgcolor = invalid_sym, text_size = get_text_size(text_size))
    else
        table.cell(table_id, column = col, row = row, text = cel_text, width = cel_width, height = cel_height, text_color = text_color, bgcolor = background_color, text_size = get_text_size(text_size))

//--------------------------------------------------------------------------
// メイン処理
//--------------------------------------------------------------------------
main() =>
    // ボリンジャーバンド位置を取得
    var bb_rate_list = array.new_int(BB_IDX_MAX, -1)
    
    array.set(bb_rate_list, BB_EURUSD_IDX, get_bb_rate_sym('EURUSD'))
    array.set(bb_rate_list, BB_USDJPY_IDX, get_bb_rate_sym('USDJPY'))
    array.set(bb_rate_list, BB_GBPUSD_IDX, get_bb_rate_sym('GBPUSD'))
    array.set(bb_rate_list, BB_AUDUSD_IDX, get_bb_rate_sym('AUDUSD'))
    array.set(bb_rate_list, BB_NZDUSD_IDX, get_bb_rate_sym('NZDUSD'))
    array.set(bb_rate_list, BB_USDCAD_IDX, get_bb_rate_sym('USDCAD'))
    array.set(bb_rate_list, BB_USDCHF_IDX, get_bb_rate_sym('USDCHF'))
    array.set(bb_rate_list, BB_EURJPY_IDX, get_bb_rate_sym('EURJPY'))
    array.set(bb_rate_list, BB_EURGBP_IDX, get_bb_rate_sym('EURGBP'))
    array.set(bb_rate_list, BB_EURAUD_IDX, get_bb_rate_sym('EURAUD'))
    array.set(bb_rate_list, BB_EURNZD_IDX, get_bb_rate_sym('EURNZD'))
    array.set(bb_rate_list, BB_EURCAD_IDX, get_bb_rate_sym('EURCAD'))
    array.set(bb_rate_list, BB_EURCHF_IDX, get_bb_rate_sym('EURCHF'))
    array.set(bb_rate_list, BB_GBPJPY_IDX, get_bb_rate_sym('GBPJPY'))
    array.set(bb_rate_list, BB_AUDJPY_IDX, get_bb_rate_sym('AUDJPY'))
    array.set(bb_rate_list, BB_NZDJPY_IDX, get_bb_rate_sym('NZDJPY'))
    array.set(bb_rate_list, BB_CADJPY_IDX, get_bb_rate_sym('CADJPY'))
    array.set(bb_rate_list, BB_CHFJPY_IDX, get_bb_rate_sym('CHFJPY'))
    array.set(bb_rate_list, BB_GBPAUD_IDX, get_bb_rate_sym('GBPAUD'))
    array.set(bb_rate_list, BB_GBPNZD_IDX, get_bb_rate_sym('GBPNZD'))
    array.set(bb_rate_list, BB_GBPCAD_IDX, get_bb_rate_sym('GBPCAD'))
    array.set(bb_rate_list, BB_GBPCHF_IDX, get_bb_rate_sym('GBPCHF'))
    array.set(bb_rate_list, BB_AUDNZD_IDX, get_bb_rate_sym('AUDNZD'))
    array.set(bb_rate_list, BB_AUDCAD_IDX, get_bb_rate_sym('AUDCAD'))
    array.set(bb_rate_list, BB_AUDCHF_IDX, get_bb_rate_sym('AUDCHF'))
    array.set(bb_rate_list, BB_NZDCAD_IDX, get_bb_rate_sym('NZDCAD'))
    array.set(bb_rate_list, BB_NZDCHF_IDX, get_bb_rate_sym('NZDCHF'))
    array.set(bb_rate_list, BB_CADCHF_IDX, get_bb_rate_sym('CADCHF'))
    array.set(bb_rate_list, BB_XAUUSD_IDX, get_bb_rate_sym('XAUUSD'))
    array.set(bb_rate_list, BB_XAUEUR_IDX, get_bb_rate_sym('XAUEUR'))
    array.set(bb_rate_list, BB_XAUJPY_IDX, get_bb_rate_sym('XAUJPY'))
    array.set(bb_rate_list, BB_XAUGBP_IDX, get_bb_rate_sym('XAUGBP'))
    array.set(bb_rate_list, BB_XAUAUD_IDX, get_bb_rate_sym('XAUAUD'))
    array.set(bb_rate_list, BB_XAUNZD_IDX, get_bb_rate_sym('XAUNZD'))
    array.set(bb_rate_list, BB_XAUCAD_IDX, get_bb_rate_sym('XAUCAD'))
    array.set(bb_rate_list, BB_XAUCHF_IDX, get_bb_rate_sym('XAUCHF'))

    // テーブルヘッダ作成
    var table_head = array.new_string(9, 'text')
    array.set(table_head, 0, 'USD')
    array.set(table_head, 1, 'EUR')
    array.set(table_head, 2, 'JPY')
    array.set(table_head, 3, 'GBP')
    array.set(table_head, 4, 'AUD')
    array.set(table_head, 5, 'NZD')
    array.set(table_head, 6, 'CAD')
    array.set(table_head, 7, 'CHF')
    array.set(table_head, 8, 'XAU')

    var bb_power_table = table.new(position = get_list_pos(list_pos), columns = 10, rows = 1 + get_sym_count(), border_color = border_color, border_width = border_width, frame_color = frame_color, frame_width = frame_width)

    // Create header table
    set_bb_power_head(bb_power_table, 0, 0, '')
    for i = 0 to array.size(table_head) - 1
        item = array.get(table_head, i)
        set_bb_power_head(bb_power_table, i + 1, 0, item)

    cell_row = 0
    // === USD ===
    if (bo_USD)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 0))
        set_bb_power_item(bb_power_table, 1, cell_row, '－')
        set_bb_power_item(bb_power_table, 2, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURUSD_IDX), false))
        set_bb_power_item(bb_power_table, 3, cell_row, get_bb_power(array.get(bb_rate_list, BB_USDJPY_IDX), true))
        set_bb_power_item(bb_power_table, 4, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPUSD_IDX), false))
        set_bb_power_item(bb_power_table, 5, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDUSD_IDX), false))
        set_bb_power_item(bb_power_table, 6, cell_row, get_bb_power(array.get(bb_rate_list, BB_NZDUSD_IDX), false))
        set_bb_power_item(bb_power_table, 7, cell_row, get_bb_power(array.get(bb_rate_list, BB_USDCAD_IDX), true))
        set_bb_power_item(bb_power_table, 8, cell_row, get_bb_power(array.get(bb_rate_list, BB_USDCHF_IDX), true))
        set_bb_power_item(bb_power_table, 9, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUUSD_IDX), false))

    // === EUR ===
    if (bo_EUR)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 1))
        set_bb_power_item(bb_power_table, 1, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURUSD_IDX), true))
        set_bb_power_item(bb_power_table, 2, cell_row, '－')
        set_bb_power_item(bb_power_table, 3, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURJPY_IDX), true))
        set_bb_power_item(bb_power_table, 4, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURGBP_IDX), true))
        set_bb_power_item(bb_power_table, 5, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURAUD_IDX), true))
        set_bb_power_item(bb_power_table, 6, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURNZD_IDX), true))
        set_bb_power_item(bb_power_table, 7, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURCAD_IDX), true))
        set_bb_power_item(bb_power_table, 8, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURCHF_IDX), true))
        set_bb_power_item(bb_power_table, 9, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUEUR_IDX), false))

    // === JPY ===
    if (bo_JPY)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 2))
        set_bb_power_item(bb_power_table, 1, cell_row, get_bb_power(array.get(bb_rate_list, BB_USDJPY_IDX), false))
        set_bb_power_item(bb_power_table, 2, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURJPY_IDX), false))
        set_bb_power_item(bb_power_table, 3, cell_row, '－')
        set_bb_power_item(bb_power_table, 4, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPJPY_IDX), false))
        set_bb_power_item(bb_power_table, 5, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDJPY_IDX), false))
        set_bb_power_item(bb_power_table, 6, cell_row, get_bb_power(array.get(bb_rate_list, BB_NZDJPY_IDX), false))
        set_bb_power_item(bb_power_table, 7, cell_row, get_bb_power(array.get(bb_rate_list, BB_CADJPY_IDX), false))
        set_bb_power_item(bb_power_table, 8, cell_row, get_bb_power(array.get(bb_rate_list, BB_CHFJPY_IDX), false))
        set_bb_power_item(bb_power_table, 9, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUJPY_IDX), false))

    // === GBP ===
    if (bo_GBP)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 3))
        set_bb_power_item(bb_power_table, 1, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPUSD_IDX), true))
        set_bb_power_item(bb_power_table, 2, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURGBP_IDX), false))
        set_bb_power_item(bb_power_table, 3, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPJPY_IDX), true))
        set_bb_power_item(bb_power_table, 4, cell_row, '－')
        set_bb_power_item(bb_power_table, 5, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPAUD_IDX), true))
        set_bb_power_item(bb_power_table, 6, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPNZD_IDX), true))
        set_bb_power_item(bb_power_table, 7, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPCAD_IDX), true))
        set_bb_power_item(bb_power_table, 8, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPCHF_IDX), true))
        set_bb_power_item(bb_power_table, 9, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUGBP_IDX), false))

    // === AUD ===
    if (bo_AUD)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 4))
        set_bb_power_item(bb_power_table, 1, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDUSD_IDX), true))
        set_bb_power_item(bb_power_table, 2, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURAUD_IDX), false))
        set_bb_power_item(bb_power_table, 3, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDJPY_IDX), true))
        set_bb_power_item(bb_power_table, 4, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPAUD_IDX), false))
        set_bb_power_item(bb_power_table, 5, cell_row, '－')
        set_bb_power_item(bb_power_table, 6, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDNZD_IDX), true))
        set_bb_power_item(bb_power_table, 7, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDCAD_IDX), true))
        set_bb_power_item(bb_power_table, 8, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDCHF_IDX), true))
        set_bb_power_item(bb_power_table, 9, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUAUD_IDX), false))

    // === NZD ===
    if (bo_NZD)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 5))
        set_bb_power_item(bb_power_table, 1, cell_row, get_bb_power(array.get(bb_rate_list, BB_NZDUSD_IDX), true))
        set_bb_power_item(bb_power_table, 2, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURNZD_IDX), false))
        set_bb_power_item(bb_power_table, 3, cell_row, get_bb_power(array.get(bb_rate_list, BB_NZDJPY_IDX), true))
        set_bb_power_item(bb_power_table, 4, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPNZD_IDX), false))
        set_bb_power_item(bb_power_table, 5, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDNZD_IDX), false))
        set_bb_power_item(bb_power_table, 6, cell_row, '－')
        set_bb_power_item(bb_power_table, 7, cell_row, get_bb_power(array.get(bb_rate_list, BB_NZDCAD_IDX), true))
        set_bb_power_item(bb_power_table, 8, cell_row, get_bb_power(array.get(bb_rate_list, BB_NZDCHF_IDX), true))
        set_bb_power_item(bb_power_table, 9, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUNZD_IDX), false))

    // === CAD ===
    if (bo_CAD)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 6))
        set_bb_power_item(bb_power_table, 1, cell_row, get_bb_power(array.get(bb_rate_list, BB_USDCAD_IDX), false))
        set_bb_power_item(bb_power_table, 2, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURCAD_IDX), false))
        set_bb_power_item(bb_power_table, 3, cell_row, get_bb_power(array.get(bb_rate_list, BB_CADJPY_IDX), true))
        set_bb_power_item(bb_power_table, 4, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPCAD_IDX), false))
        set_bb_power_item(bb_power_table, 5, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDCAD_IDX), false))
        set_bb_power_item(bb_power_table, 6, cell_row, get_bb_power(array.get(bb_rate_list, BB_NZDCAD_IDX), false))
        set_bb_power_item(bb_power_table, 7, cell_row, '－')
        set_bb_power_item(bb_power_table, 8, cell_row, get_bb_power(array.get(bb_rate_list, BB_CADCHF_IDX), true))
        set_bb_power_item(bb_power_table, 9, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUCAD_IDX), false))

    // === CHF ===
    if (bo_CHF)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 7))
        set_bb_power_item(bb_power_table, 1, cell_row, get_bb_power(array.get(bb_rate_list, BB_USDCHF_IDX), false))
        set_bb_power_item(bb_power_table, 2, cell_row, get_bb_power(array.get(bb_rate_list, BB_EURCHF_IDX), false))
        set_bb_power_item(bb_power_table, 3, cell_row, get_bb_power(array.get(bb_rate_list, BB_CHFJPY_IDX), true))
        set_bb_power_item(bb_power_table, 4, cell_row, get_bb_power(array.get(bb_rate_list, BB_GBPCHF_IDX), false))
        set_bb_power_item(bb_power_table, 5, cell_row, get_bb_power(array.get(bb_rate_list, BB_AUDCHF_IDX), false))
        set_bb_power_item(bb_power_table, 6, cell_row, get_bb_power(array.get(bb_rate_list, BB_NZDCHF_IDX), false))
        set_bb_power_item(bb_power_table, 7, cell_row, get_bb_power(array.get(bb_rate_list, BB_CADCHF_IDX), false))
        set_bb_power_item(bb_power_table, 8, cell_row, '－')
        set_bb_power_item(bb_power_table, 9, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUCHF_IDX), false))

    // === XAU ===
    if (bo_XAU)
        cell_row := cell_row + 1
        set_bb_power_head(bb_power_table, 0, cell_row, array.get(table_head, 8))
        set_bb_power_item(bb_power_table, 1, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUUSD_IDX), true))
        set_bb_power_item(bb_power_table, 2, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUEUR_IDX), true))
        set_bb_power_item(bb_power_table, 3, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUJPY_IDX), true))
        set_bb_power_item(bb_power_table, 4, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUGBP_IDX), true))
        set_bb_power_item(bb_power_table, 5, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUAUD_IDX), true))
        set_bb_power_item(bb_power_table, 6, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUNZD_IDX), true))
        set_bb_power_item(bb_power_table, 7, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUCAD_IDX), true))
        set_bb_power_item(bb_power_table, 8, cell_row, get_bb_power(array.get(bb_rate_list, BB_XAUCHF_IDX), true))
        set_bb_power_item(bb_power_table, 9, cell_row, '－')

// 表示高速化のため、最後のバーのみ処理する。
// この対応により警告出るけど気にしない
if (barstate.islast == true) and (get_sym_count() > 0)
    main()
    
    // 対象ローソク足に水平線を描画。なぜかここでしか水平線作れなかった。
    // 関数化しても駄目。
    var line line_id = na
    line.delete(line_id)
    if (bo_line)
        line_id := line.new(bar_index[bb_power_index], low, bar_index[bb_power_index], high, xloc.bar_index, extend.both, bb_power_line_color, get_line_style(bb_power_line_style), bb_power_line_width)
    

