本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0
// Cowboy Recursion — "straight lines + rotation + recursion = curves that aren't there"
// Anchor the master square with two clicks (bottom-left and top-right time/price),
// like a Gann Box. Each nested square moves every vertex λ along its side:
// A(n+1) = (1 − λ)·A(n) + λ·B(n)
// rotation per step θ = atan(λ / (1 − λ)), shrink q = √((1 − λ)² + λ²)
//@version=6
indicator("Cowboy's Recursion Squares (CRS)", shorttitle = "Cowboy's Lassos", overlay = true,
max_polylines_count = 100, max_lines_count = 50, max_labels_count = 50)
// ───────────────────────── Anchors ─────────────────────────
string G_ANCH = "Master square anchors (click on chart)"
int t1 = input.time(timestamp("2025-01-01T00:00"), "Bottom-left — Time", inline = "bl", group = G_ANCH, confirm = true)
float p1 = input.price(0.0, "Price", inline = "bl", group = G_ANCH, confirm = true)
int t2 = input.time(timestamp("2025-06-01T00:00"), "Top-right — Time", inline = "tr", group = G_ANCH, confirm = true)
float p2 = input.price(0.0, "Price", inline = "tr", group = G_ANCH, confirm = true)
// ───────────────────────── Geometry ─────────────────────────
string G_GEO = "Geometry"
string layout = input.string("2x2 Grid", "Layout", options = ["2x2 Grid", "Single Square"], group = G_GEO)
float lambda = input.float(0.10, "λ (vertex step along side)", minval = 0.005, maxval = 0.5, step = 0.005, group = G_GEO,
tooltip = "0.10 = move each vertex 10% along its side (θ ≈ 6.34° per step). Smaller λ = tighter, slower rotation.")
int depth = input.int(30, "Nested squares per quadrant (n)", minval = 1, maxval = 300, group = G_GEO)
bool mirror = input.bool(true, "Mirror quadrants (reflection reverses spin)", group = G_GEO)
bool reverse = input.bool(false, "Reverse spin direction", group = G_GEO)
// ───────────────────────── Style ─────────────────────────
string G_STY = "Style"
string colMode = input.string("Quadrant colors", "Color mode", options = ["Quadrant colors", "Mono"], group = G_STY)
color cTR = input.color(#E8925A, "Top-right", inline = "c1", group = G_STY)
color cTL = input.color(#4FB3BF, "Top-left", inline = "c1", group = G_STY)
color cBL = input.color(#8C7BE0, "Bottom-left", inline = "c2", group = G_STY)
color cBR = input.color(#D9607A, "Bottom-right", inline = "c2", group = G_STY)
color cMono = input.color(#E8DCCB, "Mono color", group = G_STY)
int outerTr = input.int(70, "Outer-square transparency", minval = 0, maxval = 100, group = G_STY)
float glow = input.float(0.55, "Core glow (lighten toward center)", minval = 0, maxval = 1, step = 0.05, group = G_STY)
int lw = input.int(1, "Square line width", minval = 1, maxval = 4, group = G_STY)
// ───────────────────────── Overlays ─────────────────────────
string G_OVR = "Overlays"
bool showFrame = input.bool(true, "Master square frame", inline = "f", group = G_OVR)
color frameCol = input.color(color.new(#E6E1F0, 10), "", inline = "f", group = G_OVR)
bool showCross = input.bool(true, "Midline cross (50% price / 50% time)", group = G_OVR)
bool showDiag = input.bool(false, "Corner-to-corner diagonals", group = G_OVR)
bool showSpir = input.bool(false, "Highlight corner spiral (path of the corners)", inline = "s", group = G_OVR)
color spirCol = input.color(color.new(#FFE6B0, 0), "", inline = "s", group = G_OVR)
bool showLbl = input.bool(true, "Price / time labels (0 · 50 · 100%)", group = G_OVR)
// ───────────────────────── Helpers ─────────────────────────
var array<polyline> polys = array.new<polyline>()
var array<line> lns = array.new<line>()
var array<label> lbls = array.new<label>()
lighten(color c, float amt) =>
color.rgb(color.r(c) + (255 - color.r(c)) * amt,
color.g(c) + (255 - color.g(c)) * amt,
color.b(c) + (255 - color.b(c)) * amt, 0)
// Normalized master coordinates (X,Y in 0..1) → chart point
toChart(float X, float Y) =>
chart.point.from_time(int(math.round(t1 + X * (t2 - t1))), p1 + Y * (p2 - p1))
// Local quadrant coords (u,v in 0..1) → chart point, applying spin + reflections
qPoint(float u, float v, float qx0, float qy0, float qs, bool fx, bool fy) =>
float uu = reverse ? u : v
float vv = reverse ? v : u
uu := fx ? 1.0 - uu : uu
vv := fy ? 1.0 - vv : vv
toChart(qx0 + uu * qs, qy0 + vv * qs)
drawQuad(float qx0, float qy0, float qs, bool fx, bool fy, color base, int maxBands) =>
color outerC = color.new(base, outerTr)
color innerC = lighten(base, glow)
// Square vertices in local coords, counter-clockwise A→B→C→D
float xA = 0.0
float yA = 0.0
float xB = 1.0
float yB = 0.0
float xC = 1.0
float yC = 1.0
float xD = 0.0
float yD = 1.0
int bands = math.max(1, math.min(depth, maxBands))
int perBand = int(math.ceil(depth / bands))
array<chart.point> spiral = array.new<chart.point>()
int n = 0
for b = 0 to bands - 1
array<chart.point> pts = array.new<chart.point>()
int nEnd = math.min(depth, n + perBand)
while n < nEnd
// Trace A→B→C→D→A. The next A lies on edge AB, so one continuous path works.
pts.push(qPoint(xA, yA, qx0, qy0, qs, fx, fy))
pts.push(qPoint(xB, yB, qx0, qy0, qs, fx, fy))
pts.push(qPoint(xC, yC, qx0, qy0, qs, fx, fy))
pts.push(qPoint(xD, yD, qx0, qy0, qs, fx, fy))
pts.push(qPoint(xA, yA, qx0, qy0, qs, fx, fy))
spiral.push(qPoint(xA, yA, qx0, qy0, qs, fx, fy))
// Pursuit step: every vertex moves λ toward the next vertex
float nxA = xA + lambda * (xB - xA)
float nyA = yA + lambda * (yB - yA)
float nxB = xB + lambda * (xC - xB)
float nyB = yB + lambda * (yC - yB)
float nxC = xC + lambda * (xD - xC)
float nyC = yC + lambda * (yD - yC)
float nxD = xD + lambda * (xA - xD)
float nyD = yD + lambda * (yA - yD)
xA := nxA
yA := nyA
xB := nxB
yB := nyB
xC := nxC
yC := nyC
xD := nxD
yD := nyD
n += 1
if pts.size() > 0
color col = color.from_gradient(b, 0, math.max(bands - 1, 1), outerC, innerC)
polys.push(polyline.new(pts, curved = false, closed = false, xloc = xloc.bar_time,
line_color = col, line_width = lw))
if showSpir and spiral.size() > 1
polys.push(polyline.new(spiral, curved = true, closed = false, xloc = xloc.bar_time,
line_color = spirCol, line_width = 2))
mkLine(float X1, float Y1, float X2, float Y2, color c, int w, string st) =>
chart.point a = toChart(X1, Y1)
chart.point z = toChart(X2, Y2)
lns.push(line.new(a.time, a.price, z.time, z.price, xloc = xloc.bar_time, color = c, width = w, style = st))
mkLabel(float X, float Y, string txt, string st) =>
chart.point a = toChart(X, Y)
lbls.push(label.new(a.time, a.price, txt, xloc = xloc.bar_time, style = st,
color = color.new(color.black, 100), textcolor = color.new(frameCol, 0), size = size.small))
// ───────────────────────── Draw ─────────────────────────
if barstate.islast
for p in polys
p.delete()
polys.clear()
for l in lns
l.delete()
lns.clear()
for lb in lbls
lb.delete()
lbls.clear()
bool valid = t2 != t1 and p2 != p1
if not valid
lbls.push(label.new(bar_index, close, "Pursuit Box: set two different anchors\n(bottom-left and top-right time/price)",
style = label.style_label_left, color = color.new(color.red, 20), textcolor = color.white))
else
bool grid = layout == "2x2 Grid"
bool mono = colMode == "Mono"
int quads = grid ? 4 : 1
int reserve = showSpir ? quads : 0
int maxBands = int(math.floor((100 - reserve - 1) / quads))
if grid
// Quadrants are reflections of each other (x→−x, y→−y), so cusps meet on the vertical midline like the video
drawQuad(0.5, 0.5, 0.5, false, mirror, mono ? cMono : cTR, maxBands)
drawQuad(0.0, 0.5, 0.5, mirror, mirror, mono ? cMono : cTL, maxBands)
drawQuad(0.5, 0.0, 0.5, false, false, mono ? cMono : cBR, maxBands)
drawQuad(0.0, 0.0, 0.5, mirror, false, mono ? cMono : cBL, maxBands)
else
drawQuad(0.0, 0.0, 1.0, false, false, mono ? cMono : cTR, maxBands)
if showFrame
mkLine(0, 0, 1, 0, frameCol, 1, line.style_solid)
mkLine(1, 0, 1, 1, frameCol, 1, line.style_solid)
mkLine(1, 1, 0, 1, frameCol, 1, line.style_solid)
mkLine(0, 1, 0, 0, frameCol, 1, line.style_solid)
if showCross
mkLine(0.5, 0, 0.5, 1, frameCol, 1, line.style_solid)
mkLine(0, 0.5, 1, 0.5, frameCol, 1, line.style_solid)
if showDiag
mkLine(0, 0, 1, 1, color.new(frameCol, 50), 1, line.style_dotted)
mkLine(0, 1, 1, 0, color.new(frameCol, 50), 1, line.style_dotted)
if showLbl
mkLabel(1, 0, str.tostring(p1, format.mintick), label.style_label_left)
mkLabel(1, 0.5, str.tostring((p1 + p2) / 2, format.mintick), label.style_label_left)
mkLabel(1, 1, str.tostring(p2, format.mintick), label.style_label_left)
mkLabel(0, 0, str.format_time(t1, "yyyy-MM-dd"), label.style_label_up)
mkLabel(0.5, 0, str.format_time(int(math.round((t1 + t2) / 2)), "yyyy-MM-dd"), label.style_label_up)
mkLabel(1, 0, str.format_time(t2, "yyyy-MM-dd"), label.style_label_up)