HarunoVolatility
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sora_Haruno

//@version=5
indicator(title="HarunoVolatility", shorttitle="HaruVola", overlay=true,max_bars_back=5000,max_labels_count=500)
tk = time(timeframe.period, "1900-2600")
rd = time(timeframe.period, "0200-0800")
ny = time(timeframe.period, "0800-1600")
TOKYO = na(tk) ? na : color.new(color.blue, 75)
London = na(rd) ? na : color.new(color.green, 75)
NewYork = na(ny) ? na : color.new(color.red, 75)
bgcolor(TOKYO, title="TK")
bgcolor(London, title="RD")
bgcolor(NewYork, title="NY")

//市場ごとのボラティリティ関数
span_vola(int open_time,int close_time) =>
    int span = na
    int s = na
    int now = hour
    //onaji = ERR
    if(open_time == close_time)//openとcloseが一緒だった時はエラー
        na
    //Nomal
    else if(open_time < close_time)//市場が日を跨がない時
        if (0 <= now and now < open_time)//市場が開く前
            s := now + 24 - close_time + 1
            span := close_time - open_time
        else if(open_time <= now and now < close_time)//市場が開いている時
            s := 0
            span := now - open_time + 1
        else if(close_time <= now and now <= 24)//市場が開いた後
            s := now - close_time + 1
            span := close_time - open_time
        else
            na
    //OverDay
    else if(open_time > close_time)//市場が日を跨ぐ時
        if(open_time <= now or now < close_time)//市場が開いている時
            s := 0
            if(open_time <= now)//市場が開く前
                span := now - open_time + 1
            else//市場が開いた後
                span := now + 24 - open_time + 1//なんか引っかかる
        else
            s := now - close_time + 1
            span := close_time + 24 - open_time
    else//当てはまらない時
        na
    hi_arr = array.new_float(span, 0.0)
    lo_arr = array.new_float(span, 0.0)
    for i = 0 to span-1
        array.set(hi_arr,i,high[s+i])
        array.set(lo_arr,i,low[s+i])
    vola_hi = array.max(hi_arr)
    vola_lo = array.min(lo_arr)
    vola = vola_hi - vola_lo
//ここまでが市場ごとのボラティリティ関数
//市場ごとの平均ボラティリティ関数
ave_vola(int open_time,int close_time,int days) =>
    int s = na
    int span = na
    int now = hour
    float avola = na
    float total_high = 0.0
    float total_low = 0.0
    float total_v = na
    float vhi = na
    float vlo =na
    int k = na
    
    //開始位置
    if(now < close_time)
        s := now - close_time + 25
    else
        s := now - close_time + 1
    //期間
    if(open_time < close_time)
        span := close_time - open_time
    else
        span := close_time - open_time + 24
    //
    hi_arr = array.new_float(span, 0.0)
    lo_arr = array.new_float(span, 0.0)
    for i = 0 to days-1
        k := s + 24 * i
        for ii = 0 to span-1
            array.set(hi_arr,ii,high[k+ii])
            array.set(lo_arr,ii,low[k+ii])
        vhi := array.max(hi_arr)
        vlo := array.min(lo_arr)
        total_high := vhi + total_high
        total_low := vlo + total_low
        //total_v := vhi - vlo + total_v
    total_v := total_high - total_low
    avola := total_v / days
//市場ごとの平均ボラティリティ関数終了
//pips関数
pips_change() =>
    int acp = na
    if(syminfo.mintick == 0.001)
        acp := 100
    else if(syminfo.mintick == 0.00001)
        acp := 10000
    else
        acp := 1000000000
//pips関数終了
//日足平均ボラティリティ関数
ave_Day_v(aDays) =>
    total_hi = 0.0
    total_lo = 0.0
    dh = request.security(syminfo.ticker,"D",high)
    dl = request.security(syminfo.ticker,"D",low)
    for i = 0 to aDays-1
        voHigh = dh[i]
        voLow = dl[i]
        total_hi := voHigh + total_hi
        total_lo := voLow + total_lo
    total_vo = total_hi - total_lo
    ave_vo = total_vo / aDays
