Azimuth Ring

hasanaksoy199264 · study · 175 行 · 点赞 19 · TradingView 原页

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

Pine Script

//@version=6
indicator("Azimuth Ring", overlay=true, max_labels_count=500)
 
// ─────────────────────────────────────────────
// Zero point : daily open (default 03:00 Istanbul = 00:00 UTC)
// x : (price - daily open) / previous day's ATR   → position
// y : smoothed rate of change of price            → momentum
// Azimuth = angle measured clockwise from the y axis (north), atan2(x, y):
//   0–90°    above the open, bullish momentum
//   90–180°  above the open, momentum fading
//   180–270° below the open, bearish momentum
//   270–360° below the open, momentum turning up
// ─────────────────────────────────────────────
 
// ── Inputs
grpD = "Daily open"
tz         = input.string("Europe/Istanbul", "Time zone", group=grpD)
anchorHour = input.int(3, "Day start hour", minval=0, maxval=23, group=grpD,
     tooltip="For crypto: 03:00 Istanbul = 00:00 UTC. Equivalent setting: time zone UTC, hour 0.")
atrLen     = input.int(14, "Daily ATR length (previous day)", minval=1, group=grpD)
 
grpR = "Ring"
momLen    = input.int(5, "Momentum smoothing", minval=1, group=grpR)
normLen   = input.int(200, "Normalization window", minval=30, group=grpR)
minRadius = input.float(0.8, "Minimum radius (signal filter)", minval=0.0, step=0.1, group=grpR)
 
grpV = "Display"
showBands  = input.bool(true,  "±0.5 / ±1 ATR bands", group=grpV)
showMarks  = input.bool(true,  "Threshold crossing marks", group=grpV)
colorBars  = input.bool(false, "Color bars by zone", group=grpV)
showInfo   = input.bool(true,  "Info table", group=grpV)
showClock  = input.bool(true,  "Clock view (azimuth hand)", group=grpV)
handScale  = input.float(2.5, "Hand length scale (hand is full length at this radius)", minval=1.0, step=0.5, group=grpV)
clockSize  = input.float(2.0, "Clock size", minval=0.5, step=0.5, group=grpV,
     tooltip="Change the size if the clock looks distorted")
