Custom TF High/Low

SAIKZEN · study · 93 行 · 点赞 33 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Custom TF High/Low", overlay=true, max_lines_count=50, max_labels_count=50)
 
// ==========================================
// --- Inputs ---
// ==========================================
grpG        = "Global UI Settings"
showLabels  = input.bool(true, "Master Label Switch", group=grpG)
labelOffset = input.int(2, "Label Offset (Bars Right)", minval=0, maxval=30, group=grpG, tooltip="Pushes the text labels to the right. The line will stretch to meet it.")
textSizeIn  = input.string("Small", "Label Text Size", options=["Tiny", "Small", "Normal", "Large"], group=grpG)
 
grpCust    = "Previous Custom TF Settings"
customTf   = input.timeframe("240", "Select Custom TF (Default: 4H)", group=grpCust)
 
showPch    = input.bool(true, "", tooltip="Show Line", inline="c1", group=grpCust)
showLabPch = input.bool(true, "", tooltip="Show Label", inline="c1", group=grpCust)
txtPch     = input.string("PCH", "", inline="c1", group=grpCust)
colPch     = input.color(color.new(color.purple, 0), "", inline="c1", group=grpCust)
styPch     = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="c1", group=grpCust)
widPch     = input.int(2, "", inline="c1", group=grpCust)
 
showPcl    = input.bool(true, "", tooltip="Show Line", inline="c2", group=grpCust)
showLabPcl = input.bool(true, "", tooltip="Show Label", inline="c2", group=grpCust)
txtPcl     = input.string("PCL", "", inline="c2", group=grpCust)
colPcl     = input.color(color.new(color.purple, 0), "", inline="c2", group=grpCust)
styPcl     = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="c2", group=grpCust)
widPcl     = input.int(2, "", inline="c2", group=grpCust)
 
// ==========================================
// --- Helper Functions ---
// ==========================================
f_style(s) => s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
f_size(s) => s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : s == "Large" ? size.large : size.small
labSz = f_size(textSizeIn)
 
f_draw(_l, _lb, _showLine, _showLabel, _t1, _y, _txt, _col, _sty, _wid) =>
    if _showLine and not na(_t1) and not na(_y)
        tf_ms = timeframe.in_seconds() * 1000
        end_time = (showLabels and _showLabel) ? time + tf_ms * labelOffset : time
        
        line.set_xy1(_l, _t1, _y)
        line.set_xy2(_l, end_time, _y)
        line.set_color(_l, _col)
        line.set_width(_l, _wid)
        line.set_style(_l, f_style(_sty))
        
        if showLabels and _showLabel
            label.set_xy(_lb, end_time, _y)
            label.set_text(_lb, _txt)
            label.set_textcolor(_lb, _col)
            label.set_size(_lb, labSz)
        else
            label.set_xy(_lb, na, na)
    else
        line.set_xy1(_l, na, na)
        label.set_xy(_lb, na, na)
 
// ==========================================
// --- Tracking Engine (Timestamps & Prices) ---
// ==========================================
custom_time = time(customTf)
newCustom   = not na(custom_time) and (ta.change(custom_time) != 0 or barstate.isfirst)
 
var float cur_c_high = na, var int cur_c_high_time = na
var float cur_c_low  = na, var int cur_c_low_time  = na
var float pc_high    = na, var int pc_high_time    = na
var float pc_low     = na, var int pc_low_time     = na
 
if newCustom
    pc_high    := cur_c_high, pc_high_time    := cur_c_high_time
    pc_low     := cur_c_low,  pc_low_time     := cur_c_low_time
    cur_c_high := high,       cur_c_high_time := time
    cur_c_low  := low,        cur_c_low_time  := time
else
    if high > cur_c_high or na(cur_c_high)
        cur_c_high      := high
        cur_c_high_time := time
    if low < cur_c_low or na(cur_c_low)
        cur_c_low       := low
        cur_c_low_time  := time
 
int safe_pch_t1 = not na(pc_high_time) ? pc_high_time : time
int safe_pcl_t1 = not na(pc_low_time)  ? pc_low_time  : time
 
// ==========================================
// --- Render Initialization & Updates ---
// ==========================================
var line pchLine = line.new(na, na, na, na, xloc=xloc.bar_time), var label pchLab = label.new(na, na, "", xloc=xloc.bar_time, color=color.new(color.white, 100), style=label.style_label_left)
var line pclLine = line.new(na, na, na, na, xloc=xloc.bar_time), var label pclLab = label.new(na, na, "", xloc=xloc.bar_time, color=color.new(color.white, 100), style=label.style_label_left)
 
if barstate.islast
    f_draw(pchLine, pchLab, showPch, showLabPch, safe_pch_t1, pc_high, txtPch, colPch, styPch, widPch)
    f_draw(pclLine, pclLab, showPcl, showLabPcl, safe_pcl_t1, pc_low,  txtPcl, colPcl, styPcl, widPcl)

← 返回列表