//@version=5 // FuturesBench COT Index for TradingView // Free and open source under the Mozilla Public License 2.0. // Page, settings and updates: https://futuresbench.com/tools/tradingview-cot-index/ // // Rebuilds the FuturesBench COT Index from TradingView's own CFTC Commitments of Traders feed: // COT index = 100 * (net - lowest net) / (highest net - lowest net) over the lookback, // counted in weekly reports. 0 = the group's most net-short reading of the lookback, // 100 = its most net-long. Same formula as futuresbench.com/methodology/. // An up/down triangle marks an inflection hook: the index was at an extreme on the previous // report, positioning had been moving deeper into it, and this report turned it back. indicator("FuturesBench COT Index", shorttitle = "FB COT Index", format = format.price, precision = 1) import TradingView/LibraryCOT/2 as cot grp = input.string("Commercials", "Trader group", options = ["Commercials", "Large speculators"]) lookback = input.int(26, "Lookback (weekly reports)", minval = 4, maxval = 520) show = input.string("COT Index", "Plot", options = ["COT Index", "Net position"]) inclOpt = input.bool(false, "Include options") codeIn = input.string("", "CFTC code (blank = use the chart's symbol)") upper = input.float(90.0, "Upper extreme", minval = 50.0, maxval = 100.0) lower = input.float(10.0, "Lower extreme", minval = 0.0, maxval = 50.0) rootToCode(string r) => switch r "GC" => "088691" "SI" => "084691" "HG" => "085692" "PL" => "076651" "PA" => "075651" "CL" => "067651" "BZ" => "06765T" "NG" => "023651" "ZC" => "002602" "ZW" => "001602" "KE" => "001612" "ZS" => "005602" "ZL" => "007601" "ZM" => "026603" "SB" => "080732" "KC" => "083731" "CC" => "073732" "CT" => "033661" "LE" => "057642" "GF" => "061641" "HE" => "054642" "6E" => "099741" "6J" => "097741" "6B" => "096742" "6C" => "090741" "6A" => "232741" "6S" => "092741" "6N" => "112741" "6M" => "095741" "DX" => "098662" "ES" => "13874A" "NQ" => "209742" "RTY" => "239742" "YM" => "124603" "VX" => "1170E1" "ZT" => "042601" "ZF" => "044601" "ZN" => "043602" "ZB" => "020601" "UB" => "020604" "SR3" => "134741" "ZQ" => "045601" "BTC" => "133741" "MBT" => "133742" "ETH" => "146021" "MET" => "146022" "MES" => "13874U" "MNQ" => "209747" "M2K" => "239747" "MGC" => "088695" => "" string code = codeIn != "" ? codeIn : rootToCode(syminfo.root) if code == "" runtime.error("FuturesBench COT Index: no CFTC code is known for " + syminfo.root + ". Enter the code in the settings; every code is listed at futuresbench.com/cot/.") string metric = grp == "Commercials" ? "Commercial Positions" : "Noncommercial Positions" float longs = request.security(cot.COTTickerid("Legacy", code, inclOpt, metric, "Long", "All"), "1W", close, ignore_invalid_symbol = true) float shorts = request.security(cot.COTTickerid("Legacy", code, inclOpt, metric, "Short", "All"), "1W", close, ignore_invalid_symbol = true) float net = longs - shorts // Weekly history: one value per week, so the index means the same thing on any chart timeframe. var float[] hist = array.new_float() if timeframe.change("1W") and not na(net[1]) array.push(hist, net[1]) while array.size(hist) > lookback - 1 array.shift(hist) float idx = na float prevIdx = na int n = array.size(hist) if not na(net) and n > 0 float lo = math.min(array.min(hist), net) float hi = math.max(array.max(hist), net) idx := hi == lo ? 50.0 : 100.0 * (net - lo) / (hi - lo) float plo = array.min(hist) float phi = array.max(hist) prevIdx := phi == plo ? 50.0 : 100.0 * (array.get(hist, n - 1) - plo) / (phi - plo) bool newReport = ta.change(net) != 0 bool hookUp = false bool hookDown = false if newReport and n >= 2 and not na(prevIdx) float n1 = array.get(hist, n - 1) float n2 = array.get(hist, n - 2) hookUp := prevIdx <= lower and n1 < n2 and net > n1 hookDown := prevIdx >= upper and n1 > n2 and net < n1 bool asIndex = show == "COT Index" plot(asIndex ? idx : net, "FuturesBench COT", color = #2a78d6, linewidth = 2) plot(asIndex ? upper : na, "Upper extreme", color = color.new(color.gray, 40), style = plot.style_linebr) plot(asIndex ? lower : na, "Lower extreme", color = color.new(color.gray, 40), style = plot.style_linebr) bgcolor(asIndex and idx >= upper ? color.new(#2a78d6, 88) : asIndex and idx <= lower ? color.new(#eb6834, 88) : na) plotshape(hookUp, "Inflection hook up", shape.triangleup, location.bottom, color = #1baf7a, size = size.tiny) plotshape(hookDown, "Inflection hook down", shape.triangledown, location.top, color = #c03d82, size = size.tiny)