Indicator · Pine Script v6 · Own pane

COT Total

by ivansoft· code credited to TradingView

An indicator in a separate pane that plots total Commitment of Traders positions for the chart symbol.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code139
Settings4inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago1 Mar 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

139 lines of Pine Script v6 from edu_std/cot_total.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

cot_total.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradingView

//@version=6
indicator("Commitment of Traders: Total", "COT Total", format = format.volume)

// Commitment of Traders: Total
// v4, 2024.11.26

// This code was written using the recommendations from the Pine Script™ User Manual's Style guide:
//    https://www.tradingview.com/pine-script-docs/writing/style-guide/



import TradingView/LibraryCOT/4 as cot



//#region ———————————————————— Constants and inputs



// Metric information types 
string IO01 = "Futures"
string IO02 = "Options"
string IO03 = "Futures + Options"

// Directions
string ST01 = "Long"
string ST02 = "Short"
string ST03 = "Long + Short"
string ST04 = "Long - Short"
string ST05 = "Long %"

// Tooltips
string TT_MODE = "Determines the type of information the indicator uses from the chart's symbol to request COT data:\n
     • 'Root' uses the root prefix of a futures symbol (e.g., 'ES' for 'ESH2020').\n
     • 'Base currency' uses the first currency from a currency pair (e.g., 'EUR' for 'EURUSD').\n
     • 'Currency' uses the currency of the symbol's quoted values (e.g., 'JPY' for 'TSE:9984' or 'USDJPY').\n
     • 'Auto' tries all the above modes in order.\n
     If no COT data is available for the specified mode, a runtime error occurs." 
string TT_OPTIONS = "Determines whether the data includes information from futures, options, or both."
string TT_DISPLAY = "Determines the information the indicator displays on the chart:\n
     • 'Long' shows the total of long positions.\n
     • 'Short' shows the total of short positions.\n
     • 'Long + Short' shows the sum of long and short positions.\n
     • 'Long - Short' shows the difference between long and short positions.\n
     • 'Long %' shows the percentage of long positions relative to the sum of long and short positions."
string TT_OVERRIDE = "This field allows manual specification of the CFTC COT code. When the field is not empty, the
     indicator uses this code instead of determining the code based on the chart symbol's root or currency information.
     For example, if the field's value is '001602', the indicator requests wheat futures data, regardless of the
     current chart."

// Inputs
string selectionModeInput  = input.string("Auto", "COT selection mode", options = ["Auto", "Root", "Base currency", "Currency"], tooltip = TT_MODE)
string includeOptionsInput = input.string(IO01,   "Futures/Options",    options = [IO01, IO02, IO03], tooltip = TT_OPTIONS)
string selectedTypeInput   = input.string(ST01,   "Display",            options = [ST01, ST02, ST03, ST04, ST05], tooltip = TT_DISPLAY)
string userCFTCCodeInput   = input.string("",     "CFTC Code",          tooltip = TT_OVERRIDE)
//#endregion



//#region ———————————————————— Functions


// @function                Requests Legacy CFTC Commitment of Traders (COT) data based on the specified parameters.
// @param CFTCCode          (string) The CFTC code for the asset. For example, wheat futures (root "ZW") have the 
//                          code "001602".
// @param metricName        (string) The name of the Legacy metric to request, corresponding to one of the identifiers 
//                          listed in the "LibraryCOT" library (e.g., "Commercial Positions").
// @param isLong            (bool) If `true`, the function requests data for long positions. Otherwise, it requests 
//                          data for short positions.
// @returns                 (float) The retrieved COT Legacy metric for futures, options, or futures and options, 
//                          depending on the specified "Futures/Options" input value.
dataRequest(cftcCode, metricName, isLong) =>
    string direction = isLong ? "Long" : "Short"
    string typeCOT = "Legacy"
    float futuresOnly        = cot.requestCommitmentOfTraders(typeCOT, cftcCode, false, metricName, direction, "All")
    float futuresWithOptions = cot.requestCommitmentOfTraders(typeCOT, cftcCode, true,  metricName, direction, "All") 
    switch includeOptionsInput
        IO01 => futuresOnly
        IO02 => futuresWithOptions - futuresOnly
        IO03 => futuresWithOptions
        => na


