// CCI Calculation
//
// The example below is based on a 20-period Commodity Channel Index (CCI) calculation. The number of CCI periods is
// also used for the calculations of the simple moving average and Mean Deviation.
//
//CCI = (Typical Price - 20-period SMA of TP) / (.015 x Mean Deviation)
//Typical Price (TP) = (High + Low + Close)/3
//Constant = .015
//
// There are four steps to calculating the Mean Deviation:
// 1 - subtract the most recent 20-period average of the typical price from each period's typical price.
// 2 - take the absolute values of these numbers.
// 3 - sum the absolute values.
// 4 - divide by the total number of periods (20).
//
Period = max(2,min(999,Period)) //2-999
M = typicalprice
MM = average[Period](M)
Sum = 0
FOR i = 0 to Period - 1
sum = sum + ABS(mm[0]-M[i])
NEXT
D = Sum/Period
MyCCI = (M - MM) / (0.015 * D)
//return MyCCI
////////////////////////////////////////////////////
//PRC_WilliamsRpercent - 01.09.2016 - Nicolas @ www.prorealcode.com
//
hh = highest[period](MyCCI)
ll = lowest[period](MyCCI)
Wp = ((hh-close)/(hh-ll))*-100
return Wp as "Williams R%"//, -20 as "-20 level", -80 as "-80 level"