ShirokiHeishi

Herrick Payoff Index

178
All credit goes to @Greeny and @shardison for the work they did on this beautiful indicator. I have made some minor modifications, but the whole of the code is not mine.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?
////////////////////////////////////////////////////////////
//  Copyright by shardison v1.0 08/05/2015
// Credit to GREENY for his work on the COT OPEN INTEREST SCRIPT
// Attention:
//The Herrick Payoff Index (HPI) was developed by John Herrick and is the only well known indicator that uses price, volume, and open interest.
// The Herrick Payoff Index is designed to show the amount of money flowing into or out of a futures contract. 
//The Index uses open interest during its calculations, therefore, the security being analyzed must contain open interest.
//NOTE: THE COT OPEN INTEREST SCRIPT ONLY pulls Weekly DATA, hence only utilize this script on Weekly bars.
//Interpretation
//
//When the Herrick Payoff Index is above zero, it shows that money is flowing into the futures contract (which is bullish). 
//When the Index is below zero, it shows that money is flowing out of the futures contract (which is bearish).
//
//The interpretation of the Herrick Payoff Index involves looking for divergences between the Index and prices.
////////////////////////////////////////////////////////////

study(title="Herrick Payoff Index", shorttitle="HPI")
//color Hexidecimal
magenta = #FF00BF
indianred = #FF6A6A
orangered = #FF4500
salmon = #FA8072
hotpink = #FF69B4
coral = #FF7F50
mistyrose = #FFE4E1
//green
lawngreen = #7CFC00
greenyellow = #ADFF2F
mintgreen = #00FF40
palegreen = #98FB98
seagreen = #54FF9F
springgreen = #00FF7F
//blue
cyan = #00FFFF
dodgerblue = #1E90FF
paleblue = #24FFBE
powderblue = #B0E0E6
slategray = #C6E2FF
//yellow
gold = #FFD700
beige= #F0e68C
valueofonecentmove = input(100, minval=1)
multiplyingfactor = input(10, minval=1)
wmaperiod = input(21, minval=1)
force_root = input("", title="Override Product")
is_includeoptions = input(false, type=bool, title="Include Options")
view    = input(1, title="HPI =1 Open Interest=2 COT=3 ALL=4 Commercials histogram=5", maxval=5,minval=1)
threshold   = input(0,type=float)
sig     = input(7, title="signal")
fxroot =
	  ticker == "USDCAD" ? "CD" : 
	  ticker == "USDCAD" ? "CD" : 
	  ticker == "USDCHF" ? "SF" : 
	  ticker == "USDCZK" ? "CZ" : 
	  ticker == "USDHUF" ? "FR" : 
	  ticker == "USDILS" ? "IS" : 
	  ticker == "USDJPY" ? "JY" : 
	  ticker == "USDMXN" ? "MP" : 
	  ticker == "USDNOK" ? "UN" : 
	  ticker == "USDPLN" ? "PZ" : 
	  ticker == "USDRUB" ? "RU" : 
	  ticker == "USDSEK" ? "SE" : 
	  ticker == "USDZAR" ? "RA" : 
	  ticker == "EURUSD" ? "EC" : 
	  ticker == "AUDUSD" ? "AD" : 
	  ticker == "GBPUSD" ? "BP" : 
	  ticker == "NZDUSD" ? "NE" : 
	  ticker == "BRLUSD" ? "BR" : 
	  ticker == "XAUUSD" ? "GC" :
	  ticker == "XAGUSD" ? "SI" :
	  ticker == "USOIL"  ? "CL" :
	  ticker == "SPX500" ? "ES" :
	  ticker == "NAS100" ? "ND" :
	  ticker == "QQQ"    ? "ND" :
	  ticker == "IAU"    ? "GC" :
	  ticker == "SLV"    ? "SI" :
	  ""

