YATR | Previous Range From Daily Open

yehielhu · study · 126 行 · 点赞 43 · TradingView 原页

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

Pine Script

//@version=6
indicator("YATR | Previous Range From Daily Open", overlay=true, max_lines_count=500, max_labels_count=500)
 
// Settings
stepPct    = input.float(0.5, "Step (%)", minval=0.1, step=0.1)
showOpen   = input.bool(true, "Show daily open")
showPrices = input.bool(true, "Show prices")
upColor    = input.color(color.lime, "Upper levels")
downColor  = input.color(color.red, "Lower levels")
maxColor   = input.color(color.orange, "MAXIMUM ATR")
openColor  = input.color(color.aqua, "Daily open")
 
if not timeframe.isintraday
    runtime.error("Use an intraday chart, such as 5m or 15m.")
 
// Use standard market prices.
source = ticker.standard(syminfo.tickerid)
 
// PREVIOUS trading day's high and low.
// Example: Thursday's range for Friday's levels.
[previousHigh, previousLow] = request.security(
     source, "1D", [high[1], low[1]],
     lookahead=barmerge.lookahead_on)
 
// CURRENT trading day's opening price.
// Example: Friday's opening price is the zero point.
[dayOpen, dayStart, dayEnd] = request.security(
     source, "1D", [open, time, time_close],
     lookahead=barmerge.lookahead_on)
 
// Previous day's high-low percentage.
// This is a single day's range, not an averaged ATR.
rangePct = not na(previousLow) and previousLow > 0 ?
     (previousHigh - previousLow) / previousLow * 100.0 : na
 
var array<line> levels = array.new<line>()
var array<label> tags = array.new<label>()
var table info = table.new(position.top_right, 1, 2)
 
// Draw a level relative to CURRENT day's open.
drawLevel(float pct, bool isMaximum) =>
    price = math.round_to_mintick(dayOpen * (1.0 + pct / 100.0))
 
    ink = isMaximum ? maxColor :
         pct > 0 ? upColor :
         pct < 0 ? downColor : openColor
 
    prefix = isMaximum ? "MAXIMUM ATR | " :
         pct == 0 ? "DAILY OPEN | " : "YATR | "
 
    pctText = (pct > 0 ? "+" : "") +
         str.tostring(pct, "0.##") + "%"
 
    caption = prefix + pctText +
         (showPrices ? " | " + str.tostring(price, format.mintick) : "")
 
    ln = line.new(
         x1=dayStart,
         y1=price,
         x2=dayEnd,
         y2=price,
         xloc=xloc.bar_time,
         color=ink,
         width=isMaximum or pct == 0 ? 2 : 1,
         style=isMaximum or pct == 0 ?
             line.style_solid : line.style_dashed)
 
    tag = label.new(
         x=time,
         y=price,
         text=caption,
         xloc=xloc.bar_time,
         style=label.style_label_left,
         color=color.new(ink, 85),
         textcolor=ink,
         size=size.small)
 
    array.push(levels, ln)
    array.push(tags, tag)
 
// Show levels for the latest trading day in the chart.
if barstate.islast
    while array.size(levels) > 0
        line.delete(array.pop(levels))
 
    while array.size(tags) > 0
        label.delete(array.pop(tags))
 
    if not na(rangePct) and not na(dayOpen) and not na(dayEnd)
        fullSteps = int(math.floor(rangePct / stepPct))
 
        if fullSteps > 240
            runtime.error("Too many levels. Increase Step (%) in settings.")
 
        if showOpen
            drawLevel(0.0, false)
 
        if fullSteps > 0
            for i = 1 to fullSteps
                pct = i * stepPct
 
                // The exact maximum is drawn separately.
                if pct < rangePct - 0.00000001
                    drawLevel(pct, false)
                    drawLevel(-pct, false)
 
        if rangePct > 0
            drawLevel(rangePct, true)
            drawLevel(-rangePct, true)
 
        table.cell(
             info, 0, 0,
             "Previous day range: " + str.tostring(rangePct, "0.##") + "%",
             text_color=maxColor,
             bgcolor=color.new(color.black, 15))
 
        table.cell(
             info, 0, 1,
             "Today's open (0%): " + str.tostring(dayOpen, format.mintick),
             text_color=openColor,
             bgcolor=color.new(color.black, 15))
    else
        table.cell(
             info, 0, 0,
             "Previous daily data unavailable",
             text_color=color.orange)

← 返回列表