Daily High Low Extended Boxes

cryptograndpapa · study · 414 行 · 点赞 15 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cryptograndpa
 
//@version=6
indicator("Daily High Low Extended Boxes", overlay=true, max_boxes_count=20, max_lines_count=20)
 
// =====================================================
// COMPILER OUTPUT
// Invisible plot so the script always has an output.
// =====================================================
 
plot(na, title="Internal Output", display=display.none)
 
// =====================================================
// GENERAL SETTINGS
// =====================================================
 
boxesToKeep = input.int(
     3,
     "Number of Boxes",
     minval=3,
     maxval=10,
     group="General")
 
borderWidth = input.int(
     2,
     "Border Width",
     minval=1,
     maxval=5,
     group="General")
 
showHighLow = input.bool(
     true,
     "Show High / Low Values",
     group="General")
 
showMidline = input.bool(
     true,
     "Show 50% Midlines",
     group="General")
 
// Light gray midpoint line
midlineColor = color.rgb(190, 190, 190)
 
// =====================================================
// BOX 1
// Fixed WHITE
// =====================================================
 
box1Color = color.white
 
box1Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 1 - White")
 
// =====================================================
// BOX 2
// Fixed YELLOW
// =====================================================
 
box2Color = color.yellow
 
box2Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 2 - Yellow")
 
// =====================================================
// BOX 3
// Fixed BLUE
// =====================================================
 
box3Color = color.blue
 
box3Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 3 - Blue")
 
// =====================================================
// BOX 4
// =====================================================
 
box4Color = input.color(
     color.orange,
     "Color",
     group="Box 4")
 
box4Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 4")
 
// =====================================================
// BOX 5
// =====================================================
 
box5Color = input.color(
     color.aqua,
     "Color",
     group="Box 5")
 
box5Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 5")
 
// =====================================================
// BOX 6
// =====================================================
 
box6Color = input.color(
     color.fuchsia,
     "Color",
     group="Box 6")
 
box6Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 6")
 
// =====================================================
// BOX 7
// =====================================================
 
box7Color = input.color(
     color.lime,
     "Color",
     group="Box 7")
 
box7Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 7")
 
// =====================================================
// BOX 8
// =====================================================
 
box8Color = input.color(
     color.red,
     "Color",
     group="Box 8")
 
box8Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 8")
 
// =====================================================
// BOX 9
// =====================================================
 
box9Color = input.color(
     color.purple,
     "Color",
     group="Box 9")
 
box9Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 9")
 
// =====================================================
// BOX 10
// =====================================================
 
box10Color = input.color(
     color.gray,
     "Color",
     group="Box 10")
 
box10Trans = input.int(
     85,
     "Transparency",
     minval=0,
     maxval=100,
     group="Box 10")
 
// =====================================================
// DAILY DATA
// Uses the previous completed daily candle.
// =====================================================
 
dHigh = request.security(
     syminfo.tickerid,
     "1D",
     high[1],
     lookahead=barmerge.lookahead_on)
 
dLow = request.security(
     syminfo.tickerid,
     "1D",
     low[1],
     lookahead=barmerge.lookahead_on)
 
dStart = request.security(
     syminfo.tickerid,
     "1D",
     time[1],
     lookahead=barmerge.lookahead_on)
 
dEnd = request.security(
     syminfo.tickerid,
     "1D",
     time_close[1],
     lookahead=barmerge.lookahead_on)
 
// =====================================================
// NEW DAILY PERIOD
// =====================================================
 
newDay = timeframe.change("1D")
 
// =====================================================
// STORAGE
// =====================================================
 
var array<box> dailyBoxes = array.new<box>()
var array<line> middleLines = array.new<line>()
 
// =====================================================
// BOX COLOR FUNCTION
// =====================================================
 