// @function                 Retrieves long and short Legacy COT metrics based on the specified CFTC code and metric 
//                          name. The function calculates a final metric based on the selected "Display" option. 
// @param CFTCCode          (string) The CFTC code for the asset. For example, wheat futures (root "ZW") have the 
//                          code "001602".
// @param metricName        (string) The name of the Legacy metric to request, corresponding to one of the identifiers 
//                          listed in the "LibraryCOT" library (e.g., "Commercial Positions").
// @returns                 (float) The calculated COT metric based on the "Display" input value:
//                              - If "Long", returns the total of long positions.
//                              - If "Short", returns the total of short positions.
//                              - If "Long + Short", returns the sum of long and short positions.
//                              - If "Long - Short", returns the difference between long and short positions.
//                              - If "Long %", returns the percentage of long positions relative to the sum of 
//                              long and short positions.
metric(cftcCode, metricName) =>
    float cotLong  = dataRequest(cftcCode, metricName, true)
    float cotShort = dataRequest(cftcCode, metricName, false)
    switch selectedTypeInput
        ST01 => cotLong
        ST02 => cotShort
        ST03 => cotLong + cotShort
        ST04 => cotLong - cotShort
        ST05 => cotLong / (cotLong + cotShort) * 100
        => na
//#endregion



//#region ———————————————————— Calculations and display


// @variable The user-specified CFTC code if the "CFTC Code" input is not empty, a chart-based CFTC code otherwise. 
var string cftcCode = userCFTCCodeInput != "" ? userCFTCCodeInput : cot.convertRootToCOTCode(selectionModeInput)

// Request COT data for noncommercial, nonreportable, and commercial positions. 
float largeTraders      = metric(cftcCode, "Noncommercial Positions")
float smallTraders      = metric(cftcCode, "Nonreportable Positions")
float commercialHedgers = metric(cftcCode, "Commercial Positions")

// Plot the requested data as step lines with diamonds.
plot(largeTraders,      "Large Traders",      color.red,   style = plot.style_stepline_diamond)
plot(smallTraders,      "Small Traders",      color.blue,  style = plot.style_stepline_diamond)
plot(commercialHedgers, "Commercial Hedgers", color.green, style = plot.style_stepline_diamond)

// @variable A `table` displaying symbol and CFTC code information in the bottom-right corner. 
var table symbolDisplay = table.new(position.bottom_right, 1, 1)
// Populate the `symbolDisplay` on the first bar. 
if barstate.isfirst
    color  TEXT_COLOR = color.white
    color  BG_COLOR   = color.new(color.blue, 50)
    string dataCOT    = userCFTCCodeInput != "" ? userCFTCCodeInput : cot.convertRootToCOTCode(selectionModeInput, false)
    string txt        = str.format("COT data for {0} {1}\nCFTC Code: {2}", dataCOT, includeOptionsInput, cftcCode)
    table.cell(symbolDisplay, 0, 0, txt, text_halign = text.align_left, text_color = TEXT_COLOR, bgcolor = BG_COLOR)
//#endregion

Overview

A display setting chooses long positions, short positions, their sum, their difference or the long share as a percentage of the sum. Other settings pick the selection mode that maps the symbol to COT data, futures or options data, and an optional CFTC code override. It uses the LibraryCOT library and errors if no COT data matches.

Details

Published by

ivansoft on GitHub

Code credit

TradingView, as written in the script's header

Draws

Separate pane below the chart

Markets

Oil & commodities, Forex, Indices

Timeframes

H4 and above

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescripttradingvew

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
COT selection modestringAutoselectionModeInput
Futures/OptionsstringIO01includeOptionsInput
DisplaystringST01selectedTypeInput
CFTC CodestringuserCFTCCodeInput
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Asymmetrical RSI

by everget
947 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 11 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
550 stars

Range Volume Change compares volume inside a chosen session window with the same window on the previous day and plots that change as a percentage. It accumulates volume during the window and, when enabled, volume outside it, drawing growing columns in a separa

Indicator v5 VolumeSessions & time Repaints
Updated 10 months ago · None stated GitHub repository of Range Volume Change

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

Mojito v3 Strategy

by namuan
318 stars

The Mojito v3 Strategy indicator evaluates volatility term structure by calculating the ratio between front-month and second-month VIX futures contracts. It smooths this ratio using a configurable median filter and compares the result against user-defined uppe

Indicator v5 Volatility & bands
Updated 2 months ago · MIT GitHub repository of Mojito v3 Strategy

More indicators

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers