前のローソク足の高値安値に線を引くインジケータ(1M/5M/15M/1H/4H/1D)

TradingView用のメモ
金曜日→月曜日が上手くいかない

//@version=5
indicator("Prev HTF H/L segments x2 + FriDaily→Mon EOD + LineStyle", overlay=true, max_lines_count=500, max_labels_count=200)

// ===== ユーザー設定 =====
group5  = "5分足"
use5    = input.bool(true,  "5分を描画", inline="g5", group=group5)
keep5   = input.int(12,     "履歴本数", minval=0, maxval=200, group=group5)
col5H   = input.color(color.red,  "高値色",  group=group5)
col5L   = input.color(color.blue, "安値色",  group=group5)

group15 = "15分足"
use15   = input.bool(true,  "15分を描画", inline="g15", group=group15)
keep15  = input.int(12,     "履歴本数", minval=0, maxval=200, group=group15)
col15H  = input.color(color.orange, "高値色", group=group15)
col15L  = input.color(color.aqua,   "安値色", group=group15)

group60 = "1時間足"
use60   = input.bool(true,  "1時間を描画", inline="g60", group=group60)
keep60  = input.int(12,     "履歴本数", minval=0, maxval=200, group=group60)
col60H  = input.color(color.fuchsia, "高値色", group=group60)
col60L  = input.color(color.teal,    "安値色", group=group60)

group240= "4時間足"
use240  = input.bool(true,  "4時間を描画", inline="g240", group=group240)
keep240 = input.int(12,     "履歴本数", minval=0, maxval=200, group=group240)
col240H = input.color(color.purple,   "高値色", group=group240)
col240L = input.color(color.navy,     "安値色", group=group240)

group1D = "日足"
useD    = input.bool(true,  "日足を描画", inline="gD", group=group1D)
keepD   = input.int(12,     "履歴本数", minval=0, maxval=200, group=group1D)
colDH   = input.color(color.lime,     "高値色", group=group1D)
colDL   = input.color(color.maroon,   "安値色", group=group1D)

// 共通
widthActive  = input.int(2, "線の太さ(最新)", minval=1, maxval=5)
widthHist    = input.int(1, "線の太さ(履歴)", minval=1, maxval=5)
showLabels   = input.bool(false, "ラベル(最新のみ)")
gradMaxAlpha = input.int(80, "履歴の最大透明度(大きいほど薄い)", minval=0, maxval=95)

// ★ 線スタイル(全TF共通)— デフォルトは点線
styleChoice  = input.string("点線", "線スタイル(全TF共通)", options=["実線","破線","点線"])
lineStyleAll = styleChoice == "実線" ? line.style_solid :
               styleChoice == "破線" ? line.style_dashed :
                                      line.style_dotted

// ===== ユーティリティ =====
f_alpha(step, total, maxA) =>
    total <= 1 ? 0 : int(math.round(math.min(maxA, step * (maxA * 1.0 / (total - 1)))))

f_refresh(arrLines, baseColor, widthActive_, widthHist_, maxA_) =>
    sz = array.size(arrLines)
    for i = 0 to sz - 1
        l = array.get(arrLines, i)
        line.set_color(l, color.new(baseColor, f_alpha(i, sz, maxA_)))
        line.set_width(l, i == 0 ? widthActive_ : widthHist_)

f_tf_ms(tf) =>
    sec = timeframe.in_seconds(tf)
    nz(sec, tf == "D" ? 86400 : tf == "240" ? 14400 : tf == "60" ? 3600 : tf == "15" ? 900 : tf == "5" ? 300 : 300) * 1000

