Psychological Levels

BigTimeGrampa · study · 76 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("Psychological Levels", "Psych Levels", overlay = true, max_lines_count = 50)
 
// Draws the nearest psychological (round-number) levels around price, on any pair and any timeframe.
// Auto spacing:
//   Forex: every 100 pips (JPY pairs every 1.00), with the 50s in between.
//   Gold, silver, oil, indices, crypto: one size below the price,
//   e.g. gold 2,400 -> every 100 with 50s between; Bitcoin 86,000 -> every 1,000 with 500s between.
// Custom: type your own main spacing for any instrument the auto rule does not fit.
// Bottom centre: date of the latest candle (day/month/year, GMT) and pair | timeframe.
 
// ===================== INPUTS =====================
nLvls   = input.int(5, "Number of levels", minval = 1, maxval = 20)
mode    = input.string("Auto", "Spacing", options = ["Auto", "Custom"])
custom  = input.float(50.0, "Custom main spacing (price units)", minval = 0)
halves  = input.bool(true, "Include half levels (the 50s)")
quarts  = input.bool(false, "Include quarter levels (Quarters Theory)")
lCol    = input.color(color.new(#787B86, 40), "Line colour")
lStyleS = input.string("Solid", "Line style", options = ["Solid", "Dotted", "Dashed"])
 
showStamp = input.bool(true, "Show date and pair", group = "Date and pair")
stampSize = input.string("Small", "Size", options = ["Small", "Normal", "Large"], group = "Date and pair")
stampCol  = input.color(color.new(#787B86, 0), "Colour", group = "Date and pair")
 
lStyle = lStyleS == "Dotted" ? line.style_dotted : lStyleS == "Dashed" ? line.style_dashed : line.style_solid
 
// ===================== SPACING =====================
f_unit() =>
    float p = close
    int e = int(math.floor(math.log10(p)))
    bool metal = syminfo.basecurrency == "XAU" or syminfo.basecurrency == "XAG"
    bool fx = syminfo.type == "forex" and not metal
    math.pow(10, fx ? e - 2 : e - 1)
 
// ===================== DRAW =====================
var array<line> lines = array.new<line>()
 
if barstate.islast
    while lines.size() > 0
        line.delete(lines.pop())
 
    if close > 0
        float U = mode == "Custom" and custom > 0 ? custom : f_unit()
        float g = quarts ? U / 4 : halves ? U / 2 : U
        float center = math.round(close / g) * g
        int lo = -int((nLvls - 1) / 2)
        int hi = lo + nLvls - 1
        for k = lo to hi
            float lvl = math.round_to_mintick(center + k * g)
            lines.push(line.new(bar_index - 1, lvl, bar_index, lvl, extend = extend.both, color = lCol, width = 1, style = lStyle))
 
// ===================== DATE AND PAIR (bottom centre) =====================
f_tfTxt() =>
    int m = timeframe.multiplier
    string s = ""
    if timeframe.isseconds
        s := str.tostring(m) + "S"
    else if timeframe.isminutes
        s := m % 60 == 0 ? str.tostring(int(m / 60)) + "H" : str.tostring(m) + "M"
    else if timeframe.isdaily
        s := m == 1 ? "D" : str.tostring(m) + "D"
    else if timeframe.isweekly
        s := m == 1 ? "W" : str.tostring(m) + "W"
    else if timeframe.ismonthly
        s := m == 1 ? "M" : m == 12 ? "1 YEAR" : str.tostring(m) + " MONTHS"
    s
 
stSz = stampSize == "Normal" ? size.normal : stampSize == "Large" ? size.large : size.small
var table stamp = table.new(position.bottom_center, 1, 2, border_width = 0, frame_width = 0)
 
if barstate.islast
    if showStamp
        table.cell(stamp, 0, 0, str.format_time(time, "d/M/yyyy", "GMT"), text_color = stampCol, text_size = stSz, text_halign = text.align_center)
        table.cell(stamp, 0, 1, syminfo.ticker + " | " + f_tfTxt(),        text_color = stampCol, text_size = stSz, text_halign = text.align_center)
    else
        table.clear(stamp, 0, 0, 0, 1)

← 返回列表