Ichimoku 3D

mm_mitsuya · study · 750 行 · 点赞 24 · TradingView 原页

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

Pine Script

//@version=6
indicator("Ichimoku 3D", shorttitle = "Ichimoku 3D", overlay = false, max_bars_back = 600,
     max_lines_count = 500, max_polylines_count = 100, max_labels_count = 50, max_boxes_count = 500)
 
// ════════ 入力 ════════
int   lenT    = input.int(9,  "Tenkan-sen length",  group = "Ichimoku")
int   lenK    = input.int(26, "Kijun-sen length",   group = "Ichimoku")
int   lenB    = input.int(52, "Senkou Span B length", group = "Ichimoku")
int   disp    = input.int(26, "Displacement",       group = "Ichimoku")
 
int   nBars   = input.int(60,  "Bars shown in 3D",   minval = 30, maxval = 300, group = "3D view")
int   widthB  = input.int(400, "Scene width (bars; wider = cleaner prisms)", minval = 60, maxval = 480, group = "3D view")
float yawIn   = input.float(20, "Yaw ° (+ right side near / - left side near)", step = 5, group = "3D view")
float pitchIn = input.float(30,  "Pitch ° (+ from above / - from below)", minval = -80, maxval = 80, step = 5, group = "3D view")
float camD    = input.float(10.0, "Camera distance (smaller = stronger perspective)", minval = 2.0, group = "3D view")
string preset = input.string("Dramatic", "View preset (all except Custom override Yaw/Pitch/Distance)",
     options = ["Dramatic", "Top-down", "From below", "Front", "Custom"], group = "3D view")
float airFade = input.float(30, "Aerial perspective (fade far candles, 0-60)", minval = 0, maxval = 60, step = 5, group = "3D view")
float cloudD  = input.float(0.45, "Cloud depth", minval = 0.1, step = 0.05, group = "3D view")
bool  showTen = input.bool(true,  "Show Tenkan-sen", group = "Lines")
bool  showKij = input.bool(true,  "Show Kijun-sen",  group = "Lines")
bool  showChi = input.bool(true,  "Show Chikou Span", group = "Lines")
bool  showTK  = input.bool(true,  "Show Tenkan/Kijun cross spheres", group = "Crosses")
int   tkDiam  = input.int(10, "Sphere diameter (bars)", minval = 4, maxval = 30, group = "Crosses")
float tkAsp   = input.float(1.8, "Screen aspect ratio (adjust if spheres look oval)", minval = 0.5, maxval = 4.0, step = 0.1, group = "Crosses")
color cGC     = input.color(color.rgb(255, 196, 40),  "Bullish cross (Tenkan crosses above)", group = "Crosses")
color cDC     = input.color(color.rgb(170, 90, 255),  "Bearish cross (Tenkan crosses below)", group = "Crosses")
int   tkMax   = input.int(6, "Number of crosses (newest first; many may hit drawing limits)", minval = 1, maxval = 12, group = "Crosses")
int   tubeW   = input.int(2, "Line thickness", minval = 1, maxval = 8, group = "Lines")
float ribGap  = input.float(0.0, "Depth gap between lines (0 = same plane, exact heights)", minval = 0.0, step = 0.05, group = "Lines")
string priceSt = input.string("3D candles", "Price display", options = ["3D candles", "Close line"], group = "Candles")
float bodyR   = input.float(0.8, "Body width (ratio of bar spacing)", minval = 0.2, maxval = 0.95, step = 0.05, group = "Candles")
float depthR  = input.float(1.0,  "Body depth (ratio of width; 1 = square prism)", minval = 0.2, maxval = 3.0, step = 0.1, group = "Candles")
color cUp     = input.color(color.rgb(38, 166, 154), "Up candle",   group = "Candles")
color cDn     = input.color(color.rgb(239, 83, 80),  "Down candle", group = "Candles")
string sway   = input.string("Off", "Auto sway (on each tick)", options = ["Off", "Horizontal", "Vertical"], group = "3D view")
float swayAmp = input.float(15,  "Sway amplitude °", group = "3D view")
 
color cBull = input.color(color.rgb(38, 166, 154),  "Bullish cloud", group = "Colors")
color cBear = input.color(color.rgb(239, 83, 80),   "Bearish cloud", group = "Colors")
color cTen  = input.color(color.rgb(41, 98, 255),   "Tenkan-sen",    group = "Colors")
color cKij  = input.color(color.rgb(255, 152, 0),   "Kijun-sen",     group = "Colors")
color cCls  = input.color(color.rgb(230, 230, 230), "Close",         group = "Colors")
color cChi  = input.color(color.rgb(186, 104, 200), "Chikou Span",   group = "Colors")
 
// ════════ 一目均衡表 ════════
donchian(int len) => math.avg(ta.lowest(low, len), ta.highest(high, len))
float tenkan = donchian(lenT)
float kijun  = donchian(lenK)
float spanA  = math.avg(tenkan, kijun)
float spanB  = donchian(lenB)
int   shift  = disp - 1
 
int   rangeLen = nBars + lenB + disp
float hiR   = ta.highest(high, rangeLen)
float loR   = ta.lowest(low, rangeLen)
float midP  = (hiR + loR) / 2
float halfP = math.max((hiR - loR) / 2, syminfo.mintick)
 
var float scTop = na   // 描画全体が収まる縦軸の上端/下端(最終バーで計算)
var float scBot = na
 
// ════════ 3Dエンジン ════════
type Cam
    float cyaw
    float syaw
    float cpit
    float spit
    float d
    float cx
    float ux
    float mid
    float uy
 
method depthOf(Cam c, float x, float y, float z) =>
    float z1 = -x * c.syaw + z * c.cyaw
    -y * c.spit + z1 * c.cpit
 