// 同長延長(開始〜終了+TF1本ぶん)
f_draw_segment_sameLen(pH, pL, tS, tE, isNew, tfMs, enabled, keepN, colH_, colL_, lblPrefix, arrHi, arrLo, lastHiLbl, lastLoLbl, lineStyle_) =>
    label _hiLbl = lastHiLbl
    label _loLbl = lastLoLbl
    if enabled
        if barstate.isfirst and not na(pH) and not na(pL) and not na(tS) and not na(tE)
            segEnd0 = tE + tfMs
            line hi0 = line.new(tS, pH, segEnd0, pH, xloc=xloc.bar_time, extend=extend.none, color=color.new(colH_, 0), style=lineStyle_, width=widthActive)
            line lo0 = line.new(tS, pL, segEnd0, pL, xloc=xloc.bar_time, extend=extend.none, color=color.new(colL_, 0), style=lineStyle_, width=widthActive)
            array.unshift(arrHi, hi0)
            array.unshift(arrLo, lo0)
            f_refresh(arrHi, colH_, widthActive, widthHist, gradMaxAlpha)
            f_refresh(arrLo, colL_, widthActive, widthHist, gradMaxAlpha)
        if isNew and not na(pH) and not na(pL) and not na(tS) and not na(tE)
            segEnd = tE + tfMs
            line hiSeg = line.new(tS, pH, segEnd, pH, xloc=xloc.bar_time, extend=extend.none, color=color.new(colH_, 0), style=lineStyle_, width=widthActive)
            line loSeg = line.new(tS, pL, segEnd, pL, xloc=xloc.bar_time, extend=extend.none, color=color.new(colL_, 0), style=lineStyle_, width=widthActive)
            array.unshift(arrHi, hiSeg)
            array.unshift(arrLo, loSeg)
            while array.size(arrHi) > keepN
                l = array.pop(arrHi)
                line.delete(l)
            while array.size(arrLo) > keepN
                l = array.pop(arrLo)
                line.delete(l)
            if showLabels
                if not na(_hiLbl)
                    label.delete(_hiLbl)
                if not na(_loLbl)
                    label.delete(_loLbl)
                txtH = lblPrefix + " H " + str.tostring(pH, format.mintick)
                txtL = lblPrefix + " L " + str.tostring(pL, format.mintick)
                _hiLbl := label.new(segEnd, pH, txtH, xloc=xloc.bar_time, style=label.style_label_right, textcolor=color.white, color=color.new(colH_, 0))
                _loLbl := label.new(segEnd, pL, txtL, xloc=xloc.bar_time, style=label.style_label_right, textcolor=color.white, color=color.new(colL_, 0))
            f_refresh(arrHi, colH_, widthActive, widthHist, gradMaxAlpha)
            f_refresh(arrLo, colL_, widthActive, widthHist, gradMaxAlpha)
    [_hiLbl, _loLbl]

// 固定終端で描く(主に日足の金曜→月曜クローズ用)
f_draw_segment_fixedEnd(pH, pL, tS, segEnd, isNew, enabled, keepN, colH_, colL_, lblPrefix, arrHi, arrLo, lastHiLbl, lastLoLbl, lineStyle_) =>
    label _hiLbl = lastHiLbl
    label _loLbl = lastLoLbl
    if enabled
        if barstate.isfirst and not na(pH) and not na(pL) and not na(tS) and not na(segEnd)
            line hi0 = line.new(tS, pH, segEnd, pH, xloc=xloc.bar_time, extend=extend.none, color=color.new(colH_, 0), style=lineStyle_, width=widthActive)
            line lo0 = line.new(tS, pL, segEnd, pL, xloc=xloc.bar_time, extend=extend.none, color=color.new(colL_, 0), style=lineStyle_, width=widthActive)
            array.unshift(arrHi, hi0)
            array.unshift(arrLo, lo0)
            f_refresh(arrHi, colH_, widthActive, widthHist, gradMaxAlpha)
            f_refresh(arrLo, colL_, widthActive, widthHist, gradMaxAlpha)
        if isNew and not na(pH) and not na(pL) and not na(tS) and not na(segEnd)
            line hiSeg = line.new(tS, pH, segEnd, pH, xloc=xloc.bar_time, extend=extend.none, color=color.new(colH_, 0), style=lineStyle_, width=widthActive)
            line loSeg = line.new(tS, pL, segEnd, pL, xloc=xloc.bar_time, extend=extend.none, color=color.new(colL_, 0), style=lineStyle_, width=widthActive)
            array.unshift(arrHi, hiSeg)
            array.unshift(arrLo, loSeg)
            while array.size(arrHi) > keepN
                l = array.pop(arrHi)
                line.delete(l)
            while array.size(arrLo) > keepN
                l = array.pop(arrLo)
                line.delete(l)
            if showLabels
                if not na(_hiLbl)
                    label.delete(_hiLbl)
                if not na(_loLbl)
                    label.delete(_loLbl)
                txtH = lblPrefix + " H " + str.tostring(pH, format.mintick)
                txtL = lblPrefix + " L " + str.tostring(pL, format.mintick)
                _hiLbl := label.new(segEnd, pH, txtH, xloc=xloc.bar_time, style=label.style_label_right, textcolor=color.white, color=color.new(colH_, 0))
                _loLbl := label.new(segEnd, pL, txtL, xloc=xloc.bar_time, style=label.style_label_right, textcolor=color.white, color=color.new(colL_, 0))
            f_refresh(arrHi, colH_, widthActive, widthHist, gradMaxAlpha)
            f_refresh(arrLo, colL_, widthActive, widthHist, gradMaxAlpha)
    [_hiLbl, _loLbl]