getBoxColor(int position) =>
    color result = color.gray
 
    if position == 1
        result := box1Color
    else if position == 2
        result := box2Color
    else if position == 3
        result := box3Color
    else if position == 4
        result := box4Color
    else if position == 5
        result := box5Color
    else if position == 6
        result := box6Color
    else if position == 7
        result := box7Color
    else if position == 8
        result := box8Color
    else if position == 9
        result := box9Color
    else if position == 10
        result := box10Color
 
    result
 
// =====================================================
// BOX TRANSPARENCY FUNCTION
// =====================================================
 
getBoxTransparency(int position) =>
    int result = 85
 
    if position == 1
        result := box1Trans
    else if position == 2
        result := box2Trans
    else if position == 3
        result := box3Trans
    else if position == 4
        result := box4Trans
    else if position == 5
        result := box5Trans
    else if position == 6
        result := box6Trans
    else if position == 7
        result := box7Trans
    else if position == 8
        result := box8Trans
    else if position == 9
        result := box9Trans
    else if position == 10
        result := box10Trans
 
    result
 
// =====================================================
// CREATE DAILY BOX
// =====================================================
 
if newDay and not na(dHigh) and not na(dLow) and not na(dStart) and not na(dEnd)
 
    // Duration of originating daily candle
    int dayLength = dEnd - dStart
 
    // Original daily candle plus two additional days
    int rightSide = dEnd + (dayLength * 2)
 
    // Exact 50% level
    float midpoint = (dHigh + dLow) / 2.0
 
    // High / Low text
    string boxText = ""
 
    if showHighLow
        boxText := "H: " + str.tostring(dHigh, format.mintick) + "\nL: " + str.tostring(dLow, format.mintick)
 
    // Create the box
    box newBox = box.new(
         left=dStart,
         top=dHigh,
         right=rightSide,
         bottom=dLow,
         xloc=xloc.bar_time,
         border_color=color.blue,
         border_width=borderWidth,
         bgcolor=color.new(color.blue, 85),
         text=boxText,
         text_color=color.blue,
         text_size=size.tiny)
 
    array.push(dailyBoxes, newBox)
 
    // Midline visibility
    color activeMidlineColor = midlineColor
 
    if not showMidline
        activeMidlineColor := color.new(midlineColor, 100)
 
    // Create 50% dashed midpoint line
    line newMidline = line.new(
         x1=dStart,
         y1=midpoint,
         x2=rightSide,
         y2=midpoint,
         xloc=xloc.bar_time,
         color=activeMidlineColor,
         style=line.style_dashed,
         width=1)
 
    array.push(middleLines, newMidline)
 
    // Delete oldest box
    if array.size(dailyBoxes) > boxesToKeep
        box oldestBox = array.shift(dailyBoxes)
        box.delete(oldestBox)
 
    // Delete matching oldest midpoint
    if array.size(middleLines) > boxesToKeep
        line oldestLine = array.shift(middleLines)
        line.delete(oldestLine)
 
// =====================================================
// UPDATE BOX APPEARANCE
// =====================================================
 
int boxCount = array.size(dailyBoxes)
 
if boxCount > 0
    for i = 0 to boxCount - 1
 
        box currentBox = array.get(dailyBoxes, i)
 
        int position = i + 1
 
        color currentColor = getBoxColor(position)
 
        int currentTransparency = getBoxTransparency(position)
 
        box.set_border_color(
             currentBox,
             currentColor)
 
        box.set_bgcolor(
             currentBox,
             color.new(currentColor, currentTransparency))
 
        box.set_text_color(
             currentBox,
             currentColor)
 
// =====================================================
// UPDATE MIDLINE VISIBILITY
// =====================================================
 
int lineCount = array.size(middleLines)
 
if lineCount > 0
    for i = 0 to lineCount - 1
 
        line currentLine = array.get(middleLines, i)
 
        if showMidline
            line.set_color(
                 currentLine,
                 midlineColor)
        else
            line.set_color(
                 currentLine,
                 color.new(midlineColor, 100))

← 返回列表