method pt(Cam c, float x, float y, float z) =>
    float x1 = x * c.cyaw + z * c.syaw
    float z1 = -x * c.syaw + z * c.cyaw
    float y2 = y * c.cpit + z1 * c.spit
    float z2 = -y * c.spit + z1 * c.cpit
    float f  = c.d / (c.d + z2)
    chart.point.from_index(int(math.round(c.cx + x1 * f * c.ux)), c.mid + y2 * f * c.uy)
 
// 画面座標(丸めない bar_index と価格)
method scr(Cam c, float x, float y, float z) =>
    float x1 = x * c.cyaw + z * c.syaw
    float z1 = -x * c.syaw + z * c.cyaw
    float y2 = y * c.cpit + z1 * c.spit
    float z2 = -y * c.spit + z1 * c.cpit
    float f  = c.d / (c.d + z2)
    [c.cx + x1 * f * c.ux, c.mid + y2 * f * c.uy]
 
wx(int k, int m, float xw) => -xw + 2.0 * xw * k / (m - 1)
 
// 色を白(t>0)または黒(t<0)へ寄せる
tint(color col, float t) =>
    float r = color.r(col)
    float g = color.g(col)
    float b = color.b(col)
    t >= 0 ? color.rgb(r + (255 - r) * t, g + (255 - g) * t, b + (255 - b) * t) : color.rgb(r * (1 + t), g * (1 + t), b * (1 + t))
 
shade(color col, float f) =>
    color.rgb(math.min(255, color.r(col) * f), math.min(255, color.g(col) * f), math.min(255, color.b(col) * f))
 
// 雲:2本の辺(値e1@z1, 値e2@z2)で挟まれた帯を陽/陰ごとに塗る
drawBand(Cam c, array<float> e1, float z1, array<float> e2, float z2, array<bool> bull,
         int tr, color up, color dn, array<polyline> pls, int m, float xw, float mid, float half, bool outline = true, int lim = 90) =>
    int k0 = 0
    for k = 1 to m - 1
        bool b0 = bull.get(k0)
        if (bull.get(k) != b0 or k == m - 1) and pls.size() < lim
            array<chart.point> pts = array.new<chart.point>()
            for j = k0 to k
                pts.push(c.pt(wx(j, m, xw), (e1.get(j) - mid) / half, z1))
            for j = k to k0
                pts.push(c.pt(wx(j, m, xw), (e2.get(j) - mid) / half, z2))
            color col = b0 ? up : dn
            pls.push(polyline.new(pts, false, true, xloc.bar_index,
                     outline ? color.new(col, math.max(0, tr - 35)) : color.new(col, 100), color.new(col, tr)))
            k0 := k
    true
 
// 雲の天面(奥行き方向に伸びた帯)。1枚の多角形で描くと、雲が急に上がる所で
// 手前の辺と奥の辺が交差して塗りが抜けるので、「交差しない区間」ごとに分けて塗る
drawCap(Cam c, array<float> cap, float z1, float z2, array<bool> bull, int tr, color up, color dn,
        array<polyline> pls, int m, float xw, float mid, float half, int lim = 90) =>
    array<chart.point> pF = array.new<chart.point>()
    array<chart.point> pB = array.new<chart.point>()
    for k = 0 to m - 1
        float y = (cap.get(k) - mid) / half
        pF.push(c.pt(wx(k, m, xw), y, z1))
        pB.push(c.pt(wx(k, m, xw), y, z2))
    // 奥行き方向の向き(画面上)
    float dx = pB.get(0).index - pF.get(0).index
    float dy = pB.get(0).price - pF.get(0).price
    int k0  = 0
    int sg0 = 0
    for k = 1 to m - 1
        float vx = pF.get(k).index - pF.get(k - 1).index
        float vy = pF.get(k).price - pF.get(k - 1).price
        int   sg = dx * vy - dy * vx >= 0 ? 1 : -1      // 区間 k-1→k が奥行き方向のどちら側に曲がるか
        if k == 1
            sg0 := sg
        // 向きか色が変わったら、ひとつ前の点までを1枚として塗る
        if (sg != sg0 or bull.get(k) != bull.get(k0)) and k - 1 > k0 and pls.size() < lim
            array<chart.point> pts = array.new<chart.point>()
            for j = k0 to k - 1
                pts.push(pF.get(j))
            for j = k - 1 to k0
                pts.push(pB.get(j))
            color col = bull.get(k0) ? up : dn
            pls.push(polyline.new(pts, false, true, xloc.bar_index, color.new(col, 100), color.new(col, tr)))
            k0 := k - 1
        else if sg != sg0 or bull.get(k) != bull.get(k0)
            k0 := k - 1
        sg0 := sg
        if k == m - 1 and k > k0 and pls.size() < lim
            array<chart.point> pts = array.new<chart.point>()
            for j = k0 to k
                pts.push(pF.get(j))
            for j = k to k0
                pts.push(pB.get(j))
            color col = bull.get(k) ? up : dn
            pls.push(polyline.new(pts, false, true, xloc.bar_index, color.new(col, 100), color.new(col, tr)))
    true
 
// リボン:天面(明るい)+ 前面の厚み(暗い)
drawRibbon(Cam c, array<float> v, float zF, float zB, float th, color col,
           array<polyline> pls, int m, float xw, float mid, float half) =>
    int n = v.size()
    array<chart.point> top  = array.new<chart.point>()
    array<chart.point> side = array.new<chart.point>()
    for j = 0 to n - 1
        float y = (v.get(j) - mid) / half
        top.push(c.pt(wx(j, m, xw), y, zF))
        side.push(c.pt(wx(j, m, xw), y, zF))
    for j = n - 1 to 0
        float y = (v.get(j) - mid) / half
        top.push(c.pt(wx(j, m, xw), y, zB))
        side.push(c.pt(wx(j, m, xw), y - th, zF))
    pls.push(polyline.new(top, false, true, xloc.bar_index, shade(col, 1.25), color.new(shade(col, 1.1), 15)))
    if th > 0
        pls.push(polyline.new(side, false, true, xloc.bar_index, shade(col, 0.45), shade(col, 0.55)))
    top.first()
 
