見出し画像

📈インジケーター(ボラ翌日窓・分析)を追加する

//@version=5
// max_labels_count=500 を追加することで、過去のラベルが消える問題を解決します
indicator("ボラ翌日窓・分析", overlay=true, max_labels_count=500)

// --- 日足判定チェック ---
is_daily = timeframe.isdaily and not timeframe.isintraday

// --- 設定項目 ---
grp_rank = "ランク判定(前日の騰落率基準)"
p_large = input.float(8.0,  "【大】前日 +%以上", group=grp_rank)
p_mid   = input.float(3.0,  "【中】前日 +%以上", group=grp_rank)
m_mid   = input.float(-3.0, "【中】前日 -%以下", group=grp_rank)
m_large = input.float(-8.0, "【大】前日 -%以下", group=grp_rank)

grp_stat = "統計設定"
lookback_len = input.int(201, "統計期間 (日数)", minval=1, group=grp_stat)

// --- 色の定義 ---
C_CYAN = #00E5FF, C_PINK = #FFC0CB, C_GOLD = #FFD700
C_WHITE = color.white, C_RED = color.red, C_BLACK = color.black

// 統計用配列
var float[] gains  = array.new_float(12, 0.0)
var int[]   counts = array.new_int(12, 0)

// 前日騰落率の計算
prev_change = is_daily ? nz((close[1] - close[2]) / close[2] * 100) : 0.0
string r_txt = ""
int r_idx = 0

// --- ランク判定 ---
if is_daily
    if prev_change >= 0
        if prev_change >= p_large
            r_txt := "+大(" + str.tostring(p_large) + "%)", r_idx := 0
        else if prev_change >= p_mid
            r_txt := "+中(" + str.tostring(p_mid) + "%)", r_idx := 1
        else
            r_txt := "+小", r_idx := 2
    else
        if prev_change <= m_large
            r_txt := "-大(" + str.tostring(math.abs(m_large)) + "%)", r_idx := 5
        else if prev_change <= m_mid
            r_txt := "-中(" + str.tostring(math.abs(m_mid)) + "%)", r_idx := 4
        else
            r_txt := "-小", r_idx := 3

// 窓判定
is_gap_up = is_daily and (open >= close[1])
color r_col = is_gap_up ? C_CYAN : C_PINK
int final_idx = is_gap_up ? r_idx : r_idx + 6

// --- 市場ステータス判定 ---
var int t1 = na
var int t2 = na
if ta.change(time("D")) != 0
    t1 := time
    t2 := int(time_close("D"))
is_active = timenow >= t1 and timenow < t2 and not na(open)

// --- ラベル描画 ---
if is_daily and bar_index > 2
    label.new(bar_index, is_gap_up ? high : low, text=r_txt, 
         color=r_col, textcolor=C_BLACK, 
         style=is_gap_up ? label.style_label_down : label.style_label_up, 
         yloc=is_gap_up ? yloc.abovebar : yloc.belowbar, size=size.small)

// --- 統計計算(設定した期間で計算) ---
if is_daily and (last_bar_index - bar_index) < lookback_len and barstate.isconfirmed
    float today_ret = ((close - open) / open) * 100
    array.set(counts, final_idx, array.get(counts, final_idx) + 1)
    array.set(gains, final_idx, array.get(gains, final_idx) + today_ret)

// --- テーブル描画 ---
var table tab = table.new(position.top_right, 3, 22, bgcolor=#111111, border_width=1, border_color=#444444)

if is_daily and barstate.islast
    table.cell(tab, 0, 0, "分析設定(%) 大:" + str.tostring(p_large) + " / 中:" + str.tostring(p_mid), text_color=C_GOLD, bgcolor=#333333, text_size=size.small)
    table.merge_cells(tab, 0, 0, 2, 0)

    table.cell(tab, 0, 1, "前日ランク", text_color=C_WHITE, bgcolor=#333333, text_size=size.tiny)
    table.cell(tab, 1, 1, "回数", text_color=C_WHITE, bgcolor=#333333, text_size=size.tiny)
    table.cell(tab, 2, 1, "平均利益", text_color=C_WHITE, bgcolor=#333333, text_size=size.tiny)
    
    lbls = array.from("+大", "+中", "+小", "-小", "-中", "-大")
    for i = 0 to 5
        for g = 0 to 1
            idx = g == 0 ? i : i + 6
            row = g == 0 ? i + 2 : i + 10
            if i == 0
                table.cell(tab, 0, g == 0 ? 1 : 9, g == 0 ? "【窓上】" : "【窓下】", text_color=g == 0 ? C_CYAN : C_PINK, text_halign=text.align_left, text_size=size.tiny)
            c = array.get(counts, idx), v = c > 0 ? array.get(gains, idx) / c : 0
            table.cell(tab, 0, row, array.get(lbls, i), text_color=i<=2?C_CYAN:C_GOLD, text_halign=text.align_left, text_size=size.tiny)
            table.cell(tab, 1, row, c > 0 ? str.tostring(c) : "-", text_color=#FFFF00, text_size=size.tiny)
            table.cell(tab, 2, row, c > 0 ? str.tostring(v, "#.##") + "%" : "-", text_color=(v >= 0 ? C_WHITE : C_RED), text_size=size.tiny)

    // --- 現在の条件(5行構成:size.small適用) ---
    table.cell(tab, 0, 17, "現在の条件:", text_color=C_WHITE, text_size=size.tiny)
    
    table.cell(tab, 0, 18, is_active ? r_txt + (is_gap_up ? " 窓上" : " 窓下") : "市場待機", bgcolor=is_active ? r_col : #333333, text_color=is_active?C_BLACK:C_WHITE, text_size=size.small)
    cur_avg = array.get(counts, final_idx) > 0 ? array.get(gains, final_idx) / array.get(counts, final_idx) : 0
    table.cell(tab, 1, 18, is_active ? str.tostring(array.get(counts, final_idx)) : "-", bgcolor=is_active ? r_col : #333333, text_color=is_active?C_BLACK:C_WHITE, text_size=size.small)
    table.cell(tab, 2, 18, is_active ? str.tostring(cur_avg, "#.##") + "%" : "-", bgcolor=is_active ? r_col : #333333, text_color=is_active?(cur_avg>=0?C_BLACK:C_RED):C_WHITE, text_size=size.small)
    
    table.cell(tab, 0, 19, "予測終値:", text_color=C_WHITE, text_size=size.tiny)
    table.cell(tab, 1, 19, is_active ? str.tostring(open * (1 + (cur_avg / 100)), "#.##") : "-", text_color=C_GOLD, text_size=size.small, text_halign=text.align_right)
    table.merge_cells(tab, 1, 19, 2, 19)

    table.cell(tab, 0, 20, "統計期間: 直近" + str.tostring(lookback_len) + "日間", text_color=C_WHITE, text_size=size.tiny)
    table.merge_cells(tab, 0, 20, 2, 20)
    
    // 5行目:空のセル(指示の5行構成を維持するためのスペーサー)
    table.cell(tab, 0, 21, "", text_size=size.tiny)
    table.merge_cells(tab, 0, 21, 2, 21)

😎暴落した翌日戻す期待値が高いか
調べてたくて作ったのだが駄目だった

現在は集計の条件を変えてテスト中。
期待できるの出来るかな?

いいなと思ったら応援しよう!

スーパーうまン🐎📈 応援あれば、とっても嬉しいです😁