UIKit

jabez4jc · library · 149 行 · 点赞 0 · 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/
// © jabez4jc
 
//@version=6
 
// @description Chart UI helpers: a dark / light table colour theme that can follow the chart background, a table
//              factory with one shared frame style, settings-string to position conversion, and a vertical gauge
//              drawn beside the last bar for oscillators.
library("UIKit", overlay = true)
 
//#region ———————————————————— Table theme
 
// @type Table colours for one panel shade, returned by `theme()`.
// @field dark The panel is dark.
// @field textSize Table text size.
// @field up Bullish / positive text.
// @field down Bearish / negative text.
// @field warn Warning / highlight text.
// @field neutral Neutral text.
// @field aqua Accent text.
// @field headerBg Header row background.
// @field bodyBg Body row background.
// @field footerBg Footer / title row background.
// @field accentBg Accent cell background.
// @field centerBg Highlighted center cell background.
// @field longBg Long-trade cell background.
// @field shortBg Short-trade cell background.
export type Theme
    bool   dark
    string textSize
    color  up
    color  down
    color  warn
    color  neutral
    color  aqua
    color  headerBg
    color  bodyBg
    color  footerBg
    color  accentBg
    color  centerBg
    color  longBg
    color  shortBg
 
// @function Table colour theme.
// @param mode (series string) "Auto" (dark when the chart background is dark), "Dark" or "Light".
// @param textSize (series string) "Small", "Normal" or "Large". Optional. Default is "Normal".
// @returns (Theme) The theme.
export theme(string mode, string textSize = "Normal") =>
    float luma = color.r(chart.bg_color) * 299 + color.g(chart.bg_color) * 587 + color.b(chart.bg_color) * 114
    bool  d    = mode == "Auto" ? luma < 140000 : mode == "Dark"
    Theme.new(d, textSize == "Large" ? size.large : textSize == "Normal" ? size.normal : size.small,
              d ? color.rgb(140, 255, 150)       : color.rgb(0, 115, 0),
              d ? color.rgb(255, 145, 145)       : color.rgb(150, 0, 0),
              d ? color.rgb(255, 214, 120)       : color.rgb(140, 92, 0),
              d ? color.rgb(240, 240, 240)       : color.rgb(30, 30, 30),
              d ? color.rgb(145, 236, 255)       : color.rgb(0, 95, 120),
              d ? color.new(color.black, 2)      : color.rgb(236, 239, 244, 6),
              d ? color.new(color.black, 16)     : color.rgb(244, 246, 250, 10),
              d ? color.new(color.black, 6)      : color.rgb(230, 234, 240, 6),
              d ? color.new(color.gray, 74)      : color.rgb(214, 220, 228, 4),
              d ? color.new(color.yellow, 82)    : color.rgb(245, 213, 110, 16),
              d ? color.new(color.green, 20)     : color.rgb(112, 203, 125, 18),
              d ? color.new(color.red, 20)       : color.rgb(214, 112, 112, 18))
 
// @function Converts a settings string such as "Top Left" or "Middle Center" to a `position.*` value.
// @param pos (series string) "Top|Middle|Bottom" + " " + "Left|Center|Right". Anything else is bottom right.
// @returns (string) The position.
export toPosition(string pos) =>
    switch pos
        "Top Left"      => position.top_left
        "Top Center"    => position.top_center
        "Top Right"     => position.top_right
        "Middle Left"   => position.middle_left
        "Middle Center" => position.middle_center
        "Middle Right"  => position.middle_right
        "Bottom Left"   => position.bottom_left
        "Bottom Center" => position.bottom_center
        =>                 position.bottom_right
 
// @function Creates a table with the shared frame style (black background, silver frame and borders). Declare the
//           result with `var` so the table is created once.
// @param pos (series string) A position string, as for `toPosition()`.
// @param columns (series int) Number of columns.
// @param rows (series int) Number of rows.
// @returns (table) The new table.
export newTable(string pos, int columns, int rows) =>
    table.new(toPosition(pos), columns, rows, bgcolor = color.new(color.black, 0), frame_color = color.new(color.silver, 25),
              frame_width = 1, border_color = color.new(color.silver, 35), border_width = 1)
 
//#endregion
 
//#region ———————————————————— Gauge
 
// @function Draws a vertical gauge `offset` bars right of the last bar: a range arrow from `bottom` to `top`, a tick at
//           `basePct`, a marker at `valuePct`, the value text under the range and the name above it. Draws on the last
//           bar only and replaces its own previous drawings, so call it on every bar (each call site keeps its own
//           drawings).
// @param offset (series int) Bars right of the last bar.
// @param top (series float) Price at 100%.
// @param bottom (series float) Price at 0%.
// @param valuePct (series float) Marker position, 0-100 of the range (clamped).
// @param marker (series string) Marker text, e.g. "◈".
// @param valueCol (series color) Colour of the marker and the value text.
// @param valueText (series string) Value text shown under the range.
// @param name (series string) Name shown above the range.
// @param rangeCol (series color) Colour of the range arrow and the tick.
// @param basePct (series float) Tick position, 0-100 of the range. Optional. Default is 50.
// @returns (void)
export gauge(int offset, float top, float bottom, float valuePct, string marker, color valueCol, string valueText, string name, color rangeCol, float basePct = 50) =>
    var line  tick = na
    var line  rng  = na
    var label dot  = na
    var label val  = na
    var label ttl  = na
    if barstate.islast
        tick.delete()
        rng.delete()
        dot.delete()
        val.delete()
        ttl.delete()
        float step = (top - bottom) / 100
        int   x    = bar_index + offset
        tick := line.new(x - 1, bottom + step * basePct, x + 1, bottom + step * basePct, color = rangeCol)
        rng  := line.new(x, top, x, bottom, color = rangeCol, style = line.style_arrow_both)
        dot  := label.new(x, bottom + step * math.max(0, math.min(100, valuePct)), marker, color = color(na), style = label.style_label_center, textcolor = valueCol, size = size.large)
        val  := label.new(x, bottom - close * 0.0001, valueText, color = color(na), style = label.style_label_up, textcolor = valueCol, size = size.normal)
        ttl  := label.new(x, top, name, color = color(na), style = label.style_label_down, textcolor = chart.fg_color, size = size.normal)
 
//#endregion
 
//#region ———————————————————— Demo
 
// A themed table, using toPosition() for the corner
Theme demoTheme = theme("Auto", "Normal")
float demoRsi   = ta.rsi(close, 14)
var table demoTbl = table.new(toPosition("Top Right"), 2, 1)
if barstate.islast
    demoTbl.cell(0, 0, "RSI", text_color = demoTheme.neutral, text_size = demoTheme.textSize, bgcolor = demoTheme.headerBg)
    demoTbl.cell(1, 0, str.tostring(demoRsi, "#.#"), text_color = demoRsi > 50 ? demoTheme.up : demoTheme.down, text_size = demoTheme.textSize, bgcolor = demoTheme.bodyBg)
 
// The same with newTable(), which adds the shared frame style
var table demoFramed = newTable("Bottom Right", 1, 1)
if barstate.islast
    demoFramed.cell(0, 0, "newTable()", text_color = demoTheme.warn, text_size = demoTheme.textSize, bgcolor = demoTheme.footerBg)
 
// RSI as a gauge against the 20-bar range
gauge(5, ta.highest(20), ta.lowest(20), demoRsi, "◈", demoRsi > 50 ? color.green : color.red, str.tostring(demoRsi, "#.#"), "RSI", color.gray)
 
//#endregion

← 返回列表