// 雲に対する位置: 1=雲の上 / 0=雲の中 / -1=雲の下
cloudState(float y, float a, float b) => y > math.max(a, b) ? 1 : y < math.min(a, b) ? -1 : 0
 
// 隠れ線の入れ物(キャビネット図のように、隠れた部分は最後に破線で描く)
type Seg
    array<chart.point> pts
    color col
 
// 視線が雲(直方体)にさえぎられる高さの範囲 [lo, hi] を返す
//   上から見る → 雲の中 + 雲の少し下(視線が雲の底面に当たる範囲)
//   下から見る → 雲の中 + 雲の少し上
hidRange(float yCB, float yCT, float z, float zNear, float camY, float camZ, bool fromAbove) =>
    float tz = math.max(0.0, math.min(0.95, (zNear - z) / (camZ - z)))
    float lo = fromAbove ? (yCB - tz * camY) / (1 - tz) : yCB
    float hi = fromAbove ? yCT : (yCT - tz * camY) / (1 - tz)
    [lo, hi]
 
// ── 隠れ判定(画面上で「後から描く雲の面」に覆われるかどうか) ──
// 雲の手前側の面(前面 + 天面の手前半分)を区間ごとの四角形として持っておき、
// 点がそのどれかの内側に入っていれば「隠れている」とみなす。
type Occ
    array<float> qx      // 四角形の頂点(1区間 8点 = 前面4点 + 天面4点)
    array<float> qy
    array<float> qmin    // 区間ごとの画面x範囲(高速化用)
    array<float> qmax
    int n
 
buildOcc(Cam c, array<float> aV, array<float> bV, array<float> cap, float zNear, float zSplit,
         int m, float xw, float mid, float half) =>
    Occ o = Occ.new(array.new<float>(), array.new<float>(), array.new<float>(), array.new<float>(), m - 1)
    for k = 0 to m - 2
        float xa = wx(k, m, xw)
        float xb = wx(k + 1, m, xw)
        [p0x, p0y] = c.scr(xa, (aV.get(k)     - mid) / half, zNear)
        [p1x, p1y] = c.scr(xb, (aV.get(k + 1) - mid) / half, zNear)
        [p2x, p2y] = c.scr(xb, (bV.get(k + 1) - mid) / half, zNear)
        [p3x, p3y] = c.scr(xa, (bV.get(k)     - mid) / half, zNear)
        [q0x, q0y] = c.scr(xa, (cap.get(k)     - mid) / half, zNear)
        [q1x, q1y] = c.scr(xb, (cap.get(k + 1) - mid) / half, zNear)
        [q2x, q2y] = c.scr(xb, (cap.get(k + 1) - mid) / half, zSplit)
        [q3x, q3y] = c.scr(xa, (cap.get(k)     - mid) / half, zSplit)
        for v in array.from(p0x, p1x, p2x, p3x, q0x, q1x, q2x, q3x)
            o.qx.push(v)
        for v in array.from(p0y, p1y, p2y, p3y, q0y, q1y, q2y, q3y)
            o.qy.push(v)
        o.qmin.push(math.min(p0x, p1x, p2x, p3x, q0x, q1x, q2x, q3x))
        o.qmax.push(math.max(p0x, p1x, p2x, p3x, q0x, q1x, q2x, q3x))
    o
 
method covered(Occ o, float px, float py) =>
    bool hit = false
    for g = 0 to o.n - 1
        if px >= o.qmin.get(g) and px <= o.qmax.get(g)
            for q = 0 to 1
                int  b      = g * 8 + q * 4
                bool inside = false
                for e = 0 to 3
                    int   i1 = b + e
                    int   i2 = b + (e + 1) % 4
                    float xi = o.qx.get(i1)
                    float yi = o.qy.get(i1)
                    float xj = o.qx.get(i2)
                    float yj = o.qy.get(i2)
                    if (yi > py) != (yj > py)
                        if px < (xj - xi) * (py - yi) / (yj - yi) + xi
                            inside := not inside
                if inside
                    hit := true
        if hit
            break
    hit
 