// ====== グローバル request.security(関数外で取得) ======
// 5m
p5H = request.security(syminfo.tickerid, "5",  high[1],        lookahead=barmerge.lookahead_on)
p5L = request.security(syminfo.tickerid, "5",   low[1],        lookahead=barmerge.lookahead_on)
t5S = request.security(syminfo.tickerid, "5",   time[1],       lookahead=barmerge.lookahead_on)
t5E = request.security(syminfo.tickerid, "5",   time_close[1], lookahead=barmerge.lookahead_on)
isNew5  = timeframe.change("5")
tf5Ms   = f_tf_ms("5")

// 15m
p15H = request.security(syminfo.tickerid, "15", high[1],        lookahead=barmerge.lookahead_on)
p15L = request.security(syminfo.tickerid, "15",  low[1],        lookahead=barmerge.lookahead_on)
t15S = request.security(syminfo.tickerid, "15",  time[1],       lookahead=barmerge.lookahead_on)
t15E = request.security(syminfo.tickerid, "15",  time_close[1], lookahead=barmerge.lookahead_on)
isNew15 = timeframe.change("15")
tf15Ms  = f_tf_ms("15")

// 60m
p60H = request.security(syminfo.tickerid, "60", high[1],        lookahead=barmerge.lookahead_on)
p60L = request.security(syminfo.tickerid, "60",  low[1],        lookahead=barmerge.lookahead_on)
t60S = request.security(syminfo.tickerid, "60",  time[1],       lookahead=barmerge.lookahead_on)
t60E = request.security(syminfo.tickerid, "60",  time_close[1], lookahead=barmerge.lookahead_on)
isNew60 = timeframe.change("60")
tf60Ms  = f_tf_ms("60")

// 240m
p240H = request.security(syminfo.tickerid, "240", high[1],        lookahead=barmerge.lookahead_on)
p240L = request.security(syminfo.tickerid, "240",  low[1],        lookahead=barmerge.lookahead_on)
t240S = request.security(syminfo.tickerid, "240",  time[1],       lookahead=barmerge.lookahead_on)
t240E = request.security(syminfo.tickerid, "240",  time_close[1], lookahead=barmerge.lookahead_on)
isNew240 = timeframe.change("240")
tf240Ms  = f_tf_ms("240")

// Daily(サンデーバー有無を吸収)
d0Dow   = request.security(syminfo.tickerid, "D", dayofweek,    lookahead=barmerge.lookahead_on)  // 現在日足の曜日
d0Close = request.security(syminfo.tickerid, "D", time_close,   lookahead=barmerge.lookahead_on)  // 現在日足のクローズ

d1H = request.security(syminfo.tickerid, "D", high[1],        lookahead=barmerge.lookahead_on)
d1L = request.security(syminfo.tickerid, "D",  low[1],        lookahead=barmerge.lookahead_on)
d1S = request.security(syminfo.tickerid, "D",  time[1],       lookahead=barmerge.lookahead_on)
d1E = request.security(syminfo.tickerid, "D",  time_close[1], lookahead=barmerge.lookahead_on)
d1Dow = request.security(syminfo.tickerid, "D", dayofweek[1], lookahead=barmerge.lookahead_on)