clockBg    = input.color(color.new(#2A2E39, 0), "Clock background color", group=grpV)
trailLen   = input.int(8, "Hand trail (bars)", minval=0, maxval=30, group=grpV)
 
if timeframe.in_seconds() >= 86400
    runtime.error("Does not work on daily or higher timeframes. Use a minute or hourly timeframe.")
 
// ── Function: azimuth (0–360°), measured clockwise from the y axis
// s = x component (east), c = y component (north)
azimuthDeg(float s, float c) =>
    float a = 0.0
    if c > 0
        a := math.todegrees(math.atan(s / c))
    else if c < 0
        a := math.todegrees(math.atan(s / c)) + 180.0
    else
        a := s >= 0 ? 90.0 : 270.0
    (a % 360.0 + 360.0) % 360.0
 
// ── Daily open (zero point)
int shifted = time - anchorHour * 3600000
bool newDay = dayofmonth(shifted, tz) != dayofmonth(shifted[1], tz)
 
var float dayOpen = na
if newDay or barstate.isfirst
    dayOpen := open
 
// Previous day's ATR (does not repaint)
float dATR = request.security(syminfo.tickerid, "D", ta.atr(atrLen)[1], lookahead=barmerge.lookahead_on)
 
// ── Ring coordinates
float x0 = (close - dayOpen) / dATR
float y0 = ta.ema(ta.change(close), momLen) / dATR
 
float xn = x0 / math.max(ta.stdev(x0, normLen), 1e-10)
float yn = y0 / math.max(ta.stdev(y0, normLen), 1e-10)
 
float az = azimuthDeg(xn, yn)
float radius = math.sqrt(xn * xn + yn * yn)
 
int q = na(az) ? na : az < 90 ? 0 : az < 180 ? 1 : az < 270 ? 2 : 3
int qp = q[1]
 
bool cross270 = qp == 2 and q == 3   // below the open, momentum turned up
bool cross0   = qp == 3 and q == 0   // crossed above the open, momentum positive
bool cross90  = qp == 0 and q == 1   // above the open, momentum turned down
bool cross180 = qp == 1 and q == 2   // crossed below the open, momentum negative
bool strong = radius > minRadius
 
color qColor = q == 0 ? color.green : q == 1 ? color.orange : q == 2 ? color.red : color.aqua
 
// ── Plots
plot(newDay ? na : dayOpen, "Daily open (0)", color=color.new(color.yellow, 0), linewidth=2, style=plot.style_linebr)
plot(showBands and not newDay ? dayOpen + 0.5 * dATR : na, "+0.5 ATR", color=color.new(color.gray, 60), style=plot.style_linebr)
plot(showBands and not newDay ? dayOpen - 0.5 * dATR : na, "-0.5 ATR", color=color.new(color.gray, 60), style=plot.style_linebr)
plot(showBands and not newDay ? dayOpen + 1.0 * dATR : na, "+1 ATR",   color=color.new(color.gray, 40), style=plot.style_linebr)
plot(showBands and not newDay ? dayOpen - 1.0 * dATR : na, "-1 ATR",   color=color.new(color.gray, 40), style=plot.style_linebr)
 
barcolor(colorBars ? qColor : na)
 
plotshape(showMarks and cross0   and strong, "0° crossing (trend up)",    shape.arrowup,   location.belowbar, color.lime,   size=size.small)
plotshape(showMarks and cross180 and strong, "180° crossing (trend down)", shape.arrowdown, location.abovebar, color.maroon, size=size.small)
 
// ── Alerts
alertcondition(cross270 and strong, "Azimuth 270° (bottom reversal)", "Azimuth crossed 270°: momentum turned up below the open")
alertcondition(cross0   and strong, "Azimuth 0° (trend up)",          "Azimuth crossed 0°: price crossed above the open, momentum positive")
alertcondition(cross90  and strong, "Azimuth 90° (top reversal)",     "Azimuth crossed 90°: momentum turned down above the open")
alertcondition(cross180 and strong, "Azimuth 180° (trend down)",      "Azimuth crossed 180°: price crossed below the open, momentum negative")
 
// ── Info table
var table info = table.new(position.top_right, 2, 4, border_width=1)
if showInfo and barstate.islast
    string zone = q == 0 ? "Bullish momentum" : q == 1 ? "Bullish fading" : q == 2 ? "Bearish momentum" : "Recovering"
    float openPct = (close / dayOpen - 1.0) * 100.0
    table.cell(info, 0, 0, "Azimuth", text_color=color.white, bgcolor=color.new(color.gray, 20))
    table.cell(info, 1, 0, str.tostring(az, "#.0") + "°", text_color=color.white, bgcolor=color.new(qColor, 20))
    table.cell(info, 0, 1, "Zone", text_color=color.white, bgcolor=color.new(color.gray, 20))
    table.cell(info, 1, 1, zone, text_color=color.white, bgcolor=color.new(qColor, 20))
    table.cell(info, 0, 2, "Radius", text_color=color.white, bgcolor=color.new(color.gray, 20))
    table.cell(info, 1, 2, str.tostring(radius, "#.00"), text_color=color.white, bgcolor=color.new(strong ? color.green : color.gray, 20))
    table.cell(info, 0, 3, "vs. open", text_color=color.white, bgcolor=color.new(color.gray, 20))
    table.cell(info, 1, 3, str.tostring(openPct, "#.00") + "%", text_color=color.white, bgcolor=color.new(openPct >= 0 ? color.green : color.red, 20))
 
// ── Clock view: azimuth hand
// 12 = 0°, 3 = 90°, 6 = 180°, 9 = 270° (clockwise, just like a clock)
// Hand length shows the radius (strength of the move); hand color shows the zone.
const int N = 11
const int MID = 5
const int RAD = 4
var table clock = table.new(position.bottom_right, N, N + 1, bgcolor=clockBg, frame_color=color.new(color.gray, 20), frame_width=2, border_width=0)
 
clockCell(int c, int r, string t, color k) =>
    table.cell(clock, c, r, t, text_color=k, text_size=size.small,
         width=clockSize * 0.6, height=clockSize * 1.2, text_halign=text.align_center)
 
if showClock and barstate.islast
    // empty background
    for rr = 0 to N - 1
        for cc = 0 to N - 1
            clockCell(cc, rr, " ", color.gray)
 
    // hour marks
    for k = 0 to 11
        float th = math.toradians(k * 30.0)
        int c1 = MID + math.round(RAD * math.sin(th))
        int r1 = MID - math.round(RAD * math.cos(th))
        bool major = k % 3 == 0
        string lbl = k == 0 ? "12" : k == 3 ? "3" : k == 6 ? "6" : k == 9 ? "9" : "·"
        clockCell(c1, r1, lbl, major ? color.new(color.white, 10) : color.new(color.gray, 30))
 
    // past hand tips (trail)
    for i = trailLen to 1
        float ai = az[i]
        float ri = radius[i]
        if not na(ai) and not na(ri)
            int Li = math.round(math.min(RAD, math.max(1.0, ri / handScale * RAD)))
            int ci = MID + math.round(Li * math.sin(math.toradians(ai)))
            int rri = MID - math.round(Li * math.cos(math.toradians(ai)))
            clockCell(ci, rri, "•", color.new(color.gray, 25))
 
    // center
    clockCell(MID, MID, "◉", color.new(color.white, 20))
 
    // current hand
    if not na(az) and not na(radius)
        int L = math.round(math.min(RAD, math.max(1.0, radius / handScale * RAD)))
        float thA = math.toradians(az)
        for s = 1 to L
            int ch = MID + math.round(s * math.sin(thA))
            int rh = MID - math.round(s * math.cos(thA))
            clockCell(ch, rh, s == L ? "●" : "•", qColor)
 
    // digital readout: angle and the matching clock time (0° = 12:00, 90° = 03:00, 180° = 06:00, 270° = 09:00)
    if not na(az)
        float azR = math.round(az)   // whole degrees shown on screen; the time is computed from this
        int hh = int(azR / 30.0)
        int mm = int((azR % 30.0) * 2.0)
        string clockText = str.tostring(hh == 0 ? 12 : hh, "00") + ":" + str.tostring(mm, "00")
        table.cell(clock, 0, N, str.tostring(int(azR)) + "°  ≈  " + clockText,
             text_color=qColor, text_size=size.normal, height=clockSize * 1.4, text_halign=text.align_center)
        table.merge_cells(clock, 0, N, N - 1, N)

← 返回列表