// ライン:見えている区間 = 角材(天面+前面+ハイライト)/ 隠れている区間 = 後で破線
drawTube(Cam c, array<float> v, float z, int w, color col, array<float> aV, array<float> bV,
         bool fromAbove, array<polyline> pls, int m, float xw, float mid, float half,
         float hd, float th, float sg, Occ occ, array<Seg> hid) =>
    int   n  = v.size()
    array<bool> hidF = array.new<bool>()
    for j = 0 to n - 1
        [sx, sy] = c.scr(wx(j, m, xw), (v.get(j) - mid) / half, z)
        hidF.push(occ.covered(sx, sy))
    int   k0 = 0
    float zF = z - hd * sg
    float zB = z + hd * sg
    for k = 1 to n - 1
        if hidF.get(k) != hidF.get(k0) or k == n - 1
            if hidF.get(k0)
                // 隠れた区間も角材の形のまま、辺を一筆書きでなぞる(後で点線で描く)
                //   手前上の縁 → 手前下の縁 → 奥の上の縁 と奥行きの辺
                array<chart.point> pts = array.new<chart.point>()
                float yT0 = (v.get(k0) - mid) / half
                pts.push(c.pt(wx(k0, m, xw), yT0, zF))
                for j = k0 to k
                    pts.push(c.pt(wx(j, m, xw), (v.get(j) - mid) / half - th, zF))
                for j = k to k0
                    pts.push(c.pt(wx(j, m, xw), (v.get(j) - mid) / half, zF))
                for j = k0 to k
                    pts.push(c.pt(wx(j, m, xw), (v.get(j) - mid) / half, zB))
                pts.push(c.pt(wx(k, m, xw), (v.get(k) - mid) / half, zF))
                hid.push(Seg.new(pts, col))
            else if pls.size() < 62
                array<chart.point> topF = array.new<chart.point>()
                array<chart.point> frnt = array.new<chart.point>()
                array<chart.point> edge = array.new<chart.point>()
                for j = k0 to k
                    float x  = wx(j, m, xw)
                    float y  = (v.get(j) - mid) / half
                    float yT = fromAbove ? y : y - th
                    topF.push(c.pt(x, yT, zF))
                    frnt.push(c.pt(x, y, zF))
                    edge.push(c.pt(x, yT, zF))
                for j = k to k0
                    float x  = wx(j, m, xw)
                    float y  = (v.get(j) - mid) / half
                    float yT = fromAbove ? y : y - th
                    topF.push(c.pt(x, yT, zB))
                    frnt.push(c.pt(x, y - th, zF))
                color cT = fromAbove ? tint(col, 0.45) : tint(col, -0.6)
                pls.push(polyline.new(topF, false, true, xloc.bar_index, cT, cT))
                pls.push(polyline.new(frnt, false, true, xloc.bar_index, tint(col, -0.35), col))
                pls.push(polyline.new(edge, line_color = tint(col, fromAbove ? 0.75 : 0.3), line_width = 1))
            k0 := k
    true
 
var array<polyline> pls = array.new<polyline>()
var array<line>     lns = array.new<line>()
var array<label>    lbs = array.new<label>()
var array<linefill> lfs = array.new<linefill>()
var array<box>      bxs = array.new<box>()
 
// ── 球のレンダリング ──
// 球を「横1バー × 縦 rows 段」のマス目に分け、マスごとに光の当たり方(拡散+鏡面反射)を計算して塗る。
// 同じ色が横に続くマスは1つのboxにまとめて、描画数を節約する。
drawSphere(array<box> bxs, float cIdx, float cPr, float rBars, float rPr, color base, int tr, int rows) =>
    // 光源:左上手前 / 視線:正面
    float lx = -0.45
    float ly = 0.6
    float lz = 0.66
    float hx = lx
    float hy = ly
    float hz = lz + 1.0
    float hn = math.sqrt(hx * hx + hy * hy + hz * hz)
    hx /= hn
    hy /= hn
    hz /= hn
    int c0 = int(math.floor(cIdx - rBars))
    int c1 = int(math.ceil(cIdx + rBars))
    for r = 0 to rows - 1
        float v   = 1.0 - (r + 0.5) * 2.0 / rows           // +1(上)〜-1(下)
        float yT  = cPr + (1.0 - r * 2.0 / rows) * rPr
        float yB  = cPr + (1.0 - (r + 1) * 2.0 / rows) * rPr
        int   runL = na
        color runC = na
        float runK = -1.0
        for ci = c0 to c1
            float u  = (ci + 0.5 - cIdx) / rBars
            float q  = 1.0 - u * u - v * v
            color cc = na
            float key = -1.0                                          // 色の段階(同じなら横に連結)
            if q > 0
                float nz   = math.sqrt(q)
                float dif  = math.max(0.0, u * lx + v * ly + nz * lz)
                float spc  = math.pow(math.max(0.0, u * hx + v * hy + nz * hz), 28)
                float rim  = math.pow(1.0 - nz, 3) * 0.25           // 縁をわずかに明るく(反射光)
                float br   = 0.18 + 0.82 * dif + rim
                br := math.round(br * 12) / 12.0                      // 段階を減らしてboxをまとめやすく
                float sp = math.round(spc * 6) / 6.0
                key := br * 100 + sp
                float rr = math.min(255, color.r(base) * br + 255 * sp * 0.9)
                float gg = math.min(255, color.g(base) * br + 255 * sp * 0.9)
                float bb = math.min(255, color.b(base) * br + 255 * sp * 0.9)
                cc := color.rgb(rr, gg, bb, tr)
            bool same = not na(runL) and key >= 0 and key == runK
            if not same
                if not na(runL) and bxs.size() < 495
                    bxs.push(box.new(runL, yT, ci, yB, border_color = color.new(color.black, 100), border_width = 0, bgcolor = runC))
                runL := key < 0 ? na : ci
                runC := cc
                runK := key
        if not na(runL) and bxs.size() < 495
            bxs.push(box.new(runL, yT, c1 + 1, yB, border_color = color.new(color.black, 100), border_width = 0, bgcolor = runC))
    true
 
// 四角柱の1面 = 向かい合う2辺のline + その間のlinefill
face(array<line> lns, array<linefill> lfs, chart.point a1, chart.point a2, chart.point b1, chart.point b2,
     color fillC, color edgeC) =>
    line l1 = line.new(a1, a2, color = edgeC)
    line l2 = line.new(b1, b2, color = edgeC)
    lns.push(l1)
    lns.push(l2)
    lfs.push(linefill.new(l1, l2, fillC))
    l2
 
