Skip to content

Instantly share code, notes, and snippets.

@sherwind
Last active March 18, 2019 16:54
Show Gist options
  • Save sherwind/497feeac8a685a78443bc8fafb7f1b73 to your computer and use it in GitHub Desktop.
Save sherwind/497feeac8a685a78443bc8fafb7f1b73 to your computer and use it in GitHub Desktop.
COT Non-Commercial Net Positions.pinescript
//@version=3
//
// Commitments of Traders (COT) - Net Positions of Non-Commercial Traders
//
// See Also:
// - https://www.cftc.gov/MarketReports/CommitmentsofTraders/index.htm
// - https://www.investopedia.com/walkthrough/forex/trading-strategies/long-term/cot-report.aspx
// - https://www.oanda.com/forex-trading/analysis/commitments-of-traders
// - https://www.babypips.com/learn/forex/commitment-of-traders-report
//
// -----------------------------------------------------------------------------
// Copyright 2019 sherwind
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// The GNU General Public License can be found here
// <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
study("COT Non-Commercial Net Positions", shorttitle="COT Non-Comm Net")
asset = input(defval="EURO FX (CME)", title="Asset: ", options=["U.S. DOLLAR INDEX (ICUS)", "JAPANESE YEN (CME)", "E-MINI S&P 500 STOCK INDEX (CME)", "CANADIAN DOLLAR (CME)", "CRUDE OIL, LIGHT SWEET (NYME)", "NEW ZEALAND DOLLAR (CME)", "AUSTRALIAN DOLLAR (CME)", "SWISS FRANC (CME)", "GOLD (CMX)", "SILVER (CMX)", "COPPER-GRADE #1 (CMX)", "EURO FX (CME)", "BRITISH POUND STERLING (CME)", "VIX FUTURES (E)", "3-MONTH EURODOLLARS (CME)", "BITCOIN-USD (E)"], type=string)
is_include_options = input(true, type=bool, title="Include Options")
is_show_inverse = input(false, type=bool, title="Show Inverse")
is_check_top_traders = input(true, type=bool, title="Highlight when Top 4 or Top 8 Traders diverge/converge")
is_highlight_shift = input(false, type=bool, title="Highlight the shift from net long to short and vice versa")
quandl_cftc(code, index) =>
quandl_ticker = "QUANDL:CFTC/" + code + (index == 0 ? "" : "|" + tostring(index))
security(quandl_ticker, "W", close)
noncomm(code) =>
cftc_code = code + (is_include_options ? "_FO" : "_F") + "_L_ALL"
noncomm_long = quandl_cftc(cftc_code, 1)
noncomm_short = quandl_cftc(cftc_code, 2)
noncomm_net = noncomm_long - noncomm_short
[noncomm_net, noncomm_long, noncomm_short]
top_traders(code) =>
cftc_code = code + (is_include_options ? "_FO" : "_F") + "_L_ALL_CR"
top4_long = quandl_cftc(cftc_code, 0)
top4_short = quandl_cftc(cftc_code, 1)
top8_long = quandl_cftc(cftc_code, 2)
top8_short = quandl_cftc(cftc_code, 3)
top4_ratio = top4_long >= top4_short ? (top4_long/top4_short) : ((top4_short/top4_long) * -1)
top8_ratio = top8_long >= top8_short ? (top8_long/top8_short) : ((top8_short/top8_long) * -1)
[top4_ratio, top8_ratio, top4_long, top4_short, top8_long, top8_short]
noncomm_vs_top(noncomm, top4_ratio, top8_ratio) =>
agree = 0
if (noncomm >= 0)
if (abs(top4_ratio) >= 2.0)
agree := top4_ratio > 0 ? 1 : -1
else
if (abs(top8_ratio) >= 2.0)
agree := top8_ratio > 0 ? 1 : -1
else
if (abs(top4_ratio) >= 2.0)
agree := top4_ratio < 0 ? 1 : -1
else
if (abs(top8_ratio) >= 2.0)
agree := top8_ratio < 0 ? 1 : -1
agree
asset2code(asset) =>
asset == "U.S. DOLLAR INDEX (ICUS)" ? "098662" :
asset == "JAPANESE YEN (CME)" ? "097741" :
asset == "E-MINI S&P 500 STOCK INDEX (CME)" ? "13874A" :
asset == "CANADIAN DOLLAR (CME)" ? "090741" :
asset == "CRUDE OIL, LIGHT SWEET (NYME)" ? "067651" :
asset == "NEW ZEALAND DOLLAR (CME)" ? "112741" :
asset == "AUSTRALIAN DOLLAR (CME)" ? "232741" :
asset == "SWISS FRANC (CME)" ? "092741" :
asset == "GOLD (CMX)" ? "088691" :
asset == "SILVER (CMX)" ? "084691" :
asset == "COPPER-GRADE #1 (CMX)" ? "085692" :
asset == "EURO FX (CME)" ? "099741" :
asset == "BRITISH POUND STERLING (CME)" ? "096742" :
asset == "VIX FUTURES (E)" ? "1170E1" :
asset == "3-MONTH EURODOLLARS (CME)" ? "132741" :
asset == "BITCOIN-USD (E)" ? "1330E1" :
"099741"
code = asset2code(asset)
[noncomm_net, noncomm_long, noncomm_short] = noncomm(code)
[top4_ratio, top8_ratio, top4_long, top4_short, top8_long, top8_short] = top_traders(code)
noncomm_and_top_agree = noncomm_vs_top(noncomm_net, top4_ratio, top8_ratio)
alert_long = noncomm_net[1] < 0 and noncomm_net > 0 ? 1 : 0
alert_short = noncomm_net[1] > 0 and noncomm_net < 0 ? 1 : 0
alertcondition(alert_long, title='Net sellers shifted to buyers', message='Non-commercials changed from net selling to buying')
alertcondition(alert_short, title='Net buyers shifted to sellers', message='Non-commercials changed from net buying to selling')
bgcolor(is_highlight_shift and alert_long ? lime : na, transp=30)
bgcolor(is_highlight_shift and alert_short ? red : na, transp=50)
// debug
//plotchar(noncomm_long, char="")
//plotchar(noncomm_short, char="")
//plotchar(top4_ratio, char="")
//plotchar(top8_ratio, char="")
//plotchar(top4_long, char="")
//plotchar(top4_short, char="")
//plotchar(top8_long, char="")
//plotchar(top8_short, char="")
hline(0, color=black, linestyle=dashed)
plot_color = not is_check_top_traders ? purple :
noncomm_and_top_agree == 1 ? green :
noncomm_and_top_agree == -1 ? red :
purple
plot(is_show_inverse ? 1 - noncomm_net : noncomm_net, color=plot_color, title='COT Non-Comm Net', style=columns, transp=60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment