// ============================================================================ // © algonexta // This script is for education and research; it does not guarantee results. // Verify this source in TradingView before use. // ============================================================================ // Pullback Map: Measured Retracement Levels. Research indicator, not a strategy: it measures how deep // past pullbacks went on this chart and draws those depths, as percentiles, on the current swing leg. // It does not place, manage or simulate orders. //@version=6 indicator("Pullback Map: Measured Retracement Levels [AlgoNexta]", shorttitle = "Pullback Map", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 200) //────────────────────────────────────────────────────────────────────────────── // Inputs //────────────────────────────────────────────────────────────────────────────── string G_SW = "1. Swings" string G_ST = "2. Statistics" string G_VI = "3. Display" int swingLen = input.int(8, "Swing length", minval = 2, maxval = 50, group = G_SW, tooltip = "Bars on each side of a swing high or low. Swings are used only after they are confirmed.") float minLegAtr = input.float(1.5, "Ignore legs smaller than (ATR)", minval = 0.0, step = 0.25, group = G_SW) string legMode = input.string("Legs that broke the previous swing", "Legs to measure", options = ["Legs that broke the previous swing", "All legs"], group = G_SW, tooltip = "Legs that broke the previous swing: an up leg counts only if it ended above the previous swing high, and a down leg only if it ended below the previous swing low. If the current leg did not break, its map uses the all-legs sample and is drawn in a muted colour. All legs: every leg counts.") int sampleMax = input.int(200, "Pullbacks in the sample (per direction)", minval = 20, maxval = 500, group = G_ST) int minSample = input.int(20, "Pullbacks needed before levels are drawn", minval = 5, maxval = 500, group = G_ST) float qLo = input.float(25, "Zone starts at percentile", minval = 1, maxval = 99, group = G_ST) float qHi = input.float(75, "Zone ends at percentile", minval = 1, maxval = 99, group = G_ST) float qDeep = input.float(90, "Deep pullback percentile", minval = 1, maxval = 99, group = G_ST) bool showZig = input.bool(true, "Show swing legs", group = G_VI) bool showFib = input.bool(false, "Show 38.2 / 50 / 61.8 for comparison", group = G_VI) bool showLbl = input.bool(true, "Show level labels", group = G_VI) bool showTable = input.bool(true, "Show statistics table", group = G_VI) string tblPos = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = G_VI) color upCol = input.color(#22ab94, "Pullback after an up leg", group = G_VI) color dnCol = input.color(#f23645, "Pullback after a down leg", group = G_VI) if barstate.isfirst and not (qLo < qHi and qHi < qDeep) runtime.error("Pullback Map: percentiles must satisfy zone start < zone end < deep.") //────────────────────────────────────────────────────────────────────────────── // State //────────────────────────────────────────────────────────────────────────────── var array zp = array.new() // swing prices var array zb = array.new() // swing bars var array zd = array.new() // 1 = swing high, -1 = swing low var array upR = array.new() // pullback depth after up legs, as a fraction of the leg var array dnR = array.new() // pullback depth after down legs var array cnt = array.new(6, 0) // up legs: all, origin lost, new extreme | down legs: same // Second sample with every leg. Used for the projection when the current leg did not break the // previous swing, so a sideways market still gets a map (drawn fainter and labelled). var array upAll = array.new() var array dnAll = array.new() var array cntAll = array.new(6, 0) var array zzLines = array.new() float atr = ta.atr(14) float ph = ta.pivothigh(high, swingLen, swingLen) float pl = ta.pivotlow(low, swingLen, swingLen) float recentLo = ta.lowest(low, swingLen) float recentHi = ta.highest(high, swingLen) f_pos(string s) => s == "Top left" ? position.top_left : s == "Bottom right" ? position.bottom_right : s == "Bottom left" ? position.bottom_left : position.top_right bool breakOnly = legMode == "Legs that broke the previous swing" // Adds a confirmed swing. Returns 1 for a new point, 2 if it replaced the last point, 0 if ignored. f_add(float price, int b, int dir) => int res = 0 int n = zp.size() if n == 0 zp.push(price) zb.push(b) zd.push(dir) res := 1 else int lastDir = zd.get(n - 1) float lastP = zp.get(n - 1) if lastDir == dir if (dir == 1 and price > lastP) or (dir == -1 and price < lastP) zp.set(n - 1, price) zb.set(n - 1, b) res := 2 else zp.push(price) zb.push(b) zd.push(dir) res := 1 res // Adds one measured pullback to a sample and its counters. f_store(array dst, array cn, int dirB, float r, bool cont) => int o = dirB == 1 ? 0 : 3 cn.set(o, cn.get(o) + 1) if r >= 1.0 cn.set(o + 1, cn.get(o + 1) + 1) else if cont cn.set(o + 2, cn.get(o + 2) + 1) dst.push(r) if dst.size() > sampleMax dst.shift() true // Called when a new point is added. Points A, B, C, D are then final: // leg A to B, pullback B to C, and D shows whether price made a new extreme beyond B. f_record() => int n = zp.size() if n >= 5 float a = zp.get(n - 5) float b = zp.get(n - 4) float c = zp.get(n - 3) float d = zp.get(n - 2) int dirB = zd.get(n - 4) float leg = math.abs(b - a) // The swing before A has the same type as B. The leg broke it if B went beyond it. bool broke = n >= 6 and (dirB == 1 ? b > zp.get(n - 6) : b < zp.get(n - 6)) if leg > 0 and leg >= minLegAtr * nz(atr) float r = math.abs(c - b) / leg bool cont = dirB == 1 ? d > b : d < b f_store(dirB == 1 ? upAll : dnAll, cntAll, dirB, r, cont) if broke or not breakOnly f_store(dirB == 1 ? upR : dnR, cnt, dirB, r, cont) n f_feed(float price, int dir) => int res = f_add(price, bar_index - swingLen, dir) int n = zp.size() if res == 1 f_record() if showZig and n >= 2 zzLines.push(line.new(zb.get(n - 2), zp.get(n - 2), zb.get(n - 1), zp.get(n - 1), color = color.new(chart.fg_color, 55), width = 1)) if res == 2 and showZig and zzLines.size() > 0 line l = zzLines.last() l.set_xy2(zb.get(n - 1), zp.get(n - 1)) res // Share of past pullbacks that were equal to or shallower than v, in percent. f_rank(array a, float v) => int c = 0 for x in a if x <= v c += 1 a.size() > 0 ? 100.0 * c / a.size() : na //────────────────────────────────────────────────────────────────────────────── // Swings: confirmed bars only //────────────────────────────────────────────────────────────────────────────── int chg = 0 if barstate.isconfirmed if not na(ph) and not na(pl) bool lastHigh = zd.size() > 0 and zd.get(zd.size() - 1) == 1 int r1 = f_feed(lastHigh ? ph : pl, lastHigh ? 1 : -1) int r2 = f_feed(lastHigh ? pl : ph, lastHigh ? -1 : 1) chg := math.max(r1, r2) else if not na(ph) chg := f_feed(ph, 1) else if not na(pl) chg := f_feed(pl, -1) if zp.size() > 60 zp.shift() zb.shift() zd.shift() if zzLines.size() > 200 zzLines.shift() //────────────────────────────────────────────────────────────────────────────── // Projection on the current leg //────────────────────────────────────────────────────────────────────────────── var array pLines = array.new() var array