// 四角柱(実体の見えている部分)。光源は左上手前:天面=明 / 前面=元の色 / 側面=暗
prism(Cam c, array<line> lns, array<linefill> lfs, float x0, float x1, float yA, float yB,
      float zcF, float zcB, float xs, float camY, color cc, int tr, bool capTop, bool capBot) =>
    color cTop  = tint(cc, 0.45)
    color cFrnt = tint(cc, 0.05)
    color cSide = tint(cc, -0.55)
    color cBot  = tint(cc, -0.75)
    if not na(xs)
        face(lns, lfs, c.pt(xs, yA, zcF), c.pt(xs, yB, zcF), c.pt(xs, yA, zcB), c.pt(xs, yB, zcB),
             color.new(cSide, tr), color.new(tint(cSide, -0.3), tr))
    if capTop and camY > yB
        face(lns, lfs, c.pt(x0, yB, zcF), c.pt(x1, yB, zcF), c.pt(x0, yB, zcB), c.pt(x1, yB, zcB),
             color.new(cTop, tr), color.new(cTop, tr))
    if capBot and camY < yA
        face(lns, lfs, c.pt(x0, yA, zcF), c.pt(x1, yA, zcF), c.pt(x0, yA, zcB), c.pt(x1, yA, zcB),
             color.new(cBot, tr), color.new(cBot, tr))
    face(lns, lfs, c.pt(x0, yA, zcF), c.pt(x0, yB, zcF), c.pt(x1, yA, zcF), c.pt(x1, yB, zcF),
         color.new(cFrnt, tr), color.new(tint(cFrnt, -0.25), tr))
    if capTop and camY > yB
        lns.push(line.new(c.pt(x0, yB, zcF), c.pt(x1, yB, zcF), color = color.new(tint(cc, 0.75), tr)))
    true
 
// ヒゲ・辺を「見える部分=実線」「隠れる部分=後で破線」に分ける(縦方向の線分)
vSeg(Cam c, array<line> lns, array<Seg> hidL, float x, float ya, float yb, float z, float lo, float hi, color col) =>
    if yb > ya
        float h0 = math.max(ya, lo)
        float h1 = math.min(yb, hi)
        if math.min(yb, lo) > ya
            lns.push(line.new(c.pt(x, ya, z), c.pt(x, math.min(yb, lo), z), color = col))
        if h1 > h0
            hidL.push(Seg.new(array.from(c.pt(x, h0, z), c.pt(x, h1, z)), col))
        if yb > math.max(ya, hi)
            lns.push(line.new(c.pt(x, math.max(ya, hi), z), c.pt(x, yb, z), color = col))
    true
 