d2H = request.security(syminfo.tickerid, "D", high[2],        lookahead=barmerge.lookahead_on)
d2L = request.security(syminfo.tickerid, "D",  low[2],        lookahead=barmerge.lookahead_on)
d2S = request.security(syminfo.tickerid, "D",  time[2],       lookahead=barmerge.lookahead_on)
d2E = request.security(syminfo.tickerid, "D",  time_close[2], lookahead=barmerge.lookahead_on)
d2Dow = request.security(syminfo.tickerid, "D", dayofweek[2], lookahead=barmerge.lookahead_on)

isNewD  = timeframe.change("D")
tfDMs   = f_tf_ms("D")

// 金曜バー特定(直前が金曜 or 直前が日曜かつその前が金曜)
friIsD1 = d1Dow == dayofweek.friday
friIsD2 = (d1Dow == dayofweek.sunday and d2Dow == dayofweek.friday)
friH = friIsD1 ? d1H : friIsD2 ? d2H : na
friL = friIsD1 ? d1L : friIsD2 ? d2L : na
friS = friIsD1 ? d1S : friIsD2 ? d2S : na
// 月曜判定
isMonday = d0Dow == dayofweek.monday

// ===== 履歴配列 =====
var array<line> hist5H   = array.new_line()
var array<line> hist5L   = array.new_line()
var array<line> hist15H  = array.new_line()
var array<line> hist15L  = array.new_line()
var array<line> hist60H  = array.new_line()
var array<line> hist60L  = array.new_line()
var array<line> hist240H = array.new_line()
var array<line> hist240L = array.new_line()
var array<line> histDH   = array.new_line()
var array<line> histDL   = array.new_line()

// ===== ラベル(最新のみ)— 1行ずつ型宣言 =====
var label last5H  = na
var label last5L  = na
var label last15H = na
var label last15L = na
var label last60H = na
var label last60L = na
var label last240H = na
var label last240L = na
var label lastDH  = na
var label lastDL  = na

// ===== 描画 =====
// 5m/15m/1H/4H:開始〜終了+同長(線スタイルは lineStyleAll)
[_h5, _l5]     = f_draw_segment_sameLen(p5H,  p5L,  t5S,  t5E,  isNew5,   tf5Ms,  use5,   keep5,   col5H,   col5L,   "5m",  hist5H,   hist5L,   last5H,   last5L,   lineStyleAll)
last5H  := _h5
last5L  := _l5

[_h15, _l15]   = f_draw_segment_sameLen(p15H, p15L, t15S, t15E, isNew15, tf15Ms, use15,  keep15,  col15H,  col15L,  "15m", hist15H,  hist15L,  last15H,  last15L,  lineStyleAll)
last15H := _h15
last15L := _l15

[_h60, _l60]   = f_draw_segment_sameLen(p60H, p60L, t60S, t60E, isNew60, tf60Ms, use60,  keep60,  col60H,  col60L,  "1H",  hist60H,  hist60L,  last60H,  last60L,  lineStyleAll)
last60H := _h60
last60L := _l60

[_h240, _l240] = f_draw_segment_sameLen(p240H,p240L,t240S,t240E,isNew240,tf240Ms,use240, keep240,col240H, col240L, "4H",  hist240H, hist240L, last240H, last240L, lineStyleAll)
last240H := _h240
last240L := _l240

// 日足:金曜→月曜クローズまで固定表示(初回ロードが月曜でも描画)
var label _dH = na
var label _dL = na
if useD
    if barstate.isfirst and isMonday and not na(friH) and not na(friL) and not na(friS) and not na(d0Close)
        [_dH, _dL] = f_draw_segment_fixedEnd(friH, friL, friS, d0Close, false, true, keepD, colDH, colDL, "1D(Fri→Mon)", histDH, histDL, lastDH, lastDL, lineStyleAll)
    if isNewD
        if isMonday and not na(friH) and not na(friL) and not na(friS) and not na(d0Close)
            [_dH, _dL] = f_draw_segment_fixedEnd(friH, friL, friS, d0Close, true,  true, keepD, colDH, colDL, "1D(Fri→Mon)", histDH, histDL, lastDH, lastDL, lineStyleAll)
        else
            [_dH, _dL] = f_draw_segment_sameLen(d1H, d1L, d1S, d1E, true, tfDMs, true, keepD, colDH, colDL, "1D",          histDH, histDL, lastDH, lastDL, lineStyleAll)
lastDH := _dH
lastDL := _dL

コメント

タイトルとURLをコピーしました