//日足平均関数終了
//日足ボラティリティ
day_vola() =>
    TodayHigh = request.security(syminfo.ticker,"D",high[0])
    TodayLow = request.security(syminfo.ticker,"D",low[0])
    TodayAverage = TodayHigh - TodayLow
//日足ボラティリティ終了

dy1 =math.round(day_vola() * pips_change(),1)
tk1 =math.round(span_vola(17,2) * pips_change(),1)
rd1 =math.round(span_vola(2,8) * pips_change(),1)
ny1 =math.round(span_vola(8,16) * pips_change(),1)

dy5 =math.round(ave_Day_v(5) * pips_change(),1)
tk5 =math.round(ave_vola(17,2,5) * pips_change(),1)
rd5 =math.round(ave_vola(2,8,5) * pips_change(),1)
ny5 =math.round(ave_vola(8,16,5) * pips_change(),1)

dy25 =math.round(ave_Day_v(25) * pips_change(),1)
tk25 =math.round(ave_vola(17,2,25) * pips_change(),1)
rd25 =math.round(ave_vola(2,8,25) * pips_change(),1)
ny25 =math.round(ave_vola(8,16,25) * pips_change(),1)

dy75 =math.round(ave_Day_v(75) * pips_change(),1)
tk75 =math.round(ave_vola(17,2,75) * pips_change(),1)
rd75 =math.round(ave_vola(2,8,75) * pips_change(),1)
ny75 =math.round(ave_vola(8,16,75) * pips_change(),1)

var testTable = table.new(position = position.top_right, columns = 5, rows = 5, bgcolor = color.yellow, border_width = 1)
if barstate.islast
    table.cell(table_id = testTable, column = 0, row = 0, text = "", bgcolor=color.teal)
    table.cell(table_id = testTable, column = 1, row = 0, text = "Day", bgcolor=color.teal)
    table.cell(table_id = testTable, column = 2, row = 0, text = "TK", bgcolor=color.teal)
    table.cell(table_id = testTable, column = 3, row = 0, text = "RD", bgcolor=color.teal)
    table.cell(table_id = testTable, column = 4, row = 0, text = "NY", bgcolor=color.teal)
    table.cell(table_id = testTable, column = 0, row = 1, text = "Today", bgcolor=color.teal)
    table.cell(table_id = testTable, column = 0, row = 2, text = "5D", bgcolor=color.teal)
    table.cell(table_id = testTable, column = 0, row = 3, text = "25D", bgcolor=color.teal)
    table.cell(table_id = testTable, column = 0, row = 4, text = "75D", bgcolor=color.teal)
    
    table.cell(table_id = testTable, column = 1, row = 1, text = str.tostring(dy1), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 2, row = 1, text = str.tostring(tk1), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 3, row = 1, text = str.tostring(rd1), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 4, row = 1, text = str.tostring(ny1), bgcolor=color.teal)
    
    table.cell(table_id = testTable, column = 1, row = 2, text = str.tostring(dy5), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 2, row = 2, text = str.tostring(tk5), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 3, row = 2, text = str.tostring(rd5), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 4, row = 2, text = str.tostring(ny5), bgcolor=color.teal)
    
    table.cell(table_id = testTable, column = 1, row = 3, text = str.tostring(dy25), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 2, row = 3, text = str.tostring(tk25), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 3, row = 3, text = str.tostring(rd25), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 4, row = 3, text = str.tostring(ny25), bgcolor=color.teal)
    
    table.cell(table_id = testTable, column = 1, row = 4, text = str.tostring(dy75), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 2, row = 4, text = str.tostring(tk75), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 3, row = 4, text = str.tostring(rd75), bgcolor=color.teal)
    table.cell(table_id = testTable, column = 4, row = 4, text = str.tostring(ny75), bgcolor=color.teal)
    