is_inversed = 
	  ticker == "USDCAD" ? true : 
	  ticker == "USDCAD" ? true : 
	  ticker == "USDCHF" ? true : 
	  ticker == "USDCZK" ? true : 
	  ticker == "USDHUF" ? true : 
	  ticker == "USDILS" ? true : 
	  ticker == "USDJPY" ? true : 
	  ticker == "USDMXN" ? true : 
	  ticker == "USDNOK" ? true : 
	  ticker == "USDPLN" ? true : 
	  ticker == "USDRUB" ? true : 
	  ticker == "USDSEK" ? true : 
	  ticker == "USDZAR" ? true : 
	  false

root = force_root == "" ? fxroot == "" ? syminfo.root : fxroot : force_root
code = root + (is_includeoptions ? "_FO" : "_F") + "_L_ALL"

oi = security("QUANDL:CFTC/"+code+"|0", "D", close)

commercial_long_total = security("QUANDL:CFTC/"+code+"|4", "W", close)
commercial_short_total = security("QUANDL:CFTC/"+code+"|5", "W", close)
long_total = security("QUANDL:CFTC/"+code+"|1", "W", close)
short_total = security("QUANDL:CFTC/"+code+"|2", "W", close)

commercial_long = is_inversed ? commercial_short_total : commercial_long_total
commercial_short = is_inversed ? commercial_long_total : commercial_short_total
long = is_inversed ? short_total : long_total
short = is_inversed ? long_total : short_total

diff = (commercial_long - commercial_short)*-1
cdiff = diff - diff[1]
cI = abs(cdiff)
cG = max(diff, diff[1])
diff2 = long - short
openinterestdiff = oi - oi[1]
I = abs(openinterestdiff)
G = max(oi, oi[1])
S = multiplyingfactor
C = valueofonecentmove
V = volume
M = (high + low) / 2
My = M[1]
K1 = (C*V*(M - My))*(1 + ((2 * I)/(G)))
K2 = (C*V*(M - My))*(1 - ((2 * I)/(G)))
K = M > My ? K1 : K2
Ky = K[1]
HPI = ((Ky +(K - Ky)) * S)/100000
HPI_Index = 100 * (HPI - lowest(HPI,100))/(highest(HPI,100) - lowest(HPI,100))
wma = wma(HPI, wmaperiod)
diffh   = highest(diff,wmaperiod)
diffl   = lowest(diff,wmaperiod)
calch   = diffh>0? diffh-(.1*diffh):diffh+(.1*diffh)
calcl   = diffl<0? diffl-(.1*diffl):diffl+(.1*diffl)
// plot (view?na:HPI, color=green, title="HPI",transp=0)
// plot (view?na:HPI_Index, color=aqua, transp=0, title="HPI Index-Turn off all others")
plot (view==1 or view==4?wma:na, color=wma>=0?dodgerblue:salmon, title="HPI Weighted Moving Average", style=histogram,transp=40,linewidth=4)
// plot(view?ema(wma,sig):na, color=maroon, transp=0, linewidth=2)
plot (view==3 or view==4?diff:na, color=white,linewidth=2,transp=0, title="Commercials Net-Position")
plot (view==3? ema(diff,sig):na, color=maroon,linewidth=2,transp=0, title="EMA Commercials net position")
// plot (view==3 or view==4?diff2:na, color=teal, linewidth=2, transp=0,title="Non-Commercials Net-Position")
plot (view==2 or view==4?oi:na, color = #2f4f4f,transp=0, title="OI")
// plot (view==1?0:na, color=#2f4f4f, title="Zero",transp=0)
// plot (view==3?threshold:na, color=#2f4f4f, transp=0)
plot (view==3?calch:na, color=#2f4f4f,transp=0)
plot (view==3?calcl:na, color=#2f4f4f,transp=0)
plot (view==5?diff:na, color=diff>0 and diff>diff[1]?lime:diff>0 and diff<diff[1]?red:diff<0 and diff<diff[1]?lime:red,transp=0, linewidth=3,style=histogram)