// ════════ 描画 ════════
if barstate.islast
    while pls.size() > 0
        pls.pop().delete()
    while lns.size() > 0
        lns.pop().delete()
    while lbs.size() > 0
        lbs.pop().delete()
    while lfs.size() > 0
        lfs.pop().delete()
    while bxs.size() > 0
        bxs.pop().delete()
 
    float wob  = swayAmp * math.sin(timenow / 1000.0 * 0.5)
    // プリセット: [Yaw, Pitch, カメラ距離]
    float yawB = switch preset
        "Dramatic"   => 35.0
        "Top-down"   => 20.0
        "From below" => 30.0
        "Front"      => 10.0
        => yawIn
    float pitB = switch preset
        "Dramatic"   => 30.0
        "Top-down"   => 55.0
        "From below" => -25.0
        "Front"      => 15.0
        => pitchIn
    float camE = switch preset
        "Dramatic"   => 10.0     // 絵画のように遠近は控えめ(遠くがほんの少し小さい)
        "Top-down"   => 10.0
        "From below" => 10.0
        "Front"      => 12.0
        => camD
    float yaw  = yawB + (sway == "Horizontal" ? wob : 0.0)
    float pNow = math.max(-85, math.min(85, pitB + (sway == "Vertical" ? wob : 0.0)))
    bool  fromAbove = pNow >= 0
    float lit  = fromAbove ? 1.0 : 0.7      // 下から見たときは裏面なので少し暗く
    // 表示する期間のデータだけで価格レンジを決める(高さ方向をフルに使う)
    float loD = 1e100
    float hiD = -1e100
    for k = 0 to nBars + shift - 1
        int i = nBars - 1 - k
        loD := math.min(loD, spanA[i + shift], spanB[i + shift])
        hiD := math.max(hiD, spanA[i + shift], spanB[i + shift])
        if i >= 0
            loD := math.min(loD, low[i], tenkan[i], kijun[i])
            hiD := math.max(hiD, high[i], tenkan[i], kijun[i])
    midP  := (hiD + loD) / 2
    halfP := math.max((hiD - loD) / 2, syminfo.mintick)
    float yr   = math.toradians(yaw)
    float pr   = math.toradians(pNow)
    float XW  = 1.6
    Cam   c   = Cam.new(math.cos(yr), math.sin(yr), math.cos(pr), math.sin(pr), camE,
                        bar_index - widthB * 0.8, widthB / (2 * XW), midP, halfP)
    // カメラのワールド座標(どの面が見えるかの判定に使う)
    float camX = math.sin(yr) * math.cos(pr) * camE
    float camY = math.sin(pr) * camE
    float camZ = -math.cos(yr) * math.cos(pr) * camE
 
    int   M      = nBars + shift
    int   kNow   = nBars - 1
    float floorY = fromAbove ? -1.1 : 1.15   // 下から見るときはグリッドを天井側へ
    float markY  = fromAbove ? 0.9 : -0.9
    // 奥の向き(+z/-z)を判定 → 終値が常に一番手前に来るようにする
    float s      = c.depthOf(0, 0, 1) > c.depthOf(0, 0, -1) ? 1.0 : -1.0
    // リボンのz位置(手前→奥):終値, 転換, 基準, 遅行
    // ローソク足は中心面、ラインはローソクの少し奥(柱がラインを隠す=ローソクが主役)
    float spW  = 2.0 * XW / (nBars + shift - 1)
    float zLn  = spW * bodyR / 2 * depthR * 1.6 * s
    float lnHD = spW * bodyR / 2 * 0.8          // ライン(角材)の半奥行き
    float lnTH = tubeW * 0.006                  // ライン(角材)の縦の厚み
    float zCls = 0.0
    float zTen = zLn
    float zKij = zLn + 1.0 * ribGap * s
    float zChi = zLn + 2.0 * ribGap * s
    float D      = math.max(cloudD, math.abs(zChi) + 0.05)   // 雲の半分の厚み(全ラインを包む)
    float zNear  = -D * s
    float zFar   =  D * s
    float zWall  =  D * 1.6 * s
    float zFloorF = -D * 1.6 * s
    color gridC  = color.new(color.gray, 80)
 
    // 空間の箱(床〜天井 × 奥行き)の8隅を投影して、縦軸をぴったり合わせる
    float pMin = 1e100
    float pMax = -1e100
    for xx in array.from(-XW, XW + 0.06)
        for yy in array.from(-1.15, 1.15)
            for zz in array.from(zFloorF, zWall)
                float pp = c.pt(xx, yy, zz).price
                pMin := math.min(pMin, pp)
                pMax := math.max(pMax, pp)
    float pad = (pMax - pMin) * 0.03
    scTop := pMax + pad
    scBot := pMin - pad
 
    // ── 右側の壁:価格目盛り(奥行き方向に伸ばすので、どのリボンの深さでも価格が読める) ──
    float xR = XW + 0.06
    lns.push(line.new(c.pt(xR, floorY, zFloorF), c.pt(xR, 1.0, zFloorF), color = color.new(color.gray, 60)))
    for y in array.from(-1.0, -0.5, 0.0, 0.5, 1.0)
        lns.push(line.new(c.pt(xR, y, zFloorF), c.pt(xR, y, zWall), color = color.new(color.gray, 55)))
        lns.push(line.new(c.pt(-XW, y, zWall), c.pt(xR, y, zWall), color = gridC, style = line.style_dotted))
        lbs.push(label.new(c.pt(xR, y, zFloorF), str.tostring(midP + y * halfP, format.mintick),
                 style = label.style_label_left, color = color.new(color.black, 100),
                 textcolor = color.new(color.gray, 20), size = size.tiny))
 
    // ── 床 ──
    for k = 0 to M - 1 by 10
        lns.push(line.new(c.pt(wx(k, M, XW), floorY, zFloorF), c.pt(wx(k, M, XW), floorY, zWall), color = gridC))
    lns.push(line.new(c.pt(-XW, floorY, zFloorF), c.pt(XW, floorY, zFloorF), color = gridC))
    lns.push(line.new(c.pt(-XW, floorY, zWall),   c.pt(XW, floorY, zWall),   color = gridC))
    pls.push(polyline.new(array.from(c.pt(wx(kNow, M, XW), floorY, zFloorF), c.pt(XW, floorY, zFloorF),
             c.pt(XW, floorY, zWall), c.pt(wx(kNow, M, XW), floorY, zWall)),
             false, true, xloc.bar_index, color.new(color.yellow, 85), color.new(color.yellow, 92)))
    lns.push(line.new(c.pt(wx(kNow, M, XW), floorY, 0), c.pt(wx(kNow, M, XW), markY, 0),
             color = color.new(color.yellow, 60), style = line.style_dashed))
    lbs.push(label.new(c.pt(wx(kNow, M, XW), markY, 0), "Now", style = fromAbove ? label.style_label_down : label.style_label_up,
             color = color.new(color.yellow, 30), textcolor = color.black, size = size.tiny))
 
    // ── データ収集 ──
    array<float> aV  = array.new<float>()
    array<float> bV  = array.new<float>()
    array<float> cap = array.new<float>()
    array<bool>  bul = array.new<bool>()
    for k = 0 to M - 1
        int   i = nBars - 1 - k
        float a = spanA[i + shift]
        float b = spanB[i + shift]
        aV.push(a)
        bV.push(b)
        cap.push(fromAbove ? math.max(a, b) : math.min(a, b))   // 上から→天面 / 下から→底面
        bul.push(a >= b)
 
    array<float> vTen = array.new<float>()
    array<float> vKij = array.new<float>()
    array<float> vCls = array.new<float>()
    array<float> vChi = array.new<float>()
    for k = 0 to kNow
        int i = nBars - 1 - k
        vTen.push(tenkan[i])
        vKij.push(kijun[i])
        vCls.push(close[i])
        if i >= shift
            vChi.push(close[i - shift])
 
    // ── 描画順:奥 → 手前 ──
    // 1) 雲の天面
    // 雲の天面を「奥半分」と「手前半分」に分けて描く。
    //   奥半分 → ローソク・ラインより先に描く(柱の背景になる)
    //   手前半分 → 後から描く(雲に沈んでいる部分を覆い隠す)
    float zSplit = zLn + lnHD * s
    Occ   occ    = buildOcc(c, aV, bV, cap, zNear, zSplit, M, XW, midP, halfP)
    // 雲もローソクと同じ光源で塗る(天面=明るい / 前面=暗い)。半透明の重なりで明暗が出ないよう、
    // 天面の奥半分と手前半分は同じ色・同じ透明度にする
    color capUp = fromAbove ? tint(cBull, 0.25) : tint(cBull, -0.6)
    color capDn = fromAbove ? tint(cBear, 0.25) : tint(cBear, -0.6)
    color frUp  = tint(cBull, -0.45)
    color frDn  = tint(cBear, -0.45)
    int   capTr = 0      // 不透明:雲の後ろの線(グリッド・奥の辺など)が透けないように
    drawCap(c, cap, zSplit, zFar, bul, capTr, capUp, capDn, pls, M, XW, midP, halfP, 40)
    bool candles = priceSt == "3D candles"
    array<Seg> hid  = array.new<Seg>()    // 隠れたライン・雲の辺(polyline)
    array<Seg> hidL = array.new<Seg>()    // 隠れたローソクの辺(line)
    // 2) ライン(細いチューブ。ローソクより奥に置くので、柱の後ろを通る)
    if showChi
        drawTube(c, vChi, zChi, 1, cChi, aV, bV, fromAbove, pls, M, XW, midP, halfP, lnHD * 0.6, lnTH * 0.6, s, occ, hid)
    if showKij
        drawTube(c, vKij, zKij, tubeW, cKij, aV, bV, fromAbove, pls, M, XW, midP, halfP, lnHD, lnTH, s, occ, hid)
    if showTen
        drawTube(c, vTen, zTen, tubeW, cTen, aV, bV, fromAbove, pls, M, XW, midP, halfP, lnHD, lnTH, s, occ, hid)
    if not candles
        drawTube(c, vCls, zCls, tubeW, cCls, aV, bV, fromAbove, pls, M, XW, midP, halfP, lnHD, lnTH, s, occ, hid)
    // 3) 3Dローソク足(四角柱:前面 + 見えている側面 + 天面/底面 を塗りつぶし)
    int maxC = math.max(0, int((495 - 70 - lns.size()) / 12))   // 隠れ線(点線)用に70本残す    // 1本あたり line 最大12本程度
    if candles and maxC > 0
        float sp  = 2.0 * XW / (M - 1)          // バー間隔(ワールド座標)
        float hw  = sp * bodyR / 2              // 実体の半幅
        float hd  = hw * depthR                 // 実体の半奥行き
        float zcF = zCls - hd * s
        float zcB = zCls + hd * s
        int kFirst = math.max(0, kNow - maxC + 1)
        // カメラから遠い順に描く(手前の柱が奥の柱を隠すように)
        array<int>   ks   = array.new<int>()
        array<float> dist = array.new<float>()
        for k = kFirst to kNow
            ks.push(k)
            dist.push(math.abs(wx(k, M, XW) - camX))
        array<int> ord = dist.sort_indices(order.descending)
        float dMin = dist.min()
        float dRng = math.max(dist.max() - dMin, 1e-9)
        for oi in ord
            int   k  = ks.get(oi)
            int   i  = nBars - 1 - k
            float x  = wx(k, M, XW)
            color cc = close[i] >= open[i] ? cUp : cDn
            float yo = (open[i]  - midP) / halfP
            float yc = (close[i] - midP) / halfP
            float yh = (high[i]  - midP) / halfP
            float yl = (low[i]   - midP) / halfP
            float y0 = math.min(yo, yc)
            float y1 = math.max(math.max(yo, yc), y0 + 0.006)
            float yCT  = (math.max(aV.get(k), bV.get(k)) - midP) / halfP   // 雲の天面
            float yCB  = (math.min(aV.get(k), bV.get(k)) - midP) / halfP   // 雲の底面
            int   tr   = int(math.min(85, airFade * (dist.get(oi) - dMin) / dRng))   // 空気遠近法
            // 高値〜安値を24点に分けて、雲に覆われる範囲[hL, hH]を調べる
            float hL = 1e9
            float hH = -1e9
            for t = 0 to 23
                float yy = yl + (yh - yl) * t / 23
                [sx, sy] = c.scr(x, yy, zcF)
                if occ.covered(sx, sy)
                    hL := math.min(hL, yy)
                    hH := math.max(hH, yy)
            if hH < hL                              // 隠れていない
                hL := yh + 1
                hH := yh + 1
            float x0 = x - hw
            float x1 = x + hw
            float xs = camX > x1 ? x1 : camX < x0 ? x0 : na    // 見えている側面
            color wick = color.new(tint(cc, 0.2), tr)
            color dash = tint(cc, 0.25)
            // ヒゲ(見える部分=実線 / 隠れる部分=破線)
            vSeg(c, lns, hidL, x, yl, y0, zCls, hL, hH, wick)
            // 実体:隠れる範囲[hL,hH]で上下に切り分ける
            float vLoTop = math.min(y1, hL)        // 雲より下で見えている部分の上端
            float hA     = math.max(y0, hL)
            float hB     = math.min(y1, hH)
            float vUpBot = math.max(y0, hH)        // 雲より上で見えている部分の下端
            if vLoTop > y0
                prism(c, lns, lfs, x0, x1, y0, vLoTop, zcF, zcB, xs, camY, cc, tr, vLoTop == y1, true)
            if hB > hA
                // 隠れた部分:四角柱の形のまま、12本の辺を一筆書きで(後で点線で描く)
                chart.point f00 = c.pt(x0, hA, zcF)
                chart.point f10 = c.pt(x1, hA, zcF)
                chart.point f11 = c.pt(x1, hB, zcF)
                chart.point f01 = c.pt(x0, hB, zcF)
                chart.point b00 = c.pt(x0, hA, zcB)
                chart.point b10 = c.pt(x1, hA, zcB)
                chart.point b11 = c.pt(x1, hB, zcB)
                chart.point b01 = c.pt(x0, hB, zcB)
                hid.push(Seg.new(array.from(f00, f10, f11, f01, f00, b00, b10, b11, b01, b00, b01, f01, f11, b11, b10, f10), dash))
            if y1 > vUpBot
                prism(c, lns, lfs, x0, x1, vUpBot, y1, zcF, zcB, xs, camY, cc, tr, true, vUpBot == y0)
            vSeg(c, lns, hidL, x, y1, yh, zCls, hL, hH, wick)
    // 4) 雲の前面(半透明)
    drawCap(c, cap, zNear, zSplit, bul, capTr, capUp, capDn, pls, M, XW, midP, halfP)   // 天面の手前半分
    drawBand(c, aV, zNear, bV, zNear, bul, 0, frUp, frDn, pls, M, XW, midP, halfP)             // 前面(暗め・不透明)
    // 雲の輪郭:手前の上の角(ハイライト)と奥の上の辺 → 箱の形が読み取れるように
    array<chart.point> eNear = array.new<chart.point>()
    for k = 0 to M - 1
        eNear.push(c.pt(wx(k, M, XW), (cap.get(k) - midP) / halfP, zNear))
    if pls.size() < 92
        pls.push(polyline.new(eNear, line_color = color.new(color.white, 45), line_width = 1))
 
    // 5) 隠れ線(キャビネット図式):雲にさえぎられた部分を破線で重ねる
    for sg in hid
        if pls.size() < 100
            pls.push(polyline.new(sg.pts, line_color = tint(sg.col, 0.2), line_width = 1, line_style = line.style_dotted))
        else
            // polylineの枠が尽きたら line で代用
            for j = 1 to sg.pts.size() - 1
                if lns.size() < 500
                    lns.push(line.new(sg.pts.get(j - 1), sg.pts.get(j), color = tint(sg.col, 0.2), style = line.style_dotted))
    for sg in hidL
        if lns.size() < 500
            lns.push(line.new(sg.pts.get(0), sg.pts.get(1), color = sg.col, width = 1, style = line.style_dotted))
 
    // ── 転換線×基準線のクロス:球体マーカー ──
    //   マス目ごとに光の計算をして塗った球(drawSphere)。
    //   床まで点線を下ろして、空間のどこに浮いているかを分かりやすくする。
    if showTK
        array<float> crX = array.new<float>()
        array<float> crY = array.new<float>()
        array<bool>  crG = array.new<bool>()
        int   lastSg = 0
        int   lastK  = 0
        float lastD  = 0.0
        for k = 0 to kNow
            float d  = vTen.get(k) - vKij.get(k)
            int   sg = d > 0 ? 1 : d < 0 ? -1 : 0
            if sg != 0
                if lastSg != 0 and sg != lastSg
                    float fr = lastD / (lastD - d)                         // 交差点までの割合
                    crX.push(wx(lastK, M, XW) + fr * (wx(k, M, XW) - wx(lastK, M, XW)))
                    crY.push(vTen.get(lastK) + fr * (vTen.get(k) - vTen.get(lastK)))
                    crG.push(sg > 0)
                lastSg := sg
                lastK  := k
                lastD  := d
        // 球の半径:横はバー数、縦は「画面上で同じ長さ」になる価格幅に換算
        float barMs   = timeframe.in_seconds() * 1000
        float visBars = math.max(10, (chart.right_visible_bar_time - chart.left_visible_bar_time) / barMs + 1)
        float rBars   = tkDiam / 2.0
        float rPr     = rBars * (scTop - scBot) / visBars * tkAsp
        int   rows    = math.max(6, math.min(20, tkDiam))     // マスがほぼ正方形になる段数
        int nC = crX.size()
        if nC > 0
            for j = nC - 1 to math.max(0, nC - tkMax)
                float x   = crX.get(j)
                float y   = (crY.get(j) - midP) / halfP
                bool  gc  = crG.get(j)
                color col = gc ? cGC : cDC
                [sx, sy]  = c.scr(x, y, zTen)
                bool  hidC = occ.covered(sx, sy)                         // 雲に隠れていれば半透明
                chart.point p  = c.pt(x, y, zTen)
                if lns.size() < 500
                    lns.push(line.new(c.pt(x, y - rPr / halfP, zTen), c.pt(x, floorY, zTen), color = color.new(col, 55), style = line.style_dotted))
                drawSphere(bxs, sx, sy, rBars, rPr, col, hidC ? 60 : 0, rows)
                if lbs.size() < 44
                    string tip = (gc ? "Bullish cross: Tenkan-sen crossed above Kijun-sen" : "Bearish cross: Tenkan-sen crossed below Kijun-sen") + "\n" + str.tostring(crY.get(j), format.mintick)
                    lbs.push(label.new(p, "", style = label.style_none, tooltip = tip))
 
    // ── 現在値と雲の位置関係(左下のテーブル。上端に置くとステータスラインを隠すため) ──
    float aN  = aV.get(kNow)
    float bN  = bV.get(kNow)
    string pos = close > math.max(aN, bN) ? "Above cloud" : close < math.min(aN, bN) ? "Below cloud" : "Inside cloud"
    color  pc  = pos == "Above cloud" ? cUp : pos == "Below cloud" ? cDn : color.gray
    var table stT = table.new(position.bottom_left, 1, 1)
    stT.cell(0, 0, "Price: " + pos + (aN >= bN ? " (bullish cloud)" : " (bearish cloud)"),
             bgcolor = color.new(pc, 15), text_color = color.white, text_size = size.normal)
 
    // ── ラベル ──
    float xe = wx(kNow, M, XW)
    if not candles
        lbs.push(label.new(c.pt(xe, (vCls.last() - midP) / halfP, zCls), "Close", style = label.style_label_left, color = color.new(color.black, 100), textcolor = cCls, size = size.small))
    if showTen
        lbs.push(label.new(c.pt(xe, (vTen.last() - midP) / halfP, zTen), "Tenkan", style = label.style_label_left, color = color.new(color.black, 100), textcolor = cTen, size = size.small))
    if showKij
        lbs.push(label.new(c.pt(xe, (vKij.last() - midP) / halfP, zKij), "Kijun", style = label.style_label_left, color = color.new(color.black, 100), textcolor = cKij, size = size.small))
    if showChi and vChi.size() > 0
        lbs.push(label.new(c.pt(wx(vChi.size() - 1, M, XW), (vChi.last() - midP) / halfP, zChi), "Chikou", style = label.style_label_left, color = color.new(color.black, 100), textcolor = cChi, size = size.small))
 
// 縦軸合わせ用の透明プロット(最終バーにだけ値がある)
plot(scTop, "scaleTop", color.new(color.black, 100), display = display.pane, editable = false)
plot(scBot, "scaleBot", color.new(color.black, 100), display = display.pane, editable = false)

← 返回列表